Changeset 132ab5d1 in mainline for uspace/app/bdsh/cmds/modules/cp/cp.c
- Timestamp:
- 2018-01-30T03:20:45Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5a6cc679
- Parents:
- 8bfb163 (diff), 6a5d05b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
uspace/app/bdsh/cmds/modules/cp/cp.c (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cp/cp.c
r8bfb163 r132ab5d1 28 28 29 29 #include <errno.h> 30 #include <str_error.h> 30 31 #include <stdio.h> 31 32 #include <stdlib.h> … … 66 67 } dentry_type_t; 67 68 68 static int 64_tcopy_file(const char *src, const char *dest,69 static int copy_file(const char *src, const char *dest, 69 70 size_t blen, int vb); 70 71 … … 175 176 } 176 177 177 static int 64_tdo_copy(const char *src, const char *dest,178 static int do_copy(const char *src, const char *dest, 178 179 size_t blen, int vb, int recursive, int force, int interactive) 179 180 { 180 int r = -1;181 int rc = EOK; 181 182 char dest_path[PATH_MAX]; 182 183 char src_path[PATH_MAX]; … … 217 218 printf("The dest directory %s does not exists", 218 219 dest_path); 220 rc = ENOENT; 219 221 goto exit; 220 222 } … … 224 226 printf("Cannot overwrite existing directory %s\n", 225 227 dest_path); 228 rc = EEXIST; 226 229 goto exit; 227 230 } else if (dest_type == TYPE_FILE) { … … 233 236 */ 234 237 if (force && !interactive) { 235 if (vfs_unlink_path(dest_path) != EOK) { 238 rc = vfs_unlink_path(dest_path); 239 if (rc != EOK) { 236 240 printf("Unable to remove %s\n", 237 241 dest_path); … … 244 248 if (overwrite) { 245 249 printf("Overwriting file: %s\n", dest_path); 246 if (vfs_unlink_path(dest_path) != EOK) { 250 rc = vfs_unlink_path(dest_path); 251 if (rc != EOK) { 247 252 printf("Unable to remove %s\n", dest_path); 248 253 goto exit; … … 250 255 } else { 251 256 printf("Not overwriting file: %s\n", dest_path); 252 r = 0;257 rc = EOK; 253 258 goto exit; 254 259 } 255 260 } else { 256 261 printf("File already exists: %s\n", dest_path); 262 rc = EEXIST; 257 263 goto exit; 258 264 } … … 260 266 261 267 /* call copy_file and exit */ 262 r = (copy_file(src, dest_path, blen, vb) < 0); 268 if (copy_file(src, dest_path, blen, vb) < 0) { 269 rc = EIO; 270 } 263 271 264 272 } else if (src_type == TYPE_DIR) { … … 268 276 printf("Cannot copy the %s directory without the " 269 277 "-r option\n", src); 278 rc = EINVAL; 270 279 goto exit; 271 280 } else if (dest_type == TYPE_FILE) { 272 281 printf("Cannot overwrite a file with a directory\n"); 282 rc = EEXIST; 273 283 goto exit; 274 284 } … … 293 303 merge_paths(dest_path, PATH_MAX, src_dirname); 294 304 295 if (vfs_link_path(dest_path, KIND_DIRECTORY, 296 NULL) != EOK) { 305 rc = vfs_link_path(dest_path, KIND_DIRECTORY, 306 NULL); 307 if (rc != EOK) { 297 308 printf("Unable to create " 298 309 "dest directory %s\n", dest_path); … … 308 319 * e.g. cp -r /src /data/new_dir_src 309 320 */ 310 if (vfs_link_path(dest_path, KIND_DIRECTORY,311 NULL)!= EOK) {321 rc = vfs_link_path(dest_path, KIND_DIRECTORY, NULL); 322 if (rc != EOK) { 312 323 printf("Unable to create " 313 324 "dest directory %s\n", dest_path); … … 321 332 /* Something strange is happening... */ 322 333 printf("Unable to open src %s directory\n", src); 334 rc = ENOENT; 323 335 goto exit; 324 336 } … … 348 360 printf("Cannot copy a directory " 349 361 "into itself\n"); 362 rc = EEXIST; 350 363 goto exit; 351 364 } … … 355 368 356 369 /* Recursively call do_copy() */ 357 r = do_copy(src_dent, dest_dent, blen, vb, recursive,370 rc = do_copy(src_dent, dest_dent, blen, vb, recursive, 358 371 force, interactive); 359 if (r )372 if (rc != EOK) 360 373 goto exit; 361 374 … … 367 380 if (dir) 368 381 closedir(dir); 369 return r ;370 } 371 372 static int 64_tcopy_file(const char *src, const char *dest,382 return rc; 383 } 384 385 static int copy_file(const char *src, const char *dest, 373 386 size_t blen, int vb) 374 387 { 375 int fd1, fd2, bytes; 388 int fd1, fd2; 389 size_t rbytes, wbytes; 390 int rc; 376 391 off64_t total; 377 int64_t copied = 0;378 392 char *buff = NULL; 379 393 aoff64_t posr = 0, posw = 0; … … 383 397 printf("Copying %s to %s\n", src, dest); 384 398 385 fd1 = vfs_lookup_open(src, WALK_REGULAR, MODE_READ);386 if ( fd1 < 0) {399 rc = vfs_lookup_open(src, WALK_REGULAR, MODE_READ, &fd1); 400 if (rc != EOK) { 387 401 printf("Unable to open source file %s\n", src); 388 402 return -1; 389 403 } 390 404 391 fd2 = vfs_lookup_open(dest, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE);392 if ( fd2 < 0) {405 rc = vfs_lookup_open(dest, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE, &fd2); 406 if (rc != EOK) { 393 407 printf("Unable to open destination file %s\n", dest); 394 408 vfs_put(fd1); … … 410 424 printf("Unable to allocate enough memory to read %s\n", 411 425 src); 412 copied = -1;426 rc = ENOMEM; 413 427 goto out; 414 428 } 415 429 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;430 while ((rc = vfs_read(fd1, &posr, buff, blen, &rbytes)) == EOK && 431 rbytes > 0) { 432 if ((rc = vfs_write(fd2, &posw, buff, rbytes, &wbytes)) != EOK) 433 break; 434 } 435 436 if (rc != EOK) { 437 printf("\nError copying %s: %s\n", src, str_error(rc)); 438 return -1; 425 439 } 426 440 … … 430 444 if (buff) 431 445 free(buff); 432 return copied; 446 if (rc != EOK) { 447 return -1; 448 } else { 449 return 0; 450 } 433 451 } 434 452 … … 461 479 int force = 0, interactive = 0; 462 480 int c, opt_ind; 463 int 64_tret;481 int ret; 464 482 465 483 con = console_init(stdin, stdout);
Note:
See TracChangeset
for help on using the changeset viewer.
