Changes in uspace/app/bdsh/cmds/modules/cp/cp.c [1569a9b:582a0b8] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cp/cp.c
r1569a9b 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 if (copy_file(src, dest_path, blen, vb) < 0) { 269 rc = EIO; 270 } 262 r = (copy_file(src, dest_path, blen, vb) < 0); 271 263 272 264 } else if (src_type == TYPE_DIR) { … … 276 268 printf("Cannot copy the %s directory without the " 277 269 "-r option\n", src); 278 rc = EINVAL;279 270 goto exit; 280 271 } else if (dest_type == TYPE_FILE) { 281 272 printf("Cannot overwrite a file with a directory\n"); 282 rc = EEXIST;283 273 goto exit; 284 274 } … … 303 293 merge_paths(dest_path, PATH_MAX, src_dirname); 304 294 305 rc = vfs_link_path(dest_path, KIND_DIRECTORY, 306 NULL); 307 if (rc != EOK) { 295 if (vfs_link_path(dest_path, KIND_DIRECTORY, 296 NULL) != EOK) { 308 297 printf("Unable to create " 309 298 "dest directory %s\n", dest_path); … … 319 308 * e.g. cp -r /src /data/new_dir_src 320 309 */ 321 rc = vfs_link_path(dest_path, KIND_DIRECTORY, NULL);322 if (rc!= EOK) {310 if (vfs_link_path(dest_path, KIND_DIRECTORY, 311 NULL) != EOK) { 323 312 printf("Unable to create " 324 313 "dest directory %s\n", dest_path); … … 332 321 /* Something strange is happening... */ 333 322 printf("Unable to open src %s directory\n", src); 334 rc = ENOENT;335 323 goto exit; 336 324 } … … 360 348 printf("Cannot copy a directory " 361 349 "into itself\n"); 362 rc = EEXIST;363 350 goto exit; 364 351 } … … 368 355 369 356 /* Recursively call do_copy() */ 370 r c= do_copy(src_dent, dest_dent, blen, vb, recursive,357 r = do_copy(src_dent, dest_dent, blen, vb, recursive, 371 358 force, interactive); 372 if (r c != EOK)359 if (r) 373 360 goto exit; 374 361 … … 380 367 if (dir) 381 368 closedir(dir); 382 return r c;383 } 384 385 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, 386 373 size_t blen, int vb) 387 374 { 388 int fd1, fd2; 389 size_t rbytes, wbytes; 390 int rc; 375 int fd1, fd2, bytes; 391 376 off64_t total; 377 int64_t copied = 0; 392 378 char *buff = NULL; 393 379 aoff64_t posr = 0, posw = 0; … … 397 383 printf("Copying %s to %s\n", src, dest); 398 384 399 rc = vfs_lookup_open(src, WALK_REGULAR, MODE_READ, &fd1);400 if ( rc != EOK) {385 fd1 = vfs_lookup_open(src, WALK_REGULAR, MODE_READ); 386 if (fd1 < 0) { 401 387 printf("Unable to open source file %s\n", src); 402 388 return -1; 403 389 } 404 390 405 rc = vfs_lookup_open(dest, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE, &fd2);406 if ( rc != EOK) {391 fd2 = vfs_lookup_open(dest, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE); 392 if (fd2 < 0) { 407 393 printf("Unable to open destination file %s\n", dest); 408 394 vfs_put(fd1); … … 424 410 printf("Unable to allocate enough memory to read %s\n", 425 411 src); 426 rc = ENOMEM;412 copied = -1; 427 413 goto out; 428 414 } 429 415 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;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; 439 425 } 440 426 … … 444 430 if (buff) 445 431 free(buff); 446 if (rc != EOK) { 447 return -1; 448 } else { 449 return 0; 450 } 432 return copied; 451 433 } 452 434 … … 479 461 int force = 0, interactive = 0; 480 462 int c, opt_ind; 481 int ret;463 int64_t ret; 482 464 483 465 con = console_init(stdin, stdout);
Note:
See TracChangeset
for help on using the changeset viewer.