Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/vfs/vfs.c

    r8d2dd7f2 rf77c1c9  
    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);
     
    127128static int root_fd = -1;
    128129
    129 static int get_parent_and_child(const char *path, char **child)
     130static int get_parent_and_child(const char *path, int *parent, char **child)
    130131{
    131132        size_t size;
     
    135136
    136137        char *slash = str_rchr(apath, L'/');
    137         int parent;
    138138        if (slash == apath) {
    139                 parent = vfs_root();
     139                *parent = vfs_root();
     140                if (*parent < 0) {
     141                        free(apath);
     142                        return EBADF;
     143                }
    140144                *child = apath;
     145                return EOK;
    141146        } else {
    142147                *slash = '\0';
    143                 parent = vfs_lookup(apath, WALK_DIRECTORY);
    144                 if (parent < 0) {
     148                int rc = vfs_lookup(apath, WALK_DIRECTORY, parent);
     149                if (rc != EOK) {
    145150                        free(apath);
    146                         return parent;
     151                        return rc;
    147152                }
    148153                *slash = '/';
     
    150155                free(apath);
    151156                if (!*child) {
    152                         vfs_put(parent);
     157                        vfs_put(*parent);
    153158                        return ENOMEM;
    154159                }
    155         }
    156 
    157         return parent;
     160
     161                return rc;
     162        }
     163
    158164}
    159165
     
    232238 * @return              New file handle on success or a negative error code
    233239 */
    234 int vfs_clone(int file_from, int file_to, bool high)
    235 {
     240int vfs_clone(int file_from, int file_to, bool high, int *handle)
     241{
     242        assert(handle != NULL);
     243
    236244        async_exch_t *vfs_exch = vfs_exchange_begin();
    237         int rc = async_req_3_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from,
    238             (sysarg_t) file_to, (sysarg_t) high);
     245        sysarg_t ret;
     246        int rc = async_req_3_1(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from,
     247            (sysarg_t) file_to, (sysarg_t) high, &ret);
    239248        vfs_exchange_end(vfs_exch);
     249
     250        if (rc == EOK) {
     251                *handle = ret;
     252        }
    240253        return rc;
    241254}
     
    276289                return ENOMEM;
    277290       
    278         int fd = vfs_lookup(abs, WALK_DIRECTORY);
    279         if (fd < 0) {
     291        int fd;
     292        int rc = vfs_lookup(abs, WALK_DIRECTORY, &fd);
     293        if (rc != EOK) {
    280294                free(abs);
    281                 return fd;
     295                return rc;
    282296        }
    283297       
     
    490504{
    491505        int flags = (kind == KIND_DIRECTORY) ? WALK_DIRECTORY : WALK_REGULAR;
    492         int file = vfs_walk(parent, child, WALK_MUST_CREATE | flags);
    493 
    494         if (file < 0)
    495                 return file;
     506        int file = -1;
     507        int rc = vfs_walk(parent, child, WALK_MUST_CREATE | flags, &file);
     508        if (rc != EOK)
     509                return rc;
    496510
    497511        if (linkedfd)
     
    520534{
    521535        char *child;
    522         int parent = get_parent_and_child(path, &child);
    523         if (parent < 0)
    524                 return parent;
    525 
    526         int rc = vfs_link(parent, child, kind, linkedfd);
     536        int parent;
     537        int rc = get_parent_and_child(path, &parent, &child);
     538        if (rc != EOK)
     539                return rc;
     540
     541        rc = vfs_link(parent, child, kind, linkedfd);
    527542
    528543        free(child);
    529544        vfs_put(parent);
    530545        return rc;
    531 }       
     546}
    532547
    533548/** Lookup a path relative to the local root
     
    535550 * @param path  Path to be looked up
    536551 * @param flags Walk flags
    537  *
    538  * @return      File handle representing the result on success or a negative
    539  *              error code on error
    540  */
    541 int vfs_lookup(const char *path, int flags)
     552 * @param[out] handle Pointer to variable where handle is to be written.
     553 *
     554 * @return      EOK on success or an error code.
     555 */
     556int vfs_lookup(const char *path, int flags, int *handle)
    542557{
    543558        size_t size;
     
    545560        if (!p)
    546561                return ENOMEM;
     562
    547563        int root = vfs_root();
    548564        if (root < 0) {
     
    550566                return ENOENT;
    551567        }
    552         int rc = vfs_walk(root, p, flags);
     568
     569        int rc = vfs_walk(root, p, flags, handle);
    553570        vfs_put(root);
    554571        free(p);
     
    563580 * @param flags Walk flags
    564581 * @param mode  Mode in which to open file in
     582 * @param[out] handle Pointer to variable where handle is to be written.
    565583 *
    566584 * @return      EOK on success or a negative error code
    567585 */
    568 int vfs_lookup_open(const char *path, int flags, int mode)
    569 {
    570         int file = vfs_lookup(path, flags);
    571         if (file < 0)
    572                 return file;
    573 
    574         int rc = vfs_open(file, mode);
     586int vfs_lookup_open(const char *path, int flags, int mode, int *handle)
     587{
     588        int file;
     589        int rc = vfs_lookup(path, flags, &file);
     590        if (rc != EOK)
     591                return rc;
     592
     593        rc = vfs_open(file, mode);
    575594        if (rc != EOK) {
    576595                vfs_put(file);
    577596                return rc;
    578597        }
    579        
    580         return file;
     598
     599        *handle = file;
     600        return EOK;
    581601}
    582602
     
    706726                }
    707727               
    708                 int mpfd = vfs_walk(root_fd, mpa, WALK_DIRECTORY);
    709                 if (mpfd >= 0) {
     728                int mpfd;
     729                rc = vfs_walk(root_fd, mpa, WALK_DIRECTORY, &mpfd);
     730                if (rc == EOK) {
    710731                        rc = vfs_mount(mpfd, fs_name, service_id, opts, flags,
    711732                            instance, NULL);
    712733                        vfs_put(mpfd);
    713                 } else {
    714                         rc = mpfd;
    715734                }
    716735        }
     
    774793 * @param high   If true, the received file handle will be allocated from high
    775794 *               indices
     795 * @param[out] handle  Received handle.
    776796 *
    777797 * @return       EOK on success or a negative error code
    778798 */
    779 int vfs_receive_handle(bool high)
     799int vfs_receive_handle(bool high, int *handle)
    780800{
    781801        ipc_callid_t callid;
     
    794814        async_exchange_end(vfs_exch);
    795815
    796         if (rc != EOK)
    797                 return rc;
    798         return ret;
     816        if (rc == EOK) {
     817                *handle = (int) ret;
     818        }
     819
     820        return rc;
    799821}
    800822
     
    808830 * @param buf           Buffer, @a nbytes bytes long
    809831 * @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)
     832 * @param nread         Place to store number of bytes actually read
     833 *
     834 * @return              On success, EOK and @a *nread is filled with number
     835 *                      of bytes actually read.
     836 * @return              On failure, an error code
     837 */
     838int vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte, size_t *nread)
    815839{
    816840        ssize_t cnt = 0;
    817         size_t nread = 0;
     841        size_t nr = 0;
    818842        uint8_t *bp = (uint8_t *) buf;
    819843        int rc;
     
    821845        do {
    822846                bp += cnt;
    823                 nread += cnt;
     847                nr += cnt;
    824848                *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)
    829                 return rc;
    830        
     849                rc = vfs_read_short(file, *pos, bp, nbyte - nr, &cnt);
     850        } while (rc == EOK && cnt > 0 && (nbyte - nr - cnt) > 0);
     851       
     852        if (rc != EOK) {
     853                *nread = nr;
     854                return rc;
     855        }
     856       
     857        nr += cnt;
    831858        *pos += cnt;
    832         return nread + cnt;
     859        *nread = nr;
     860        return EOK;
    833861}
    834862
     
    969997/** Return a new file handle representing the local root
    970998 *
    971  * @return      A clone of the local root file handle or a negative error code
     999 * @return      A clone of the local root file handle or -1
    9721000 */
    9731001int vfs_root(void)
    9741002{
    975         fibril_mutex_lock(&root_mutex);
    976         int r;
    977         if (root_fd < 0)
    978                 r = ENOENT;
    979         else
    980                 r = vfs_clone(root_fd, -1, true);
     1003        fibril_mutex_lock(&root_mutex);
     1004        int fd;
     1005        if (root_fd < 0) {
     1006                fd = -1;
     1007        } else {
     1008                int rc = vfs_clone(root_fd, -1, true, &fd);
     1009                if (rc != EOK) {
     1010                        fd = -1;
     1011                }
     1012        }
    9811013        fibril_mutex_unlock(&root_mutex);
    982         return r;
     1014        return fd;
    9831015}
    9841016
     
    9901022 *
    9911023 * @param nroot The new local root file handle
    992  */
    993 void vfs_root_set(int nroot)
    994 {
     1024 *
     1025 * @return  Error code
     1026 */
     1027int vfs_root_set(int nroot)
     1028{
     1029        int new_root;
     1030        int rc = vfs_clone(nroot, -1, true, &new_root);
     1031        if (rc != EOK) {
     1032                return rc;
     1033        }
     1034
    9951035        fibril_mutex_lock(&root_mutex);
    9961036        if (root_fd >= 0)
    9971037                vfs_put(root_fd);
    998         root_fd = vfs_clone(nroot, -1, true);
     1038        root_fd = new_root;
    9991039        fibril_mutex_unlock(&root_mutex);
     1040
     1041        return EOK;
    10001042}
    10011043
     
    10431085int vfs_stat_path(const char *path, struct stat *stat)
    10441086{
    1045         int file = vfs_lookup(path, 0);
    1046         if (file < 0)
    1047                 return file;
    1048        
    1049         int rc = vfs_stat(file, stat);
     1087        int file;
     1088        int rc = vfs_lookup(path, 0, &file);
     1089        if (rc != EOK)
     1090                return rc;
     1091       
     1092        rc = vfs_stat(file, stat);
    10501093
    10511094        vfs_put(file);
     
    10881131int vfs_statfs_path(const char *path, struct statfs *st)
    10891132{
    1090         int file = vfs_lookup(path, 0);
    1091         if (file < 0)
    1092                 return file;
    1093        
    1094         int rc = vfs_statfs(file, st);
     1133        int file;
     1134        int rc = vfs_lookup(path, 0, &file);
     1135        if (rc != EOK)
     1136                return rc;
     1137       
     1138        rc = vfs_statfs(file, st);
    10951139
    10961140        vfs_put(file);
     
    11581202int vfs_unlink_path(const char *path)
    11591203{
    1160         int expect = vfs_lookup(path, 0);
    1161         if (expect < 0)
    1162                 return expect;
     1204        int expect;
     1205        int rc = vfs_lookup(path, 0, &expect);
     1206        if (rc != EOK)
     1207                return rc;
    11631208
    11641209        char *child;
    1165         int parent = get_parent_and_child(path, &child);
    1166         if (parent < 0) {
     1210        int parent;
     1211        rc = get_parent_and_child(path, &parent, &child);
     1212        if (rc != EOK) {
    11671213                vfs_put(expect);
    1168                 return parent;
    1169         }
    1170 
    1171         int rc = vfs_unlink(parent, child, expect);
     1214                return rc;
     1215        }
     1216
     1217        rc = vfs_unlink(parent, child, expect);
    11721218       
    11731219        free(child);
     
    11991245int vfs_unmount_path(const char *mpp)
    12001246{
    1201         int mp = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY);
    1202         if (mp < 0)
    1203                 return mp;
    1204        
    1205         int rc = vfs_unmount(mp);
     1247        int mp;
     1248        int rc = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY, &mp);
     1249        if (rc != EOK)
     1250                return rc;
     1251       
     1252        rc = vfs_unmount(mp);
    12061253        vfs_put(mp);
    12071254        return rc;
     
    12131260 * @param path          Parent-relative path to be walked
    12141261 * @param flags         Flags influencing the walk
    1215  *
    1216  * @retrun              File handle representing the result on success or
    1217  *                      a negative error code on error
    1218  */
    1219 int vfs_walk(int parent, const char *path, int flags)
     1262 * @param[out] handle   File handle representing the result on success.
     1263 *
     1264 * @return              Error code.
     1265 */
     1266int vfs_walk(int parent, const char *path, int flags, int *handle)
    12201267{
    12211268        async_exch_t *exch = vfs_exchange_begin();
     
    12351282                return (int) rc;
    12361283       
    1237         return (int) IPC_GET_ARG1(answer);
     1284        *handle = (int) IPC_GET_ARG1(answer);
     1285        return EOK;
    12381286}
    12391287
     
    12471295 * @param buf           Data, @a nbytes bytes long
    12481296 * @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)
     1297 * @param nwritten      Place to store number of bytes written
     1298 *
     1299 * @return              On success, EOK, @a *nwr is filled with number
     1300 *                      of bytes written
     1301 * @return              On failure, an error code
     1302 */
     1303int vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte,
     1304    size_t *nwritten)
    12541305{
    12551306        ssize_t cnt = 0;
    1256         ssize_t nwritten = 0;
     1307        ssize_t nwr = 0;
    12571308        const uint8_t *bp = (uint8_t *) buf;
    12581309        int rc;
     
    12601311        do {
    12611312                bp += cnt;
    1262                 nwritten += cnt;
     1313                nwr += cnt;
    12631314                *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)
    1268                 return rc;
    1269 
     1315                rc = vfs_write_short(file, *pos, bp, nbyte - nwr, &cnt);
     1316        } while (rc == EOK && ((ssize_t )nbyte - nwr - cnt) > 0);
     1317
     1318        if (rc != EOK) {
     1319                *nwritten = nwr;
     1320                return rc;
     1321        }
     1322
     1323        nwr += cnt;
    12701324        *pos += cnt;
    1271         return nbyte;
     1325        *nwritten = nwr;
     1326        return EOK;
    12721327}
    12731328
Note: See TracChangeset for help on using the changeset viewer.