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