Changeset b19e892 in mainline for uspace/lib/c/generic/io/io.c


Ignore:
Timestamp:
2017-04-02T10:39:13Z (7 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().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.