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