Changeset f77c1c9 in mainline for uspace/lib/c/generic/vfs/vfs.c


Ignore:
Timestamp:
2017-12-08T21:03:35Z (8 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c19a5a59
Parents:
c1694b6b
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2017-12-07 19:44:55)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2017-12-08 21:03:35)
Message:

Return VFS handles separately from error codes.

File:
1 edited

Legend:

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

    rc1694b6b rf77c1c9  
    128128static int root_fd = -1;
    129129
    130 static int get_parent_and_child(const char *path, char **child)
     130static int get_parent_and_child(const char *path, int *parent, char **child)
    131131{
    132132        size_t size;
     
    136136
    137137        char *slash = str_rchr(apath, L'/');
    138         int parent;
    139138        if (slash == apath) {
    140                 parent = vfs_root();
     139                *parent = vfs_root();
     140                if (*parent < 0) {
     141                        free(apath);
     142                        return EBADF;
     143                }
    141144                *child = apath;
     145                return EOK;
    142146        } else {
    143147                *slash = '\0';
    144                 parent = vfs_lookup(apath, WALK_DIRECTORY);
    145                 if (parent < 0) {
     148                int rc = vfs_lookup(apath, WALK_DIRECTORY, parent);
     149                if (rc != EOK) {
    146150                        free(apath);
    147                         return parent;
     151                        return rc;
    148152                }
    149153                *slash = '/';
     
    151155                free(apath);
    152156                if (!*child) {
    153                         vfs_put(parent);
     157                        vfs_put(*parent);
    154158                        return ENOMEM;
    155159                }
    156         }
    157 
    158         return parent;
     160
     161                return rc;
     162        }
     163
    159164}
    160165
     
    233238 * @return              New file handle on success or a negative error code
    234239 */
    235 int vfs_clone(int file_from, int file_to, bool high)
    236 {
     240int vfs_clone(int file_from, int file_to, bool high, int *handle)
     241{
     242        assert(handle != NULL);
     243
    237244        async_exch_t *vfs_exch = vfs_exchange_begin();
    238         int rc = async_req_3_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from,
    239             (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);
    240248        vfs_exchange_end(vfs_exch);
     249
     250        if (rc == EOK) {
     251                *handle = ret;
     252        }
    241253        return rc;
    242254}
     
    277289                return ENOMEM;
    278290       
    279         int fd = vfs_lookup(abs, WALK_DIRECTORY);
    280         if (fd < 0) {
     291        int fd;
     292        int rc = vfs_lookup(abs, WALK_DIRECTORY, &fd);
     293        if (rc != EOK) {
    281294                free(abs);
    282                 return fd;
     295                return rc;
    283296        }
    284297       
     
    491504{
    492505        int flags = (kind == KIND_DIRECTORY) ? WALK_DIRECTORY : WALK_REGULAR;
    493         int file = vfs_walk(parent, child, WALK_MUST_CREATE | flags);
    494 
    495         if (file < 0)
    496                 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;
    497510
    498511        if (linkedfd)
     
    521534{
    522535        char *child;
    523         int parent = get_parent_and_child(path, &child);
    524         if (parent < 0)
    525                 return parent;
    526 
    527         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);
    528542
    529543        free(child);
    530544        vfs_put(parent);
    531545        return rc;
    532 }       
     546}
    533547
    534548/** Lookup a path relative to the local root
     
    536550 * @param path  Path to be looked up
    537551 * @param flags Walk flags
    538  *
    539  * @return      File handle representing the result on success or a negative
    540  *              error code on error
    541  */
    542 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)
    543557{
    544558        size_t size;
     
    546560        if (!p)
    547561                return ENOMEM;
     562
    548563        int root = vfs_root();
    549564        if (root < 0) {
     
    551566                return ENOENT;
    552567        }
    553         int rc = vfs_walk(root, p, flags);
     568
     569        int rc = vfs_walk(root, p, flags, handle);
    554570        vfs_put(root);
    555571        free(p);
     
    564580 * @param flags Walk flags
    565581 * @param mode  Mode in which to open file in
     582 * @param[out] handle Pointer to variable where handle is to be written.
    566583 *
    567584 * @return      EOK on success or a negative error code
    568585 */
    569 int vfs_lookup_open(const char *path, int flags, int mode)
    570 {
    571         int file = vfs_lookup(path, flags);
    572         if (file < 0)
    573                 return file;
    574 
    575         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);
    576594        if (rc != EOK) {
    577595                vfs_put(file);
    578596                return rc;
    579597        }
    580        
    581         return file;
     598
     599        *handle = file;
     600        return EOK;
    582601}
    583602
     
    707726                }
    708727               
    709                 int mpfd = vfs_walk(root_fd, mpa, WALK_DIRECTORY);
    710                 if (mpfd >= 0) {
     728                int mpfd;
     729                rc = vfs_walk(root_fd, mpa, WALK_DIRECTORY, &mpfd);
     730                if (rc == EOK) {
    711731                        rc = vfs_mount(mpfd, fs_name, service_id, opts, flags,
    712732                            instance, NULL);
    713733                        vfs_put(mpfd);
    714                 } else {
    715                         rc = mpfd;
    716734                }
    717735        }
     
    775793 * @param high   If true, the received file handle will be allocated from high
    776794 *               indices
     795 * @param[out] handle  Received handle.
    777796 *
    778797 * @return       EOK on success or a negative error code
    779798 */
    780 int vfs_receive_handle(bool high)
     799int vfs_receive_handle(bool high, int *handle)
    781800{
    782801        ipc_callid_t callid;
     
    795814        async_exchange_end(vfs_exch);
    796815
    797         if (rc != EOK)
    798                 return rc;
    799         return ret;
     816        if (rc == EOK) {
     817                *handle = (int) ret;
     818        }
     819
     820        return rc;
    800821}
    801822
     
    976997/** Return a new file handle representing the local root
    977998 *
    978  * @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
    9791000 */
    9801001int vfs_root(void)
    9811002{
    982         fibril_mutex_lock(&root_mutex);
    983         int r;
    984         if (root_fd < 0)
    985                 r = ENOENT;
    986         else
    987                 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        }
    9881013        fibril_mutex_unlock(&root_mutex);
    989         return r;
     1014        return fd;
    9901015}
    9911016
     
    9971022 *
    9981023 * @param nroot The new local root file handle
    999  */
    1000 void vfs_root_set(int nroot)
    1001 {
     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
    10021035        fibril_mutex_lock(&root_mutex);
    10031036        if (root_fd >= 0)
    10041037                vfs_put(root_fd);
    1005         root_fd = vfs_clone(nroot, -1, true);
     1038        root_fd = new_root;
    10061039        fibril_mutex_unlock(&root_mutex);
     1040
     1041        return EOK;
    10071042}
    10081043
     
    10501085int vfs_stat_path(const char *path, struct stat *stat)
    10511086{
    1052         int file = vfs_lookup(path, 0);
    1053         if (file < 0)
    1054                 return file;
    1055        
    1056         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);
    10571093
    10581094        vfs_put(file);
     
    10951131int vfs_statfs_path(const char *path, struct statfs *st)
    10961132{
    1097         int file = vfs_lookup(path, 0);
    1098         if (file < 0)
    1099                 return file;
    1100        
    1101         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);
    11021139
    11031140        vfs_put(file);
     
    11651202int vfs_unlink_path(const char *path)
    11661203{
    1167         int expect = vfs_lookup(path, 0);
    1168         if (expect < 0)
    1169                 return expect;
     1204        int expect;
     1205        int rc = vfs_lookup(path, 0, &expect);
     1206        if (rc != EOK)
     1207                return rc;
    11701208
    11711209        char *child;
    1172         int parent = get_parent_and_child(path, &child);
    1173         if (parent < 0) {
     1210        int parent;
     1211        rc = get_parent_and_child(path, &parent, &child);
     1212        if (rc != EOK) {
    11741213                vfs_put(expect);
    1175                 return parent;
    1176         }
    1177 
    1178         int rc = vfs_unlink(parent, child, expect);
     1214                return rc;
     1215        }
     1216
     1217        rc = vfs_unlink(parent, child, expect);
    11791218       
    11801219        free(child);
     
    12061245int vfs_unmount_path(const char *mpp)
    12071246{
    1208         int mp = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY);
    1209         if (mp < 0)
    1210                 return mp;
    1211        
    1212         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);
    12131253        vfs_put(mp);
    12141254        return rc;
     
    12201260 * @param path          Parent-relative path to be walked
    12211261 * @param flags         Flags influencing the walk
    1222  *
    1223  * @retrun              File handle representing the result on success or
    1224  *                      a negative error code on error
    1225  */
    1226 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)
    12271267{
    12281268        async_exch_t *exch = vfs_exchange_begin();
     
    12421282                return (int) rc;
    12431283       
    1244         return (int) IPC_GET_ARG1(answer);
     1284        *handle = (int) IPC_GET_ARG1(answer);
     1285        return EOK;
    12451286}
    12461287
Note: See TracChangeset for help on using the changeset viewer.