Rev 4581 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4581 | Rev 4718 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup libc |
29 | /** @addtogroup libc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <vfs/vfs.h> |
35 | #include <vfs/vfs.h> |
36 | #include <vfs/canonify.h> |
36 | #include <vfs/canonify.h> |
37 | #include <stdlib.h> |
37 | #include <stdlib.h> |
38 | #include <unistd.h> |
38 | #include <unistd.h> |
39 | #include <dirent.h> |
39 | #include <dirent.h> |
40 | #include <fcntl.h> |
40 | #include <fcntl.h> |
41 | #include <sys/stat.h> |
- | |
42 | #include <stdio.h> |
41 | #include <stdio.h> |
- | 42 | #include <sys/stat.h> |
|
43 | #include <sys/types.h> |
43 | #include <sys/types.h> |
44 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
45 | #include <ipc/services.h> |
45 | #include <ipc/services.h> |
46 | #include <async.h> |
46 | #include <async.h> |
47 | #include <atomic.h> |
47 | #include <atomic.h> |
48 | #include <futex.h> |
48 | #include <futex.h> |
49 | #include <errno.h> |
49 | #include <errno.h> |
50 | #include <string.h> |
50 | #include <string.h> |
51 | #include <devmap.h> |
51 | #include <devmap.h> |
52 | #include <ipc/vfs.h> |
52 | #include <ipc/vfs.h> |
53 | #include <ipc/devmap.h> |
53 | #include <ipc/devmap.h> |
54 | 54 | ||
55 | static int vfs_phone = -1; |
55 | static int vfs_phone = -1; |
56 | static futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
56 | static futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
57 | static futex_t cwd_futex = FUTEX_INITIALIZER; |
57 | static futex_t cwd_futex = FUTEX_INITIALIZER; |
58 | 58 | ||
59 | DIR *cwd_dir = NULL; |
59 | DIR *cwd_dir = NULL; |
60 | char *cwd_path = NULL; |
60 | char *cwd_path = NULL; |
61 | size_t cwd_size = 0; |
61 | size_t cwd_size = 0; |
62 | 62 | ||
63 | char *absolutize(const char *path, size_t *retlen) |
63 | char *absolutize(const char *path, size_t *retlen) |
64 | { |
64 | { |
65 | char *ncwd_path; |
65 | char *ncwd_path; |
66 | char *ncwd_path_nc; |
66 | char *ncwd_path_nc; |
67 | 67 | ||
68 | futex_down(&cwd_futex); |
68 | futex_down(&cwd_futex); |
69 | size_t size = str_size(path); |
69 | size_t size = str_size(path); |
70 | if (*path != '/') { |
70 | if (*path != '/') { |
71 | if (!cwd_path) { |
71 | if (!cwd_path) { |
72 | futex_up(&cwd_futex); |
72 | futex_up(&cwd_futex); |
73 | return NULL; |
73 | return NULL; |
74 | } |
74 | } |
75 | ncwd_path_nc = malloc(cwd_size + 1 + size + 1); |
75 | ncwd_path_nc = malloc(cwd_size + 1 + size + 1); |
76 | if (!ncwd_path_nc) { |
76 | if (!ncwd_path_nc) { |
77 | futex_up(&cwd_futex); |
77 | futex_up(&cwd_futex); |
78 | return NULL; |
78 | return NULL; |
79 | } |
79 | } |
80 | str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path); |
80 | str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path); |
81 | ncwd_path_nc[cwd_size] = '/'; |
81 | ncwd_path_nc[cwd_size] = '/'; |
82 | ncwd_path_nc[cwd_size + 1] = '\0'; |
82 | ncwd_path_nc[cwd_size + 1] = '\0'; |
83 | } else { |
83 | } else { |
84 | ncwd_path_nc = malloc(size + 1); |
84 | ncwd_path_nc = malloc(size + 1); |
85 | if (!ncwd_path_nc) { |
85 | if (!ncwd_path_nc) { |
86 | futex_up(&cwd_futex); |
86 | futex_up(&cwd_futex); |
87 | return NULL; |
87 | return NULL; |
88 | } |
88 | } |
89 | ncwd_path_nc[0] = '\0'; |
89 | ncwd_path_nc[0] = '\0'; |
90 | } |
90 | } |
91 | str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path); |
91 | str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path); |
92 | ncwd_path = canonify(ncwd_path_nc, retlen); |
92 | ncwd_path = canonify(ncwd_path_nc, retlen); |
93 | if (!ncwd_path) { |
93 | if (!ncwd_path) { |
94 | futex_up(&cwd_futex); |
94 | futex_up(&cwd_futex); |
95 | free(ncwd_path_nc); |
95 | free(ncwd_path_nc); |
96 | return NULL; |
96 | return NULL; |
97 | } |
97 | } |
98 | /* |
98 | /* |
99 | * We need to clone ncwd_path because canonify() works in-place and thus |
99 | * We need to clone ncwd_path because canonify() works in-place and thus |
100 | * the address in ncwd_path need not be the same as ncwd_path_nc, even |
100 | * the address in ncwd_path need not be the same as ncwd_path_nc, even |
101 | * though they both point into the same dynamically allocated buffer. |
101 | * though they both point into the same dynamically allocated buffer. |
102 | */ |
102 | */ |
103 | ncwd_path = str_dup(ncwd_path); |
103 | ncwd_path = str_dup(ncwd_path); |
104 | free(ncwd_path_nc); |
104 | free(ncwd_path_nc); |
105 | if (!ncwd_path) { |
105 | if (!ncwd_path) { |
106 | futex_up(&cwd_futex); |
106 | futex_up(&cwd_futex); |
107 | return NULL; |
107 | return NULL; |
108 | } |
108 | } |
109 | futex_up(&cwd_futex); |
109 | futex_up(&cwd_futex); |
110 | return ncwd_path; |
110 | return ncwd_path; |
111 | } |
111 | } |
112 | 112 | ||
113 | static void vfs_connect(void) |
113 | static void vfs_connect(void) |
114 | { |
114 | { |
115 | while (vfs_phone < 0) |
115 | while (vfs_phone < 0) |
116 | vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
116 | vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
117 | } |
117 | } |
118 | 118 | ||
119 | int mount(const char *fs_name, const char *mp, const char *dev, |
119 | int mount(const char *fs_name, const char *mp, const char *dev, |
120 | const char *opts, unsigned int flags) |
120 | const char *opts, unsigned int flags) |
121 | { |
121 | { |
122 | int res; |
122 | int res; |
123 | ipcarg_t rc; |
123 | ipcarg_t rc; |
124 | aid_t req; |
124 | aid_t req; |
125 | dev_handle_t dev_handle; |
125 | dev_handle_t dev_handle; |
126 | 126 | ||
127 | res = devmap_device_get_handle(dev, &dev_handle, flags); |
127 | res = devmap_device_get_handle(dev, &dev_handle, flags); |
128 | if (res != EOK) |
128 | if (res != EOK) |
129 | return res; |
129 | return res; |
130 | 130 | ||
131 | size_t mpa_size; |
131 | size_t mpa_size; |
132 | char *mpa = absolutize(mp, &mpa_size); |
132 | char *mpa = absolutize(mp, &mpa_size); |
133 | if (!mpa) |
133 | if (!mpa) |
134 | return ENOMEM; |
134 | return ENOMEM; |
135 | 135 | ||
136 | futex_down(&vfs_phone_futex); |
136 | futex_down(&vfs_phone_futex); |
137 | async_serialize_start(); |
137 | async_serialize_start(); |
138 | vfs_connect(); |
138 | vfs_connect(); |
139 | 139 | ||
140 | req = async_send_2(vfs_phone, VFS_MOUNT, dev_handle, flags, NULL); |
140 | req = async_send_2(vfs_phone, VFS_IN_MOUNT, dev_handle, flags, NULL); |
141 | rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_size); |
141 | rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_size); |
142 | if (rc != EOK) { |
142 | if (rc != EOK) { |
143 | async_wait_for(req, NULL); |
143 | async_wait_for(req, NULL); |
144 | async_serialize_end(); |
144 | async_serialize_end(); |
145 | futex_up(&vfs_phone_futex); |
145 | futex_up(&vfs_phone_futex); |
146 | free(mpa); |
146 | free(mpa); |
147 | return (int) rc; |
147 | return (int) rc; |
148 | } |
148 | } |
149 | 149 | ||
150 | rc = ipc_data_write_start(vfs_phone, (void *) opts, str_size(opts)); |
150 | rc = ipc_data_write_start(vfs_phone, (void *) opts, str_size(opts)); |
151 | if (rc != EOK) { |
151 | if (rc != EOK) { |
152 | async_wait_for(req, NULL); |
152 | async_wait_for(req, NULL); |
153 | async_serialize_end(); |
153 | async_serialize_end(); |
154 | futex_up(&vfs_phone_futex); |
154 | futex_up(&vfs_phone_futex); |
155 | free(mpa); |
155 | free(mpa); |
156 | return (int) rc; |
156 | return (int) rc; |
157 | } |
157 | } |
158 | 158 | ||
159 | rc = ipc_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name)); |
159 | rc = ipc_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name)); |
160 | if (rc != EOK) { |
160 | if (rc != EOK) { |
161 | async_wait_for(req, NULL); |
161 | async_wait_for(req, NULL); |
162 | async_serialize_end(); |
162 | async_serialize_end(); |
163 | futex_up(&vfs_phone_futex); |
163 | futex_up(&vfs_phone_futex); |
164 | free(mpa); |
164 | free(mpa); |
165 | return (int) rc; |
165 | return (int) rc; |
166 | } |
166 | } |
167 | 167 | ||
168 | /* Ask VFS whether it likes fs_name. */ |
168 | /* Ask VFS whether it likes fs_name. */ |
169 | rc = async_req_0_0(vfs_phone, IPC_M_PING); |
169 | rc = async_req_0_0(vfs_phone, IPC_M_PING); |
170 | if (rc != EOK) { |
170 | if (rc != EOK) { |
171 | async_wait_for(req, NULL); |
171 | async_wait_for(req, NULL); |
172 | async_serialize_end(); |
172 | async_serialize_end(); |
173 | futex_up(&vfs_phone_futex); |
173 | futex_up(&vfs_phone_futex); |
174 | free(mpa); |
174 | free(mpa); |
175 | return (int) rc; |
175 | return (int) rc; |
176 | } |
176 | } |
177 | 177 | ||
178 | async_wait_for(req, &rc); |
178 | async_wait_for(req, &rc); |
179 | async_serialize_end(); |
179 | async_serialize_end(); |
180 | futex_up(&vfs_phone_futex); |
180 | futex_up(&vfs_phone_futex); |
181 | free(mpa); |
181 | free(mpa); |
182 | 182 | ||
183 | return (int) rc; |
183 | return (int) rc; |
184 | } |
184 | } |
185 | 185 | ||
186 | static int _open(const char *path, int lflag, int oflag, ...) |
186 | static int _open(const char *path, int lflag, int oflag, ...) |
187 | { |
187 | { |
188 | ipcarg_t rc; |
188 | ipcarg_t rc; |
189 | ipc_call_t answer; |
189 | ipc_call_t answer; |
190 | aid_t req; |
190 | aid_t req; |
191 | 191 | ||
192 | size_t pa_size; |
192 | size_t pa_size; |
193 | char *pa = absolutize(path, &pa_size); |
193 | char *pa = absolutize(path, &pa_size); |
194 | if (!pa) |
194 | if (!pa) |
195 | return ENOMEM; |
195 | return ENOMEM; |
196 | 196 | ||
197 | futex_down(&vfs_phone_futex); |
197 | futex_down(&vfs_phone_futex); |
198 | async_serialize_start(); |
198 | async_serialize_start(); |
199 | vfs_connect(); |
199 | vfs_connect(); |
200 | 200 | ||
201 | req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer); |
201 | req = async_send_3(vfs_phone, VFS_IN_OPEN, lflag, oflag, 0, &answer); |
202 | rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
202 | rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
203 | if (rc != EOK) { |
203 | if (rc != EOK) { |
204 | async_wait_for(req, NULL); |
204 | async_wait_for(req, NULL); |
205 | async_serialize_end(); |
205 | async_serialize_end(); |
206 | futex_up(&vfs_phone_futex); |
206 | futex_up(&vfs_phone_futex); |
207 | free(pa); |
207 | free(pa); |
208 | return (int) rc; |
208 | return (int) rc; |
209 | } |
209 | } |
210 | async_wait_for(req, &rc); |
210 | async_wait_for(req, &rc); |
211 | async_serialize_end(); |
211 | async_serialize_end(); |
212 | futex_up(&vfs_phone_futex); |
212 | futex_up(&vfs_phone_futex); |
213 | free(pa); |
213 | free(pa); |
214 | 214 | ||
215 | if (rc != EOK) |
215 | if (rc != EOK) |
216 | return (int) rc; |
216 | return (int) rc; |
217 | 217 | ||
218 | return (int) IPC_GET_ARG1(answer); |
218 | return (int) IPC_GET_ARG1(answer); |
219 | } |
219 | } |
220 | 220 | ||
221 | int open(const char *path, int oflag, ...) |
221 | int open(const char *path, int oflag, ...) |
222 | { |
222 | { |
223 | return _open(path, L_FILE, oflag); |
223 | return _open(path, L_FILE, oflag); |
224 | } |
224 | } |
225 | 225 | ||
226 | int open_node(fdi_node_t *node, int oflag) |
226 | int open_node(fdi_node_t *node, int oflag) |
227 | { |
227 | { |
228 | futex_down(&vfs_phone_futex); |
228 | futex_down(&vfs_phone_futex); |
229 | async_serialize_start(); |
229 | async_serialize_start(); |
230 | vfs_connect(); |
230 | vfs_connect(); |
231 | 231 | ||
232 | ipc_call_t answer; |
232 | ipc_call_t answer; |
233 | aid_t req = async_send_4(vfs_phone, VFS_OPEN_NODE, node->fs_handle, |
233 | aid_t req = async_send_4(vfs_phone, VFS_IN_OPEN_NODE, node->fs_handle, |
234 | node->dev_handle, node->index, oflag, &answer); |
234 | node->dev_handle, node->index, oflag, &answer); |
235 | 235 | ||
236 | ipcarg_t rc; |
236 | ipcarg_t rc; |
237 | async_wait_for(req, &rc); |
237 | async_wait_for(req, &rc); |
238 | async_serialize_end(); |
238 | async_serialize_end(); |
239 | futex_up(&vfs_phone_futex); |
239 | futex_up(&vfs_phone_futex); |
240 | 240 | ||
241 | if (rc != EOK) |
241 | if (rc != EOK) |
242 | return (int) rc; |
242 | return (int) rc; |
243 | 243 | ||
244 | return (int) IPC_GET_ARG1(answer); |
244 | return (int) IPC_GET_ARG1(answer); |
245 | } |
245 | } |
246 | 246 | ||
247 | int close(int fildes) |
247 | int close(int fildes) |
248 | { |
248 | { |
249 | ipcarg_t rc; |
249 | ipcarg_t rc; |
250 | 250 | ||
251 | futex_down(&vfs_phone_futex); |
251 | futex_down(&vfs_phone_futex); |
252 | async_serialize_start(); |
252 | async_serialize_start(); |
253 | vfs_connect(); |
253 | vfs_connect(); |
254 | 254 | ||
255 | rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes); |
255 | rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes); |
256 | 256 | ||
257 | async_serialize_end(); |
257 | async_serialize_end(); |
258 | futex_up(&vfs_phone_futex); |
258 | futex_up(&vfs_phone_futex); |
259 | 259 | ||
260 | return (int)rc; |
260 | return (int)rc; |
261 | } |
261 | } |
262 | 262 | ||
263 | ssize_t read(int fildes, void *buf, size_t nbyte) |
263 | ssize_t read(int fildes, void *buf, size_t nbyte) |
264 | { |
264 | { |
265 | ipcarg_t rc; |
265 | ipcarg_t rc; |
266 | ipc_call_t answer; |
266 | ipc_call_t answer; |
267 | aid_t req; |
267 | aid_t req; |
268 | 268 | ||
269 | futex_down(&vfs_phone_futex); |
269 | futex_down(&vfs_phone_futex); |
270 | async_serialize_start(); |
270 | async_serialize_start(); |
271 | vfs_connect(); |
271 | vfs_connect(); |
272 | 272 | ||
273 | req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
273 | req = async_send_1(vfs_phone, VFS_IN_READ, fildes, &answer); |
274 | rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte); |
274 | rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte); |
275 | if (rc != EOK) { |
275 | if (rc != EOK) { |
276 | async_wait_for(req, NULL); |
276 | async_wait_for(req, NULL); |
277 | async_serialize_end(); |
277 | async_serialize_end(); |
278 | futex_up(&vfs_phone_futex); |
278 | futex_up(&vfs_phone_futex); |
279 | return (ssize_t) rc; |
279 | return (ssize_t) rc; |
280 | } |
280 | } |
281 | async_wait_for(req, &rc); |
281 | async_wait_for(req, &rc); |
282 | async_serialize_end(); |
282 | async_serialize_end(); |
283 | futex_up(&vfs_phone_futex); |
283 | futex_up(&vfs_phone_futex); |
284 | if (rc == EOK) |
284 | if (rc == EOK) |
285 | return (ssize_t) IPC_GET_ARG1(answer); |
285 | return (ssize_t) IPC_GET_ARG1(answer); |
286 | else |
286 | else |
287 | return rc; |
287 | return rc; |
288 | } |
288 | } |
289 | 289 | ||
290 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
290 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
291 | { |
291 | { |
292 | ipcarg_t rc; |
292 | ipcarg_t rc; |
293 | ipc_call_t answer; |
293 | ipc_call_t answer; |
294 | aid_t req; |
294 | aid_t req; |
295 | 295 | ||
296 | futex_down(&vfs_phone_futex); |
296 | futex_down(&vfs_phone_futex); |
297 | async_serialize_start(); |
297 | async_serialize_start(); |
298 | vfs_connect(); |
298 | vfs_connect(); |
299 | 299 | ||
300 | req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
300 | req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer); |
301 | rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte); |
301 | rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte); |
302 | if (rc != EOK) { |
302 | if (rc != EOK) { |
303 | async_wait_for(req, NULL); |
303 | async_wait_for(req, NULL); |
304 | async_serialize_end(); |
304 | async_serialize_end(); |
305 | futex_up(&vfs_phone_futex); |
305 | futex_up(&vfs_phone_futex); |
306 | return (ssize_t) rc; |
306 | return (ssize_t) rc; |
307 | } |
307 | } |
308 | async_wait_for(req, &rc); |
308 | async_wait_for(req, &rc); |
309 | async_serialize_end(); |
309 | async_serialize_end(); |
310 | futex_up(&vfs_phone_futex); |
310 | futex_up(&vfs_phone_futex); |
311 | if (rc == EOK) |
311 | if (rc == EOK) |
312 | return (ssize_t) IPC_GET_ARG1(answer); |
312 | return (ssize_t) IPC_GET_ARG1(answer); |
313 | else |
313 | else |
314 | return -1; |
314 | return -1; |
315 | } |
315 | } |
316 | 316 | ||
317 | int fd_phone(int fildes) |
317 | int fsync(int fildes) |
318 | { |
318 | { |
319 | futex_down(&vfs_phone_futex); |
319 | futex_down(&vfs_phone_futex); |
320 | async_serialize_start(); |
320 | async_serialize_start(); |
321 | vfs_connect(); |
321 | vfs_connect(); |
322 | 322 | ||
323 | ipcarg_t device; |
- | |
324 | ipcarg_t rc = async_req_1_1(vfs_phone, VFS_DEVICE, fildes, &device); |
323 | ipcarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes); |
325 | 324 | ||
326 | async_serialize_end(); |
325 | async_serialize_end(); |
327 | futex_up(&vfs_phone_futex); |
326 | futex_up(&vfs_phone_futex); |
328 | 327 | ||
329 | if (rc != EOK) |
- | |
330 | return -1; |
328 | return (int) rc; |
331 | - | ||
332 | return devmap_device_connect((dev_handle_t) device, 0); |
- | |
333 | } |
329 | } |
334 | 330 | ||
335 | int fd_node(int fildes, fdi_node_t *node) |
331 | off_t lseek(int fildes, off_t offset, int whence) |
336 | { |
332 | { |
- | 333 | ipcarg_t rc; |
|
- | 334 | ||
337 | futex_down(&vfs_phone_futex); |
335 | futex_down(&vfs_phone_futex); |
338 | async_serialize_start(); |
336 | async_serialize_start(); |
339 | vfs_connect(); |
337 | vfs_connect(); |
340 | 338 | ||
341 | ipcarg_t fs_handle; |
- | |
342 | ipcarg_t dev_handle; |
- | |
343 | ipcarg_t index; |
339 | ipcarg_t newoffs; |
344 | ipcarg_t rc = async_req_1_3(vfs_phone, VFS_NODE, fildes, &fs_handle, |
340 | rc = async_req_3_1(vfs_phone, VFS_IN_SEEK, fildes, offset, whence, |
345 | &dev_handle, &index); |
341 | &newoffs); |
346 | 342 | ||
347 | async_serialize_end(); |
343 | async_serialize_end(); |
348 | futex_up(&vfs_phone_futex); |
344 | futex_up(&vfs_phone_futex); |
- | 345 | ||
- | 346 | if (rc != EOK) |
|
- | 347 | return (off_t) -1; |
|
349 | 348 | ||
350 | if (rc == EOK) { |
- | |
351 | node->fs_handle = (fs_handle_t) fs_handle; |
- | |
352 | node->dev_handle = (dev_handle_t) dev_handle; |
- | |
353 | node->index = (fs_index_t) index; |
- | |
354 | } |
- | |
355 | - | ||
356 | return rc; |
349 | return (off_t) newoffs; |
357 | } |
350 | } |
358 | 351 | ||
359 | int fsync(int fildes) |
352 | int ftruncate(int fildes, off_t length) |
360 | { |
353 | { |
- | 354 | ipcarg_t rc; |
|
- | 355 | ||
361 | futex_down(&vfs_phone_futex); |
356 | futex_down(&vfs_phone_futex); |
362 | async_serialize_start(); |
357 | async_serialize_start(); |
363 | vfs_connect(); |
358 | vfs_connect(); |
364 | 359 | ||
365 | ipcarg_t rc = async_req_1_0(vfs_phone, VFS_SYNC, fildes); |
360 | rc = async_req_2_0(vfs_phone, VFS_IN_TRUNCATE, fildes, length); |
366 | - | ||
367 | async_serialize_end(); |
361 | async_serialize_end(); |
368 | futex_up(&vfs_phone_futex); |
362 | futex_up(&vfs_phone_futex); |
369 | - | ||
370 | return (int) rc; |
363 | return (int) rc; |
371 | } |
364 | } |
372 | 365 | ||
373 | off_t lseek(int fildes, off_t offset, int whence) |
366 | int fstat(int fildes, struct stat *stat) |
374 | { |
367 | { |
375 | ipcarg_t rc; |
368 | ipcarg_t rc; |
- | 369 | aid_t req; |
|
376 | 370 | ||
377 | futex_down(&vfs_phone_futex); |
371 | futex_down(&vfs_phone_futex); |
378 | async_serialize_start(); |
372 | async_serialize_start(); |
379 | vfs_connect(); |
373 | vfs_connect(); |
380 | 374 | ||
- | 375 | req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL); |
|
- | 376 | rc = ipc_data_read_start(vfs_phone, (void *)stat, sizeof(struct stat)); |
|
381 | ipcarg_t newoffs; |
377 | if (rc != EOK) { |
- | 378 | async_wait_for(req, NULL); |
|
- | 379 | async_serialize_end(); |
|
382 | rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
380 | futex_up(&vfs_phone_futex); |
383 | &newoffs); |
381 | return (ssize_t) rc; |
384 | 382 | } |
|
- | 383 | async_wait_for(req, &rc); |
|
385 | async_serialize_end(); |
384 | async_serialize_end(); |
386 | futex_up(&vfs_phone_futex); |
385 | futex_up(&vfs_phone_futex); |
387 | 386 | ||
388 | if (rc != EOK) |
- | |
389 | return (off_t) -1; |
387 | return rc; |
390 | - | ||
391 | return (off_t) newoffs; |
- | |
392 | } |
388 | } |
393 | 389 | ||
394 | int ftruncate(int fildes, off_t length) |
390 | int stat(const char *path, struct stat *stat) |
395 | { |
391 | { |
396 | ipcarg_t rc; |
392 | ipcarg_t rc; |
- | 393 | aid_t req; |
|
- | 394 | ||
- | 395 | size_t pa_size; |
|
- | 396 | char *pa = absolutize(path, &pa_size); |
|
- | 397 | if (!pa) |
|
- | 398 | return ENOMEM; |
|
397 | 399 | ||
398 | futex_down(&vfs_phone_futex); |
400 | futex_down(&vfs_phone_futex); |
399 | async_serialize_start(); |
401 | async_serialize_start(); |
400 | vfs_connect(); |
402 | vfs_connect(); |
401 | 403 | ||
402 | rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
404 | req = async_send_0(vfs_phone, VFS_IN_STAT, NULL); |
- | 405 | rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
|
- | 406 | if (rc != EOK) { |
|
- | 407 | async_wait_for(req, NULL); |
|
- | 408 | async_serialize_end(); |
|
- | 409 | futex_up(&vfs_phone_futex); |
|
- | 410 | free(pa); |
|
- | 411 | return (int) rc; |
|
- | 412 | } |
|
- | 413 | rc = ipc_data_read_start(vfs_phone, stat, sizeof(struct stat)); |
|
- | 414 | if (rc != EOK) { |
|
- | 415 | async_wait_for(req, NULL); |
|
- | 416 | async_serialize_end(); |
|
- | 417 | futex_up(&vfs_phone_futex); |
|
- | 418 | free(pa); |
|
- | 419 | return (int) rc; |
|
- | 420 | } |
|
- | 421 | async_wait_for(req, &rc); |
|
403 | async_serialize_end(); |
422 | async_serialize_end(); |
404 | futex_up(&vfs_phone_futex); |
423 | futex_up(&vfs_phone_futex); |
- | 424 | free(pa); |
|
405 | return (int) rc; |
425 | return rc; |
406 | } |
426 | } |
407 | 427 | ||
408 | DIR *opendir(const char *dirname) |
428 | DIR *opendir(const char *dirname) |
409 | { |
429 | { |
410 | DIR *dirp = malloc(sizeof(DIR)); |
430 | DIR *dirp = malloc(sizeof(DIR)); |
411 | if (!dirp) |
431 | if (!dirp) |
412 | return NULL; |
432 | return NULL; |
413 | dirp->fd = _open(dirname, L_DIRECTORY, 0); |
433 | dirp->fd = _open(dirname, L_DIRECTORY, 0); |
414 | if (dirp->fd < 0) { |
434 | if (dirp->fd < 0) { |
415 | free(dirp); |
435 | free(dirp); |
416 | return NULL; |
436 | return NULL; |
417 | } |
437 | } |
418 | return dirp; |
438 | return dirp; |
419 | } |
439 | } |
420 | 440 | ||
421 | struct dirent *readdir(DIR *dirp) |
441 | struct dirent *readdir(DIR *dirp) |
422 | { |
442 | { |
423 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1); |
443 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1); |
424 | if (len <= 0) |
444 | if (len <= 0) |
425 | return NULL; |
445 | return NULL; |
426 | return &dirp->res; |
446 | return &dirp->res; |
427 | } |
447 | } |
428 | 448 | ||
429 | void rewinddir(DIR *dirp) |
449 | void rewinddir(DIR *dirp) |
430 | { |
450 | { |
431 | (void) lseek(dirp->fd, 0, SEEK_SET); |
451 | (void) lseek(dirp->fd, 0, SEEK_SET); |
432 | } |
452 | } |
433 | 453 | ||
434 | int closedir(DIR *dirp) |
454 | int closedir(DIR *dirp) |
435 | { |
455 | { |
436 | (void) close(dirp->fd); |
456 | (void) close(dirp->fd); |
437 | free(dirp); |
457 | free(dirp); |
438 | return 0; |
458 | return 0; |
439 | } |
459 | } |
440 | 460 | ||
441 | int mkdir(const char *path, mode_t mode) |
461 | int mkdir(const char *path, mode_t mode) |
442 | { |
462 | { |
443 | ipcarg_t rc; |
463 | ipcarg_t rc; |
444 | aid_t req; |
464 | aid_t req; |
445 | 465 | ||
446 | size_t pa_size; |
466 | size_t pa_size; |
447 | char *pa = absolutize(path, &pa_size); |
467 | char *pa = absolutize(path, &pa_size); |
448 | if (!pa) |
468 | if (!pa) |
449 | return ENOMEM; |
469 | return ENOMEM; |
450 | 470 | ||
451 | futex_down(&vfs_phone_futex); |
471 | futex_down(&vfs_phone_futex); |
452 | async_serialize_start(); |
472 | async_serialize_start(); |
453 | vfs_connect(); |
473 | vfs_connect(); |
454 | 474 | ||
455 | req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL); |
475 | req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL); |
456 | rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
476 | rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
457 | if (rc != EOK) { |
477 | if (rc != EOK) { |
458 | async_wait_for(req, NULL); |
478 | async_wait_for(req, NULL); |
459 | async_serialize_end(); |
479 | async_serialize_end(); |
460 | futex_up(&vfs_phone_futex); |
480 | futex_up(&vfs_phone_futex); |
461 | free(pa); |
481 | free(pa); |
462 | return (int) rc; |
482 | return (int) rc; |
463 | } |
483 | } |
464 | async_wait_for(req, &rc); |
484 | async_wait_for(req, &rc); |
465 | async_serialize_end(); |
485 | async_serialize_end(); |
466 | futex_up(&vfs_phone_futex); |
486 | futex_up(&vfs_phone_futex); |
467 | free(pa); |
487 | free(pa); |
468 | return rc; |
488 | return rc; |
469 | } |
489 | } |
470 | 490 | ||
471 | static int _unlink(const char *path, int lflag) |
491 | static int _unlink(const char *path, int lflag) |
472 | { |
492 | { |
473 | ipcarg_t rc; |
493 | ipcarg_t rc; |
474 | aid_t req; |
494 | aid_t req; |
475 | 495 | ||
476 | size_t pa_size; |
496 | size_t pa_size; |
477 | char *pa = absolutize(path, &pa_size); |
497 | char *pa = absolutize(path, &pa_size); |
478 | if (!pa) |
498 | if (!pa) |
479 | return ENOMEM; |
499 | return ENOMEM; |
480 | 500 | ||
481 | futex_down(&vfs_phone_futex); |
501 | futex_down(&vfs_phone_futex); |
482 | async_serialize_start(); |
502 | async_serialize_start(); |
483 | vfs_connect(); |
503 | vfs_connect(); |
484 | 504 | ||
485 | req = async_send_0(vfs_phone, VFS_UNLINK, NULL); |
505 | req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL); |
486 | rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
506 | rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
487 | if (rc != EOK) { |
507 | if (rc != EOK) { |
488 | async_wait_for(req, NULL); |
508 | async_wait_for(req, NULL); |
489 | async_serialize_end(); |
509 | async_serialize_end(); |
490 | futex_up(&vfs_phone_futex); |
510 | futex_up(&vfs_phone_futex); |
491 | free(pa); |
511 | free(pa); |
492 | return (int) rc; |
512 | return (int) rc; |
493 | } |
513 | } |
494 | async_wait_for(req, &rc); |
514 | async_wait_for(req, &rc); |
495 | async_serialize_end(); |
515 | async_serialize_end(); |
496 | futex_up(&vfs_phone_futex); |
516 | futex_up(&vfs_phone_futex); |
497 | free(pa); |
517 | free(pa); |
498 | return rc; |
518 | return rc; |
499 | } |
519 | } |
500 | 520 | ||
501 | int unlink(const char *path) |
521 | int unlink(const char *path) |
502 | { |
522 | { |
503 | return _unlink(path, L_NONE); |
523 | return _unlink(path, L_NONE); |
504 | } |
524 | } |
505 | 525 | ||
506 | int rmdir(const char *path) |
526 | int rmdir(const char *path) |
507 | { |
527 | { |
508 | return _unlink(path, L_DIRECTORY); |
528 | return _unlink(path, L_DIRECTORY); |
509 | } |
529 | } |
510 | 530 | ||
511 | int rename(const char *old, const char *new) |
531 | int rename(const char *old, const char *new) |
512 | { |
532 | { |
513 | ipcarg_t rc; |
533 | ipcarg_t rc; |
514 | aid_t req; |
534 | aid_t req; |
515 | 535 | ||
516 | size_t olda_size; |
536 | size_t olda_size; |
517 | char *olda = absolutize(old, &olda_size); |
537 | char *olda = absolutize(old, &olda_size); |
518 | if (!olda) |
538 | if (!olda) |
519 | return ENOMEM; |
539 | return ENOMEM; |
520 | 540 | ||
521 | size_t newa_size; |
541 | size_t newa_size; |
522 | char *newa = absolutize(new, &newa_size); |
542 | char *newa = absolutize(new, &newa_size); |
523 | if (!newa) { |
543 | if (!newa) { |
524 | free(olda); |
544 | free(olda); |
525 | return ENOMEM; |
545 | return ENOMEM; |
526 | } |
546 | } |
527 | 547 | ||
528 | futex_down(&vfs_phone_futex); |
548 | futex_down(&vfs_phone_futex); |
529 | async_serialize_start(); |
549 | async_serialize_start(); |
530 | vfs_connect(); |
550 | vfs_connect(); |
531 | 551 | ||
532 | req = async_send_0(vfs_phone, VFS_RENAME, NULL); |
552 | req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL); |
533 | rc = ipc_data_write_start(vfs_phone, olda, olda_size); |
553 | rc = ipc_data_write_start(vfs_phone, olda, olda_size); |
534 | if (rc != EOK) { |
554 | if (rc != EOK) { |
535 | async_wait_for(req, NULL); |
555 | async_wait_for(req, NULL); |
536 | async_serialize_end(); |
556 | async_serialize_end(); |
537 | futex_up(&vfs_phone_futex); |
557 | futex_up(&vfs_phone_futex); |
538 | free(olda); |
558 | free(olda); |
539 | free(newa); |
559 | free(newa); |
540 | return (int) rc; |
560 | return (int) rc; |
541 | } |
561 | } |
542 | rc = ipc_data_write_start(vfs_phone, newa, newa_size); |
562 | rc = ipc_data_write_start(vfs_phone, newa, newa_size); |
543 | if (rc != EOK) { |
563 | if (rc != EOK) { |
544 | async_wait_for(req, NULL); |
564 | async_wait_for(req, NULL); |
545 | async_serialize_end(); |
565 | async_serialize_end(); |
546 | futex_up(&vfs_phone_futex); |
566 | futex_up(&vfs_phone_futex); |
547 | free(olda); |
567 | free(olda); |
548 | free(newa); |
568 | free(newa); |
549 | return (int) rc; |
569 | return (int) rc; |
550 | } |
570 | } |
551 | async_wait_for(req, &rc); |
571 | async_wait_for(req, &rc); |
552 | async_serialize_end(); |
572 | async_serialize_end(); |
553 | futex_up(&vfs_phone_futex); |
573 | futex_up(&vfs_phone_futex); |
554 | free(olda); |
574 | free(olda); |
555 | free(newa); |
575 | free(newa); |
556 | return rc; |
576 | return rc; |
557 | } |
577 | } |
558 | 578 | ||
559 | int chdir(const char *path) |
579 | int chdir(const char *path) |
560 | { |
580 | { |
561 | size_t pa_size; |
581 | size_t pa_size; |
562 | char *pa = absolutize(path, &pa_size); |
582 | char *pa = absolutize(path, &pa_size); |
563 | if (!pa) |
583 | if (!pa) |
564 | return ENOMEM; |
584 | return ENOMEM; |
565 | 585 | ||
566 | DIR *d = opendir(pa); |
586 | DIR *d = opendir(pa); |
567 | if (!d) { |
587 | if (!d) { |
568 | free(pa); |
588 | free(pa); |
569 | return ENOENT; |
589 | return ENOENT; |
570 | } |
590 | } |
571 | 591 | ||
572 | futex_down(&cwd_futex); |
592 | futex_down(&cwd_futex); |
573 | if (cwd_dir) { |
593 | if (cwd_dir) { |
574 | closedir(cwd_dir); |
594 | closedir(cwd_dir); |
575 | cwd_dir = NULL; |
595 | cwd_dir = NULL; |
576 | free(cwd_path); |
596 | free(cwd_path); |
577 | cwd_path = NULL; |
597 | cwd_path = NULL; |
578 | cwd_size = 0; |
598 | cwd_size = 0; |
579 | } |
599 | } |
580 | cwd_dir = d; |
600 | cwd_dir = d; |
581 | cwd_path = pa; |
601 | cwd_path = pa; |
582 | cwd_size = pa_size; |
602 | cwd_size = pa_size; |
583 | futex_up(&cwd_futex); |
603 | futex_up(&cwd_futex); |
584 | return EOK; |
604 | return EOK; |
585 | } |
605 | } |
586 | 606 | ||
587 | char *getcwd(char *buf, size_t size) |
607 | char *getcwd(char *buf, size_t size) |
588 | { |
608 | { |
589 | if (!size) |
609 | if (!size) |
590 | return NULL; |
610 | return NULL; |
591 | futex_down(&cwd_futex); |
611 | futex_down(&cwd_futex); |
592 | if (size < cwd_size + 1) { |
612 | if (size < cwd_size + 1) { |
593 | futex_up(&cwd_futex); |
613 | futex_up(&cwd_futex); |
594 | return NULL; |
614 | return NULL; |
595 | } |
615 | } |
596 | str_cpy(buf, size, cwd_path); |
616 | str_cpy(buf, size, cwd_path); |
597 | futex_up(&cwd_futex); |
617 | futex_up(&cwd_futex); |
598 | return buf; |
618 | return buf; |
599 | } |
619 | } |
- | 620 | ||
- | 621 | int fd_phone(int fildes) |
|
- | 622 | { |
|
- | 623 | struct stat stat; |
|
- | 624 | int rc; |
|
- | 625 | ||
- | 626 | rc = fstat(fildes, &stat); |
|
- | 627 | ||
- | 628 | if (!stat.devfs_stat.device) |
|
- | 629 | return -1; |
|
- | 630 | ||
- | 631 | return devmap_device_connect(stat.devfs_stat.device, 0); |
|
- | 632 | } |
|
- | 633 | ||
- | 634 | int fd_node(int fildes, fdi_node_t *node) |
|
- | 635 | { |
|
- | 636 | struct stat stat; |
|
- | 637 | int rc; |
|
- | 638 | ||
- | 639 | rc = fstat(fildes, &stat); |
|
- | 640 | ||
- | 641 | if (rc == EOK) { |
|
- | 642 | node->fs_handle = stat.fs_handle; |
|
- | 643 | node->dev_handle = stat.dev_handle; |
|
- | 644 | node->index = stat.index; |
|
- | 645 | } |
|
- | 646 | ||
- | 647 | return rc; |
|
- | 648 | } |
|
600 | 649 | ||
601 | /** @} |
650 | /** @} |
602 | */ |
651 | */ |
603 | 652 |