Changeset 75406dc in mainline


Ignore:
Timestamp:
2011-07-28T17:39:51Z (13 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:
955c2b0
Parents:
58115ae
Message:

Additional wrappers and fixes for VFS functions.

Location:
uspace/lib/posix
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/internal/common.h

    r58115ae r75406dc  
    4343    __func__, __FILE__, __LINE__), abort())
    4444
     45/* A little helper macro to avoid typing this over and over. */
     46#define errnify(func, ...) ({ \
     47        int rc = func(__VA_ARGS__); \
     48        if (rc < 0) { \
     49                errno = -rc; \
     50                rc = -1; \
     51        } \
     52        rc; \
     53})
     54
    4555#endif /* LIBPOSIX_COMMON_H_ */
    4656
  • uspace/lib/posix/stdio.c

    r58115ae r75406dc  
    722722
    723723/**
     724 * Rename a file or directory.
     725 *
     726 * @param old
     727 * @param new
     728 * @return Zero on success, -1 (with errno set) otherwise.
     729 */
     730int posix_rename(const char *old, const char *new)
     731{
     732        return errnify(rename, old, new);
     733}
     734
     735/**
    724736 *
    725737 * @param s
  • uspace/lib/posix/stdio.h

    r58115ae r75406dc  
    116116extern int posix_remove(const char *path);
    117117
     118/* Renaming Files */
     119extern int posix_rename(const char *old, const char *new);
     120
    118121/* Temporary Files */
    119122#undef L_tmpnam
     
    170173        #define remove posix_remove
    171174
     175        #define rename posix_rename
     176
    172177        #define tmpnam posix_tmpnam
    173178#endif
  • uspace/lib/posix/unistd.c

    r58115ae r75406dc  
    111111        }
    112112        char *ret = getcwd(buf, size);
    113         if (ret == NULL && errno == EOK) {
     113        if (ret == NULL) {
    114114                errno = ERANGE;
    115115        }
    116116        return ret;
     117}
     118
     119/**
     120 * Change the current working directory.
     121 *
     122 * @param path New working directory.
     123 */
     124int posix_chdir(const char *path)
     125{
     126        return errnify(chdir, path);
    117127}
    118128
     
    157167        /* There is currently no support for user accounts in HelenOS. */
    158168        return 0;
     169}
     170
     171/**
     172 * Close a file.
     173 *
     174 * @param fildes
     175 * @return 0 on success, -1 on error.
     176 */
     177int posix_close(int fildes)
     178{
     179        return errnify(close, fildes);
    159180}
    160181
     
    169190ssize_t posix_read(int fildes, void *buf, size_t nbyte)
    170191{
    171         int rc = read(fildes, buf, nbyte);
    172         if (rc < 0) {
    173                 errno = -rc;
    174                 return -1;
    175         } else {
    176                 return rc;
    177         }
     192        return errnify(read, fildes, buf, nbyte);
     193}
     194
     195/**
     196 * Write to a file.
     197 *
     198 * @param fildes File descriptor of the opened file.
     199 * @param buf Buffer to write.
     200 * @param nbyte Size of the buffer.
     201 * @return Number of written bytes on success, -1 otherwise.
     202 */
     203ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
     204{
     205        return errnify(write, fildes, buf, nbyte);
     206}
     207
     208/**
     209 * Requests outstanding data to be written to the underlying storage device.
     210 *
     211 * @param fildes
     212 */
     213int posix_fsync(int fildes)
     214{
     215        return errnify(fsync, fildes);
     216}
     217
     218int posix_ftruncate(int fildes, posix_off_t length)
     219{
     220        return errnify(ftruncate, fildes, (aoff64_t) length);
    178221}
    179222
     
    186229int posix_rmdir(const char *path)
    187230{
    188         int rc = rmdir(path);
    189         if (rc != EOK) {
    190                 errno = -rc;
    191                 return -1;
    192         }
    193         return 0;
     231        return errnify(rmdir, path);
    194232}
    195233
     
    202240int posix_unlink(const char *path)
    203241{
    204         int rc = unlink(path);
    205         if (rc < 0) {
    206                 errno = -rc;
    207                 return -1;
    208         } else {
    209                 return rc;
    210         }
     242        return errnify(unlink, path);
     243}
     244
     245int posix_dup(int fildes)
     246{
     247        return posix_fcntl(fildes, F_DUPFD, 0);
     248}
     249
     250int posix_dup2(int fildes, int fildes2)
     251{
     252        return errnify(dup2, fildes, fildes2);
    211253}
    212254
     
    220262int posix_access(const char *path, int amode)
    221263{
    222         if (amode == F_OK) {
    223                 /* Check file existence by attempt to open it. */
     264        if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
     265                /* HelenOS doesn't support permissions, permission checks
     266                 * are equal to existence check.
     267                 *
     268                 * Check file existence by attempting to open it.
     269                 */
    224270                int fd = open(path, O_RDONLY);
    225                 if (fd != -1) {
    226                         close(fd);
     271                if (fd < 0) {
     272                        errno = -fd;
     273                        return 0;
    227274                }
    228                 return fd;
    229         } else if (amode & (X_OK | W_OK | R_OK)) {
    230                 /* HelenOS doesn't support permissions, return success. */
    231                 return 0;
     275                close(fd);
     276                return 1;
    232277        } else {
    233278                /* Invalid amode argument. */
  • uspace/lib/posix/unistd.h

    r58115ae r75406dc  
    6060/* Working Directory */
    6161extern char *posix_getcwd(char *buf, size_t size);
     62extern int posix_chdir(const char *path);
    6263
    6364/* Query Memory Parameters */
     
    6970extern posix_gid_t posix_getgid(void);
    7071
    71 /* File Input/Output */
     72/* File Manipulation */
     73extern int posix_close(int fildes);
     74
    7275extern ssize_t posix_read(int fildes, void *buf, size_t nbyte);
     76extern ssize_t posix_write(int fildes, const void *buf, size_t nbyte);
    7377
    74 /* Deleting Files */
     78extern int posix_fsync(int fildes);
     79extern int posix_ftruncate(int fildes, posix_off_t length);
     80
    7581extern int posix_rmdir(const char *path);
    7682extern int posix_unlink(const char *path);
     83
     84extern int posix_dup(int fildes);
     85extern int posix_dup2(int fildes, int fildes2);
    7786
    7887/* Standard Streams */
     
    145154
    146155        #define getcwd posix_getcwd
     156        #define chdir posix_chdir
    147157
    148158        #define isatty posix_isatty
     
    155165        #define getgid posix_getgid
    156166
     167        #define close posix_close
    157168        #define read posix_read
    158 
     169        #define write posix_write
     170        #define fsync posix_fsync
     171        #define ftruncate posix_ftruncate
    159172        #define rmdir posix_rmdir
    160173        #define unlink posix_unlink
     174        #define dup posix_dup
     175        #define dup2 posix_dup2
    161176
    162177        #define access posix_access
Note: See TracChangeset for help on using the changeset viewer.