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