Changes in uspace/app/bdsh/cmds/modules/cp/cp.c [dd8ab1c:582a0b8] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cp/cp.c
rdd8ab1c r582a0b8 28 28 29 29 #include <errno.h> 30 #include <str_error.h>31 30 #include <stdio.h> 32 31 #include <stdlib.h> … … 67 66 } dentry_type_t; 68 67 69 static int copy_file(const char *src, const char *dest,68 static int64_t copy_file(const char *src, const char *dest, 70 69 size_t blen, int vb); 71 70 … … 176 175 } 177 176 178 static int do_copy(const char *src, const char *dest,177 static int64_t do_copy(const char *src, const char *dest, 179 178 size_t blen, int vb, int recursive, int force, int interactive) 180 179 { 181 int r c = EOK;180 int r = -1; 182 181 char dest_path[PATH_MAX]; 183 182 char src_path[PATH_MAX]; … … 218 217 printf("The dest directory %s does not exists", 219 218 dest_path); 220 rc = ENOENT;221 219 goto exit; 222 220 } … … 226 224 printf("Cannot overwrite existing directory %s\n", 227 225 dest_path); 228 rc = EEXIST;229 226 goto exit; 230 227 } else if (dest_type == TYPE_FILE) { … … 236 233 */ 237 234 if (force && !interactive) { 238 rc = vfs_unlink_path(dest_path); 239 if (rc != EOK) { 235 if (vfs_unlink_path(dest_path) != EOK) { 240 236 printf("Unable to remove %s\n", 241 237 dest_path); … … 248 244 if (overwrite) { 249 245 printf("Overwriting file: %s\n", dest_path); 250 rc = vfs_unlink_path(dest_path); 251 if (rc != EOK) { 246 if (vfs_unlink_path(dest_path) != EOK) { 252 247 printf("Unable to remove %s\n", dest_path); 253 248 goto exit; … … 255 250 } else { 256 251 printf("Not overwriting file: %s\n", dest_path); 257 r c = EOK;252 r = 0; 258 253 goto exit; 259 254 } 260 255 } else { 261 256 printf("File already exists: %s\n", dest_path); 262 rc = EEXIST;263 257 goto exit; 264 258 } … … 266 260 267 261 /* call copy_file and exit */ 268 r c= (copy_file(src, dest_path, blen, vb) < 0);262 r = (copy_file(src, dest_path, blen, vb) < 0); 269 263 270 264 } else if (src_type == TYPE_DIR) { … … 274 268 printf("Cannot copy the %s directory without the " 275 269 "-r option\n", src); 276 rc = EINVAL;277 270 goto exit; 278 271 } else if (dest_type == TYPE_FILE) { 279 272 printf("Cannot overwrite a file with a directory\n"); 280 rc = EEXIST;281 273 goto exit; 282 274 } … … 301 293 merge_paths(dest_path, PATH_MAX, src_dirname); 302 294 303 rc = vfs_link_path(dest_path, KIND_DIRECTORY, 304 NULL); 305 if (rc != EOK) { 295 if (vfs_link_path(dest_path, KIND_DIRECTORY, 296 NULL) != EOK) { 306 297 printf("Unable to create " 307 298 "dest directory %s\n", dest_path); … … 317 308 * e.g. cp -r /src /data/new_dir_src 318 309 */ 319 rc = vfs_link_path(dest_path, KIND_DIRECTORY, NULL);320 if (rc!= EOK) {310 if (vfs_link_path(dest_path, KIND_DIRECTORY, 311 NULL) != EOK) { 321 312 printf("Unable to create " 322 313 "dest directory %s\n", dest_path); … … 330 321 /* Something strange is happening... */ 331 322 printf("Unable to open src %s directory\n", src); 332 rc = ENOENT;333 323 goto exit; 334 324 } … … 358 348 printf("Cannot copy a directory " 359 349 "into itself\n"); 360 rc = EEXIST;361 350 goto exit; 362 351 } … … 366 355 367 356 /* Recursively call do_copy() */ 368 r c= do_copy(src_dent, dest_dent, blen, vb, recursive,357 r = do_copy(src_dent, dest_dent, blen, vb, recursive, 369 358 force, interactive); 370 if (r c != EOK)359 if (r) 371 360 goto exit; 372 361 … … 378 367 if (dir) 379 368 closedir(dir); 380 return r c;381 } 382 383 static int copy_file(const char *src, const char *dest,369 return r; 370 } 371 372 static int64_t copy_file(const char *src, const char *dest, 384 373 size_t blen, int vb) 385 374 { 386 int fd1, fd2; 387 size_t rbytes, wbytes; 388 int rc; 375 int fd1, fd2, bytes; 389 376 off64_t total; 377 int64_t copied = 0; 390 378 char *buff = NULL; 391 379 aoff64_t posr = 0, posw = 0; … … 395 383 printf("Copying %s to %s\n", src, dest); 396 384 397 rc = vfs_lookup_open(src, WALK_REGULAR, MODE_READ, &fd1);398 if ( rc != EOK) {385 fd1 = vfs_lookup_open(src, WALK_REGULAR, MODE_READ); 386 if (fd1 < 0) { 399 387 printf("Unable to open source file %s\n", src); 400 388 return -1; 401 389 } 402 390 403 rc = vfs_lookup_open(dest, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE, &fd2);404 if ( rc != EOK) {391 fd2 = vfs_lookup_open(dest, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE); 392 if (fd2 < 0) { 405 393 printf("Unable to open destination file %s\n", dest); 406 394 vfs_put(fd1); … … 422 410 printf("Unable to allocate enough memory to read %s\n", 423 411 src); 424 rc = ENOMEM;412 copied = -1; 425 413 goto out; 426 414 } 427 415 428 while (( rc = vfs_read(fd1, &posr, buff, blen, &rbytes)) == EOK &&429 rbytes > 0) {430 if ((rc = vfs_write(fd2, &posw, buff, rbytes, &wbytes)) != EOK)431 break;432 } 433 434 if ( rc != EOK) {435 printf("\nError copying %s : %s\n", src, str_error(rc));436 return rc;416 while ((bytes = vfs_read(fd1, &posr, buff, blen)) > 0) { 417 if ((bytes = vfs_write(fd2, &posw, buff, bytes)) < 0) 418 break; 419 copied += bytes; 420 } 421 422 if (bytes < 0) { 423 printf("\nError copying %s, (%d)\n", src, bytes); 424 copied = bytes; 437 425 } 438 426 … … 442 430 if (buff) 443 431 free(buff); 444 return rc;432 return copied; 445 433 } 446 434
Note:
See TracChangeset
for help on using the changeset viewer.