Ignore:
File:
1 edited

Legend:

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

    rf77c1c9 r8d2dd7f2  
    8686 * and consume system resources.
    8787 *
    88  * Functions that return int return an error code on error and do not
     88 * Functions that return int return a negative 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  *      size_t nread;
    107  *      rc = vfs_read(file, &pos, buf, sizeof(buf), &nread);
    108  *      if (rc != EOK) {
     106 *      ssize_t size = vfs_read(file, &pos, buf, sizeof(buf));
     107 *      if (size < 0) {
    109108 *              vfs_put(file);
    110  *              return rc;
     109 *              return size;
    111110 *      }
    112111 *
    113  *      // buf is now filled with nread bytes from file
     112 *      // buf is now filled with data from file
    114113 *
    115114 *      vfs_put(file);
     
    128127static int root_fd = -1;
    129128
    130 static int get_parent_and_child(const char *path, int *parent, char **child)
     129static int get_parent_and_child(const char *path, char **child)
    131130{
    132131        size_t size;
     
    136135
    137136        char *slash = str_rchr(apath, L'/');
     137        int parent;
    138138        if (slash == apath) {
    139                 *parent = vfs_root();
    140                 if (*parent < 0) {
    141                         free(apath);
    142                         return EBADF;
    143                 }
     139                parent = vfs_root();
    144140                *child = apath;
    145                 return EOK;
    146141        } else {
    147142                *slash = '\0';
    148                 int rc = vfs_lookup(apath, WALK_DIRECTORY, parent);
    149                 if (rc != EOK) {
     143                parent = vfs_lookup(apath, WALK_DIRECTORY);
     144                if (parent < 0) {
    150145                        free(apath);
    151                         return rc;
     146                        return parent;
    152147                }
    153148                *slash = '/';
     
    155150                free(apath);
    156151                if (!*child) {
    157                         vfs_put(*parent);
     152                        vfs_put(parent);
    158153                        return ENOMEM;
    159154                }
    160 
    161                 return rc;
    162         }
    163 
     155        }
     156
     157        return parent;
    164158}
    165159
     
    238232 * @return              New file handle on success or a negative error code
    239233 */
    240 int vfs_clone(int file_from, int file_to, bool high, int *handle)
    241 {
    242         assert(handle != NULL);
    243 
     234int vfs_clone(int file_from, int file_to, bool high)
     235{
    244236        async_exch_t *vfs_exch = vfs_exchange_begin();
    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);
     237        int rc = async_req_3_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from,
     238            (sysarg_t) file_to, (sysarg_t) high);
    248239        vfs_exchange_end(vfs_exch);
    249 
    250         if (rc == EOK) {
    251                 *handle = ret;
    252         }
    253240        return rc;
    254241}
     
    289276                return ENOMEM;
    290277       
    291         int fd;
    292         int rc = vfs_lookup(abs, WALK_DIRECTORY, &fd);
    293         if (rc != EOK) {
     278        int fd = vfs_lookup(abs, WALK_DIRECTORY);
     279        if (fd < 0) {
    294280                free(abs);
    295                 return rc;
     281                return fd;
    296282        }
    297283       
     
    504490{
    505491        int flags = (kind == KIND_DIRECTORY) ? WALK_DIRECTORY : WALK_REGULAR;
    506         int file = -1;
    507         int rc = vfs_walk(parent, child, WALK_MUST_CREATE | flags, &file);
    508         if (rc != EOK)
    509                 return rc;
     492        int file = vfs_walk(parent, child, WALK_MUST_CREATE | flags);
     493
     494        if (file < 0)
     495                return file;
    510496
    511497        if (linkedfd)
     
    534520{
    535521        char *child;
    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);
     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);
    542527
    543528        free(child);
    544529        vfs_put(parent);
    545530        return rc;
    546 }
     531}       
    547532
    548533/** Lookup a path relative to the local root
     
    550535 * @param path  Path to be looked up
    551536 * @param flags Walk 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  */
    556 int vfs_lookup(const char *path, int flags, int *handle)
     537 *
     538 * @return      File handle representing the result on success or a negative
     539 *              error code on error
     540 */
     541int vfs_lookup(const char *path, int flags)
    557542{
    558543        size_t size;
     
    560545        if (!p)
    561546                return ENOMEM;
    562 
    563547        int root = vfs_root();
    564548        if (root < 0) {
     
    566550                return ENOENT;
    567551        }
    568 
    569         int rc = vfs_walk(root, p, flags, handle);
     552        int rc = vfs_walk(root, p, flags);
    570553        vfs_put(root);
    571554        free(p);
     
    580563 * @param flags Walk flags
    581564 * @param mode  Mode in which to open file in
    582  * @param[out] handle Pointer to variable where handle is to be written.
    583565 *
    584566 * @return      EOK on success or a negative error code
    585567 */
    586 int 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);
     568int 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);
    594575        if (rc != EOK) {
    595576                vfs_put(file);
    596577                return rc;
    597578        }
    598 
    599         *handle = file;
    600         return EOK;
     579       
     580        return file;
    601581}
    602582
     
    726706                }
    727707               
    728                 int mpfd;
    729                 rc = vfs_walk(root_fd, mpa, WALK_DIRECTORY, &mpfd);
    730                 if (rc == EOK) {
     708                int mpfd = vfs_walk(root_fd, mpa, WALK_DIRECTORY);
     709                if (mpfd >= 0) {
    731710                        rc = vfs_mount(mpfd, fs_name, service_id, opts, flags,
    732711                            instance, NULL);
    733712                        vfs_put(mpfd);
     713                } else {
     714                        rc = mpfd;
    734715                }
    735716        }
     
    793774 * @param high   If true, the received file handle will be allocated from high
    794775 *               indices
    795  * @param[out] handle  Received handle.
    796776 *
    797777 * @return       EOK on success or a negative error code
    798778 */
    799 int vfs_receive_handle(bool high, int *handle)
     779int vfs_receive_handle(bool high)
    800780{
    801781        ipc_callid_t callid;
     
    814794        async_exchange_end(vfs_exch);
    815795
    816         if (rc == EOK) {
    817                 *handle = (int) ret;
    818         }
    819 
    820         return rc;
     796        if (rc != EOK)
     797                return rc;
     798        return ret;
    821799}
    822800
     
    830808 * @param buf           Buffer, @a nbytes bytes long
    831809 * @param nbytes        Number of bytes to read
    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  */
    838 int vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte, size_t *nread)
     810 *
     811 * @return              On success, non-negative number of bytes read
     812 * @return              On failure, a negative error code
     813 */
     814ssize_t vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte)
    839815{
    840816        ssize_t cnt = 0;
    841         size_t nr = 0;
     817        size_t nread = 0;
    842818        uint8_t *bp = (uint8_t *) buf;
    843819        int rc;
     
    845821        do {
    846822                bp += cnt;
    847                 nr += cnt;
     823                nread += cnt;
    848824                *pos += cnt;
    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;
     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)
    854829                return rc;
    855         }
    856        
    857         nr += cnt;
     830       
    858831        *pos += cnt;
    859         *nread = nr;
    860         return EOK;
     832        return nread + cnt;
    861833}
    862834
     
    997969/** Return a new file handle representing the local root
    998970 *
    999  * @return      A clone of the local root file handle or -1
     971 * @return      A clone of the local root file handle or a negative error code
    1000972 */
    1001973int vfs_root(void)
    1002974{
    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         }
     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);
    1013981        fibril_mutex_unlock(&root_mutex);
    1014         return fd;
     982        return r;
    1015983}
    1016984
     
    1022990 *
    1023991 * @param nroot The new local root file handle
    1024  *
    1025  * @return  Error code
    1026  */
    1027 int 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 
     992 */
     993void vfs_root_set(int nroot)
     994{
    1035995        fibril_mutex_lock(&root_mutex);
    1036996        if (root_fd >= 0)
    1037997                vfs_put(root_fd);
    1038         root_fd = new_root;
     998        root_fd = vfs_clone(nroot, -1, true);
    1039999        fibril_mutex_unlock(&root_mutex);
    1040 
    1041         return EOK;
    10421000}
    10431001
     
    10851043int vfs_stat_path(const char *path, struct stat *stat)
    10861044{
    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);
     1045        int file = vfs_lookup(path, 0);
     1046        if (file < 0)
     1047                return file;
     1048       
     1049        int rc = vfs_stat(file, stat);
    10931050
    10941051        vfs_put(file);
     
    11311088int vfs_statfs_path(const char *path, struct statfs *st)
    11321089{
    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);
     1090        int file = vfs_lookup(path, 0);
     1091        if (file < 0)
     1092                return file;
     1093       
     1094        int rc = vfs_statfs(file, st);
    11391095
    11401096        vfs_put(file);
     
    12021158int vfs_unlink_path(const char *path)
    12031159{
    1204         int expect;
    1205         int rc = vfs_lookup(path, 0, &expect);
    1206         if (rc != EOK)
    1207                 return rc;
     1160        int expect = vfs_lookup(path, 0);
     1161        if (expect < 0)
     1162                return expect;
    12081163
    12091164        char *child;
    1210         int parent;
    1211         rc = get_parent_and_child(path, &parent, &child);
    1212         if (rc != EOK) {
     1165        int parent = get_parent_and_child(path, &child);
     1166        if (parent < 0) {
    12131167                vfs_put(expect);
    1214                 return rc;
    1215         }
    1216 
    1217         rc = vfs_unlink(parent, child, expect);
     1168                return parent;
     1169        }
     1170
     1171        int rc = vfs_unlink(parent, child, expect);
    12181172       
    12191173        free(child);
     
    12451199int vfs_unmount_path(const char *mpp)
    12461200{
    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);
     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);
    12531206        vfs_put(mp);
    12541207        return rc;
     
    12601213 * @param path          Parent-relative path to be walked
    12611214 * @param flags         Flags influencing the walk
    1262  * @param[out] handle   File handle representing the result on success.
    1263  *
    1264  * @return              Error code.
    1265  */
    1266 int vfs_walk(int parent, const char *path, int flags, int *handle)
     1215 *
     1216 * @retrun              File handle representing the result on success or
     1217 *                      a negative error code on error
     1218 */
     1219int vfs_walk(int parent, const char *path, int flags)
    12671220{
    12681221        async_exch_t *exch = vfs_exchange_begin();
     
    12821235                return (int) rc;
    12831236       
    1284         *handle = (int) IPC_GET_ARG1(answer);
    1285         return EOK;
     1237        return (int) IPC_GET_ARG1(answer);
    12861238}
    12871239
     
    12951247 * @param buf           Data, @a nbytes bytes long
    12961248 * @param nbytes        Number of bytes to write
    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  */
    1303 int vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte,
    1304     size_t *nwritten)
     1249 *
     1250 * @return              On success, non-negative number of bytes written
     1251 * @return              On failure, a negative error code
     1252 */
     1253ssize_t vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte)
    13051254{
    13061255        ssize_t cnt = 0;
    1307         ssize_t nwr = 0;
     1256        ssize_t nwritten = 0;
    13081257        const uint8_t *bp = (uint8_t *) buf;
    13091258        int rc;
     
    13111260        do {
    13121261                bp += cnt;
    1313                 nwr += cnt;
     1262                nwritten += cnt;
    13141263                *pos += cnt;
    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;
     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)
    13201268                return rc;
    1321         }
    1322 
    1323         nwr += cnt;
     1269
    13241270        *pos += cnt;
    1325         *nwritten = nwr;
    1326         return EOK;
     1271        return nbyte;
    13271272}
    13281273
Note: See TracChangeset for help on using the changeset viewer.