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