Changeset f77c1c9 in mainline for uspace/lib/c/generic/vfs/vfs.c
- Timestamp:
- 2017-12-08T21:03:35Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c19a5a59
- Parents:
- c1694b6b
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2017-12-07 19:44:55)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2017-12-08 21:03:35)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/vfs/vfs.c
rc1694b6b rf77c1c9 128 128 static int root_fd = -1; 129 129 130 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) 131 131 { 132 132 size_t size; … … 136 136 137 137 char *slash = str_rchr(apath, L'/'); 138 int parent;139 138 if (slash == apath) { 140 parent = vfs_root(); 139 *parent = vfs_root(); 140 if (*parent < 0) { 141 free(apath); 142 return EBADF; 143 } 141 144 *child = apath; 145 return EOK; 142 146 } else { 143 147 *slash = '\0'; 144 parent = vfs_lookup(apath, WALK_DIRECTORY);145 if ( parent < 0) {148 int rc = vfs_lookup(apath, WALK_DIRECTORY, parent); 149 if (rc != EOK) { 146 150 free(apath); 147 return parent;151 return rc; 148 152 } 149 153 *slash = '/'; … … 151 155 free(apath); 152 156 if (!*child) { 153 vfs_put( parent);157 vfs_put(*parent); 154 158 return ENOMEM; 155 159 } 156 } 157 158 return parent; 160 161 return rc; 162 } 163 159 164 } 160 165 … … 233 238 * @return New file handle on success or a negative error code 234 239 */ 235 int vfs_clone(int file_from, int file_to, bool high) 236 { 240 int vfs_clone(int file_from, int file_to, bool high, int *handle) 241 { 242 assert(handle != NULL); 243 237 244 async_exch_t *vfs_exch = vfs_exchange_begin(); 238 int rc = async_req_3_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from, 239 (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); 240 248 vfs_exchange_end(vfs_exch); 249 250 if (rc == EOK) { 251 *handle = ret; 252 } 241 253 return rc; 242 254 } … … 277 289 return ENOMEM; 278 290 279 int fd = vfs_lookup(abs, WALK_DIRECTORY); 280 if (fd < 0) { 291 int fd; 292 int rc = vfs_lookup(abs, WALK_DIRECTORY, &fd); 293 if (rc != EOK) { 281 294 free(abs); 282 return fd;295 return rc; 283 296 } 284 297 … … 491 504 { 492 505 int flags = (kind == KIND_DIRECTORY) ? WALK_DIRECTORY : WALK_REGULAR; 493 int file = vfs_walk(parent, child, WALK_MUST_CREATE | flags);494 495 if ( file < 0)496 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; 497 510 498 511 if (linkedfd) … … 521 534 { 522 535 char *child; 523 int parent = get_parent_and_child(path, &child); 524 if (parent < 0) 525 return parent; 526 527 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); 528 542 529 543 free(child); 530 544 vfs_put(parent); 531 545 return rc; 532 } 546 } 533 547 534 548 /** Lookup a path relative to the local root … … 536 550 * @param path Path to be looked up 537 551 * @param flags Walk flags 538 * 539 * @return File handle representing the result on success or a negative540 * error code on error541 */ 542 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) 543 557 { 544 558 size_t size; … … 546 560 if (!p) 547 561 return ENOMEM; 562 548 563 int root = vfs_root(); 549 564 if (root < 0) { … … 551 566 return ENOENT; 552 567 } 553 int rc = vfs_walk(root, p, flags); 568 569 int rc = vfs_walk(root, p, flags, handle); 554 570 vfs_put(root); 555 571 free(p); … … 564 580 * @param flags Walk flags 565 581 * @param mode Mode in which to open file in 582 * @param[out] handle Pointer to variable where handle is to be written. 566 583 * 567 584 * @return EOK on success or a negative error code 568 585 */ 569 int vfs_lookup_open(const char *path, int flags, int mode) 570 { 571 int file = vfs_lookup(path, flags); 572 if (file < 0) 573 return file; 574 575 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); 576 594 if (rc != EOK) { 577 595 vfs_put(file); 578 596 return rc; 579 597 } 580 581 return file; 598 599 *handle = file; 600 return EOK; 582 601 } 583 602 … … 707 726 } 708 727 709 int mpfd = vfs_walk(root_fd, mpa, WALK_DIRECTORY); 710 if (mpfd >= 0) { 728 int mpfd; 729 rc = vfs_walk(root_fd, mpa, WALK_DIRECTORY, &mpfd); 730 if (rc == EOK) { 711 731 rc = vfs_mount(mpfd, fs_name, service_id, opts, flags, 712 732 instance, NULL); 713 733 vfs_put(mpfd); 714 } else {715 rc = mpfd;716 734 } 717 735 } … … 775 793 * @param high If true, the received file handle will be allocated from high 776 794 * indices 795 * @param[out] handle Received handle. 777 796 * 778 797 * @return EOK on success or a negative error code 779 798 */ 780 int vfs_receive_handle(bool high )799 int vfs_receive_handle(bool high, int *handle) 781 800 { 782 801 ipc_callid_t callid; … … 795 814 async_exchange_end(vfs_exch); 796 815 797 if (rc != EOK) 798 return rc; 799 return ret; 816 if (rc == EOK) { 817 *handle = (int) ret; 818 } 819 820 return rc; 800 821 } 801 822 … … 976 997 /** Return a new file handle representing the local root 977 998 * 978 * @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 979 1000 */ 980 1001 int vfs_root(void) 981 1002 { 982 fibril_mutex_lock(&root_mutex); 983 int r; 984 if (root_fd < 0) 985 r = ENOENT; 986 else 987 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 } 988 1013 fibril_mutex_unlock(&root_mutex); 989 return r;1014 return fd; 990 1015 } 991 1016 … … 997 1022 * 998 1023 * @param nroot The new local root file handle 999 */ 1000 void vfs_root_set(int nroot) 1001 { 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 1002 1035 fibril_mutex_lock(&root_mutex); 1003 1036 if (root_fd >= 0) 1004 1037 vfs_put(root_fd); 1005 root_fd = vfs_clone(nroot, -1, true);1038 root_fd = new_root; 1006 1039 fibril_mutex_unlock(&root_mutex); 1040 1041 return EOK; 1007 1042 } 1008 1043 … … 1050 1085 int vfs_stat_path(const char *path, struct stat *stat) 1051 1086 { 1052 int file = vfs_lookup(path, 0); 1053 if (file < 0) 1054 return file; 1055 1056 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); 1057 1093 1058 1094 vfs_put(file); … … 1095 1131 int vfs_statfs_path(const char *path, struct statfs *st) 1096 1132 { 1097 int file = vfs_lookup(path, 0); 1098 if (file < 0) 1099 return file; 1100 1101 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); 1102 1139 1103 1140 vfs_put(file); … … 1165 1202 int vfs_unlink_path(const char *path) 1166 1203 { 1167 int expect = vfs_lookup(path, 0); 1168 if (expect < 0) 1169 return expect; 1204 int expect; 1205 int rc = vfs_lookup(path, 0, &expect); 1206 if (rc != EOK) 1207 return rc; 1170 1208 1171 1209 char *child; 1172 int parent = get_parent_and_child(path, &child); 1173 if (parent < 0) { 1210 int parent; 1211 rc = get_parent_and_child(path, &parent, &child); 1212 if (rc != EOK) { 1174 1213 vfs_put(expect); 1175 return parent;1176 } 1177 1178 intrc = vfs_unlink(parent, child, expect);1214 return rc; 1215 } 1216 1217 rc = vfs_unlink(parent, child, expect); 1179 1218 1180 1219 free(child); … … 1206 1245 int vfs_unmount_path(const char *mpp) 1207 1246 { 1208 int mp = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY); 1209 if (mp < 0) 1210 return mp; 1211 1212 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); 1213 1253 vfs_put(mp); 1214 1254 return rc; … … 1220 1260 * @param path Parent-relative path to be walked 1221 1261 * @param flags Flags influencing the walk 1222 * 1223 * @retrun File handle representing the result on success or1224 * a negative error code on error1225 */ 1226 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) 1227 1267 { 1228 1268 async_exch_t *exch = vfs_exchange_begin(); … … 1242 1282 return (int) rc; 1243 1283 1244 return (int) IPC_GET_ARG1(answer); 1284 *handle = (int) IPC_GET_ARG1(answer); 1285 return EOK; 1245 1286 } 1246 1287
Note:
See TracChangeset
for help on using the changeset viewer.