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