Changeset 368ee04 in mainline for uspace/lib/posix
- Timestamp:
- 2017-04-05T18:10:39Z (9 years ago)
- 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. - Location:
- uspace/lib/posix
- Files:
-
- 9 edited
-
include/posix/fcntl.h (modified) (1 diff)
-
include/posix/sys/stat.h (modified) (2 diffs)
-
include/posix/sys/types.h (modified) (1 diff)
-
source/fcntl.c (modified) (1 diff)
-
source/internal/common.h (modified) (2 diffs)
-
source/stdio.c (modified) (5 diffs)
-
source/stdlib.c (modified) (1 diff)
-
source/sys/stat.c (modified) (8 diffs)
-
source/unistd.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/include/posix/fcntl.h
r39f892a9 r368ee04 41 41 42 42 #include "sys/types.h" 43 #include "libc/fcntl.h"44 43 #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 45 59 46 60 /* Mask for file access modes. */ -
uspace/lib/posix/include/posix/sys/stat.h
r39f892a9 r368ee04 115 115 __POSIX_DEF__(dev_t) st_dev; /* ID of device containing file */ 116 116 __POSIX_DEF__(ino_t) st_ino; /* inode number */ 117 mode_tst_mode; /* protection */117 __POSIX_DEF__(mode_t) st_mode; /* protection */ 118 118 __POSIX_DEF__(nlink_t) st_nlink; /* number of hard links */ 119 119 __POSIX_DEF__(uid_t) st_uid; /* user ID of owner */ … … 131 131 extern int __POSIX_DEF__(lstat)(const char *restrict path, struct __POSIX_DEF__(stat) *restrict st); 132 132 extern int __POSIX_DEF__(stat)(const char *restrict path, struct __POSIX_DEF__(stat) *restrict st); 133 extern int __POSIX_DEF__(chmod)(const char *path, mode_tmode);134 extern mode_t __POSIX_DEF__(umask)(mode_tmask);135 extern int mkdir(const char *, mode_t);133 extern int __POSIX_DEF__(chmod)(const char *path, __POSIX_DEF__(mode_t) mode); 134 extern __POSIX_DEF__(mode_t) __POSIX_DEF__(umask)(__POSIX_DEF__(mode_t) mask); 135 extern int __POSIX_DEF__(mkdir)(const char *path, __POSIX_DEF__(mode_t) mode); 136 136 137 137 -
uspace/lib/posix/include/posix/sys/types.h
r39f892a9 r368ee04 53 53 typedef int64_t __POSIX_DEF__(pid_t); 54 54 typedef sysarg_t __POSIX_DEF__(dev_t); 55 typedef unsigned int __POSIX_DEF__(mode_t); 55 56 56 57 /* PThread Types */ -
uspace/lib/posix/source/fcntl.c
r39f892a9 r368ee04 100 100 * 101 101 * @param pathname Path to the file. 102 * @param flags Access mode flags.102 * @param posix_flags Access mode flags. 103 103 */ 104 int posix_open(const char *pathname, int flags, ...)104 int posix_open(const char *pathname, int posix_flags, ...) 105 105 { 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) { 108 109 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); 111 112 va_end(args); 113 (void) posix_mode; 112 114 } 113 115 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; 115 159 } 116 160 117 161 /** @} 118 162 */ 163 -
uspace/lib/posix/source/internal/common.h
r39f892a9 r368ee04 38 38 #include <stdio.h> 39 39 #include <stdlib.h> 40 #include <sys/types.h> 41 #include <vfs/vfs.h> 40 42 41 43 #define not_implemented() do { \ … … 57 59 }) 58 60 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 69 extern aoff64_t posix_pos[MAX_OPEN_FILES]; 70 59 71 #endif /* LIBPOSIX_COMMON_H_ */ 60 72 -
uspace/lib/posix/source/stdio.c
r39f892a9 r368ee04 53 53 #include "libc/malloc.h" 54 54 #include "libc/adt/list.h" 55 #include "libc/sys/stat.h"56 55 57 56 /** Clears the stream's error and end-of-file indicators. … … 344 343 static int _dprintf_str_write(const char *str, size_t size, void *fd) 345 344 { 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); 347 347 if (wr < 0) 348 return errno;348 return wr; 349 349 return str_nlength(str, wr); 350 350 } … … 371 371 } 372 372 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) 374 375 break; 375 }376 376 377 377 chars++; … … 575 575 int posix_remove(const char *path) 576 576 { 577 return negerrno(remove, path); 577 if (rcerrno(vfs_unlink_path, path) != EOK) 578 return -1; 579 else 580 return 0; 578 581 } 579 582 … … 587 590 int posix_rename(const char *old, const char *new) 588 591 { 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; 590 597 } 591 598 -
uspace/lib/posix/source/stdlib.c
r39f892a9 r368ee04 431 431 } 432 432 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); 434 434 435 435 if (fd == -1) { -
uspace/lib/posix/source/sys/stat.c
r39f892a9 r368ee04 39 39 #include "../internal/common.h" 40 40 #include "posix/sys/stat.h" 41 #include "libc/ sys/stat.h"41 #include "libc/vfs/vfs.h" 42 42 43 43 #include "posix/errno.h" … … 49 49 * @param dest POSIX stat struct. 50 50 * @param src HelenOS stat struct. 51 * 52 * @return 0 on success, -1 on error. 51 53 */ 52 static voidstat_to_posix(struct posix_stat *dest, struct stat *src)54 static int stat_to_posix(struct posix_stat *dest, struct stat *src) 53 55 { 54 56 memset(dest, 0, sizeof(struct posix_stat)); … … 68 70 dest->st_nlink = src->lnkcnt; 69 71 dest->st_size = src->size; 72 73 if (src->size > INT64_MAX) { 74 errno = ERANGE; 75 return -1; 76 } 77 78 return 0; 70 79 } 71 80 … … 80 89 { 81 90 struct stat hst; 82 int rc = negerrno(fstat, fd, &hst);91 int rc = rcerrno(vfs_stat, fd, &hst); 83 92 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); 87 95 } 88 96 … … 110 118 { 111 119 struct stat hst; 112 int rc = negerrno(stat, path, &hst);120 int rc = rcerrno(vfs_stat_path, path, &hst); 113 121 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); 117 124 } 118 125 … … 124 131 * @return Zero on success, -1 otherwise. 125 132 */ 126 int posix_chmod(const char *path, mode_t mode)133 int posix_chmod(const char *path, posix_mode_t mode) 127 134 { 128 135 /* HelenOS doesn't support permissions, return success. */ … … 137 144 * @return Previous file mode creation mask. 138 145 */ 139 mode_t posix_umask(mode_t mask)146 posix_mode_t posix_umask(posix_mode_t mask) 140 147 { 141 148 /* HelenOS doesn't support permissions, return empty mask. */ … … 143 150 } 144 151 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 */ 159 int 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 145 168 /** @} 146 169 */ -
uspace/lib/posix/source/unistd.c
r39f892a9 r368ee04 47 47 #include "libc/stats.h" 48 48 #include "libc/malloc.h" 49 #include "libc/vfs/vfs.h" 50 51 aoff64_t posix_pos[MAX_OPEN_FILES]; 49 52 50 53 /* Array of environment variable strings (NAME=VALUE). */ … … 105 108 char *posix_getcwd(char *buf, size_t size) 106 109 { 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) 111 112 return NULL; 112 } 113 114 return p; 113 return buf; 115 114 } 116 115 … … 122 121 int posix_chdir(const char *path) 123 122 { 124 return negerrno(chdir, path); 123 int rc = rcerrno(vfs_cwd_set, path); 124 if (rc != EOK) 125 return -1; 126 return 0; 125 127 } 126 128 … … 175 177 int posix_close(int fildes) 176 178 { 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; 178 185 } 179 186 … … 188 195 ssize_t posix_read(int fildes, void *buf, size_t nbyte) 189 196 { 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; 191 201 } 192 202 … … 201 211 ssize_t posix_write(int fildes, const void *buf, size_t nbyte) 202 212 { 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; 204 217 } 205 218 … … 215 228 posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence) 216 229 { 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]; 218 253 } 219 254 … … 226 261 int posix_fsync(int fildes) 227 262 { 228 return negerrno(fsync, fildes); 263 if (rcerrno(vfs_sync, fildes) != EOK) 264 return -1; 265 else 266 return 0; 229 267 } 230 268 … … 238 276 int posix_ftruncate(int fildes, posix_off_t length) 239 277 { 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; 241 282 } 242 283 … … 249 290 int posix_rmdir(const char *path) 250 291 { 251 return negerrno(rmdir, path); 292 if (rcerrno(vfs_unlink_path, path) != EOK) 293 return -1; 294 else 295 return 0; 252 296 } 253 297 … … 260 304 int posix_unlink(const char *path) 261 305 { 262 return negerrno(unlink, path); 306 if (rcerrno(vfs_unlink_path, path) != EOK) 307 return -1; 308 else 309 return 0; 263 310 } 264 311 … … 284 331 int posix_dup2(int fildes, int fildes2) 285 332 { 286 return negerrno( dup2, fildes, fildes2);333 return negerrno(vfs_clone, fildes, fildes2, false); 287 334 } 288 335 … … 302 349 * Check file existence by attempting to open it. 303 350 */ 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) 307 353 return -1; 308 } 309 close(fd); 354 posix_close(fd); 310 355 return 0; 311 356 } else {
Note:
See TracChangeset
for help on using the changeset viewer.
