Changeset b19e892 in mainline for uspace/lib/c


Ignore:
Timestamp:
2017-04-02T10:39:13Z (8 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9c4cf0d
Parents:
80743a1
Message:

Merge open() with posix_open() and provide vfs_lookup_open() instead

vfs_lookup_open() is really just a convenience wrapper around
vfs_lookup() and vfs_open().

Location:
uspace/lib/c
Files:
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/elf/elf_mod.c

    r80743a1 rb19e892  
    9797
    9898        int ofile = vfs_clone(file, -1, true);
    99         int rc = _vfs_open(ofile, MODE_READ);
     99        int rc = vfs_open(ofile, MODE_READ);
    100100        if (rc != EOK) {
    101101                return rc;
  • uspace/lib/c/generic/io/io.c

    r80743a1 rb19e892  
    3535#include <stdio.h>
    3636#include <unistd.h>
    37 #include <fcntl.h>
    3837#include <assert.h>
    3938#include <str.h>
     
    115114                int stdinfd = vfs_clone(infd, -1, false);
    116115                assert(stdinfd == 0);
    117                 _vfs_open(stdinfd, MODE_READ);
     116                vfs_open(stdinfd, MODE_READ);
    118117                stdin = fdopen(stdinfd, "r");
    119118        } else {
     
    128127                while (stdoutfd < 1)
    129128                        stdoutfd = vfs_clone(outfd, -1, false);
    130                 _vfs_open(stdoutfd, MODE_APPEND);
     129                vfs_open(stdoutfd, MODE_APPEND);
    131130                stdout = fdopen(stdoutfd, "a");
    132131        } else {
     
    141140                while (stderrfd < 2)
    142141                        stderrfd = vfs_clone(errfd, -1, false);
    143                 _vfs_open(stderrfd, MODE_APPEND);
     142                vfs_open(stderrfd, MODE_APPEND);
    144143                stderr = fdopen(stderrfd, "a");
    145144        } else {
     
    157156}
    158157
    159 static bool parse_mode(const char *mode, int *flags)
     158static bool parse_mode(const char *fmode, int *mode, bool *create, bool *truncate)
    160159{
    161160        /* Parse mode except first character. */
    162         const char *mp = mode;
     161        const char *mp = fmode;
    163162        if (*mp++ == 0) {
    164163                errno = EINVAL;
     
    180179                return false;
    181180        }
    182        
    183         /* Parse first character of mode and determine flags for open(). */
    184         switch (mode[0]) {
     181
     182        *create = false;
     183        *truncate = false;
     184       
     185        /* Parse first character of fmode and determine mode for vfs_open(). */
     186        switch (fmode[0]) {
    185187        case 'r':
    186                 *flags = plus ? O_RDWR : O_RDONLY;
     188                *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
    187189                break;
    188190        case 'w':
    189                 *flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
     191                *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
     192                *create = true;
     193                if (!plus)
     194                        *truncate = true;
    190195                break;
    191196        case 'a':
     
    195200                        return false;
    196201                }
    197                 *flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
     202
     203                *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
     204                *create = true;
    198205                break;
    199206        default:
     
    269276 *
    270277 */
    271 FILE *fopen(const char *path, const char *mode)
    272 {
    273         int flags;
    274         if (!parse_mode(mode, &flags))
     278FILE *fopen(const char *path, const char *fmode)
     279{
     280        int mode;
     281        bool create;
     282        bool truncate;
     283
     284        if (!parse_mode(fmode, &mode, &create, &truncate))
    275285                return NULL;
    276286       
     
    281291                return NULL;
    282292        }
    283        
    284         stream->fd = open(path, flags, 0666);
    285         if (stream->fd < 0) {
    286                 /* errno was set by open() */
     293
     294        int flags = WALK_REGULAR;
     295        if (create)
     296                flags |= WALK_MAY_CREATE;
     297        int file = vfs_lookup(path, flags);
     298        if (file < 0) {
     299                errno = file;
    287300                free(stream);
    288301                return NULL;
    289302        }
    290        
     303
     304        int rc = vfs_open(file, mode);
     305        if (rc != EOK) {
     306                errno = rc;
     307                close(file);
     308                free(stream);
     309                return NULL;
     310        }
     311       
     312        if (truncate) {
     313                rc = vfs_resize(file, 0);
     314                if (rc != EOK) {
     315                        errno = rc;
     316                        close(file);
     317                        free(stream);
     318                        return NULL;
     319                }
     320        }
     321
     322        stream->fd = file;
    291323        stream->pos = 0;
    292324        stream->error = false;
  • uspace/lib/c/generic/rtld/module.c

    r80743a1 rb19e892  
    3838#include <elf/elf_load.h>
    3939#include <errno.h>
    40 #include <fcntl.h>
    4140#include <loader/pcb.h>
    4241#include <stdio.h>
  • uspace/lib/c/generic/vfs/vfs.c

    r80743a1 rb19e892  
    4141#include <unistd.h>
    4242#include <dirent.h>
    43 #include <fcntl.h>
    4443#include <stdio.h>
    4544#include <sys/types.h>
     
    155154}
    156155
    157 int _vfs_open(int fildes, int mode)
    158 {
    159         async_exch_t *exch = vfs_exchange_begin();
    160         sysarg_t rc = async_req_2_0(exch, VFS_IN_OPEN, fildes, mode);
    161         vfs_exchange_end(exch);
    162        
    163         return (int) rc;
     156int vfs_open(int file, int mode)
     157{
     158        async_exch_t *exch = vfs_exchange_begin();
     159        int rc = async_req_2_0(exch, VFS_IN_OPEN, file, mode);
     160        vfs_exchange_end(exch);
     161       
     162        return rc;
     163}
     164
     165int vfs_lookup_open(const char *path, int flags, int mode)
     166{
     167        int file = vfs_lookup(path, flags);
     168        if (file < 0)
     169                return file;
     170
     171        int rc = vfs_open(file, mode);
     172        if (rc != EOK) {
     173                close(file);
     174                return rc;
     175        }
     176       
     177        return file;
    164178}
    165179
     
    353367}
    354368
    355 static int walk_flags(int oflags)
    356 {
    357         int flags = 0;
    358         if (oflags & O_CREAT) {
    359                 if (oflags & O_EXCL)
    360                         flags |= WALK_MUST_CREATE;
    361                 else
    362                         flags |= WALK_MAY_CREATE;
    363         }
    364         return flags;
    365 }
    366 
    367 /** Open file.
    368  *
    369  * @param path File path
    370  * @param oflag O_xxx flags
    371  * @param mode File mode (only with O_CREAT)
    372  *
    373  * @return Nonnegative file descriptor on success. On error -1 is returned
    374  *         and errno is set.
    375  */
    376 int open(const char *path, int oflag, ...)
    377 {
    378         if (((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||
    379             ((oflag & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||
    380             ((oflag & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||
    381             ((oflag & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {
    382                 errno = EINVAL;
    383                 return -1;
    384         }
    385        
    386         int fd = vfs_lookup(path, walk_flags(oflag) | WALK_REGULAR);
    387         if (fd < 0) {
    388                 errno = fd;
    389                 return -1;
    390         }
    391        
    392         int mode =
    393             ((oflag & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |
    394             ((oflag & O_RDONLY) ? MODE_READ : 0) |
    395             ((oflag & O_WRONLY) ? MODE_WRITE : 0) |
    396             ((oflag & O_APPEND) ? MODE_APPEND : 0);
    397        
    398         int rc = _vfs_open(fd, mode);
    399         if (rc < 0) {
    400                 close(fd);
    401                 errno = rc;
    402                 return -1;
    403         }
    404        
    405         if (oflag & O_TRUNC) {
    406                 assert(oflag & O_WRONLY || oflag & O_RDWR);
    407                 assert(!(oflag & O_APPEND));
    408                
    409                 (void) vfs_resize(fd, 0);
    410         }
    411 
    412         return fd;
    413 }
    414 
    415369/** Close file.
    416370 *
     
    703657        }
    704658       
    705         int rc = _vfs_open(fd, MODE_READ);
     659        int rc = vfs_open(fd, MODE_READ);
    706660        if (rc < 0) {
    707661                free(dirp);
  • uspace/lib/c/include/vfs/vfs.h

    r80743a1 rb19e892  
    8383
    8484extern int _vfs_walk(int, const char *, int);
    85 extern int _vfs_open(int, int);
    86 extern int vfs_lookup(const char *, int);
    8785
    8886extern int vfs_pass_handle(async_exch_t *, int, async_exch_t *);
     
    9290extern int vfs_link(int, const char *, vfs_file_kind_t);
    9391extern int vfs_link_path(const char *, vfs_file_kind_t);
     92extern int vfs_lookup(const char *, int);
     93extern int vfs_lookup_open(const char *, int, int);
    9494extern int vfs_mount_path(const char *, const char *, const char *,
    9595    const char *, unsigned int, unsigned int);
    9696extern int vfs_mount(int, const char *, service_id_t, const char *, unsigned,
    9797    unsigned, int *);
     98extern int vfs_open(int, int);
    9899extern int vfs_resize(int, aoff64_t);
    99100extern int vfs_root(void);
Note: See TracChangeset for help on using the changeset viewer.