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