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