Changeset f77c1c9 in mainline for uspace/lib/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.

Location:
uspace/lib/c
Files:
6 edited

Legend:

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

    rc1694b6b rf77c1c9  
    5454        }
    5555       
    56         int fd = vfs_lookup(dirname, WALK_DIRECTORY);
    57         if (fd < 0) {
     56        int fd;
     57        int rc = vfs_lookup(dirname, WALK_DIRECTORY, &fd);
     58        if (rc != EOK) {
    5859                free(dirp);
    59                 errno = fd;
     60                errno = rc;
    6061                return NULL;
    6162        }
    6263       
    63         int rc = vfs_open(fd, MODE_READ);
    64         if (rc < 0) {
     64        rc = vfs_open(fd, MODE_READ);
     65        if (rc != EOK) {
    6566                free(dirp);
    6667                vfs_put(fd);
  • uspace/lib/c/generic/elf/elf_mod.c

    rc1694b6b rf77c1c9  
    9797        elf_ld_t elf;
    9898
    99         int ofile = vfs_clone(file, -1, true);
    100         int rc = vfs_open(ofile, MODE_READ);
     99        int ofile;
     100        int rc = vfs_clone(file, -1, true, &ofile);
     101        if (rc == EOK) {
     102                rc = vfs_open(ofile, MODE_READ);
     103        }
    101104        if (rc != EOK) {
    102105                return rc;
     
    116119    elf_finfo_t *info)
    117120{
    118         int file = vfs_lookup(path, 0);
    119         int rc = elf_load_file(file, so_bias, flags, info);
    120         vfs_put(file);
     121        int file;
     122        int rc = vfs_lookup(path, 0, &file);
     123        if (rc == EOK) {
     124                rc = elf_load_file(file, so_bias, flags, info);
     125                vfs_put(file);
     126        }
    121127        return rc;
    122128}
  • uspace/lib/c/generic/io/io.c

    rc1694b6b rf77c1c9  
    108108         * This will probably be removed later.
    109109         */
    110          
    111110        int infd = inbox_get("stdin");
    112111        if (infd >= 0) {
    113                 int stdinfd = vfs_clone(infd, -1, false);
     112                int stdinfd = -1;
     113                (void) vfs_clone(infd, -1, false, &stdinfd);
    114114                assert(stdinfd == 0);
    115115                vfs_open(stdinfd, MODE_READ);
     
    122122        int outfd = inbox_get("stdout");
    123123        if (outfd >= 0) {
    124                 int stdoutfd = vfs_clone(outfd, -1, false);
     124                int stdoutfd = -1;
     125                (void) vfs_clone(outfd, -1, false, &stdoutfd);
    125126                assert(stdoutfd <= 1);
    126127                while (stdoutfd < 1)
    127                         stdoutfd = vfs_clone(outfd, -1, false);
     128                        (void) vfs_clone(outfd, -1, false, &stdoutfd);
    128129                vfs_open(stdoutfd, MODE_APPEND);
    129130                stdout = fdopen(stdoutfd, "a");
     
    135136        int errfd = inbox_get("stderr");
    136137        if (errfd >= 0) {
    137                 int stderrfd = vfs_clone(errfd, -1, false);
     138                int stderrfd = -1;
     139                (void) vfs_clone(errfd, -1, false, &stderrfd);
    138140                assert(stderrfd <= 2);
    139141                while (stderrfd < 2)
    140                         stderrfd = vfs_clone(errfd, -1, false);
     142                        (void) vfs_clone(errfd, -1, false, &stderrfd);
    141143                vfs_open(stderrfd, MODE_APPEND);
    142144                stderr = fdopen(stderrfd, "a");
     
    294296        if (create)
    295297                flags |= WALK_MAY_CREATE;
    296         int file = vfs_lookup(path, flags);
    297         if (file < 0) {
    298                 errno = file;
     298        int file;
     299        int rc = vfs_lookup(path, flags, &file);
     300        if (rc != EOK) {
     301                errno = rc;
    299302                free(stream);
    300303                return NULL;
    301304        }
    302305
    303         int rc = vfs_open(file, mode);
     306        rc = vfs_open(file, mode);
    304307        if (rc != EOK) {
    305308                errno = rc;
     
    373376        list_remove(&stream->link);
    374377       
    375         if (rc != 0) {
    376                 /* errno was set by close() */
     378        if (rc != EOK) {
     379                errno = rc;
    377380                return EOF;
    378381        }
  • uspace/lib/c/generic/loader.c

    rc1694b6b rf77c1c9  
    5252 * @param name Symbolic name to set on the newly created task.
    5353 *
    54  * @return Pointer to the loader connection structure (should be
    55  *         deallocated using free() after use).
    56  *
     54 * @return Error code.
    5755 */
    5856int loader_spawn(const char *name)
     
    198196        }
    199197       
    200         int fd = vfs_lookup(path, 0);
    201         if (fd < 0) {
    202                 return fd;
    203         }
    204        
    205         int rc = loader_set_program(ldr, name, fd);
     198        int fd;
     199        int rc = vfs_lookup(path, 0, &fd);
     200        if (rc != EOK) {
     201                return rc;
     202        }
     203       
     204        rc = loader_set_program(ldr, name, fd);
    206205        vfs_put(fd);
    207206        return rc;
  • 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
  • uspace/lib/c/include/vfs/vfs.h

    rc1694b6b rf77c1c9  
    8585
    8686extern char *vfs_absolutize(const char *, size_t *);
    87 extern int vfs_clone(int, int, bool);
     87extern int vfs_clone(int, int, bool, int *);
    8888extern int vfs_cwd_get(char *path, size_t);
    8989extern int vfs_cwd_set(const char *path);
     
    9595extern int vfs_link(int, const char *, vfs_file_kind_t, int *);
    9696extern int vfs_link_path(const char *, vfs_file_kind_t, int *);
    97 extern int vfs_lookup(const char *, int);
    98 extern int vfs_lookup_open(const char *, int, int);
     97extern int vfs_lookup(const char *, int, int *);
     98extern int vfs_lookup_open(const char *, int, int, int *);
    9999extern int vfs_mount_path(const char *, const char *, const char *,
    100100    const char *, unsigned int, unsigned int);
     
    106106extern int vfs_read(int, aoff64_t *, void *, size_t, size_t *);
    107107extern int vfs_read_short(int, aoff64_t, void *, size_t, ssize_t *);
    108 extern int vfs_receive_handle(bool);
     108extern int vfs_receive_handle(bool, int *);
    109109extern int vfs_rename_path(const char *, const char *);
    110110extern int vfs_resize(int, aoff64_t);
    111111extern int vfs_root(void);
    112 extern void vfs_root_set(int);
     112extern int vfs_root_set(int);
    113113extern int vfs_stat(int, struct stat *);
    114114extern int vfs_stat_path(const char *, struct stat *);
     
    120120extern int vfs_unmount(int);
    121121extern int vfs_unmount_path(const char *);
    122 extern int vfs_walk(int, const char *, int);
     122extern int vfs_walk(int, const char *, int, int *);
    123123extern 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 *);
Note: See TracChangeset for help on using the changeset viewer.