Rev 3386 | Rev 4263 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3386 | Rev 4153 | ||
---|---|---|---|
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> |
41 | #include <sys/stat.h> |
42 | #include <stdio.h> |
42 | #include <stdio.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 <ipc/devmap.h> |
51 | #include <ipc/devmap.h> |
52 | #include "../../srv/vfs/vfs.h" |
52 | #include "../../../srv/vfs/vfs.h" |
53 | 53 | ||
54 | int vfs_phone = -1; |
54 | int vfs_phone = -1; |
55 | futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
55 | futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
56 | 56 | ||
57 | futex_t cwd_futex = FUTEX_INITIALIZER; |
57 | futex_t cwd_futex = FUTEX_INITIALIZER; |
58 | DIR *cwd_dir = NULL; |
58 | DIR *cwd_dir = NULL; |
59 | char *cwd_path = NULL; |
59 | char *cwd_path = NULL; |
60 | size_t cwd_len = 0; |
60 | size_t cwd_len = 0; |
61 | 61 | ||
62 | char *absolutize(const char *path, size_t *retlen) |
62 | char *absolutize(const char *path, size_t *retlen) |
63 | { |
63 | { |
64 | char *ncwd_path; |
64 | char *ncwd_path; |
65 | char *ncwd_path_nc; |
65 | char *ncwd_path_nc; |
66 | 66 | ||
67 | futex_down(&cwd_futex); |
67 | futex_down(&cwd_futex); |
68 | size_t len = strlen(path); |
68 | size_t len = strlen(path); |
69 | if (*path != '/') { |
69 | if (*path != '/') { |
70 | if (!cwd_path) { |
70 | if (!cwd_path) { |
71 | futex_up(&cwd_futex); |
71 | futex_up(&cwd_futex); |
72 | return NULL; |
72 | return NULL; |
73 | } |
73 | } |
74 | ncwd_path_nc = malloc(cwd_len + 1 + len + 1); |
74 | ncwd_path_nc = malloc(cwd_len + 1 + len + 1); |
75 | if (!ncwd_path_nc) { |
75 | if (!ncwd_path_nc) { |
76 | futex_up(&cwd_futex); |
76 | futex_up(&cwd_futex); |
77 | return NULL; |
77 | return NULL; |
78 | } |
78 | } |
79 | strcpy(ncwd_path_nc, cwd_path); |
79 | strcpy(ncwd_path_nc, cwd_path); |
80 | ncwd_path_nc[cwd_len] = '/'; |
80 | ncwd_path_nc[cwd_len] = '/'; |
81 | ncwd_path_nc[cwd_len + 1] = '\0'; |
81 | ncwd_path_nc[cwd_len + 1] = '\0'; |
82 | } else { |
82 | } else { |
83 | ncwd_path_nc = malloc(len + 1); |
83 | ncwd_path_nc = malloc(len + 1); |
84 | if (!ncwd_path_nc) { |
84 | if (!ncwd_path_nc) { |
85 | futex_up(&cwd_futex); |
85 | futex_up(&cwd_futex); |
86 | return NULL; |
86 | return NULL; |
87 | } |
87 | } |
88 | ncwd_path_nc[0] = '\0'; |
88 | ncwd_path_nc[0] = '\0'; |
89 | } |
89 | } |
90 | strcat(ncwd_path_nc, path); |
90 | strcat(ncwd_path_nc, path); |
91 | ncwd_path = canonify(ncwd_path_nc, retlen); |
91 | ncwd_path = canonify(ncwd_path_nc, retlen); |
92 | if (!ncwd_path) { |
92 | if (!ncwd_path) { |
93 | futex_up(&cwd_futex); |
93 | futex_up(&cwd_futex); |
94 | free(ncwd_path_nc); |
94 | free(ncwd_path_nc); |
95 | return NULL; |
95 | return NULL; |
96 | } |
96 | } |
97 | /* |
97 | /* |
98 | * We need to clone ncwd_path because canonify() works in-place and thus |
98 | * We need to clone ncwd_path because canonify() works in-place and thus |
99 | * the address in ncwd_path need not be the same as ncwd_path_nc, even |
99 | * the address in ncwd_path need not be the same as ncwd_path_nc, even |
100 | * though they both point into the same dynamically allocated buffer. |
100 | * though they both point into the same dynamically allocated buffer. |
101 | */ |
101 | */ |
102 | ncwd_path = strdup(ncwd_path); |
102 | ncwd_path = strdup(ncwd_path); |
103 | free(ncwd_path_nc); |
103 | free(ncwd_path_nc); |
104 | if (!ncwd_path) { |
104 | if (!ncwd_path) { |
105 | futex_up(&cwd_futex); |
105 | futex_up(&cwd_futex); |
106 | return NULL; |
106 | return NULL; |
107 | } |
107 | } |
108 | futex_up(&cwd_futex); |
108 | futex_up(&cwd_futex); |
109 | return ncwd_path; |
109 | return ncwd_path; |
110 | } |
110 | } |
111 | 111 | ||
112 | static int vfs_connect(void) |
112 | static void vfs_connect(void) |
113 | { |
113 | { |
114 | if (vfs_phone < 0) |
114 | while (vfs_phone < 0) |
115 | vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0); |
115 | vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
116 | return vfs_phone; |
- | |
117 | } |
116 | } |
118 | 117 | ||
119 | static int device_get_handle(char *name, dev_handle_t *handle) |
118 | static int device_get_handle(const char *name, dev_handle_t *handle, |
- | 119 | const unsigned int flags) |
|
120 | { |
120 | { |
- | 121 | int phone; |
|
- | 122 | ||
- | 123 | if (flags & IPC_FLAG_BLOCKING) |
|
121 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, |
124 | phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0); |
122 | 0); |
125 | else |
- | 126 | phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0); |
|
- | 127 | ||
123 | if (phone < 0) |
128 | if (phone < 0) |
124 | return phone; |
129 | return phone; |
125 | 130 | ||
126 | ipc_call_t answer; |
131 | ipc_call_t answer; |
127 | aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0, |
132 | aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0, |
128 | &answer); |
133 | &answer); |
129 | 134 | ||
130 | ipcarg_t retval = ipc_data_write_start(phone, name, strlen(name) + 1); |
135 | ipcarg_t retval = ipc_data_write_start(phone, name, strlen(name) + 1); |
131 | 136 | ||
132 | if (retval != EOK) { |
137 | if (retval != EOK) { |
133 | async_wait_for(req, NULL); |
138 | async_wait_for(req, NULL); |
134 | ipc_hangup(phone); |
139 | ipc_hangup(phone); |
135 | return retval; |
140 | return retval; |
136 | } |
141 | } |
137 | 142 | ||
138 | async_wait_for(req, &retval); |
143 | async_wait_for(req, &retval); |
139 | 144 | ||
140 | if (handle != NULL) |
145 | if (handle != NULL) |
141 | *handle = -1; |
146 | *handle = -1; |
142 | 147 | ||
143 | if (retval == EOK) { |
148 | if (retval == EOK) { |
144 | if (handle != NULL) |
149 | if (handle != NULL) |
145 | *handle = (dev_handle_t) IPC_GET_ARG1(answer); |
150 | *handle = (dev_handle_t) IPC_GET_ARG1(answer); |
146 | } |
151 | } |
147 | 152 | ||
148 | ipc_hangup(phone); |
153 | ipc_hangup(phone); |
149 | return retval; |
154 | return retval; |
150 | } |
155 | } |
151 | 156 | ||
152 | int mount(const char *fs_name, const char *mp, const char *dev) |
157 | int mount(const char *fs_name, const char *mp, const char *dev, |
- | 158 | const unsigned int flags) |
|
153 | { |
159 | { |
154 | int res; |
160 | int res; |
155 | ipcarg_t rc; |
161 | ipcarg_t rc; |
156 | aid_t req; |
162 | aid_t req; |
157 | dev_handle_t dev_handle; |
163 | dev_handle_t dev_handle; |
158 | 164 | ||
159 | res = device_get_handle(dev, &dev_handle); |
165 | res = device_get_handle(dev, &dev_handle, flags); |
160 | if (res != EOK) |
166 | if (res != EOK) |
161 | return res; |
167 | return res; |
162 | 168 | ||
163 | size_t mpa_len; |
169 | size_t mpa_len; |
164 | char *mpa = absolutize(mp, &mpa_len); |
170 | char *mpa = absolutize(mp, &mpa_len); |
165 | if (!mpa) |
171 | if (!mpa) |
166 | return ENOMEM; |
172 | return ENOMEM; |
167 | 173 | ||
168 | futex_down(&vfs_phone_futex); |
174 | futex_down(&vfs_phone_futex); |
169 | async_serialize_start(); |
175 | async_serialize_start(); |
170 | if (vfs_phone < 0) { |
- | |
171 | res = vfs_connect(); |
176 | vfs_connect(); |
172 | if (res < 0) { |
- | |
173 | async_serialize_end(); |
- | |
174 | futex_up(&vfs_phone_futex); |
- | |
175 | free(mpa); |
- | |
176 | return res; |
- | |
177 | } |
- | |
178 | } |
177 | |
179 | req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL); |
178 | req = async_send_2(vfs_phone, VFS_MOUNT, dev_handle, flags, NULL); |
180 | rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name)); |
179 | rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_len); |
181 | if (rc != EOK) { |
- | |
182 | async_wait_for(req, NULL); |
- | |
183 | async_serialize_end(); |
- | |
184 | futex_up(&vfs_phone_futex); |
- | |
185 | free(mpa); |
- | |
186 | return (int) rc; |
- | |
187 | } |
- | |
188 | /* Ask VFS whether it likes fs_name. */ |
- | |
189 | rc = async_req_0_0(vfs_phone, IPC_M_PING); |
- | |
190 | if (rc != EOK) { |
180 | if (rc != EOK) { |
191 | async_wait_for(req, NULL); |
181 | async_wait_for(req, NULL); |
192 | async_serialize_end(); |
182 | async_serialize_end(); |
193 | futex_up(&vfs_phone_futex); |
183 | futex_up(&vfs_phone_futex); |
194 | free(mpa); |
184 | free(mpa); |
195 | return (int) rc; |
185 | return (int) rc; |
196 | } |
186 | } |
- | 187 | ||
197 | rc = ipc_data_write_start(vfs_phone, (void *)mpa, mpa_len); |
188 | rc = ipc_data_write_start(vfs_phone, (void *) fs_name, strlen(fs_name)); |
198 | if (rc != EOK) { |
189 | if (rc != EOK) { |
199 | async_wait_for(req, NULL); |
190 | async_wait_for(req, NULL); |
200 | async_serialize_end(); |
191 | async_serialize_end(); |
201 | futex_up(&vfs_phone_futex); |
192 | futex_up(&vfs_phone_futex); |
202 | free(mpa); |
193 | free(mpa); |
203 | return (int) rc; |
194 | return (int) rc; |
204 | } |
195 | } |
- | 196 | ||
205 | async_wait_for(req, &rc); |
197 | async_wait_for(req, &rc); |
206 | async_serialize_end(); |
198 | async_serialize_end(); |
207 | futex_up(&vfs_phone_futex); |
199 | futex_up(&vfs_phone_futex); |
208 | free(mpa); |
200 | free(mpa); |
- | 201 | ||
209 | return (int) rc; |
202 | return (int) rc; |
210 | } |
203 | } |
211 | 204 | ||
212 | static int _open(const char *path, int lflag, int oflag, ...) |
205 | static int _open(const char *path, int lflag, int oflag, ...) |
213 | { |
206 | { |
214 | int res; |
- | |
215 | ipcarg_t rc; |
207 | ipcarg_t rc; |
216 | ipc_call_t answer; |
208 | ipc_call_t answer; |
217 | aid_t req; |
209 | aid_t req; |
218 | 210 | ||
219 | size_t pa_len; |
211 | size_t pa_len; |
220 | char *pa = absolutize(path, &pa_len); |
212 | char *pa = absolutize(path, &pa_len); |
221 | if (!pa) |
213 | if (!pa) |
222 | return ENOMEM; |
214 | return ENOMEM; |
223 | 215 | ||
224 | futex_down(&vfs_phone_futex); |
216 | futex_down(&vfs_phone_futex); |
225 | async_serialize_start(); |
217 | async_serialize_start(); |
226 | if (vfs_phone < 0) { |
- | |
227 | res = vfs_connect(); |
218 | vfs_connect(); |
228 | if (res < 0) { |
- | |
229 | async_serialize_end(); |
- | |
230 | futex_up(&vfs_phone_futex); |
- | |
231 | free(pa); |
- | |
232 | return res; |
- | |
233 | } |
- | |
234 | } |
219 | |
235 | req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer); |
220 | req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer); |
236 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
221 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
237 | if (rc != EOK) { |
222 | if (rc != EOK) { |
238 | async_wait_for(req, NULL); |
223 | async_wait_for(req, NULL); |
239 | async_serialize_end(); |
224 | async_serialize_end(); |
240 | futex_up(&vfs_phone_futex); |
225 | futex_up(&vfs_phone_futex); |
241 | free(pa); |
226 | free(pa); |
242 | return (int) rc; |
227 | return (int) rc; |
243 | } |
228 | } |
244 | async_wait_for(req, &rc); |
229 | async_wait_for(req, &rc); |
245 | async_serialize_end(); |
230 | async_serialize_end(); |
246 | futex_up(&vfs_phone_futex); |
231 | futex_up(&vfs_phone_futex); |
247 | free(pa); |
232 | free(pa); |
248 | 233 | ||
249 | if (rc != EOK) |
234 | if (rc != EOK) |
250 | return (int) rc; |
235 | return (int) rc; |
251 | return (int) IPC_GET_ARG1(answer); |
236 | return (int) IPC_GET_ARG1(answer); |
252 | } |
237 | } |
253 | 238 | ||
254 | int open(const char *path, int oflag, ...) |
239 | int open(const char *path, int oflag, ...) |
255 | { |
240 | { |
256 | return _open(path, L_FILE, oflag); |
241 | return _open(path, L_FILE, oflag); |
257 | } |
242 | } |
258 | 243 | ||
259 | int close(int fildes) |
244 | int close(int fildes) |
260 | { |
245 | { |
261 | int res; |
- | |
262 | ipcarg_t rc; |
246 | ipcarg_t rc; |
263 | 247 | ||
264 | futex_down(&vfs_phone_futex); |
248 | futex_down(&vfs_phone_futex); |
265 | async_serialize_start(); |
249 | async_serialize_start(); |
266 | if (vfs_phone < 0) { |
- | |
267 | res = vfs_connect(); |
250 | vfs_connect(); |
268 | if (res < 0) { |
- | |
269 | async_serialize_end(); |
- | |
270 | futex_up(&vfs_phone_futex); |
- | |
271 | return res; |
- | |
272 | } |
- | |
273 | } |
- | |
274 | 251 | ||
275 | rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes); |
252 | rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes); |
276 | 253 | ||
277 | async_serialize_end(); |
254 | async_serialize_end(); |
278 | futex_up(&vfs_phone_futex); |
255 | futex_up(&vfs_phone_futex); |
279 | 256 | ||
280 | return (int)rc; |
257 | return (int)rc; |
281 | } |
258 | } |
282 | 259 | ||
283 | ssize_t read(int fildes, void *buf, size_t nbyte) |
260 | ssize_t read(int fildes, void *buf, size_t nbyte) |
284 | { |
261 | { |
285 | int res; |
- | |
286 | ipcarg_t rc; |
262 | ipcarg_t rc; |
287 | ipc_call_t answer; |
263 | ipc_call_t answer; |
288 | aid_t req; |
264 | aid_t req; |
289 | 265 | ||
290 | futex_down(&vfs_phone_futex); |
266 | futex_down(&vfs_phone_futex); |
291 | async_serialize_start(); |
267 | async_serialize_start(); |
292 | if (vfs_phone < 0) { |
- | |
293 | res = vfs_connect(); |
268 | vfs_connect(); |
294 | if (res < 0) { |
- | |
295 | async_serialize_end(); |
- | |
296 | futex_up(&vfs_phone_futex); |
- | |
297 | return res; |
- | |
298 | } |
- | |
299 | } |
269 | |
300 | req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
270 | req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
301 | rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte); |
271 | rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte); |
302 | if (rc != EOK) { |
272 | if (rc != EOK) { |
303 | async_wait_for(req, NULL); |
273 | async_wait_for(req, NULL); |
304 | async_serialize_end(); |
274 | async_serialize_end(); |
305 | futex_up(&vfs_phone_futex); |
275 | futex_up(&vfs_phone_futex); |
306 | return (ssize_t) rc; |
276 | return (ssize_t) rc; |
307 | } |
277 | } |
308 | async_wait_for(req, &rc); |
278 | async_wait_for(req, &rc); |
309 | async_serialize_end(); |
279 | async_serialize_end(); |
310 | futex_up(&vfs_phone_futex); |
280 | futex_up(&vfs_phone_futex); |
311 | if (rc == EOK) |
281 | if (rc == EOK) |
312 | return (ssize_t) IPC_GET_ARG1(answer); |
282 | return (ssize_t) IPC_GET_ARG1(answer); |
313 | else |
283 | else |
314 | return rc; |
284 | return rc; |
315 | } |
285 | } |
316 | 286 | ||
317 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
287 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
318 | { |
288 | { |
319 | int res; |
- | |
320 | ipcarg_t rc; |
289 | ipcarg_t rc; |
321 | ipc_call_t answer; |
290 | ipc_call_t answer; |
322 | aid_t req; |
291 | aid_t req; |
323 | 292 | ||
324 | futex_down(&vfs_phone_futex); |
293 | futex_down(&vfs_phone_futex); |
325 | async_serialize_start(); |
294 | async_serialize_start(); |
326 | if (vfs_phone < 0) { |
- | |
327 | res = vfs_connect(); |
295 | vfs_connect(); |
328 | if (res < 0) { |
- | |
329 | async_serialize_end(); |
- | |
330 | futex_up(&vfs_phone_futex); |
- | |
331 | return res; |
- | |
332 | } |
- | |
333 | } |
296 | |
334 | req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
297 | req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
335 | rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte); |
298 | rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte); |
336 | if (rc != EOK) { |
299 | if (rc != EOK) { |
337 | async_wait_for(req, NULL); |
300 | async_wait_for(req, NULL); |
338 | async_serialize_end(); |
301 | async_serialize_end(); |
339 | futex_up(&vfs_phone_futex); |
302 | futex_up(&vfs_phone_futex); |
340 | return (ssize_t) rc; |
303 | return (ssize_t) rc; |
341 | } |
304 | } |
342 | async_wait_for(req, &rc); |
305 | async_wait_for(req, &rc); |
343 | async_serialize_end(); |
306 | async_serialize_end(); |
344 | futex_up(&vfs_phone_futex); |
307 | futex_up(&vfs_phone_futex); |
345 | if (rc == EOK) |
308 | if (rc == EOK) |
346 | return (ssize_t) IPC_GET_ARG1(answer); |
309 | return (ssize_t) IPC_GET_ARG1(answer); |
347 | else |
310 | else |
348 | return -1; |
311 | return -1; |
349 | } |
312 | } |
350 | 313 | ||
351 | off_t lseek(int fildes, off_t offset, int whence) |
314 | off_t lseek(int fildes, off_t offset, int whence) |
352 | { |
315 | { |
353 | int res; |
- | |
354 | ipcarg_t rc; |
316 | ipcarg_t rc; |
355 | 317 | ||
356 | futex_down(&vfs_phone_futex); |
318 | futex_down(&vfs_phone_futex); |
357 | async_serialize_start(); |
319 | async_serialize_start(); |
358 | if (vfs_phone < 0) { |
- | |
359 | res = vfs_connect(); |
320 | vfs_connect(); |
360 | if (res < 0) { |
- | |
361 | async_serialize_end(); |
- | |
362 | futex_up(&vfs_phone_futex); |
- | |
363 | return res; |
- | |
364 | } |
- | |
365 | } |
- | |
366 | 321 | ||
367 | off_t newoffs; |
322 | ipcarg_t newoffs; |
368 | rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
323 | rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
369 | (ipcarg_t)&newoffs); |
324 | &newoffs); |
370 | 325 | ||
371 | async_serialize_end(); |
326 | async_serialize_end(); |
372 | futex_up(&vfs_phone_futex); |
327 | futex_up(&vfs_phone_futex); |
373 | 328 | ||
374 | if (rc != EOK) |
329 | if (rc != EOK) |
375 | return (off_t) -1; |
330 | return (off_t) -1; |
376 | 331 | ||
377 | return newoffs; |
332 | return (off_t) newoffs; |
378 | } |
333 | } |
379 | 334 | ||
380 | int ftruncate(int fildes, off_t length) |
335 | int ftruncate(int fildes, off_t length) |
381 | { |
336 | { |
382 | int res; |
- | |
383 | ipcarg_t rc; |
337 | ipcarg_t rc; |
384 | 338 | ||
385 | futex_down(&vfs_phone_futex); |
339 | futex_down(&vfs_phone_futex); |
386 | async_serialize_start(); |
340 | async_serialize_start(); |
387 | if (vfs_phone < 0) { |
- | |
388 | res = vfs_connect(); |
341 | vfs_connect(); |
389 | if (res < 0) { |
- | |
390 | async_serialize_end(); |
- | |
391 | futex_up(&vfs_phone_futex); |
- | |
392 | return res; |
- | |
393 | } |
- | |
394 | } |
342 | |
395 | rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
343 | rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
396 | async_serialize_end(); |
344 | async_serialize_end(); |
397 | futex_up(&vfs_phone_futex); |
345 | futex_up(&vfs_phone_futex); |
398 | return (int) rc; |
346 | return (int) rc; |
399 | } |
347 | } |
400 | 348 | ||
401 | DIR *opendir(const char *dirname) |
349 | DIR *opendir(const char *dirname) |
402 | { |
350 | { |
403 | DIR *dirp = malloc(sizeof(DIR)); |
351 | DIR *dirp = malloc(sizeof(DIR)); |
404 | if (!dirp) |
352 | if (!dirp) |
405 | return NULL; |
353 | return NULL; |
406 | dirp->fd = _open(dirname, L_DIRECTORY, 0); |
354 | dirp->fd = _open(dirname, L_DIRECTORY, 0); |
407 | if (dirp->fd < 0) { |
355 | if (dirp->fd < 0) { |
408 | free(dirp); |
356 | free(dirp); |
409 | return NULL; |
357 | return NULL; |
410 | } |
358 | } |
411 | return dirp; |
359 | return dirp; |
412 | } |
360 | } |
413 | 361 | ||
414 | struct dirent *readdir(DIR *dirp) |
362 | struct dirent *readdir(DIR *dirp) |
415 | { |
363 | { |
416 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1); |
364 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1); |
417 | if (len <= 0) |
365 | if (len <= 0) |
418 | return NULL; |
366 | return NULL; |
419 | return &dirp->res; |
367 | return &dirp->res; |
420 | } |
368 | } |
421 | 369 | ||
422 | void rewinddir(DIR *dirp) |
370 | void rewinddir(DIR *dirp) |
423 | { |
371 | { |
424 | (void) lseek(dirp->fd, 0, SEEK_SET); |
372 | (void) lseek(dirp->fd, 0, SEEK_SET); |
425 | } |
373 | } |
426 | 374 | ||
427 | int closedir(DIR *dirp) |
375 | int closedir(DIR *dirp) |
428 | { |
376 | { |
429 | (void) close(dirp->fd); |
377 | (void) close(dirp->fd); |
430 | free(dirp); |
378 | free(dirp); |
431 | return 0; |
379 | return 0; |
432 | } |
380 | } |
433 | 381 | ||
434 | int mkdir(const char *path, mode_t mode) |
382 | int mkdir(const char *path, mode_t mode) |
435 | { |
383 | { |
436 | int res; |
- | |
437 | ipcarg_t rc; |
384 | ipcarg_t rc; |
438 | aid_t req; |
385 | aid_t req; |
439 | 386 | ||
440 | size_t pa_len; |
387 | size_t pa_len; |
441 | char *pa = absolutize(path, &pa_len); |
388 | char *pa = absolutize(path, &pa_len); |
442 | if (!pa) |
389 | if (!pa) |
443 | return ENOMEM; |
390 | return ENOMEM; |
444 | 391 | ||
445 | futex_down(&vfs_phone_futex); |
392 | futex_down(&vfs_phone_futex); |
446 | async_serialize_start(); |
393 | async_serialize_start(); |
447 | if (vfs_phone < 0) { |
- | |
448 | res = vfs_connect(); |
394 | vfs_connect(); |
449 | if (res < 0) { |
- | |
450 | async_serialize_end(); |
- | |
451 | futex_up(&vfs_phone_futex); |
- | |
452 | free(pa); |
- | |
453 | return res; |
- | |
454 | } |
- | |
455 | } |
395 | |
456 | req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL); |
396 | req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL); |
457 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
397 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
458 | if (rc != EOK) { |
398 | if (rc != EOK) { |
459 | async_wait_for(req, NULL); |
399 | async_wait_for(req, NULL); |
460 | async_serialize_end(); |
400 | async_serialize_end(); |
461 | futex_up(&vfs_phone_futex); |
401 | futex_up(&vfs_phone_futex); |
462 | free(pa); |
402 | free(pa); |
463 | return (int) rc; |
403 | return (int) rc; |
464 | } |
404 | } |
465 | async_wait_for(req, &rc); |
405 | async_wait_for(req, &rc); |
466 | async_serialize_end(); |
406 | async_serialize_end(); |
467 | futex_up(&vfs_phone_futex); |
407 | futex_up(&vfs_phone_futex); |
468 | free(pa); |
408 | free(pa); |
469 | return rc; |
409 | return rc; |
470 | } |
410 | } |
471 | 411 | ||
472 | static int _unlink(const char *path, int lflag) |
412 | static int _unlink(const char *path, int lflag) |
473 | { |
413 | { |
474 | int res; |
- | |
475 | ipcarg_t rc; |
414 | ipcarg_t rc; |
476 | aid_t req; |
415 | aid_t req; |
477 | 416 | ||
478 | size_t pa_len; |
417 | size_t pa_len; |
479 | char *pa = absolutize(path, &pa_len); |
418 | char *pa = absolutize(path, &pa_len); |
480 | if (!pa) |
419 | if (!pa) |
481 | return ENOMEM; |
420 | return ENOMEM; |
482 | 421 | ||
483 | futex_down(&vfs_phone_futex); |
422 | futex_down(&vfs_phone_futex); |
484 | async_serialize_start(); |
423 | async_serialize_start(); |
485 | if (vfs_phone < 0) { |
- | |
486 | res = vfs_connect(); |
424 | vfs_connect(); |
487 | if (res < 0) { |
- | |
488 | async_serialize_end(); |
- | |
489 | futex_up(&vfs_phone_futex); |
- | |
490 | free(pa); |
- | |
491 | return res; |
- | |
492 | } |
- | |
493 | } |
425 | |
494 | req = async_send_0(vfs_phone, VFS_UNLINK, NULL); |
426 | req = async_send_0(vfs_phone, VFS_UNLINK, NULL); |
495 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
427 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
496 | if (rc != EOK) { |
428 | if (rc != EOK) { |
497 | async_wait_for(req, NULL); |
429 | async_wait_for(req, NULL); |
498 | async_serialize_end(); |
430 | async_serialize_end(); |
499 | futex_up(&vfs_phone_futex); |
431 | futex_up(&vfs_phone_futex); |
500 | free(pa); |
432 | free(pa); |
501 | return (int) rc; |
433 | return (int) rc; |
502 | } |
434 | } |
503 | async_wait_for(req, &rc); |
435 | async_wait_for(req, &rc); |
504 | async_serialize_end(); |
436 | async_serialize_end(); |
505 | futex_up(&vfs_phone_futex); |
437 | futex_up(&vfs_phone_futex); |
506 | free(pa); |
438 | free(pa); |
507 | return rc; |
439 | return rc; |
508 | } |
440 | } |
509 | 441 | ||
510 | int unlink(const char *path) |
442 | int unlink(const char *path) |
511 | { |
443 | { |
512 | return _unlink(path, L_NONE); |
444 | return _unlink(path, L_NONE); |
513 | } |
445 | } |
514 | 446 | ||
515 | int rmdir(const char *path) |
447 | int rmdir(const char *path) |
516 | { |
448 | { |
517 | return _unlink(path, L_DIRECTORY); |
449 | return _unlink(path, L_DIRECTORY); |
518 | } |
450 | } |
519 | 451 | ||
520 | int rename(const char *old, const char *new) |
452 | int rename(const char *old, const char *new) |
521 | { |
453 | { |
522 | int res; |
- | |
523 | ipcarg_t rc; |
454 | ipcarg_t rc; |
524 | aid_t req; |
455 | aid_t req; |
525 | 456 | ||
526 | size_t olda_len; |
457 | size_t olda_len; |
527 | char *olda = absolutize(old, &olda_len); |
458 | char *olda = absolutize(old, &olda_len); |
528 | if (!olda) |
459 | if (!olda) |
529 | return ENOMEM; |
460 | return ENOMEM; |
530 | 461 | ||
531 | size_t newa_len; |
462 | size_t newa_len; |
532 | char *newa = absolutize(new, &newa_len); |
463 | char *newa = absolutize(new, &newa_len); |
533 | if (!newa) { |
464 | if (!newa) { |
534 | free(olda); |
465 | free(olda); |
535 | return ENOMEM; |
466 | return ENOMEM; |
536 | } |
467 | } |
537 | 468 | ||
538 | futex_down(&vfs_phone_futex); |
469 | futex_down(&vfs_phone_futex); |
539 | async_serialize_start(); |
470 | async_serialize_start(); |
540 | if (vfs_phone < 0) { |
- | |
541 | res = vfs_connect(); |
471 | vfs_connect(); |
542 | if (res < 0) { |
- | |
543 | async_serialize_end(); |
- | |
544 | futex_up(&vfs_phone_futex); |
- | |
545 | free(olda); |
- | |
546 | free(newa); |
- | |
547 | return res; |
- | |
548 | } |
- | |
549 | } |
472 | |
550 | req = async_send_0(vfs_phone, VFS_RENAME, NULL); |
473 | req = async_send_0(vfs_phone, VFS_RENAME, NULL); |
551 | rc = ipc_data_write_start(vfs_phone, olda, olda_len); |
474 | rc = ipc_data_write_start(vfs_phone, olda, olda_len); |
552 | if (rc != EOK) { |
475 | if (rc != EOK) { |
553 | async_wait_for(req, NULL); |
476 | async_wait_for(req, NULL); |
554 | async_serialize_end(); |
477 | async_serialize_end(); |
555 | futex_up(&vfs_phone_futex); |
478 | futex_up(&vfs_phone_futex); |
556 | free(olda); |
479 | free(olda); |
557 | free(newa); |
480 | free(newa); |
558 | return (int) rc; |
481 | return (int) rc; |
559 | } |
482 | } |
560 | rc = ipc_data_write_start(vfs_phone, newa, newa_len); |
483 | rc = ipc_data_write_start(vfs_phone, newa, newa_len); |
561 | if (rc != EOK) { |
484 | if (rc != EOK) { |
562 | async_wait_for(req, NULL); |
485 | async_wait_for(req, NULL); |
563 | async_serialize_end(); |
486 | async_serialize_end(); |
564 | futex_up(&vfs_phone_futex); |
487 | futex_up(&vfs_phone_futex); |
565 | free(olda); |
488 | free(olda); |
566 | free(newa); |
489 | free(newa); |
567 | return (int) rc; |
490 | return (int) rc; |
568 | } |
491 | } |
569 | async_wait_for(req, &rc); |
492 | async_wait_for(req, &rc); |
570 | async_serialize_end(); |
493 | async_serialize_end(); |
571 | futex_up(&vfs_phone_futex); |
494 | futex_up(&vfs_phone_futex); |
572 | free(olda); |
495 | free(olda); |
573 | free(newa); |
496 | free(newa); |
574 | return rc; |
497 | return rc; |
575 | } |
498 | } |
576 | 499 | ||
577 | int chdir(const char *path) |
500 | int chdir(const char *path) |
578 | { |
501 | { |
579 | size_t pa_len; |
502 | size_t pa_len; |
580 | char *pa = absolutize(path, &pa_len); |
503 | char *pa = absolutize(path, &pa_len); |
581 | if (!pa) |
504 | if (!pa) |
582 | return ENOMEM; |
505 | return ENOMEM; |
583 | 506 | ||
584 | DIR *d = opendir(pa); |
507 | DIR *d = opendir(pa); |
585 | if (!d) { |
508 | if (!d) { |
586 | free(pa); |
509 | free(pa); |
587 | return ENOENT; |
510 | return ENOENT; |
588 | } |
511 | } |
589 | 512 | ||
590 | futex_down(&cwd_futex); |
513 | futex_down(&cwd_futex); |
591 | if (cwd_dir) { |
514 | if (cwd_dir) { |
592 | closedir(cwd_dir); |
515 | closedir(cwd_dir); |
593 | cwd_dir = NULL; |
516 | cwd_dir = NULL; |
594 | free(cwd_path); |
517 | free(cwd_path); |
595 | cwd_path = NULL; |
518 | cwd_path = NULL; |
596 | cwd_len = 0; |
519 | cwd_len = 0; |
597 | } |
520 | } |
598 | cwd_dir = d; |
521 | cwd_dir = d; |
599 | cwd_path = pa; |
522 | cwd_path = pa; |
600 | cwd_len = pa_len; |
523 | cwd_len = pa_len; |
601 | futex_up(&cwd_futex); |
524 | futex_up(&cwd_futex); |
- | 525 | return EOK; |
|
602 | } |
526 | } |
603 | 527 | ||
604 | char *getcwd(char *buf, size_t size) |
528 | char *getcwd(char *buf, size_t size) |
605 | { |
529 | { |
606 | if (!size) |
530 | if (!size) |
607 | return NULL; |
531 | return NULL; |
608 | futex_down(&cwd_futex); |
532 | futex_down(&cwd_futex); |
609 | if (size < cwd_len + 1) { |
533 | if (size < cwd_len + 1) { |
610 | futex_up(&cwd_futex); |
534 | futex_up(&cwd_futex); |
611 | return NULL; |
535 | return NULL; |
612 | } |
536 | } |
613 | strcpy(buf, cwd_path); |
537 | strcpy(buf, cwd_path); |
614 | futex_up(&cwd_futex); |
538 | futex_up(&cwd_futex); |
615 | return buf; |
539 | return buf; |
616 | } |
540 | } |
617 | 541 | ||
618 | /** @} |
542 | /** @} |
619 | */ |
543 | */ |
620 | 544 |