Changeset 8e3498b in mainline for uspace/app/bdsh


Ignore:
Timestamp:
2017-12-04T18:44:24Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bde5c04
Parents:
40feeac
Message:

vfs_read/write() should return error code separately from number of bytes transferred.

Location:
uspace/app/bdsh/cmds/modules
Files:
4 edited

Legend:

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

    r40feeac r8e3498b  
    180180    off64_t head, off64_t tail, bool tail_first)
    181181{
    182         int fd, bytes = 0, count = 0, reads = 0;
     182        int fd, count = 0, reads = 0;
     183        size_t bytes;
    183184        char *buff = NULL;
    184         int i;
     185        size_t i;
    185186        size_t offset = 0, copied_bytes = 0;
    186187        off64_t file_size = 0, length = 0;
    187188        aoff64_t pos = 0;
     189        int rc;
    188190
    189191        bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0);
     
    250252                }
    251253               
    252                 bytes = vfs_read(fd, &pos, buff + copied_bytes, bytes_to_read);
     254                rc = vfs_read(fd, &pos, buff + copied_bytes, bytes_to_read,
     255                    &bytes);
    253256                copied_bytes = 0;
    254257
    255                 if (bytes > 0) {
     258                if (rc == EOK && bytes > 0) {
    256259                        buff[bytes] = '\0';
    257260                        offset = 0;
     
    284287                if (reading_stdin)
    285288                        fflush(stdout);
    286         } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));
     289        } while (rc == EOK && bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));
    287290
    288291        vfs_put(fd);
    289         if (bytes == -1) {
     292        if (rc != EOK) {
    290293                printf("Error reading %s\n", fname);
    291294                free(buff);
  • uspace/app/bdsh/cmds/modules/cmp/cmp.c

    r40feeac r8e3498b  
    7272static int cmp_files(const char *fn0, const char *fn1)
    7373{
    74         int rc = 0;
     74        int rc = EOK;
    7575        const char *fn[2] = {fn0, fn1};
    7676        int fd[2] = {-1, -1};
    7777        char buffer[2][CMP_BUFLEN];
    78         ssize_t offset[2];
     78        size_t offset[2];
    7979        aoff64_t pos[2] = {};
    8080
     
    9090        do {
    9191                for (int i = 0; i < 2; i++) {
    92                         offset[i] = 0;
    93                         ssize_t size;
    94                         do {
    95                                 size = vfs_read(fd[i], &pos[i],
    96                                     buffer[i] + offset[i],
    97                                     CMP_BUFLEN - offset[i]);
    98                                 if (size < 0) {
    99                                         rc = size;
    100                                         printf("Error reading from %s\n",
    101                                             fn[i]);
    102                                         goto end;
    103                                 }
    104                                 offset[i] += size;
    105                         } while (size && offset[i] < CMP_BUFLEN);
     92                        rc = vfs_read(fd[i], &pos[i], buffer[i], CMP_BUFLEN,
     93                            &offset[i]);
     94                        if (rc != EOK) {
     95                                printf("Error reading from %s\n",
     96                                    fn[i]);
     97                                goto end;
     98                        }
    10699                }
    107100
    108101                if (offset[0] != offset[1] ||
    109102                    memcmp(buffer[0], buffer[1], offset[0]) != 0) {
     103                        printf("Return 1\n");
    110104                        rc = 1;
    111105                        goto end;
     
    149143
    150144        rc = cmp_files(argv[optind], argv[optind + 1]);
    151         if (rc)
     145        if (rc != EOK)
    152146                return CMD_FAILURE;
    153147        else
  • uspace/app/bdsh/cmds/modules/cp/cp.c

    r40feeac r8e3498b  
    6666} dentry_type_t;
    6767
    68 static int64_t copy_file(const char *src, const char *dest,
     68static int copy_file(const char *src, const char *dest,
    6969    size_t blen, int vb);
    7070
     
    175175}
    176176
    177 static int64_t do_copy(const char *src, const char *dest,
     177static int do_copy(const char *src, const char *dest,
    178178    size_t blen, int vb, int recursive, int force, int interactive)
    179179{
    180         int r = -1;
     180        int rc = EOK;
    181181        char dest_path[PATH_MAX];
    182182        char src_path[PATH_MAX];
     
    217217                                printf("The dest directory %s does not exists",
    218218                                    dest_path);
     219                                rc = ENOENT;
    219220                                goto exit;
    220221                        }
     
    224225                        printf("Cannot overwrite existing directory %s\n",
    225226                            dest_path);
     227                        rc = EEXIST;
    226228                        goto exit;
    227229                } else if (dest_type == TYPE_FILE) {
     
    233235                         */
    234236                        if (force && !interactive) {
    235                                 if (vfs_unlink_path(dest_path) != EOK) {
     237                                rc = vfs_unlink_path(dest_path);
     238                                if (rc != EOK) {
    236239                                        printf("Unable to remove %s\n",
    237240                                            dest_path);
     
    244247                                if (overwrite) {
    245248                                        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) {
    247251                                                printf("Unable to remove %s\n", dest_path);
    248252                                                goto exit;
     
    250254                                } else {
    251255                                        printf("Not overwriting file: %s\n", dest_path);
    252                                         r = 0;
     256                                        rc = EOK;
    253257                                        goto exit;
    254258                                }
    255259                        } else {
    256260                                printf("File already exists: %s\n", dest_path);
     261                                rc = EEXIST;
    257262                                goto exit;
    258263                        }
     
    260265
    261266                /* 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);
    263268
    264269        } else if (src_type == TYPE_DIR) {
     
    268273                        printf("Cannot copy the %s directory without the "
    269274                            "-r option\n", src);
     275                        rc = EINVAL;
    270276                        goto exit;
    271277                } else if (dest_type == TYPE_FILE) {
    272278                        printf("Cannot overwrite a file with a directory\n");
     279                        rc = EEXIST;
    273280                        goto exit;
    274281                }
     
    293300                                merge_paths(dest_path, PATH_MAX, src_dirname);
    294301
    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) {
    297305                                        printf("Unable to create "
    298306                                            "dest directory %s\n", dest_path);
     
    308316                         * e.g. cp -r /src /data/new_dir_src
    309317                         */
    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) {
    312320                                printf("Unable to create "
    313321                                    "dest directory %s\n", dest_path);
     
    321329                        /* Something strange is happening... */
    322330                        printf("Unable to open src %s directory\n", src);
     331                        rc = ENOENT;
    323332                        goto exit;
    324333                }
     
    348357                                printf("Cannot copy a directory "
    349358                                    "into itself\n");
     359                                rc = EEXIST;
    350360                                goto exit;
    351361                        }
     
    355365
    356366                        /* 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,
    358368                            force, interactive);
    359                         if (r)
     369                        if (rc != EOK)
    360370                                goto exit;
    361371
     
    367377        if (dir)
    368378                closedir(dir);
    369         return r;
    370 }
    371 
    372 static int64_t copy_file(const char *src, const char *dest,
     379        return rc;
     380}
     381
     382static int copy_file(const char *src, const char *dest,
    373383        size_t blen, int vb)
    374384{
    375         int fd1, fd2, bytes;
     385        int fd1, fd2;
     386        size_t rbytes, wbytes;
     387        int rc;
    376388        off64_t total;
    377         int64_t copied = 0;
    378389        char *buff = NULL;
    379390        aoff64_t posr = 0, posw = 0;
     
    410421                printf("Unable to allocate enough memory to read %s\n",
    411422                    src);
    412                 copied = -1;
     423                rc = ENOMEM;
    413424                goto out;
    414425        }
    415426
    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;
    425436        }
    426437
     
    430441        if (buff)
    431442                free(buff);
    432         return copied;
     443        return rc;
    433444}
    434445
  • uspace/app/bdsh/cmds/modules/mkfile/mkfile.c

    r40feeac r8e3498b  
    8585 *
    8686 * @param str   String containing the size specification.
    87  * @return      Non-negative size in bytes on success, -1 on failure.
     87 * @param rsize Place to store size in bytes
     88 * @return      EOK on success or error code
    8889 */
    89 static ssize_t read_size(const char *str)
     90static int read_size(const char *str, size_t *rsize)
    9091{
    91         ssize_t number, unit;
     92        size_t number, unit;
    9293        char *ep;
    9394
    9495        number = strtol(str, &ep, 10);
    95         if (ep[0] == '\0')
    96                 return number;
     96        if (ep[0] == '\0') {
     97                *rsize = number;
     98                return EOK;
     99        }
    97100
    98101        if (ep[1] != '\0')
    99                     return -1;
     102                    return EINVAL;
    100103
    101104        switch (tolower(ep[0])) {
     
    103106        case 'm': unit = 1024*1024; break;
    104107        case 'g': unit = 1024*1024*1024; break;
    105         default: return -1;
    106         }
    107 
    108         return number * unit;
     108        default: return EINVAL;
     109        }
     110
     111        *rsize = number * unit;
     112        return EOK;
    109113}
    110114
     
    114118        int c, opt_ind;
    115119        int fd;
    116         ssize_t file_size;
    117         ssize_t total_written;
    118         ssize_t to_write, rc, rc2 = 0;
     120        size_t file_size;
     121        size_t total_written;
     122        size_t to_write;
     123        size_t nwritten;
     124        int rc;
    119125        char *file_name;
    120126        void *buffer;
     
    136142                        break;
    137143                case 's':
    138                         file_size = read_size(optarg);
    139                         if (file_size < 0) {
     144                        rc = read_size(optarg, &file_size);
     145                        if (rc != EOK) {
    140146                                printf("%s: Invalid file size specification.\n",
    141147                                    cmdname);
     
    166172               
    167173                pos = file_size - 1;
    168                 rc2 = vfs_write(fd, &pos, &byte, sizeof(char));
    169                 if (rc2 < 0) {
     174                rc = vfs_write(fd, &pos, &byte, sizeof(char), &nwritten);
     175                if (rc != EOK) {
    170176                        vfs_put(fd);
    171177                        goto error;
     
    183189        while (total_written < file_size) {
    184190                to_write = min(file_size - total_written, BUFFER_SIZE);
    185                 rc = vfs_write(fd, &pos, buffer, to_write);
    186                 if (rc <= 0) {
     191                rc = vfs_write(fd, &pos, buffer, to_write, &nwritten);
     192                if (rc != EOK) {
    187193                        printf("%s: Error writing file (%d).\n", cmdname, errno);
    188194                        vfs_put(fd);
     
    190196                        return CMD_FAILURE;
    191197                }
    192                 total_written += rc;
     198                total_written += nwritten;
    193199        }
    194200
Note: See TracChangeset for help on using the changeset viewer.