Changeset 8e3498b in mainline for uspace/app/bdsh/cmds/modules/cp/cp.c
- Timestamp:
- 2017-12-04T18:44:24Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- bde5c04
- Parents:
- 40feeac
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cp/cp.c
r40feeac r8e3498b 66 66 } dentry_type_t; 67 67 68 static int 64_tcopy_file(const char *src, const char *dest,68 static int copy_file(const char *src, const char *dest, 69 69 size_t blen, int vb); 70 70 … … 175 175 } 176 176 177 static int 64_tdo_copy(const char *src, const char *dest,177 static int do_copy(const char *src, const char *dest, 178 178 size_t blen, int vb, int recursive, int force, int interactive) 179 179 { 180 int r = -1;180 int rc = EOK; 181 181 char dest_path[PATH_MAX]; 182 182 char src_path[PATH_MAX]; … … 217 217 printf("The dest directory %s does not exists", 218 218 dest_path); 219 rc = ENOENT; 219 220 goto exit; 220 221 } … … 224 225 printf("Cannot overwrite existing directory %s\n", 225 226 dest_path); 227 rc = EEXIST; 226 228 goto exit; 227 229 } else if (dest_type == TYPE_FILE) { … … 233 235 */ 234 236 if (force && !interactive) { 235 if (vfs_unlink_path(dest_path) != EOK) { 237 rc = vfs_unlink_path(dest_path); 238 if (rc != EOK) { 236 239 printf("Unable to remove %s\n", 237 240 dest_path); … … 244 247 if (overwrite) { 245 248 printf("Overwriting file: %s\n", dest_path); 246 if (vfs_unlink_path(dest_path) != EOK) { 249 rc = vfs_unlink_path(dest_path); 250 if (rc != EOK) { 247 251 printf("Unable to remove %s\n", dest_path); 248 252 goto exit; … … 250 254 } else { 251 255 printf("Not overwriting file: %s\n", dest_path); 252 r = 0;256 rc = EOK; 253 257 goto exit; 254 258 } 255 259 } else { 256 260 printf("File already exists: %s\n", dest_path); 261 rc = EEXIST; 257 262 goto exit; 258 263 } … … 260 265 261 266 /* call copy_file and exit */ 262 r = (copy_file(src, dest_path, blen, vb) < 0);267 rc = (copy_file(src, dest_path, blen, vb) < 0); 263 268 264 269 } else if (src_type == TYPE_DIR) { … … 268 273 printf("Cannot copy the %s directory without the " 269 274 "-r option\n", src); 275 rc = EINVAL; 270 276 goto exit; 271 277 } else if (dest_type == TYPE_FILE) { 272 278 printf("Cannot overwrite a file with a directory\n"); 279 rc = EEXIST; 273 280 goto exit; 274 281 } … … 293 300 merge_paths(dest_path, PATH_MAX, src_dirname); 294 301 295 if (vfs_link_path(dest_path, KIND_DIRECTORY, 296 NULL) != EOK) { 302 rc = vfs_link_path(dest_path, KIND_DIRECTORY, 303 NULL); 304 if (rc != EOK) { 297 305 printf("Unable to create " 298 306 "dest directory %s\n", dest_path); … … 308 316 * e.g. cp -r /src /data/new_dir_src 309 317 */ 310 if (vfs_link_path(dest_path, KIND_DIRECTORY,311 NULL)!= EOK) {318 rc = vfs_link_path(dest_path, KIND_DIRECTORY, NULL); 319 if (rc != EOK) { 312 320 printf("Unable to create " 313 321 "dest directory %s\n", dest_path); … … 321 329 /* Something strange is happening... */ 322 330 printf("Unable to open src %s directory\n", src); 331 rc = ENOENT; 323 332 goto exit; 324 333 } … … 348 357 printf("Cannot copy a directory " 349 358 "into itself\n"); 359 rc = EEXIST; 350 360 goto exit; 351 361 } … … 355 365 356 366 /* Recursively call do_copy() */ 357 r = do_copy(src_dent, dest_dent, blen, vb, recursive,367 rc = do_copy(src_dent, dest_dent, blen, vb, recursive, 358 368 force, interactive); 359 if (r )369 if (rc != EOK) 360 370 goto exit; 361 371 … … 367 377 if (dir) 368 378 closedir(dir); 369 return r ;370 } 371 372 static int 64_tcopy_file(const char *src, const char *dest,379 return rc; 380 } 381 382 static int copy_file(const char *src, const char *dest, 373 383 size_t blen, int vb) 374 384 { 375 int fd1, fd2, bytes; 385 int fd1, fd2; 386 size_t rbytes, wbytes; 387 int rc; 376 388 off64_t total; 377 int64_t copied = 0;378 389 char *buff = NULL; 379 390 aoff64_t posr = 0, posw = 0; … … 410 421 printf("Unable to allocate enough memory to read %s\n", 411 422 src); 412 copied = -1;423 rc = ENOMEM; 413 424 goto out; 414 425 } 415 426 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;427 while ((rc = vfs_read(fd1, &posr, buff, blen, &rbytes)) == EOK && 428 rbytes > 0) { 429 if ((rc = vfs_write(fd2, &posw, buff, rbytes, &wbytes)) != EOK) 430 break; 431 } 432 433 if (rc != EOK) { 434 printf("\nError copying %s, (%d)\n", src, rc); 435 return rc; 425 436 } 426 437 … … 430 441 if (buff) 431 442 free(buff); 432 return copied;443 return rc; 433 444 } 434 445
Note:
See TracChangeset
for help on using the changeset viewer.