Changeset 8e3498b in mainline


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
Files:
21 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
  • uspace/app/sysinst/futil.c

    r40feeac r8e3498b  
    5757{
    5858        int sf, df;
    59         ssize_t nr, nw;
     59        size_t nr, nw;
    6060        int rc;
    6161        aoff64_t posr = 0, posw = 0;
     
    7272
    7373        do {
    74                 nr = vfs_read(sf, &posr, buf, BUF_SIZE);
     74                rc = vfs_read(sf, &posr, buf, BUF_SIZE, &nr);
     75                if (rc != EOK)
     76                        goto error;
    7577                if (nr == 0)
    7678                        break;
    77                 if (nr < 0)
    78                         return EIO;
    79 
    80                 nw = vfs_write(df, &posw, buf, nr);
    81                 if (nw <= 0)
    82                         return EIO;
    83         } while (true);
     79
     80                rc= vfs_write(df, &posw, buf, nr, &nw);
     81                if (rc != EOK)
     82                        goto error;
     83
     84        } while (nr == BUF_SIZE);
    8485
    8586        (void) vfs_put(sf);
     
    9091
    9192        return EOK;
     93error:
     94        vfs_put(sf);
     95        vfs_put(df);
     96        return rc;
    9297}
    9398
     
    156161{
    157162        int sf;
    158         ssize_t nr;
     163        size_t nr;
     164        int rc;
    159165        size_t fsize;
    160166        char *data;
     
    168174                vfs_put(sf);
    169175                return EIO;
    170         }       
     176        }
    171177
    172178        fsize = st.size;
     
    178184        }
    179185
    180         nr = vfs_read(sf, (aoff64_t []) { 0 }, data, fsize);
    181         if (nr != (ssize_t)fsize) {
     186        rc = vfs_read(sf, (aoff64_t []) { 0 }, data, fsize, &nr);
     187        if (rc != EOK || nr != fsize) {
    182188                vfs_put(sf);
    183189                free(data);
  • uspace/app/taskdump/elf_core.c

    r40feeac r8e3498b  
    9797
    9898        int fd;
    99         ssize_t rc;
     99        int rc;
     100        size_t nwr;
    100101        unsigned int i;
    101102
     
    206207        }
    207208
    208         rc = vfs_write(fd, &pos, &elf_hdr, sizeof(elf_hdr));
    209         if (rc != sizeof(elf_hdr)) {
     209        rc = vfs_write(fd, &pos, &elf_hdr, sizeof(elf_hdr), &nwr);
     210        if (rc != EOK) {
    210211                printf("Failed writing ELF header.\n");
    211212                free(p_hdr);
     
    214215
    215216        for (i = 0; i < n_ph; ++i) {
    216                 rc = vfs_write(fd, &pos, &p_hdr[i], sizeof(p_hdr[i]));
    217                 if (rc != sizeof(p_hdr[i])) {
     217                rc = vfs_write(fd, &pos, &p_hdr[i], sizeof(p_hdr[i]), &nwr);
     218                if (rc != EOK) {
    218219                        printf("Failed writing program header.\n");
    219220                        free(p_hdr);
     
    231232        note.type = NT_PRSTATUS;
    232233
    233         rc = vfs_write(fd, &pos, &note, sizeof(elf_note_t));
    234         if (rc != sizeof(elf_note_t)) {
     234        rc = vfs_write(fd, &pos, &note, sizeof(elf_note_t), &nwr);
     235        if (rc != EOK) {
    235236                printf("Failed writing note header.\n");
    236237                free(p_hdr);
     
    238239        }
    239240
    240         rc = vfs_write(fd, &pos, "CORE", note.namesz);
    241         if (rc != (ssize_t) note.namesz) {
     241        rc = vfs_write(fd, &pos, "CORE", note.namesz, &nwr);
     242        if (rc != EOK) {
    242243                printf("Failed writing note header.\n");
    243244                free(p_hdr);
     
    247248        pos = ALIGN_UP(pos, word_size);
    248249
    249         rc = vfs_write(fd, &pos, &pr_status, sizeof(elf_prstatus_t));
    250         if (rc != sizeof(elf_prstatus_t)) {
     250        rc = vfs_write(fd, &pos, &pr_status, sizeof(elf_prstatus_t), &nwr);
     251        if (rc != EOK) {
    251252                printf("Failed writing register data.\n");
    252253                free(p_hdr);
     
    296297        size_t total;
    297298        uintptr_t addr;
    298         ssize_t rc;
     299        int rc;
     300        size_t nwr;
    299301
    300302        addr = area->start_addr;
     
    309311                }
    310312
    311                 rc = vfs_write(fd, pos, buffer, to_copy);
    312                 if (rc != (ssize_t) to_copy) {
     313                rc = vfs_write(fd, pos, buffer, to_copy, &nwr);
     314                if (rc != EOK) {
    313315                        printf("Failed writing memory contents.\n");
    314316                        return EIO;
  • uspace/app/taskdump/symtab.c

    r40feeac r8e3498b  
    7171        int fd;
    7272        int rc;
     73        size_t nread;
    7374        int i;
    7475
     
    8889        }
    8990
    90         rc = vfs_read(fd, &pos, &elf_hdr, sizeof(elf_header_t));
    91         if (rc != sizeof(elf_header_t)) {
     91        rc = vfs_read(fd, &pos, &elf_hdr, sizeof(elf_header_t), &nread);
     92        if (rc != EOK || nread != sizeof(elf_header_t)) {
    9293                printf("failed reading elf header\n");
    9394                free(stab);
     
    304305{
    305306        int rc;
     307        size_t nread;
    306308        aoff64_t pos = elf_hdr->e_shoff + idx * sizeof(elf_section_header_t);
    307309
    308         rc = vfs_read(fd, &pos, sec_hdr, sizeof(elf_section_header_t));
    309         if (rc != sizeof(elf_section_header_t))
     310        rc = vfs_read(fd, &pos, sec_hdr, sizeof(elf_section_header_t), &nread);
     311        if (rc != EOK || nread != sizeof(elf_section_header_t))
    310312                return EIO;
    311313
     
    326328static int chunk_load(int fd, off64_t start, size_t size, void **ptr)
    327329{
    328         ssize_t rc;
     330        int rc;
     331        size_t nread;
    329332        aoff64_t pos = start;
    330333
     
    335338        }
    336339
    337         rc = vfs_read(fd, &pos, *ptr, size);
    338         if (rc != (ssize_t) size) {
     340        rc = vfs_read(fd, &pos, *ptr, size, &nread);
     341        if (rc != EOK || nread != size) {
    339342                printf("failed reading chunk\n");
    340343                free(*ptr);
  • uspace/app/tester/mm/pager1.c

    r40feeac r8e3498b  
    4444static void *create_paged_area(size_t size)
    4545{
     46        size_t nwr;
     47        int rc;
     48
    4649        TPRINTF("Creating temporary file...\n");
    4750
     
    5255        (void) vfs_unlink_path(TEST_FILE);
    5356
    54         if (vfs_write(fd, (aoff64_t []) {0}, text, sizeof(text)) < 0) {
     57        rc = vfs_write(fd, (aoff64_t []) {0}, text, sizeof(text), &nwr);
     58        if (rc != EOK) {
    5559                vfs_put(fd);
    5660                return NULL;
  • uspace/app/tester/vfs/vfs1.c

    r40feeac r8e3498b  
    8484       
    8585        size_t size = sizeof(text);
    86         ssize_t cnt = vfs_write(fd0, &pos, text, size);
    87         if (cnt < 0)
     86        size_t cnt;
     87        rc  = vfs_write(fd0, &pos, text, size, &cnt);
     88        if (rc != EOK)
    8889                return "write() failed";
    8990        TPRINTF("Written %zd bytes\n", cnt);
     
    9394        char buf[BUF_SIZE];
    9495        TPRINTF("read..\n");
    95         while ((cnt = vfs_read(fd0, &pos, buf, BUF_SIZE))) {
    96                 TPRINTF("read returns %zd\n", cnt);
    97                 if (cnt < 0)
     96        while ((rc = vfs_read(fd0, &pos, buf, BUF_SIZE, &cnt))) {
     97                TPRINTF("read returns rc = %d, cnt = %zu\n", rc, cnt);
     98                if (rc != EOK)
    9899                        return "read() failed";
    99100               
    100                 int _cnt = (int) cnt;
    101                 if (_cnt != cnt) {
     101                int icnt = (int) cnt;
     102                if ((size_t) icnt != cnt) {
    102103                        /* Count overflow, just to be sure. */
    103104                        TPRINTF("Read %zd bytes\n", cnt);
    104105                } else {
    105                         TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, _cnt, buf);
     106                        TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, icnt, buf);
    106107                }
    107108        }
  • uspace/app/viewer/viewer.c

    r40feeac r8e3498b  
    126126        }
    127127
    128         ssize_t rd = vfs_read(fd, (aoff64_t []) {0}, tga, stat.size);
    129         if ((rd < 0) || (rd != (ssize_t) stat.size)) {
     128        size_t nread;
     129        rc = vfs_read(fd, (aoff64_t []) {0}, tga, stat.size, &nread);
     130        if (rc != EOK || nread != stat.size) {
    130131                free(tga);
    131132                vfs_put(fd);
  • uspace/app/websrv/websrv.c

    r40feeac r8e3498b  
    247247        char *fname = NULL;
    248248        int rc;
     249        size_t nr;
    249250        int fd = -1;
    250251       
     
    279280        aoff64_t pos = 0;
    280281        while (true) {
    281                 ssize_t nr = vfs_read(fd, &pos, fbuf, BUFFER_SIZE);
     282                rc = vfs_read(fd, &pos, fbuf, BUFFER_SIZE, &nr);
     283                if (rc != EOK)
     284                        goto out;
     285               
    282286                if (nr == 0)
    283287                        break;
    284                
    285                 if (nr < 0) {
    286                         rc = EIO;
    287                         goto out;
    288                 }
    289288               
    290289                rc = tcp_conn_send(conn, fbuf, nr);
  • uspace/drv/bus/isa/isa.c

    r40feeac r8e3498b  
    277277        int fd;
    278278        size_t len;
    279         ssize_t r;
     279        int rc;
     280        size_t nread;
    280281        struct stat st;
    281282
     
    306307        }
    307308
    308         r = vfs_read(fd, (aoff64_t []) {0}, buf, len);
    309         if (r < 0) {
     309        rc = vfs_read(fd, (aoff64_t []) {0}, buf, len, &nread);
     310        if (rc != EOK) {
    310311                ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
    311312                goto cleanup;
    312313        }
    313314
    314         buf[len] = 0;
     315        buf[nread] = 0;
    315316
    316317        suc = true;
  • uspace/lib/bithenge/src/file.c

    r40feeac r8e3498b  
    7777                return ELIMIT;
    7878
    79         ssize_t amount_read = vfs_read(blob->fd, &offset, buffer, *size);
    80         if (amount_read < 0)
     79        size_t amount_read;
     80        int rc = vfs_read(blob->fd, &offset, buffer, *size, &amount_read);
     81        if (rc != EOK)
    8182                return errno;
    8283        *size += amount_read;
  • uspace/lib/c/generic/elf/elf_mod.c

    r40feeac r8e3498b  
    137137        elf_header_t *header = &header_buf;
    138138        aoff64_t pos = 0;
     139        size_t nr;
    139140        int i, rc;
    140141
    141         rc = vfs_read(elf->fd, &pos, header, sizeof(elf_header_t));
    142         if (rc != sizeof(elf_header_t)) {
     142        rc = vfs_read(elf->fd, &pos, header, sizeof(elf_header_t), &nr);
     143        if (rc != EOK || nr != sizeof(elf_header_t)) {
    143144                DPRINTF("Read error.\n");
    144145                return EE_INVALID;
     
    199200                pos = header->e_phoff + i * sizeof(elf_segment_header_t);
    200201                rc = vfs_read(elf->fd, &pos, &segment_hdr,
    201                     sizeof(elf_segment_header_t));
    202                 if (rc != sizeof(elf_segment_header_t)) {
     202                    sizeof(elf_segment_header_t), &nr);
     203                if (rc != EOK || nr != sizeof(elf_segment_header_t)) {
    203204                        DPRINTF("Read error.\n");
    204205                        return EE_INVALID;
     
    218219                pos = header->e_shoff + i * sizeof(elf_section_header_t);
    219220                rc = vfs_read(elf->fd, &pos, &section_hdr,
    220                     sizeof(elf_section_header_t));
    221                 if (rc != sizeof(elf_section_header_t)) {
     221                    sizeof(elf_section_header_t), &nr);
     222                if (rc != EOK || nr != sizeof(elf_section_header_t)) {
    222223                        DPRINTF("Read error.\n");
    223224                        return EE_INVALID;
     
    330331        size_t mem_sz;
    331332        aoff64_t pos;
    332         ssize_t rc;
     333        int rc;
     334        size_t nr;
    333335
    334336        bias = elf->bias;
     
    388390         */
    389391        pos = entry->p_offset;
    390         rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz);
    391         if (rc < 0) {
     392        rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz, &nr);
     393        if (rc != EOK || nr != entry->p_filesz) {
    392394                DPRINTF("read error\n");
    393395                return EE_INVALID;
  • uspace/lib/c/generic/io/io.c

    r40feeac r8e3498b  
    430430static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
    431431{
     432        int rc;
     433        size_t nread;
     434
    432435        if (size == 0 || nmemb == 0)
    433436                return 0;
    434437
    435         ssize_t rd = vfs_read(stream->fd, &stream->pos, buf, size * nmemb);
    436         if (rd < 0) {
    437                 errno = rd;
     438        rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
     439        if (rc != EOK) {
     440                errno = rc;
    438441                stream->error = true;
    439                 rd = 0;
    440         } else if (rd == 0) {
     442        } else if (nread == 0) {
    441443                stream->eof = true;
    442444        }
    443        
    444         return (rd / size);
     445
     446        return (nread / size);
    445447}
    446448
     
    457459static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
    458460{
     461        int rc;
     462        size_t nwritten;
     463
    459464        if (size == 0 || nmemb == 0)
    460465                return 0;
    461466
    462         ssize_t wr;
    463467        if (stream->kio) {
    464                 size_t nwritten;
    465                 wr = kio_write(buf, size * nmemb, &nwritten);
    466                 if (wr != EOK) {
     468                rc = kio_write(buf, size * nmemb, &nwritten);
     469                if (rc != EOK) {
    467470                        stream->error = true;
    468                         wr = 0;
    469                 } else {
    470                         wr = nwritten;
     471                        nwritten = 0;
    471472                }
    472473        } else {
    473                 wr = vfs_write(stream->fd, &stream->pos, buf, size * nmemb);
    474                 if (wr < 0) {
    475                         errno = wr;
     474                rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb,
     475                    &nwritten);
     476                if (rc != EOK) {
     477                        errno = rc;
    476478                        stream->error = true;
    477                         wr = 0;
    478479                }
    479480        }
    480481
    481         if (wr > 0)
     482        if (nwritten > 0)
    482483                stream->need_sync = true;
    483        
    484         return (wr / size);
     484
     485        return (nwritten / size);
    485486}
    486487
     
    491492static void _ffillbuf(FILE *stream)
    492493{
    493         ssize_t rc;
     494        int rc;
     495        size_t nread;
    494496
    495497        stream->buf_head = stream->buf_tail = stream->buf;
    496498
    497         rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size);
    498         if (rc < 0) {
     499        rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
     500            &nread);
     501        if (rc != EOK) {
    499502                errno = rc;
    500503                stream->error = true;
     
    502505        }
    503506
    504         if (rc == 0) {
     507        if (nread == 0) {
    505508                stream->eof = true;
    506509                return;
    507510        }
    508511
    509         stream->buf_head += rc;
     512        stream->buf_head += nread;
    510513        stream->buf_state = _bs_read;
    511514}
  • uspace/lib/c/generic/vfs/vfs.c

    r40feeac r8e3498b  
    8686 * and consume system resources.
    8787 *
    88  * Functions that return int return a negative error code on error and do not
     88 * Functions that return int return an error code on error and do not
    8989 * set errno. Depending on function, success is signalled by returning either
    9090 * EOK or a non-negative file handle.
     
    104104 *      aoff64_t pos = 42;
    105105 *      char buf[512];
    106  *      ssize_t size = vfs_read(file, &pos, buf, sizeof(buf));
    107  *      if (size < 0) {
     106 *      size_t nread;
     107 *      rc = vfs_read(file, &pos, buf, sizeof(buf), &nread);
     108 *      if (rc != EOK) {
    108109 *              vfs_put(file);
    109  *              return size;
     110 *              return rc;
    110111 *      }
    111112 *
    112  *      // buf is now filled with data from file
     113 *      // buf is now filled with nread bytes from file
    113114 *
    114115 *      vfs_put(file);
     
    808809 * @param buf           Buffer, @a nbytes bytes long
    809810 * @param nbytes        Number of bytes to read
    810  *
    811  * @return              On success, non-negative number of bytes read
    812  * @return              On failure, a negative error code
    813  */
    814 ssize_t vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte)
     811 * @param nread         Place to store number of bytes actually read
     812 *
     813 * @return              On success, EOK and @a *nread is filled with number
     814 *                      of bytes actually read.
     815 * @return              On failure, an error code
     816 */
     817int vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte, size_t *nread)
    815818{
    816819        ssize_t cnt = 0;
    817         size_t nread = 0;
     820        size_t nr = 0;
    818821        uint8_t *bp = (uint8_t *) buf;
    819822        int rc;
     
    821824        do {
    822825                bp += cnt;
    823                 nread += cnt;
     826                nr += cnt;
    824827                *pos += cnt;
    825                 rc = vfs_read_short(file, *pos, bp, nbyte - nread, &cnt);
    826         } while (rc == EOK && cnt > 0 && (nbyte - nread - cnt) > 0);
    827        
    828         if (rc != EOK)
     828                rc = vfs_read_short(file, *pos, bp, nbyte - nr, &cnt);
     829        } while (rc == EOK && cnt > 0 && (nbyte - nr - cnt) > 0);
     830       
     831        if (rc != EOK) {
     832                *nread = nr;
    829833                return rc;
    830        
     834        }
     835       
     836        nr += cnt;
    831837        *pos += cnt;
    832         return nread + cnt;
     838        *nread = nr;
     839        return EOK;
    833840}
    834841
     
    12471254 * @param buf           Data, @a nbytes bytes long
    12481255 * @param nbytes        Number of bytes to write
    1249  *
    1250  * @return              On success, non-negative number of bytes written
    1251  * @return              On failure, a negative error code
    1252  */
    1253 ssize_t vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte)
     1256 * @param nwritten      Place to store number of bytes written
     1257 *
     1258 * @return              On success, EOK, @a *nwr is filled with number
     1259 *                      of bytes written
     1260 * @return              On failure, an error code
     1261 */
     1262int vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte,
     1263    size_t *nwritten)
    12541264{
    12551265        ssize_t cnt = 0;
    1256         ssize_t nwritten = 0;
     1266        ssize_t nwr = 0;
    12571267        const uint8_t *bp = (uint8_t *) buf;
    12581268        int rc;
     
    12601270        do {
    12611271                bp += cnt;
    1262                 nwritten += cnt;
     1272                nwr += cnt;
    12631273                *pos += cnt;
    1264                 rc = vfs_write_short(file, *pos, bp, nbyte - nwritten, &cnt);
    1265         } while (rc == EOK && ((ssize_t )nbyte - nwritten - cnt) > 0);
    1266 
    1267         if (rc != EOK)
     1274                rc = vfs_write_short(file, *pos, bp, nbyte - nwr, &cnt);
     1275        } while (rc == EOK && ((ssize_t )nbyte - nwr - cnt) > 0);
     1276
     1277        if (rc != EOK) {
     1278                *nwritten = nwr;
    12681279                return rc;
    1269 
     1280        }
     1281
     1282        nwr += cnt;
    12701283        *pos += cnt;
    1271         return nbyte;
     1284        *nwritten = nwr;
     1285        return EOK;
    12721286}
    12731287
  • uspace/lib/c/include/vfs/vfs.h

    r40feeac r8e3498b  
    104104extern int vfs_pass_handle(async_exch_t *, int, async_exch_t *);
    105105extern int vfs_put(int);
    106 extern ssize_t vfs_read(int, aoff64_t *, void *, size_t);
     106extern int vfs_read(int, aoff64_t *, void *, size_t, size_t *);
    107107extern int vfs_read_short(int, aoff64_t, void *, size_t, ssize_t *);
    108108extern int vfs_receive_handle(bool);
     
    121121extern int vfs_unmount_path(const char *);
    122122extern int vfs_walk(int, const char *, int);
    123 extern ssize_t vfs_write(int, aoff64_t *, const void *, size_t);
     123extern int vfs_write(int, aoff64_t *, const void *, size_t, size_t *);
    124124extern int vfs_write_short(int, aoff64_t, const void *, size_t, ssize_t *);
    125125
  • uspace/lib/pcut/src/os/helenos.c

    r40feeac r8e3498b  
    213213
    214214        aoff64_t pos = 0;
    215         vfs_read(tempfile, &pos, extra_output_buffer, OUTPUT_BUFFER_SIZE);
     215        size_t nread;
     216        vfs_read(tempfile, &pos, extra_output_buffer, OUTPUT_BUFFER_SIZE, &nread);
    216217
    217218leave_close_tempfile:
  • uspace/lib/posix/source/stdio.c

    r40feeac r8e3498b  
    343343{
    344344        const int fildes = *(int *) fd;
    345         ssize_t wr = vfs_write(fildes, &posix_pos[fildes], str, size);
    346         if (wr < 0)
    347                 return wr;
     345        size_t wr;
     346        int rc = vfs_write(fildes, &posix_pos[fildes], str, size, &wr);
     347        if (rc != EOK)
     348                return rc;
    348349        return str_nlength(str, wr);
    349350}
     
    371372               
    372373                const int fildes = *(int *) fd;
    373                 if (vfs_write(fildes, &posix_pos[fildes], buf, sz) < 0)
     374                size_t nwr;
     375                if (vfs_write(fildes, &posix_pos[fildes], buf, sz, &nwr) != EOK)
    374376                        break;
    375377               
  • uspace/lib/posix/source/unistd.c

    r40feeac r8e3498b  
    213213ssize_t posix_read(int fildes, void *buf, size_t nbyte)
    214214{
    215         ssize_t size = rcerrno(vfs_read, fildes, &posix_pos[fildes], buf, nbyte);
    216         if (size < 0)
    217                 return -1;
    218         return size;
     215        size_t nread;
     216        int rc;
     217
     218        rc = rcerrno(vfs_read, fildes, &posix_pos[fildes], buf, nbyte, &nread);
     219        if (rc != EOK)
     220                return -1;
     221        return (ssize_t) nread;
    219222}
    220223
     
    229232ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
    230233{
    231         ssize_t size = rcerrno(vfs_write, fildes, &posix_pos[fildes], buf, nbyte);
    232         if (size < 0)
    233                 return -1;
    234         return size;
     234        size_t nwr;
     235        int rc;
     236
     237        rc = rcerrno(vfs_write, fildes, &posix_pos[fildes], buf, nbyte, &nwr);
     238        if (rc != EOK)
     239                return -1;
     240        return nwr;
    235241}
    236242
  • uspace/srv/devman/match.c

    r40feeac r8e3498b  
    229229        }
    230230       
    231         ssize_t read_bytes = vfs_read(fd, (aoff64_t []) {0}, buf, len);
    232         if (read_bytes <= 0) {
     231        size_t read_bytes;
     232        int rc = vfs_read(fd, (aoff64_t []) {0}, buf, len, &read_bytes);
     233        if (rc != EOK) {
    233234                log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to read file '%s' (%d).", conf_path,
    234235                    errno);
Note: See TracChangeset for help on using the changeset viewer.