Changes in uspace/lib/c/generic/vfs/vfs.c [8d2dd7f2:cde999a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/vfs/vfs.c
r8d2dd7f2 rcde999a 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 … … 230 236 * handle will be allocated from high indices 231 237 * 232 * @return New file handle on success or a negative error code 233 */ 234 int vfs_clone(int file_from, int file_to, bool high) 235 { 238 * @return New file handle on success or an error code 239 */ 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 } … … 246 259 * @param size Size of @a buf 247 260 * 248 * @return EOK on success or a non- negativeerror code261 * @return EOK on success or a non-error code 249 262 */ 250 263 int vfs_cwd_get(char *buf, size_t size) … … 267 280 * @param path Path of the new working directory 268 281 * 269 * @return EOK on success or a negativeerror code282 * @return EOK on success or an error code 270 283 */ 271 284 int vfs_cwd_set(const char *path) … … 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 … … 356 370 * @param info Place to store volume identification information 357 371 * 358 * @return EOK on success or a negativeerror code372 * @return EOK on success or an error code 359 373 */ 360 374 int vfs_fsprobe(const char *fs_name, service_id_t serv, 361 375 vfs_fs_probe_info_t *info) 362 376 { 363 sysarg_t rc;377 int rc; 364 378 365 379 ipc_call_t answer; … … 390 404 * fstypes->fstypes[0..]. To free the list use vfs_fstypes_free(). 391 405 * 392 * @return EOK on success or a negativeerror code406 * @return EOK on success or an error code 393 407 */ 394 408 int vfs_fstypes(vfs_fstypes_t *fstypes) … … 485 499 * @param[out] linkedfd If not NULL, will receive a file handle to the linked 486 500 * child 487 * @return EOK on success or a negativeerror code501 * @return EOK on success or an error code 488 502 */ 489 503 int vfs_link(int parent, const char *child, vfs_file_kind_t kind, int *linkedfd) 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) … … 515 529 * @param[out] linkedfd If not NULL, will receive a file handle to the linked 516 530 * child 517 * @return EOK on success or a negativeerror code531 * @return EOK on success or an error code 518 532 */ 519 533 int vfs_link_path(const char *path, vfs_file_kind_t kind, int *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 // XXX: Workaround for GCC diagnostics. 570 *handle = -1; 571 572 int rc = vfs_walk(root, p, flags, handle); 553 573 vfs_put(root); 554 574 free(p); … … 563 583 * @param flags Walk flags 564 584 * @param mode Mode in which to open file in 565 * 566 * @return EOK on success or a negative error code 567 */ 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); 585 * @param[out] handle Pointer to variable where handle is to be written. 586 * 587 * @return EOK on success or an error code 588 */ 589 int vfs_lookup_open(const char *path, int flags, int mode, int *handle) 590 { 591 int file; 592 int rc = vfs_lookup(path, flags, &file); 593 if (rc != EOK) 594 return rc; 595 596 rc = vfs_open(file, mode); 575 597 if (rc != EOK) { 576 598 vfs_put(file); 577 599 return rc; 578 600 } 579 580 return file; 601 602 *handle = file; 603 return EOK; 581 604 } 582 605 … … 591 614 * @param[out] mountedfd File handle of the mounted root if not NULL 592 615 * 593 * @return EOK on success or a negativeerror code616 * @return EOK on success or an error code 594 617 */ 595 618 int vfs_mount(int mp, const char *fs_name, service_id_t serv, const char *opts, 596 619 unsigned int flags, unsigned int instance, int *mountedfd) 597 620 { 598 sysarg_t rc, rc1;621 int rc, rc1; 599 622 600 623 if (!mountedfd) … … 635 658 * @param[in] instance Instance number of the file system server 636 659 * 637 * @return EOK on success or a negativeerror code660 * @return EOK on success or an error code 638 661 */ 639 662 int vfs_mount_path(const char *mp, const char *fs_name, const char *fqsn, … … 706 729 } 707 730 708 int mpfd = vfs_walk(root_fd, mpa, WALK_DIRECTORY); 709 if (mpfd >= 0) { 731 int mpfd; 732 rc = vfs_walk(root_fd, mpa, WALK_DIRECTORY, &mpfd); 733 if (rc == EOK) { 710 734 rc = vfs_mount(mpfd, fs_name, service_id, opts, flags, 711 735 instance, NULL); 712 736 vfs_put(mpfd); 713 } else {714 rc = mpfd;715 737 } 716 738 } … … 730 752 * @param mode Mode in which to open file in 731 753 * 732 * @return EOK on success or a negativeerror code754 * @return EOK on success or an error code 733 755 */ 734 756 int vfs_open(int file, int mode) … … 747 769 * @param exch Exchange to the acceptor 748 770 * 749 * @return EOK on success or a negativeerror code771 * @return EOK on success or an error code 750 772 */ 751 773 int vfs_pass_handle(async_exch_t *vfs_exch, int file, async_exch_t *exch) … … 759 781 * @param file File handle to put 760 782 * 761 * @return EOK on success or a negativeerror code783 * @return EOK on success or an error code 762 784 */ 763 785 int vfs_put(int file) … … 774 796 * @param high If true, the received file handle will be allocated from high 775 797 * indices 776 * 777 * @return EOK on success or a negative error code 778 */ 779 int vfs_receive_handle(bool high) 798 * @param[out] handle Received handle. 799 * 800 * @return EOK on success or an error code 801 */ 802 int vfs_receive_handle(bool high, int *handle) 780 803 { 781 804 ipc_callid_t callid; … … 790 813 791 814 sysarg_t ret; 792 sysarg_t rc = async_req_1_1(vfs_exch, VFS_IN_WAIT_HANDLE, high, &ret);815 int rc = async_req_1_1(vfs_exch, VFS_IN_WAIT_HANDLE, high, &ret); 793 816 794 817 async_exchange_end(vfs_exch); 795 818 796 if (rc != EOK) 797 return rc; 798 return ret; 819 if (rc == EOK) { 820 *handle = (int) ret; 821 } 822 823 return rc; 799 824 } 800 825 … … 808 833 * @param buf Buffer, @a nbytes bytes long 809 834 * @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) 835 * @param nread Place to store number of bytes actually read 836 * 837 * @return On success, EOK and @a *nread is filled with number 838 * of bytes actually read. 839 * @return On failure, an error code 840 */ 841 int vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte, size_t *nread) 815 842 { 816 843 ssize_t cnt = 0; 817 size_t nr ead= 0;844 size_t nr = 0; 818 845 uint8_t *bp = (uint8_t *) buf; 819 846 int rc; … … 821 848 do { 822 849 bp += cnt; 823 nr ead+= cnt;850 nr += cnt; 824 851 *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 852 rc = vfs_read_short(file, *pos, bp, nbyte - nr, &cnt); 853 } while (rc == EOK && cnt > 0 && (nbyte - nr - cnt) > 0); 854 855 if (rc != EOK) { 856 *nread = nr; 857 return rc; 858 } 859 860 nr += cnt; 831 861 *pos += cnt; 832 return nread + cnt; 862 *nread = nr; 863 return EOK; 833 864 } 834 865 … … 846 877 * @param[out] nread Actual number of bytes read (0 or more) 847 878 * 848 * @return EOK on success or a negativeerror code879 * @return EOK on success or an error code 849 880 */ 850 881 int vfs_read_short(int file, aoff64_t pos, void *buf, size_t nbyte, 851 882 ssize_t *nread) 852 883 { 853 sysarg_t rc;884 int rc; 854 885 ipc_call_t answer; 855 886 aid_t req; … … 888 919 * @param new New path 889 920 * 890 * @return EOK on success or a negativeerror code921 * @return EOK on success or an error code 891 922 */ 892 923 int vfs_rename_path(const char *old, const char *new) 893 924 { 894 sysarg_t rc;895 sysarg_t rc_orig;925 int rc; 926 int rc_orig; 896 927 aid_t req; 897 928 … … 955 986 * @param length New length 956 987 * 957 * @return EOK on success or a negativeerror code988 * @return EOK on success or an error code 958 989 */ 959 990 int vfs_resize(int file, aoff64_t length) … … 969 1000 /** Return a new file handle representing the local root 970 1001 * 971 * @return A clone of the local root file handle or a negative error code1002 * @return A clone of the local root file handle or -1 972 1003 */ 973 1004 int vfs_root(void) 974 1005 { 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); 1006 fibril_mutex_lock(&root_mutex); 1007 int fd; 1008 if (root_fd < 0) { 1009 fd = -1; 1010 } else { 1011 int rc = vfs_clone(root_fd, -1, true, &fd); 1012 if (rc != EOK) { 1013 fd = -1; 1014 } 1015 } 981 1016 fibril_mutex_unlock(&root_mutex); 982 return r;1017 return fd; 983 1018 } 984 1019 … … 990 1025 * 991 1026 * @param nroot The new local root file handle 992 */ 993 void vfs_root_set(int nroot) 994 { 1027 * 1028 * @return Error code 1029 */ 1030 int vfs_root_set(int nroot) 1031 { 1032 int new_root; 1033 int rc = vfs_clone(nroot, -1, true, &new_root); 1034 if (rc != EOK) { 1035 return rc; 1036 } 1037 995 1038 fibril_mutex_lock(&root_mutex); 996 1039 if (root_fd >= 0) 997 1040 vfs_put(root_fd); 998 root_fd = vfs_clone(nroot, -1, true);1041 root_fd = new_root; 999 1042 fibril_mutex_unlock(&root_mutex); 1043 1044 return EOK; 1000 1045 } 1001 1046 … … 1005 1050 * @param[out] stat Place to store file information 1006 1051 * 1007 * @return EOK on success or a negativeerror code1052 * @return EOK on success or an error code 1008 1053 */ 1009 1054 int vfs_stat(int file, struct stat *stat) 1010 1055 { 1011 sysarg_t rc;1056 int rc; 1012 1057 aid_t req; 1013 1058 … … 1019 1064 vfs_exchange_end(exch); 1020 1065 1021 sysarg_t rc_orig;1066 int rc_orig; 1022 1067 async_wait_for(req, &rc_orig); 1023 1068 … … 1039 1084 * @param[out] stat Place to store file information 1040 1085 * 1041 * @return EOK on success or a negativeerror code1086 * @return EOK on success or an error code 1042 1087 */ 1043 1088 int vfs_stat_path(const char *path, struct stat *stat) 1044 1089 { 1045 int file = vfs_lookup(path, 0); 1046 if (file < 0) 1047 return file; 1048 1049 int rc = vfs_stat(file, stat); 1090 int file; 1091 int rc = vfs_lookup(path, 0, &file); 1092 if (rc != EOK) 1093 return rc; 1094 1095 rc = vfs_stat(file, stat); 1050 1096 1051 1097 vfs_put(file); … … 1059 1105 * @param[out] st Buffer for storing information 1060 1106 * 1061 * @return EOK on success or a negativeerror code1107 * @return EOK on success or an error code 1062 1108 */ 1063 1109 int vfs_statfs(int file, struct statfs *st) 1064 1110 { 1065 sysarg_t rc, ret;1111 int rc, ret; 1066 1112 aid_t req; 1067 1113 … … 1084 1130 * @param[out] st Buffer for storing information 1085 1131 * 1086 * @return EOK on success or a negativeerror code1132 * @return EOK on success or an error code 1087 1133 */ 1088 1134 int vfs_statfs_path(const char *path, struct statfs *st) 1089 1135 { 1090 int file = vfs_lookup(path, 0); 1091 if (file < 0) 1092 return file; 1093 1094 int rc = vfs_statfs(file, st); 1136 int file; 1137 int rc = vfs_lookup(path, 0, &file); 1138 if (rc != EOK) 1139 return rc; 1140 1141 rc = vfs_statfs(file, st); 1095 1142 1096 1143 vfs_put(file); … … 1103 1150 * @param file File handle to synchronize 1104 1151 * 1105 * @return EOK on success or a negativeerror code1152 * @return EOK on success or an error code 1106 1153 */ 1107 1154 int vfs_sync(int file) … … 1125 1172 * @param expect File handle of the unlinked child 1126 1173 * 1127 * @return EOK on success or a negativeerror code1174 * @return EOK on success or an error code 1128 1175 */ 1129 1176 int vfs_unlink(int parent, const char *child, int expect) 1130 1177 { 1131 sysarg_t rc;1178 int rc; 1132 1179 aid_t req; 1133 1180 … … 1139 1186 vfs_exchange_end(exch); 1140 1187 1141 sysarg_t rc_orig;1188 int rc_orig; 1142 1189 async_wait_for(req, &rc_orig); 1143 1190 … … 1154 1201 * @param path Old path to be unlinked 1155 1202 * 1156 * @return EOK on success or a negativeerror code1203 * @return EOK on success or an error code 1157 1204 */ 1158 1205 int vfs_unlink_path(const char *path) 1159 1206 { 1160 int expect = vfs_lookup(path, 0); 1161 if (expect < 0) 1162 return expect; 1207 int expect; 1208 int rc = vfs_lookup(path, 0, &expect); 1209 if (rc != EOK) 1210 return rc; 1163 1211 1164 1212 char *child; 1165 int parent = get_parent_and_child(path, &child); 1166 if (parent < 0) { 1213 int parent; 1214 rc = get_parent_and_child(path, &parent, &child); 1215 if (rc != EOK) { 1167 1216 vfs_put(expect); 1168 return parent;1169 } 1170 1171 intrc = vfs_unlink(parent, child, expect);1217 return rc; 1218 } 1219 1220 rc = vfs_unlink(parent, child, expect); 1172 1221 1173 1222 free(child); … … 1181 1230 * @param mp File handle representing the mount-point 1182 1231 * 1183 * @return EOK on success or a negativeerror code1232 * @return EOK on success or an error code 1184 1233 */ 1185 1234 int vfs_unmount(int mp) … … 1195 1244 * @param mpp Mount-point path 1196 1245 * 1197 * @return EOK on success or a negativeerror code1246 * @return EOK on success or an error code 1198 1247 */ 1199 1248 int vfs_unmount_path(const char *mpp) 1200 1249 { 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); 1250 int mp; 1251 int rc = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY, &mp); 1252 if (rc != EOK) 1253 return rc; 1254 1255 rc = vfs_unmount(mp); 1206 1256 vfs_put(mp); 1207 1257 return rc; … … 1213 1263 * @param path Parent-relative path to be walked 1214 1264 * @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 )1265 * @param[out] handle File handle representing the result on success. 1266 * 1267 * @return Error code. 1268 */ 1269 int vfs_walk(int parent, const char *path, int flags, int *handle) 1220 1270 { 1221 1271 async_exch_t *exch = vfs_exchange_begin(); … … 1223 1273 ipc_call_t answer; 1224 1274 aid_t req = async_send_2(exch, VFS_IN_WALK, parent, flags, &answer); 1225 sysarg_t rc = async_data_write_start(exch, path, str_size(path));1275 int rc = async_data_write_start(exch, path, str_size(path)); 1226 1276 vfs_exchange_end(exch); 1227 1277 1228 sysarg_t rc_orig;1278 int rc_orig; 1229 1279 async_wait_for(req, &rc_orig); 1230 1280 … … 1235 1285 return (int) rc; 1236 1286 1237 return (int) IPC_GET_ARG1(answer); 1287 *handle = (int) IPC_GET_ARG1(answer); 1288 return EOK; 1238 1289 } 1239 1290 … … 1247 1298 * @param buf Data, @a nbytes bytes long 1248 1299 * @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) 1300 * @param nwritten Place to store number of bytes written 1301 * 1302 * @return On success, EOK, @a *nwr is filled with number 1303 * of bytes written 1304 * @return On failure, an error code 1305 */ 1306 int vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte, 1307 size_t *nwritten) 1254 1308 { 1255 1309 ssize_t cnt = 0; 1256 ssize_t nwr itten= 0;1310 ssize_t nwr = 0; 1257 1311 const uint8_t *bp = (uint8_t *) buf; 1258 1312 int rc; … … 1260 1314 do { 1261 1315 bp += cnt; 1262 nwr itten+= cnt;1316 nwr += cnt; 1263 1317 *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 1318 rc = vfs_write_short(file, *pos, bp, nbyte - nwr, &cnt); 1319 } while (rc == EOK && ((ssize_t )nbyte - nwr - cnt) > 0); 1320 1321 if (rc != EOK) { 1322 *nwritten = nwr; 1323 return rc; 1324 } 1325 1326 nwr += cnt; 1270 1327 *pos += cnt; 1271 return nbyte; 1328 *nwritten = nwr; 1329 return EOK; 1272 1330 } 1273 1331 … … 1283 1341 * @param[out] nread Actual number of bytes written (0 or more) 1284 1342 * 1285 * @return EOK on success or a negativeerror code1343 * @return EOK on success or an error code 1286 1344 */ 1287 1345 int vfs_write_short(int file, aoff64_t pos, const void *buf, size_t nbyte, 1288 1346 ssize_t *nwritten) 1289 1347 { 1290 sysarg_t rc;1348 int rc; 1291 1349 ipc_call_t answer; 1292 1350 aid_t req;
Note:
See TracChangeset
for help on using the changeset viewer.