Changeset 368ee04 in mainline for uspace/lib/posix


Ignore:
Timestamp:
2017-04-05T18:10:39Z (9 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
93ad8166
Parents:
39f892a9 (diff), 2166728 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jakub/helenos/vfs-2.5-cherrypick

This merge cherry-picks some of the changesets from Jiri Zarevucky's:

lp:~zarevucky-jiri/helenos/vfs-2.5

and then continues independently, yet sometime in a similar vein.

Roughly speaking, Jiri's branch is merged entirely up to its revision
1926 and then cherry-picked on and off until its revision 1965. Among
these changes are:

  • relativization of the API,
  • client-side roots,
  • server-side mounts,
  • inbox for passing arbitrary files from parent to child,
  • some streamlining and cleanup.

Additional changes include:

  • addressing issues introduced by the above changes,
  • client-side I/O cursors (file positions),
  • all HelenOS file system APIs begin with the vfs_ prefix and can be used after including vfs/vfs.h,
  • removal of some POSIX-ish headers and definitions,
  • additional cleanup.
Location:
uspace/lib/posix
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/include/posix/fcntl.h

    r39f892a9 r368ee04  
    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/include/posix/sys/stat.h

    r39f892a9 r368ee04  
    115115        __POSIX_DEF__(dev_t)     st_dev;     /* ID of device containing file */
    116116        __POSIX_DEF__(ino_t)     st_ino;     /* inode number */
    117         mode_t          st_mode;    /* protection */
     117        __POSIX_DEF__(mode_t)    st_mode;    /* protection */
    118118        __POSIX_DEF__(nlink_t)   st_nlink;   /* number of hard links */
    119119        __POSIX_DEF__(uid_t)     st_uid;     /* user ID of owner */
     
    131131extern int __POSIX_DEF__(lstat)(const char *restrict path, struct __POSIX_DEF__(stat) *restrict st);
    132132extern int __POSIX_DEF__(stat)(const char *restrict path, struct __POSIX_DEF__(stat) *restrict st);
    133 extern int __POSIX_DEF__(chmod)(const char *path, mode_t mode);
    134 extern mode_t __POSIX_DEF__(umask)(mode_t mask);
    135 extern int mkdir(const char *, mode_t);
     133extern int __POSIX_DEF__(chmod)(const char *path, __POSIX_DEF__(mode_t) mode);
     134extern __POSIX_DEF__(mode_t) __POSIX_DEF__(umask)(__POSIX_DEF__(mode_t) mask);
     135extern int __POSIX_DEF__(mkdir)(const char *path, __POSIX_DEF__(mode_t) mode);
    136136
    137137
  • uspace/lib/posix/include/posix/sys/types.h

    r39f892a9 r368ee04  
    5353typedef int64_t __POSIX_DEF__(pid_t);
    5454typedef sysarg_t __POSIX_DEF__(dev_t);
     55typedef unsigned int __POSIX_DEF__(mode_t);
    5556
    5657/* PThread Types */
  • uspace/lib/posix/source/fcntl.c

    r39f892a9 r368ee04  
    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        posix_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, posix_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                vfs_put(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                                vfs_put(file);
     153                                return -1;
     154                        }
     155                }
     156        }
     157
     158        return file;
    115159}
    116160
    117161/** @}
    118162 */
     163
  • uspace/lib/posix/source/internal/common.h

    r39f892a9 r368ee04  
    3838#include <stdio.h>
    3939#include <stdlib.h>
     40#include <sys/types.h>
     41#include <vfs/vfs.h>
    4042
    4143#define not_implemented() do { \
     
    5759})
    5860
     61/* Convert error code to positive errno and -1 return value */
     62#define rcerrno(func, ...) ({ \
     63        int rc = func(__VA_ARGS__); \
     64        if (rc < 0) \
     65                errno = -rc; \
     66        rc; \
     67})
     68
     69extern aoff64_t posix_pos[MAX_OPEN_FILES];
     70
    5971#endif /* LIBPOSIX_COMMON_H_ */
    6072
  • uspace/lib/posix/source/stdio.c

    r39f892a9 r368ee04  
    5353#include "libc/malloc.h"
    5454#include "libc/adt/list.h"
    55 #include "libc/sys/stat.h"
    5655
    5756/** Clears the stream's error and end-of-file indicators.
     
    344343static int _dprintf_str_write(const char *str, size_t size, void *fd)
    345344{
    346         ssize_t wr = write(*(int *) fd, str, size);
     345        const int fildes = *(int *) fd;
     346        ssize_t wr = vfs_write(fildes, &posix_pos[fildes], str, size);
    347347        if (wr < 0)
    348                 return errno;
     348                return wr;
    349349        return str_nlength(str, wr);
    350350}
     
    371371                }
    372372               
    373                 if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
     373                const int fildes = *(int *) fd;
     374                if (vfs_write(fildes, &posix_pos[fildes], buf, sz) < 0)
    374375                        break;
    375                 }
    376376               
    377377                chars++;
     
    575575int posix_remove(const char *path)
    576576{
    577         return negerrno(remove, path);
     577        if (rcerrno(vfs_unlink_path, path) != EOK)
     578                return -1;
     579        else
     580                return 0;
    578581}
    579582
     
    587590int posix_rename(const char *old, const char *new)
    588591{
    589         return negerrno(rename, old, new);
     592        int rc = rcerrno(vfs_rename_path, old, new);
     593        if (rc != EOK)
     594                return -1;
     595        else
     596                return 0;
    590597}
    591598
  • uspace/lib/posix/source/stdlib.c

    r39f892a9 r368ee04  
    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/sys/stat.c

    r39f892a9 r368ee04  
    3939#include "../internal/common.h"
    4040#include "posix/sys/stat.h"
    41 #include "libc/sys/stat.h"
     41#include "libc/vfs/vfs.h"
    4242
    4343#include "posix/errno.h"
     
    4949 * @param dest POSIX stat struct.
    5050 * @param src HelenOS stat struct.
     51 *
     52 * @return 0 on success, -1 on error.
    5153 */
    52 static void stat_to_posix(struct posix_stat *dest, struct stat *src)
     54static int stat_to_posix(struct posix_stat *dest, struct stat *src)
    5355{
    5456        memset(dest, 0, sizeof(struct posix_stat));
     
    6870        dest->st_nlink = src->lnkcnt;
    6971        dest->st_size = src->size;
     72
     73        if (src->size > INT64_MAX) {
     74                errno = ERANGE;
     75                return -1;
     76        }
     77
     78        return 0;
    7079}
    7180
     
    8089{
    8190        struct stat hst;
    82         int rc = negerrno(fstat, fd, &hst);
     91        int rc = rcerrno(vfs_stat, fd, &hst);
    8392        if (rc < 0)
    84                 return rc;
    85         stat_to_posix(st, &hst);
    86         return 0;
     93                return -1;
     94        return stat_to_posix(st, &hst);
    8795}
    8896
     
    110118{
    111119        struct stat hst;
    112         int rc = negerrno(stat, path, &hst);
     120        int rc = rcerrno(vfs_stat_path, path, &hst);
    113121        if (rc < 0)
    114                 return rc;
    115         stat_to_posix(st, &hst);
    116         return 0;
     122                return -1;
     123        return stat_to_posix(st, &hst);
    117124}
    118125
     
    124131 * @return Zero on success, -1 otherwise.
    125132 */
    126 int posix_chmod(const char *path, mode_t mode)
     133int posix_chmod(const char *path, posix_mode_t mode)
    127134{
    128135        /* HelenOS doesn't support permissions, return success. */
     
    137144 * @return Previous file mode creation mask.
    138145 */
    139 mode_t posix_umask(mode_t mask)
     146posix_mode_t posix_umask(posix_mode_t mask)
    140147{
    141148        /* HelenOS doesn't support permissions, return empty mask. */
     
    143150}
    144151
     152/**
     153 * Create a directory.
     154 *
     155 * @param path Path to the new directory.
     156 * @param mode Permission bits to be set.
     157 * @return Zero on success, -1 otherwise.
     158 */
     159int posix_mkdir(const char *path, posix_mode_t mode)
     160{
     161        int rc = rcerrno(vfs_link_path, path, KIND_DIRECTORY, NULL);
     162        if (rc != EOK)
     163                return -1;
     164        else
     165                return 0;
     166}
     167
    145168/** @}
    146169 */
  • uspace/lib/posix/source/unistd.c

    r39f892a9 r368ee04  
    4747#include "libc/stats.h"
    4848#include "libc/malloc.h"
     49#include "libc/vfs/vfs.h"
     50
     51aoff64_t posix_pos[MAX_OPEN_FILES];
    4952
    5053/* Array of environment variable strings (NAME=VALUE). */
     
    105108char *posix_getcwd(char *buf, size_t size)
    106109{
    107         char *p = getcwd(buf, size);
    108 
    109         if (p == NULL) {
    110                 errno = -errno;
     110        int rc = rcerrno(vfs_cwd_get, buf, size);
     111        if (rc != EOK)
    111112                return NULL;
    112         }
    113 
    114         return p;
     113        return buf;
    115114}
    116115
     
    122121int posix_chdir(const char *path)
    123122{
    124         return negerrno(chdir, path);
     123        int rc = rcerrno(vfs_cwd_set, path);
     124        if (rc != EOK)
     125                return -1;
     126        return 0;
    125127}
    126128
     
    175177int posix_close(int fildes)
    176178{
    177         return negerrno(close, fildes);
     179        posix_pos[fildes] = 0;
     180        int rc = rcerrno(vfs_put, fildes);
     181        if (rc != EOK)
     182                return -1;
     183        else
     184                return 0;
    178185}
    179186
     
    188195ssize_t posix_read(int fildes, void *buf, size_t nbyte)
    189196{
    190         return negerrno(read, fildes, buf, nbyte);
     197        ssize_t size = rcerrno(vfs_read, fildes, &posix_pos[fildes], buf, nbyte);
     198        if (size < 0)
     199                return -1;
     200        return size;
    191201}
    192202
     
    201211ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
    202212{
    203         return negerrno(write, fildes, buf, nbyte);
     213        ssize_t size = rcerrno(vfs_write, fildes, &posix_pos[fildes], buf, nbyte);
     214        if (size < 0)
     215                return -1;
     216        return size;
    204217}
    205218
     
    215228posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
    216229{
    217         return negerrno(lseek, fildes, offset, whence);
     230        struct stat st;
     231        int rc;
     232
     233        switch (whence) {
     234        case SEEK_SET:
     235                posix_pos[fildes] = offset;
     236                break;
     237        case SEEK_CUR:
     238                posix_pos[fildes] += offset;
     239                break;
     240        case SEEK_END:
     241                rc = rcerrno(vfs_stat, fildes, &st);
     242                if (rc != EOK)
     243                        return -1;
     244                posix_pos[fildes] = st.size + offset;
     245                break;
     246        }
     247        if (posix_pos[fildes] > INT64_MAX) {
     248                /* The native width is too large for the POSIX interface. */
     249                errno = ERANGE;
     250                return -1;
     251        }
     252        return posix_pos[fildes];
    218253}
    219254
     
    226261int posix_fsync(int fildes)
    227262{
    228         return negerrno(fsync, fildes);
     263        if (rcerrno(vfs_sync, fildes) != EOK)
     264                return -1;
     265        else
     266                return 0;
    229267}
    230268
     
    238276int posix_ftruncate(int fildes, posix_off_t length)
    239277{
    240         return negerrno(ftruncate, fildes, (aoff64_t) length);
     278        if (rcerrno(vfs_resize, fildes, (aoff64_t) length) != EOK)
     279                return -1;
     280        else
     281                return 0;
    241282}
    242283
     
    249290int posix_rmdir(const char *path)
    250291{
    251         return negerrno(rmdir, path);
     292        if (rcerrno(vfs_unlink_path, path) != EOK)
     293                return -1;
     294        else
     295                return 0;
    252296}
    253297
     
    260304int posix_unlink(const char *path)
    261305{
    262         return negerrno(unlink, path);
     306        if (rcerrno(vfs_unlink_path, path) != EOK)
     307                return -1;
     308        else
     309                return 0;
    263310}
    264311
     
    284331int posix_dup2(int fildes, int fildes2)
    285332{
    286         return negerrno(dup2, fildes, fildes2);
     333        return negerrno(vfs_clone, fildes, fildes2, false);
    287334}
    288335
     
    302349                 * Check file existence by attempting to open it.
    303350                 */
    304                 int fd = negerrno(open, path, O_RDONLY);
    305                 if (fd < 0) {
    306                         /* errno was set by open() */
     351                int fd = posix_open(path, O_RDONLY);
     352                if (fd < 0)
    307353                        return -1;
    308                 }
    309                 close(fd);
     354                posix_close(fd);
    310355                return 0;
    311356        } else {
Note: See TracChangeset for help on using the changeset viewer.