Ignore:
Timestamp:
2018-01-30T03:20:45Z (8 years ago)
Author:
Jenda <jenda.jzqk73@…>
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.
Message:

Merge commit '6a5d05bd2551e64111bea4f9332dd7448c26ce84' into forwardport

Separate return value from error code in gen_irq_code*().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/cp/cp.c

    r8bfb163 r132ab5d1  
    2828
    2929#include <errno.h>
     30#include <str_error.h>
    3031#include <stdio.h>
    3132#include <stdlib.h>
     
    6667} dentry_type_t;
    6768
    68 static int64_t copy_file(const char *src, const char *dest,
     69static int copy_file(const char *src, const char *dest,
    6970    size_t blen, int vb);
    7071
     
    175176}
    176177
    177 static int64_t do_copy(const char *src, const char *dest,
     178static int do_copy(const char *src, const char *dest,
    178179    size_t blen, int vb, int recursive, int force, int interactive)
    179180{
    180         int r = -1;
     181        int rc = EOK;
    181182        char dest_path[PATH_MAX];
    182183        char src_path[PATH_MAX];
     
    217218                                printf("The dest directory %s does not exists",
    218219                                    dest_path);
     220                                rc = ENOENT;
    219221                                goto exit;
    220222                        }
     
    224226                        printf("Cannot overwrite existing directory %s\n",
    225227                            dest_path);
     228                        rc = EEXIST;
    226229                        goto exit;
    227230                } else if (dest_type == TYPE_FILE) {
     
    233236                         */
    234237                        if (force && !interactive) {
    235                                 if (vfs_unlink_path(dest_path) != EOK) {
     238                                rc = vfs_unlink_path(dest_path);
     239                                if (rc != EOK) {
    236240                                        printf("Unable to remove %s\n",
    237241                                            dest_path);
     
    244248                                if (overwrite) {
    245249                                        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) {
    247252                                                printf("Unable to remove %s\n", dest_path);
    248253                                                goto exit;
     
    250255                                } else {
    251256                                        printf("Not overwriting file: %s\n", dest_path);
    252                                         r = 0;
     257                                        rc = EOK;
    253258                                        goto exit;
    254259                                }
    255260                        } else {
    256261                                printf("File already exists: %s\n", dest_path);
     262                                rc = EEXIST;
    257263                                goto exit;
    258264                        }
     
    260266
    261267                /* 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                }
    263271
    264272        } else if (src_type == TYPE_DIR) {
     
    268276                        printf("Cannot copy the %s directory without the "
    269277                            "-r option\n", src);
     278                        rc = EINVAL;
    270279                        goto exit;
    271280                } else if (dest_type == TYPE_FILE) {
    272281                        printf("Cannot overwrite a file with a directory\n");
     282                        rc = EEXIST;
    273283                        goto exit;
    274284                }
     
    293303                                merge_paths(dest_path, PATH_MAX, src_dirname);
    294304
    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) {
    297308                                        printf("Unable to create "
    298309                                            "dest directory %s\n", dest_path);
     
    308319                         * e.g. cp -r /src /data/new_dir_src
    309320                         */
    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) {
    312323                                printf("Unable to create "
    313324                                    "dest directory %s\n", dest_path);
     
    321332                        /* Something strange is happening... */
    322333                        printf("Unable to open src %s directory\n", src);
     334                        rc = ENOENT;
    323335                        goto exit;
    324336                }
     
    348360                                printf("Cannot copy a directory "
    349361                                    "into itself\n");
     362                                rc = EEXIST;
    350363                                goto exit;
    351364                        }
     
    355368
    356369                        /* 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,
    358371                            force, interactive);
    359                         if (r)
     372                        if (rc != EOK)
    360373                                goto exit;
    361374
     
    367380        if (dir)
    368381                closedir(dir);
    369         return r;
    370 }
    371 
    372 static int64_t copy_file(const char *src, const char *dest,
     382        return rc;
     383}
     384
     385static int copy_file(const char *src, const char *dest,
    373386        size_t blen, int vb)
    374387{
    375         int fd1, fd2, bytes;
     388        int fd1, fd2;
     389        size_t rbytes, wbytes;
     390        int rc;
    376391        off64_t total;
    377         int64_t copied = 0;
    378392        char *buff = NULL;
    379393        aoff64_t posr = 0, posw = 0;
     
    383397                printf("Copying %s to %s\n", src, dest);
    384398
    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) {
    387401                printf("Unable to open source file %s\n", src);
    388402                return -1;
    389403        }
    390404
    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) {
    393407                printf("Unable to open destination file %s\n", dest);
    394408                vfs_put(fd1);
     
    410424                printf("Unable to allocate enough memory to read %s\n",
    411425                    src);
    412                 copied = -1;
     426                rc = ENOMEM;
    413427                goto out;
    414428        }
    415429
    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;
    425439        }
    426440
     
    430444        if (buff)
    431445                free(buff);
    432         return copied;
     446        if (rc != EOK) {
     447                return -1;
     448        } else {
     449                return 0;
     450        }
    433451}
    434452
     
    461479        int force = 0, interactive = 0;
    462480        int c, opt_ind;
    463         int64_t ret;
     481        int ret;
    464482
    465483        con = console_init(stdin, stdout);
Note: See TracChangeset for help on using the changeset viewer.