Changes in uspace/lib/c/generic/vfs/vfs.c [f77c1c9:8d2dd7f2] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/vfs/vfs.c
rf77c1c9 r8d2dd7f2 86 86 * and consume system resources. 87 87 * 88 * Functions that return int return a nerror code on error and do not88 * Functions that return int return a negative error code on error and do not 89 89 * set errno. Depending on function, success is signalled by returning either 90 90 * EOK or a non-negative file handle. … … 104 104 * aoff64_t pos = 42; 105 105 * char buf[512]; 106 * size_t nread; 107 * rc = vfs_read(file, &pos, buf, sizeof(buf), &nread); 108 * if (rc != EOK) { 106 * ssize_t size = vfs_read(file, &pos, buf, sizeof(buf)); 107 * if (size < 0) { 109 108 * vfs_put(file); 110 * return rc;109 * return size; 111 110 * } 112 111 * 113 * // buf is now filled with nread bytesfrom file112 * // buf is now filled with data from file 114 113 * 115 114 * vfs_put(file); … … 128 127 static int root_fd = -1; 129 128 130 static int get_parent_and_child(const char *path, int *parent,char **child)129 static int get_parent_and_child(const char *path, char **child) 131 130 { 132 131 size_t size; … … 136 135 137 136 char *slash = str_rchr(apath, L'/'); 137 int parent; 138 138 if (slash == apath) { 139 *parent = vfs_root(); 140 if (*parent < 0) { 141 free(apath); 142 return EBADF; 143 } 139 parent = vfs_root(); 144 140 *child = apath; 145 return EOK;146 141 } else { 147 142 *slash = '\0'; 148 int rc = vfs_lookup(apath, WALK_DIRECTORY, parent);149 if ( rc != EOK) {143 parent = vfs_lookup(apath, WALK_DIRECTORY); 144 if (parent < 0) { 150 145 free(apath); 151 return rc;146 return parent; 152 147 } 153 148 *slash = '/'; … … 155 150 free(apath); 156 151 if (!*child) { 157 vfs_put( *parent);152 vfs_put(parent); 158 153 return ENOMEM; 159 154 } 160 161 return rc; 162 } 163 155 } 156 157 return parent; 164 158 } 165 159 … … 238 232 * @return New file handle on success or a negative error code 239 233 */ 240 int vfs_clone(int file_from, int file_to, bool high, int *handle) 241 { 242 assert(handle != NULL); 243 234 int vfs_clone(int file_from, int file_to, bool high) 235 { 244 236 async_exch_t *vfs_exch = vfs_exchange_begin(); 245 sysarg_t ret; 246 int rc = async_req_3_1(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from, 247 (sysarg_t) file_to, (sysarg_t) high, &ret); 237 int rc = async_req_3_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from, 238 (sysarg_t) file_to, (sysarg_t) high); 248 239 vfs_exchange_end(vfs_exch); 249 250 if (rc == EOK) {251 *handle = ret;252 }253 240 return rc; 254 241 } … … 289 276 return ENOMEM; 290 277 291 int fd; 292 int rc = vfs_lookup(abs, WALK_DIRECTORY, &fd); 293 if (rc != EOK) { 278 int fd = vfs_lookup(abs, WALK_DIRECTORY); 279 if (fd < 0) { 294 280 free(abs); 295 return rc;281 return fd; 296 282 } 297 283 … … 504 490 { 505 491 int flags = (kind == KIND_DIRECTORY) ? WALK_DIRECTORY : WALK_REGULAR; 506 int file = -1;507 int rc = vfs_walk(parent, child, WALK_MUST_CREATE | flags, &file); 508 if ( rc != EOK)509 return rc;492 int file = vfs_walk(parent, child, WALK_MUST_CREATE | flags); 493 494 if (file < 0) 495 return file; 510 496 511 497 if (linkedfd) … … 534 520 { 535 521 char *child; 536 int parent; 537 int rc = get_parent_and_child(path, &parent, &child); 538 if (rc != EOK) 539 return rc; 540 541 rc = vfs_link(parent, child, kind, linkedfd); 522 int parent = get_parent_and_child(path, &child); 523 if (parent < 0) 524 return parent; 525 526 int rc = vfs_link(parent, child, kind, linkedfd); 542 527 543 528 free(child); 544 529 vfs_put(parent); 545 530 return rc; 546 } 531 } 547 532 548 533 /** Lookup a path relative to the local root … … 550 535 * @param path Path to be looked up 551 536 * @param flags Walk flags 552 * @param[out] handle Pointer to variable where handle is to be written.553 * 554 * @return EOK on success or an error code.555 */ 556 int vfs_lookup(const char *path, int flags , int *handle)537 * 538 * @return File handle representing the result on success or a negative 539 * error code on error 540 */ 541 int vfs_lookup(const char *path, int flags) 557 542 { 558 543 size_t size; … … 560 545 if (!p) 561 546 return ENOMEM; 562 563 547 int root = vfs_root(); 564 548 if (root < 0) { … … 566 550 return ENOENT; 567 551 } 568 569 int rc = vfs_walk(root, p, flags, handle); 552 int rc = vfs_walk(root, p, flags); 570 553 vfs_put(root); 571 554 free(p); … … 580 563 * @param flags Walk flags 581 564 * @param mode Mode in which to open file in 582 * @param[out] handle Pointer to variable where handle is to be written.583 565 * 584 566 * @return EOK on success or a negative error code 585 567 */ 586 int vfs_lookup_open(const char *path, int flags, int mode, int *handle) 587 { 588 int file; 589 int rc = vfs_lookup(path, flags, &file); 590 if (rc != EOK) 591 return rc; 592 593 rc = vfs_open(file, mode); 568 int vfs_lookup_open(const char *path, int flags, int mode) 569 { 570 int file = vfs_lookup(path, flags); 571 if (file < 0) 572 return file; 573 574 int rc = vfs_open(file, mode); 594 575 if (rc != EOK) { 595 576 vfs_put(file); 596 577 return rc; 597 578 } 598 599 *handle = file; 600 return EOK; 579 580 return file; 601 581 } 602 582 … … 726 706 } 727 707 728 int mpfd; 729 rc = vfs_walk(root_fd, mpa, WALK_DIRECTORY, &mpfd); 730 if (rc == EOK) { 708 int mpfd = vfs_walk(root_fd, mpa, WALK_DIRECTORY); 709 if (mpfd >= 0) { 731 710 rc = vfs_mount(mpfd, fs_name, service_id, opts, flags, 732 711 instance, NULL); 733 712 vfs_put(mpfd); 713 } else { 714 rc = mpfd; 734 715 } 735 716 } … … 793 774 * @param high If true, the received file handle will be allocated from high 794 775 * indices 795 * @param[out] handle Received handle.796 776 * 797 777 * @return EOK on success or a negative error code 798 778 */ 799 int vfs_receive_handle(bool high , int *handle)779 int vfs_receive_handle(bool high) 800 780 { 801 781 ipc_callid_t callid; … … 814 794 async_exchange_end(vfs_exch); 815 795 816 if (rc == EOK) { 817 *handle = (int) ret; 818 } 819 820 return rc; 796 if (rc != EOK) 797 return rc; 798 return ret; 821 799 } 822 800 … … 830 808 * @param buf Buffer, @a nbytes bytes long 831 809 * @param nbytes Number of bytes to read 832 * @param nread Place to store number of bytes actually read 833 * 834 * @return On success, EOK and @a *nread is filled with number 835 * of bytes actually read. 836 * @return On failure, an error code 837 */ 838 int vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte, size_t *nread) 810 * 811 * @return On success, non-negative number of bytes read 812 * @return On failure, a negative error code 813 */ 814 ssize_t vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte) 839 815 { 840 816 ssize_t cnt = 0; 841 size_t nr = 0;817 size_t nread = 0; 842 818 uint8_t *bp = (uint8_t *) buf; 843 819 int rc; … … 845 821 do { 846 822 bp += cnt; 847 nr += cnt;823 nread += cnt; 848 824 *pos += cnt; 849 rc = vfs_read_short(file, *pos, bp, nbyte - nr, &cnt); 850 } while (rc == EOK && cnt > 0 && (nbyte - nr - cnt) > 0); 851 852 if (rc != EOK) { 853 *nread = nr; 825 rc = vfs_read_short(file, *pos, bp, nbyte - nread, &cnt); 826 } while (rc == EOK && cnt > 0 && (nbyte - nread - cnt) > 0); 827 828 if (rc != EOK) 854 829 return rc; 855 } 856 857 nr += cnt; 830 858 831 *pos += cnt; 859 *nread = nr; 860 return EOK; 832 return nread + cnt; 861 833 } 862 834 … … 997 969 /** Return a new file handle representing the local root 998 970 * 999 * @return A clone of the local root file handle or -1971 * @return A clone of the local root file handle or a negative error code 1000 972 */ 1001 973 int vfs_root(void) 1002 974 { 1003 fibril_mutex_lock(&root_mutex); 1004 int fd; 1005 if (root_fd < 0) { 1006 fd = -1; 1007 } else { 1008 int rc = vfs_clone(root_fd, -1, true, &fd); 1009 if (rc != EOK) { 1010 fd = -1; 1011 } 1012 } 975 fibril_mutex_lock(&root_mutex); 976 int r; 977 if (root_fd < 0) 978 r = ENOENT; 979 else 980 r = vfs_clone(root_fd, -1, true); 1013 981 fibril_mutex_unlock(&root_mutex); 1014 return fd;982 return r; 1015 983 } 1016 984 … … 1022 990 * 1023 991 * @param nroot The new local root file handle 1024 * 1025 * @return Error code 1026 */ 1027 int vfs_root_set(int nroot) 1028 { 1029 int new_root; 1030 int rc = vfs_clone(nroot, -1, true, &new_root); 1031 if (rc != EOK) { 1032 return rc; 1033 } 1034 992 */ 993 void vfs_root_set(int nroot) 994 { 1035 995 fibril_mutex_lock(&root_mutex); 1036 996 if (root_fd >= 0) 1037 997 vfs_put(root_fd); 1038 root_fd = new_root;998 root_fd = vfs_clone(nroot, -1, true); 1039 999 fibril_mutex_unlock(&root_mutex); 1040 1041 return EOK;1042 1000 } 1043 1001 … … 1085 1043 int vfs_stat_path(const char *path, struct stat *stat) 1086 1044 { 1087 int file; 1088 int rc = vfs_lookup(path, 0, &file); 1089 if (rc != EOK) 1090 return rc; 1091 1092 rc = vfs_stat(file, stat); 1045 int file = vfs_lookup(path, 0); 1046 if (file < 0) 1047 return file; 1048 1049 int rc = vfs_stat(file, stat); 1093 1050 1094 1051 vfs_put(file); … … 1131 1088 int vfs_statfs_path(const char *path, struct statfs *st) 1132 1089 { 1133 int file; 1134 int rc = vfs_lookup(path, 0, &file); 1135 if (rc != EOK) 1136 return rc; 1137 1138 rc = vfs_statfs(file, st); 1090 int file = vfs_lookup(path, 0); 1091 if (file < 0) 1092 return file; 1093 1094 int rc = vfs_statfs(file, st); 1139 1095 1140 1096 vfs_put(file); … … 1202 1158 int vfs_unlink_path(const char *path) 1203 1159 { 1204 int expect; 1205 int rc = vfs_lookup(path, 0, &expect); 1206 if (rc != EOK) 1207 return rc; 1160 int expect = vfs_lookup(path, 0); 1161 if (expect < 0) 1162 return expect; 1208 1163 1209 1164 char *child; 1210 int parent; 1211 rc = get_parent_and_child(path, &parent, &child); 1212 if (rc != EOK) { 1165 int parent = get_parent_and_child(path, &child); 1166 if (parent < 0) { 1213 1167 vfs_put(expect); 1214 return rc;1215 } 1216 1217 rc = vfs_unlink(parent, child, expect);1168 return parent; 1169 } 1170 1171 int rc = vfs_unlink(parent, child, expect); 1218 1172 1219 1173 free(child); … … 1245 1199 int vfs_unmount_path(const char *mpp) 1246 1200 { 1247 int mp; 1248 int rc = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY, &mp); 1249 if (rc != EOK) 1250 return rc; 1251 1252 rc = vfs_unmount(mp); 1201 int mp = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY); 1202 if (mp < 0) 1203 return mp; 1204 1205 int rc = vfs_unmount(mp); 1253 1206 vfs_put(mp); 1254 1207 return rc; … … 1260 1213 * @param path Parent-relative path to be walked 1261 1214 * @param flags Flags influencing the walk 1262 * @param[out] handle File handle representing the result on success.1263 * 1264 * @return Error code.1265 */ 1266 int vfs_walk(int parent, const char *path, int flags , int *handle)1215 * 1216 * @retrun File handle representing the result on success or 1217 * a negative error code on error 1218 */ 1219 int vfs_walk(int parent, const char *path, int flags) 1267 1220 { 1268 1221 async_exch_t *exch = vfs_exchange_begin(); … … 1282 1235 return (int) rc; 1283 1236 1284 *handle = (int) IPC_GET_ARG1(answer); 1285 return EOK; 1237 return (int) IPC_GET_ARG1(answer); 1286 1238 } 1287 1239 … … 1295 1247 * @param buf Data, @a nbytes bytes long 1296 1248 * @param nbytes Number of bytes to write 1297 * @param nwritten Place to store number of bytes written 1298 * 1299 * @return On success, EOK, @a *nwr is filled with number 1300 * of bytes written 1301 * @return On failure, an error code 1302 */ 1303 int vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte, 1304 size_t *nwritten) 1249 * 1250 * @return On success, non-negative number of bytes written 1251 * @return On failure, a negative error code 1252 */ 1253 ssize_t vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte) 1305 1254 { 1306 1255 ssize_t cnt = 0; 1307 ssize_t nwr = 0;1256 ssize_t nwritten = 0; 1308 1257 const uint8_t *bp = (uint8_t *) buf; 1309 1258 int rc; … … 1311 1260 do { 1312 1261 bp += cnt; 1313 nwr += cnt;1262 nwritten += cnt; 1314 1263 *pos += cnt; 1315 rc = vfs_write_short(file, *pos, bp, nbyte - nwr, &cnt); 1316 } while (rc == EOK && ((ssize_t )nbyte - nwr - cnt) > 0); 1317 1318 if (rc != EOK) { 1319 *nwritten = nwr; 1264 rc = vfs_write_short(file, *pos, bp, nbyte - nwritten, &cnt); 1265 } while (rc == EOK && ((ssize_t )nbyte - nwritten - cnt) > 0); 1266 1267 if (rc != EOK) 1320 1268 return rc; 1321 } 1322 1323 nwr += cnt; 1269 1324 1270 *pos += cnt; 1325 *nwritten = nwr; 1326 return EOK; 1271 return nbyte; 1327 1272 } 1328 1273
Note:
See TracChangeset
for help on using the changeset viewer.