Changeset b19e892 in mainline for uspace/lib


Ignore:
Timestamp:
2017-04-02T10:39:13Z (9 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
Files:
1 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/bithenge/src/file.c

    r80743a1 rb19e892  
    3838#include <assert.h>
    3939#include <errno.h>
    40 #include <fcntl.h>
    4140#include <stdio.h>
    4241#include <stdlib.h>
     
    146145        assert(filename);
    147146
    148         int fd = open(filename, O_RDONLY);
     147        int fd = vfs_lookup_open(filename, WALK_REGULAR, MODE_READ);
    149148        if (fd < 0)
    150149                return errno;
  • 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);
  • uspace/lib/pcut/src/os/helenos.c

    r80743a1 rb19e892  
    4141#include <stdio.h>
    4242#include <task.h>
    43 #include <fcntl.h>
    4443#include <fibril_synch.h>
    4544#include <vfs/vfs.h>
     
    162161        char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
    163162        snprintf(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1, "pcut_%lld.tmp", (unsigned long long) task_get_id());
    164         int tempfile = open(tempfile_name, O_CREAT | O_RDWR);
     163        int tempfile = vfs_lookup_open(tempfile_name, WALK_REGULAR | WALK_MAY_CREATE, MODE_READ | MODE_WRITE);
    165164        if (tempfile < 0) {
    166165                pcut_report_test_done(test, TEST_OUTCOME_ERROR, "Failed to create temporary file.", NULL, NULL);
  • uspace/lib/posix/include/posix/fcntl.h

    r80743a1 rb19e892  
    4141
    4242#include "sys/types.h"
    43 #include "libc/fcntl.h"
    4443#include "errno.h"
     44
     45#undef O_CREAT
     46#undef O_EXCL
     47#undef O_TRUNC
     48#undef O_APPEND
     49#undef O_RDONLY
     50#undef O_RDWR
     51#undef O_WRONLY
     52#define O_CREAT   1
     53#define O_EXCL    2
     54#define O_TRUNC   4
     55#define O_APPEND  8
     56#define O_RDONLY  16
     57#define O_RDWR    32
     58#define O_WRONLY  64
    4559
    4660/* Mask for file access modes. */
  • uspace/lib/posix/source/fcntl.c

    r80743a1 rb19e892  
    100100 *
    101101 * @param pathname Path to the file.
    102  * @param flags Access mode flags.
     102 * @param posix_flags Access mode flags.
    103103 */
    104 int posix_open(const char *pathname, int flags, ...)
     104int posix_open(const char *pathname, int posix_flags, ...)
    105105{
    106         mode_t mode = 0;
    107         if ((flags & O_CREAT) > 0) {
     106        int rc;
     107        mode_t posix_mode = 0;
     108        if (posix_flags & O_CREAT) {
    108109                va_list args;
    109                 va_start(args, flags);
    110                 mode = va_arg(args, mode_t);
     110                va_start(args, posix_flags);
     111                posix_mode = va_arg(args, mode_t);
    111112                va_end(args);
     113                (void) posix_mode;
    112114        }
    113115
    114         return negerrno(open, pathname, flags, mode);
     116        if (((posix_flags & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||
     117            ((posix_flags & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||
     118            ((posix_flags & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||
     119            ((posix_flags & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {
     120                errno = EINVAL;
     121                return -1;
     122        }
     123
     124        int flags = WALK_REGULAR;
     125        if (posix_flags & O_CREAT) {
     126                if (posix_flags & O_EXCL)
     127                        flags |= WALK_MUST_CREATE;
     128                else
     129                        flags |= WALK_MAY_CREATE;
     130        }
     131
     132        int mode =
     133            ((posix_flags & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |
     134            ((posix_flags & O_RDONLY) ? MODE_READ : 0) |
     135            ((posix_flags & O_WRONLY) ? MODE_WRITE : 0) |
     136            ((posix_flags & O_APPEND) ? MODE_APPEND : 0);
     137
     138        int file = rcerrno(vfs_lookup, pathname, flags);
     139        if (file < 0)
     140                return -1;
     141
     142        rc = rcerrno(vfs_open, file, mode);
     143        if (rc != EOK) {
     144                close (file);
     145                return -1;
     146        }
     147
     148        if (posix_flags & O_TRUNC) {
     149                if (posix_flags & (O_RDWR | O_WRONLY)) {
     150                        rc = rcerrno(vfs_resize, file, 0);
     151                        if (rc != EOK) {
     152                                close(file);
     153                                return -1;
     154                        }
     155                }
     156        }
     157
     158        return file;
    115159}
    116160
    117161/** @}
    118162 */
     163
  • uspace/lib/posix/source/stdlib.c

    r80743a1 rb19e892  
    431431                }
    432432               
    433                 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
     433                fd = posix_open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
    434434               
    435435                if (fd == -1) {
  • uspace/lib/posix/source/unistd.c

    r80743a1 rb19e892  
    335335                 * Check file existence by attempting to open it.
    336336                 */
    337                 int fd = negerrno(open, path, O_RDONLY);
    338                 if (fd < 0) {
    339                         /* errno was set by open() */
     337                int fd = posix_open(path, O_RDONLY);
     338                if (fd < 0)
    340339                        return -1;
    341                 }
    342340                close(fd);
    343341                return 0;
Note: See TracChangeset for help on using the changeset viewer.