1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <vfs/canonify.h>
|
---|
36 | #include <vfs/vfs.h>
|
---|
37 | #include <vfs/vfs_sess.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <unistd.h>
|
---|
41 | #include <dirent.h>
|
---|
42 | #include <fcntl.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <sys/stat.h>
|
---|
45 | #include <sys/types.h>
|
---|
46 | #include <ipc/services.h>
|
---|
47 | #include <ns.h>
|
---|
48 | #include <async.h>
|
---|
49 | #include <fibril_synch.h>
|
---|
50 | #include <errno.h>
|
---|
51 | #include <assert.h>
|
---|
52 | #include <str.h>
|
---|
53 | #include <loc.h>
|
---|
54 | #include <ipc/vfs.h>
|
---|
55 | #include <ipc/loc.h>
|
---|
56 |
|
---|
57 | static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
|
---|
58 | static async_sess_t *vfs_sess = NULL;
|
---|
59 |
|
---|
60 | static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
|
---|
61 |
|
---|
62 | static int cwd_fd = -1;
|
---|
63 | static char *cwd_path = NULL;
|
---|
64 | static size_t cwd_size = 0;
|
---|
65 |
|
---|
66 | /** Start an async exchange on the VFS session.
|
---|
67 | *
|
---|
68 | * @return New exchange.
|
---|
69 | *
|
---|
70 | */
|
---|
71 | async_exch_t *vfs_exchange_begin(void)
|
---|
72 | {
|
---|
73 | fibril_mutex_lock(&vfs_mutex);
|
---|
74 |
|
---|
75 | while (vfs_sess == NULL)
|
---|
76 | vfs_sess = service_connect_blocking(EXCHANGE_PARALLEL, SERVICE_VFS,
|
---|
77 | 0, 0);
|
---|
78 |
|
---|
79 | fibril_mutex_unlock(&vfs_mutex);
|
---|
80 |
|
---|
81 | return async_exchange_begin(vfs_sess);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Finish an async exchange on the VFS session.
|
---|
85 | *
|
---|
86 | * @param exch Exchange to be finished.
|
---|
87 | *
|
---|
88 | */
|
---|
89 | void vfs_exchange_end(async_exch_t *exch)
|
---|
90 | {
|
---|
91 | async_exchange_end(exch);
|
---|
92 | }
|
---|
93 |
|
---|
94 | char *absolutize(const char *path, size_t *retlen)
|
---|
95 | {
|
---|
96 | char *ncwd_path;
|
---|
97 | char *ncwd_path_nc;
|
---|
98 |
|
---|
99 | fibril_mutex_lock(&cwd_mutex);
|
---|
100 | size_t size = str_size(path);
|
---|
101 | if (*path != '/') {
|
---|
102 | if (!cwd_path) {
|
---|
103 | fibril_mutex_unlock(&cwd_mutex);
|
---|
104 | return NULL;
|
---|
105 | }
|
---|
106 | ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
|
---|
107 | if (!ncwd_path_nc) {
|
---|
108 | fibril_mutex_unlock(&cwd_mutex);
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 | str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
|
---|
112 | ncwd_path_nc[cwd_size] = '/';
|
---|
113 | ncwd_path_nc[cwd_size + 1] = '\0';
|
---|
114 | } else {
|
---|
115 | ncwd_path_nc = malloc(size + 1);
|
---|
116 | if (!ncwd_path_nc) {
|
---|
117 | fibril_mutex_unlock(&cwd_mutex);
|
---|
118 | return NULL;
|
---|
119 | }
|
---|
120 | ncwd_path_nc[0] = '\0';
|
---|
121 | }
|
---|
122 | str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
|
---|
123 | ncwd_path = canonify(ncwd_path_nc, retlen);
|
---|
124 | if (!ncwd_path) {
|
---|
125 | fibril_mutex_unlock(&cwd_mutex);
|
---|
126 | free(ncwd_path_nc);
|
---|
127 | return NULL;
|
---|
128 | }
|
---|
129 | /*
|
---|
130 | * We need to clone ncwd_path because canonify() works in-place and thus
|
---|
131 | * the address in ncwd_path need not be the same as ncwd_path_nc, even
|
---|
132 | * though they both point into the same dynamically allocated buffer.
|
---|
133 | */
|
---|
134 | ncwd_path = str_dup(ncwd_path);
|
---|
135 | free(ncwd_path_nc);
|
---|
136 | if (!ncwd_path) {
|
---|
137 | fibril_mutex_unlock(&cwd_mutex);
|
---|
138 | return NULL;
|
---|
139 | }
|
---|
140 | fibril_mutex_unlock(&cwd_mutex);
|
---|
141 | return ncwd_path;
|
---|
142 | }
|
---|
143 |
|
---|
144 | int mount(const char *fs_name, const char *mp, const char *fqsn,
|
---|
145 | const char *opts, unsigned int flags, unsigned int instance)
|
---|
146 | {
|
---|
147 | int null_id = -1;
|
---|
148 | char null[LOC_NAME_MAXLEN];
|
---|
149 |
|
---|
150 | if (str_cmp(fqsn, "") == 0) {
|
---|
151 | /* No device specified, create a fresh
|
---|
152 | null/%d device instead */
|
---|
153 | null_id = loc_null_create();
|
---|
154 |
|
---|
155 | if (null_id == -1)
|
---|
156 | return ENOMEM;
|
---|
157 |
|
---|
158 | snprintf(null, LOC_NAME_MAXLEN, "null/%d", null_id);
|
---|
159 | fqsn = null;
|
---|
160 | }
|
---|
161 |
|
---|
162 | service_id_t service_id;
|
---|
163 | int res = loc_service_get_id(fqsn, &service_id, flags);
|
---|
164 | if (res != EOK) {
|
---|
165 | if (null_id != -1)
|
---|
166 | loc_null_destroy(null_id);
|
---|
167 |
|
---|
168 | return res;
|
---|
169 | }
|
---|
170 |
|
---|
171 | size_t mpa_size;
|
---|
172 | char *mpa = absolutize(mp, &mpa_size);
|
---|
173 | if (!mpa) {
|
---|
174 | if (null_id != -1)
|
---|
175 | loc_null_destroy(null_id);
|
---|
176 |
|
---|
177 | return ENOMEM;
|
---|
178 | }
|
---|
179 |
|
---|
180 | async_exch_t *exch = vfs_exchange_begin();
|
---|
181 |
|
---|
182 | sysarg_t rc_orig;
|
---|
183 | aid_t req = async_send_3(exch, VFS_IN_MOUNT, service_id, flags,
|
---|
184 | instance, NULL);
|
---|
185 | sysarg_t rc = async_data_write_start(exch, (void *) mpa, mpa_size);
|
---|
186 | if (rc != EOK) {
|
---|
187 | vfs_exchange_end(exch);
|
---|
188 | free(mpa);
|
---|
189 | async_wait_for(req, &rc_orig);
|
---|
190 |
|
---|
191 | if (null_id != -1)
|
---|
192 | loc_null_destroy(null_id);
|
---|
193 |
|
---|
194 | if (rc_orig == EOK)
|
---|
195 | return (int) rc;
|
---|
196 | else
|
---|
197 | return (int) rc_orig;
|
---|
198 | }
|
---|
199 |
|
---|
200 | rc = async_data_write_start(exch, (void *) opts, str_size(opts));
|
---|
201 | if (rc != EOK) {
|
---|
202 | vfs_exchange_end(exch);
|
---|
203 | free(mpa);
|
---|
204 | async_wait_for(req, &rc_orig);
|
---|
205 |
|
---|
206 | if (null_id != -1)
|
---|
207 | loc_null_destroy(null_id);
|
---|
208 |
|
---|
209 | if (rc_orig == EOK)
|
---|
210 | return (int) rc;
|
---|
211 | else
|
---|
212 | return (int) rc_orig;
|
---|
213 | }
|
---|
214 |
|
---|
215 | rc = async_data_write_start(exch, (void *) fs_name, str_size(fs_name));
|
---|
216 | if (rc != EOK) {
|
---|
217 | vfs_exchange_end(exch);
|
---|
218 | free(mpa);
|
---|
219 | async_wait_for(req, &rc_orig);
|
---|
220 |
|
---|
221 | if (null_id != -1)
|
---|
222 | loc_null_destroy(null_id);
|
---|
223 |
|
---|
224 | if (rc_orig == EOK)
|
---|
225 | return (int) rc;
|
---|
226 | else
|
---|
227 | return (int) rc_orig;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Ask VFS whether it likes fs_name. */
|
---|
231 | rc = async_req_0_0(exch, VFS_IN_PING);
|
---|
232 | if (rc != EOK) {
|
---|
233 | vfs_exchange_end(exch);
|
---|
234 | free(mpa);
|
---|
235 | async_wait_for(req, &rc_orig);
|
---|
236 |
|
---|
237 | if (null_id != -1)
|
---|
238 | loc_null_destroy(null_id);
|
---|
239 |
|
---|
240 | if (rc_orig == EOK)
|
---|
241 | return (int) rc;
|
---|
242 | else
|
---|
243 | return (int) rc_orig;
|
---|
244 | }
|
---|
245 |
|
---|
246 | vfs_exchange_end(exch);
|
---|
247 | free(mpa);
|
---|
248 | async_wait_for(req, &rc);
|
---|
249 |
|
---|
250 | if ((rc != EOK) && (null_id != -1))
|
---|
251 | loc_null_destroy(null_id);
|
---|
252 |
|
---|
253 | return (int) rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 | int unmount(const char *mp)
|
---|
257 | {
|
---|
258 | sysarg_t rc;
|
---|
259 | sysarg_t rc_orig;
|
---|
260 | aid_t req;
|
---|
261 | size_t mpa_size;
|
---|
262 | char *mpa;
|
---|
263 |
|
---|
264 | mpa = absolutize(mp, &mpa_size);
|
---|
265 | if (!mpa)
|
---|
266 | return ENOMEM;
|
---|
267 |
|
---|
268 | async_exch_t *exch = vfs_exchange_begin();
|
---|
269 |
|
---|
270 | req = async_send_0(exch, VFS_IN_UNMOUNT, NULL);
|
---|
271 | rc = async_data_write_start(exch, (void *) mpa, mpa_size);
|
---|
272 | if (rc != EOK) {
|
---|
273 | vfs_exchange_end(exch);
|
---|
274 | free(mpa);
|
---|
275 | async_wait_for(req, &rc_orig);
|
---|
276 | if (rc_orig == EOK)
|
---|
277 | return (int) rc;
|
---|
278 | else
|
---|
279 | return (int) rc_orig;
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | vfs_exchange_end(exch);
|
---|
284 | free(mpa);
|
---|
285 | async_wait_for(req, &rc);
|
---|
286 |
|
---|
287 | return (int) rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 | static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag)
|
---|
291 | {
|
---|
292 | async_exch_t *exch = vfs_exchange_begin();
|
---|
293 |
|
---|
294 | ipc_call_t answer;
|
---|
295 | aid_t req = async_send_3(exch, VFS_IN_OPEN, lflag, oflag, 0, &answer);
|
---|
296 | sysarg_t rc = async_data_write_start(exch, abs, abs_size);
|
---|
297 |
|
---|
298 | if (rc != EOK) {
|
---|
299 | vfs_exchange_end(exch);
|
---|
300 |
|
---|
301 | sysarg_t rc_orig;
|
---|
302 | async_wait_for(req, &rc_orig);
|
---|
303 |
|
---|
304 | if (rc_orig == EOK)
|
---|
305 | return (int) rc;
|
---|
306 | else
|
---|
307 | return (int) rc_orig;
|
---|
308 | }
|
---|
309 |
|
---|
310 | vfs_exchange_end(exch);
|
---|
311 | async_wait_for(req, &rc);
|
---|
312 |
|
---|
313 | if (rc != EOK)
|
---|
314 | return (int) rc;
|
---|
315 |
|
---|
316 | return (int) IPC_GET_ARG1(answer);
|
---|
317 | }
|
---|
318 |
|
---|
319 | int open(const char *path, int oflag, ...)
|
---|
320 | {
|
---|
321 | size_t abs_size;
|
---|
322 | char *abs = absolutize(path, &abs_size);
|
---|
323 | if (!abs)
|
---|
324 | return ENOMEM;
|
---|
325 |
|
---|
326 | int ret = open_internal(abs, abs_size, L_FILE, oflag);
|
---|
327 | free(abs);
|
---|
328 |
|
---|
329 | return ret;
|
---|
330 | }
|
---|
331 |
|
---|
332 | int close(int fildes)
|
---|
333 | {
|
---|
334 | sysarg_t rc;
|
---|
335 |
|
---|
336 | async_exch_t *exch = vfs_exchange_begin();
|
---|
337 | rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes);
|
---|
338 | vfs_exchange_end(exch);
|
---|
339 |
|
---|
340 | return (int) rc;
|
---|
341 | }
|
---|
342 |
|
---|
343 | ssize_t read(int fildes, void *buf, size_t nbyte)
|
---|
344 | {
|
---|
345 | sysarg_t rc;
|
---|
346 | ipc_call_t answer;
|
---|
347 | aid_t req;
|
---|
348 |
|
---|
349 | async_exch_t *exch = vfs_exchange_begin();
|
---|
350 |
|
---|
351 | req = async_send_1(exch, VFS_IN_READ, fildes, &answer);
|
---|
352 | rc = async_data_read_start(exch, (void *)buf, nbyte);
|
---|
353 | if (rc != EOK) {
|
---|
354 | vfs_exchange_end(exch);
|
---|
355 |
|
---|
356 | sysarg_t rc_orig;
|
---|
357 | async_wait_for(req, &rc_orig);
|
---|
358 |
|
---|
359 | if (rc_orig == EOK)
|
---|
360 | return (ssize_t) rc;
|
---|
361 | else
|
---|
362 | return (ssize_t) rc_orig;
|
---|
363 | }
|
---|
364 | vfs_exchange_end(exch);
|
---|
365 | async_wait_for(req, &rc);
|
---|
366 | if (rc == EOK)
|
---|
367 | return (ssize_t) IPC_GET_ARG1(answer);
|
---|
368 | else
|
---|
369 | return rc;
|
---|
370 | }
|
---|
371 |
|
---|
372 | ssize_t write(int fildes, const void *buf, size_t nbyte)
|
---|
373 | {
|
---|
374 | sysarg_t rc;
|
---|
375 | ipc_call_t answer;
|
---|
376 | aid_t req;
|
---|
377 |
|
---|
378 | async_exch_t *exch = vfs_exchange_begin();
|
---|
379 |
|
---|
380 | req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer);
|
---|
381 | rc = async_data_write_start(exch, (void *)buf, nbyte);
|
---|
382 | if (rc != EOK) {
|
---|
383 | vfs_exchange_end(exch);
|
---|
384 |
|
---|
385 | sysarg_t rc_orig;
|
---|
386 | async_wait_for(req, &rc_orig);
|
---|
387 |
|
---|
388 | if (rc_orig == EOK)
|
---|
389 | return (ssize_t) rc;
|
---|
390 | else
|
---|
391 | return (ssize_t) rc_orig;
|
---|
392 | }
|
---|
393 | vfs_exchange_end(exch);
|
---|
394 | async_wait_for(req, &rc);
|
---|
395 | if (rc == EOK)
|
---|
396 | return (ssize_t) IPC_GET_ARG1(answer);
|
---|
397 | else
|
---|
398 | return -1;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /** Read entire buffer.
|
---|
402 | *
|
---|
403 | * In face of short reads this function continues reading until either
|
---|
404 | * the entire buffer is read or no more data is available (at end of file).
|
---|
405 | *
|
---|
406 | * @param fildes File descriptor
|
---|
407 | * @param buf Buffer, @a nbytes bytes long
|
---|
408 | * @param nbytes Number of bytes to read
|
---|
409 | *
|
---|
410 | * @return On success, positive number of bytes read.
|
---|
411 | * On failure, negative error code from read().
|
---|
412 | */
|
---|
413 | ssize_t read_all(int fildes, void *buf, size_t nbyte)
|
---|
414 | {
|
---|
415 | ssize_t cnt = 0;
|
---|
416 | size_t nread = 0;
|
---|
417 | uint8_t *bp = (uint8_t *) buf;
|
---|
418 |
|
---|
419 | do {
|
---|
420 | bp += cnt;
|
---|
421 | nread += cnt;
|
---|
422 | cnt = read(fildes, bp, nbyte - nread);
|
---|
423 | } while (cnt > 0 && (nbyte - nread - cnt) > 0);
|
---|
424 |
|
---|
425 | if (cnt < 0)
|
---|
426 | return cnt;
|
---|
427 |
|
---|
428 | return nread + cnt;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /** Write entire buffer.
|
---|
432 | *
|
---|
433 | * This function fails if it cannot write exactly @a len bytes to the file.
|
---|
434 | *
|
---|
435 | * @param fildes File descriptor
|
---|
436 | * @param buf Data, @a nbytes bytes long
|
---|
437 | * @param nbytes Number of bytes to write
|
---|
438 | *
|
---|
439 | * @return EOK on error, return value from write() if writing
|
---|
440 | * failed.
|
---|
441 | */
|
---|
442 | ssize_t write_all(int fildes, const void *buf, size_t nbyte)
|
---|
443 | {
|
---|
444 | ssize_t cnt = 0;
|
---|
445 | ssize_t nwritten = 0;
|
---|
446 | const uint8_t *bp = (uint8_t *) buf;
|
---|
447 |
|
---|
448 | do {
|
---|
449 | bp += cnt;
|
---|
450 | nwritten += cnt;
|
---|
451 | cnt = write(fildes, bp, nbyte - nwritten);
|
---|
452 | } while (cnt > 0 && ((ssize_t )nbyte - nwritten - cnt) > 0);
|
---|
453 |
|
---|
454 | if (cnt < 0)
|
---|
455 | return cnt;
|
---|
456 |
|
---|
457 | if ((ssize_t)nbyte - nwritten - cnt > 0)
|
---|
458 | return EIO;
|
---|
459 |
|
---|
460 | return nbyte;
|
---|
461 | }
|
---|
462 |
|
---|
463 | int fsync(int fildes)
|
---|
464 | {
|
---|
465 | async_exch_t *exch = vfs_exchange_begin();
|
---|
466 | sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes);
|
---|
467 | vfs_exchange_end(exch);
|
---|
468 |
|
---|
469 | return (int) rc;
|
---|
470 | }
|
---|
471 |
|
---|
472 | off64_t lseek(int fildes, off64_t offset, int whence)
|
---|
473 | {
|
---|
474 | async_exch_t *exch = vfs_exchange_begin();
|
---|
475 |
|
---|
476 | sysarg_t newoff_lo;
|
---|
477 | sysarg_t newoff_hi;
|
---|
478 | sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes,
|
---|
479 | LOWER32(offset), UPPER32(offset), whence,
|
---|
480 | &newoff_lo, &newoff_hi);
|
---|
481 |
|
---|
482 | vfs_exchange_end(exch);
|
---|
483 |
|
---|
484 | if (rc != EOK)
|
---|
485 | return (off64_t) -1;
|
---|
486 |
|
---|
487 | return (off64_t) MERGE_LOUP32(newoff_lo, newoff_hi);
|
---|
488 | }
|
---|
489 |
|
---|
490 | int ftruncate(int fildes, aoff64_t length)
|
---|
491 | {
|
---|
492 | sysarg_t rc;
|
---|
493 |
|
---|
494 | async_exch_t *exch = vfs_exchange_begin();
|
---|
495 | rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes,
|
---|
496 | LOWER32(length), UPPER32(length));
|
---|
497 | vfs_exchange_end(exch);
|
---|
498 |
|
---|
499 | return (int) rc;
|
---|
500 | }
|
---|
501 |
|
---|
502 | int fstat(int fildes, struct stat *stat)
|
---|
503 | {
|
---|
504 | sysarg_t rc;
|
---|
505 | aid_t req;
|
---|
506 |
|
---|
507 | async_exch_t *exch = vfs_exchange_begin();
|
---|
508 |
|
---|
509 | req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL);
|
---|
510 | rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
|
---|
511 | if (rc != EOK) {
|
---|
512 | vfs_exchange_end(exch);
|
---|
513 |
|
---|
514 | sysarg_t rc_orig;
|
---|
515 | async_wait_for(req, &rc_orig);
|
---|
516 |
|
---|
517 | if (rc_orig == EOK)
|
---|
518 | return (ssize_t) rc;
|
---|
519 | else
|
---|
520 | return (ssize_t) rc_orig;
|
---|
521 | }
|
---|
522 | vfs_exchange_end(exch);
|
---|
523 | async_wait_for(req, &rc);
|
---|
524 |
|
---|
525 | return rc;
|
---|
526 | }
|
---|
527 |
|
---|
528 | int stat(const char *path, struct stat *stat)
|
---|
529 | {
|
---|
530 | sysarg_t rc;
|
---|
531 | sysarg_t rc_orig;
|
---|
532 | aid_t req;
|
---|
533 |
|
---|
534 | size_t pa_size;
|
---|
535 | char *pa = absolutize(path, &pa_size);
|
---|
536 | if (!pa)
|
---|
537 | return ENOMEM;
|
---|
538 |
|
---|
539 | async_exch_t *exch = vfs_exchange_begin();
|
---|
540 |
|
---|
541 | req = async_send_0(exch, VFS_IN_STAT, NULL);
|
---|
542 | rc = async_data_write_start(exch, pa, pa_size);
|
---|
543 | if (rc != EOK) {
|
---|
544 | vfs_exchange_end(exch);
|
---|
545 | free(pa);
|
---|
546 | async_wait_for(req, &rc_orig);
|
---|
547 | if (rc_orig == EOK)
|
---|
548 | return (int) rc;
|
---|
549 | else
|
---|
550 | return (int) rc_orig;
|
---|
551 | }
|
---|
552 | rc = async_data_read_start(exch, stat, sizeof(struct stat));
|
---|
553 | if (rc != EOK) {
|
---|
554 | vfs_exchange_end(exch);
|
---|
555 | free(pa);
|
---|
556 | async_wait_for(req, &rc_orig);
|
---|
557 | if (rc_orig == EOK)
|
---|
558 | return (int) rc;
|
---|
559 | else
|
---|
560 | return (int) rc_orig;
|
---|
561 | }
|
---|
562 | vfs_exchange_end(exch);
|
---|
563 | free(pa);
|
---|
564 | async_wait_for(req, &rc);
|
---|
565 | return rc;
|
---|
566 | }
|
---|
567 |
|
---|
568 | DIR *opendir(const char *dirname)
|
---|
569 | {
|
---|
570 | DIR *dirp = malloc(sizeof(DIR));
|
---|
571 | if (!dirp)
|
---|
572 | return NULL;
|
---|
573 |
|
---|
574 | size_t abs_size;
|
---|
575 | char *abs = absolutize(dirname, &abs_size);
|
---|
576 | if (!abs) {
|
---|
577 | free(dirp);
|
---|
578 | return NULL;
|
---|
579 | }
|
---|
580 |
|
---|
581 | int ret = open_internal(abs, abs_size, L_DIRECTORY, 0);
|
---|
582 | free(abs);
|
---|
583 |
|
---|
584 | if (ret < 0) {
|
---|
585 | free(dirp);
|
---|
586 | return NULL;
|
---|
587 | }
|
---|
588 |
|
---|
589 | dirp->fd = ret;
|
---|
590 | return dirp;
|
---|
591 | }
|
---|
592 |
|
---|
593 | struct dirent *readdir(DIR *dirp)
|
---|
594 | {
|
---|
595 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
|
---|
596 | if (len <= 0)
|
---|
597 | return NULL;
|
---|
598 | return &dirp->res;
|
---|
599 | }
|
---|
600 |
|
---|
601 | void rewinddir(DIR *dirp)
|
---|
602 | {
|
---|
603 | (void) lseek(dirp->fd, 0, SEEK_SET);
|
---|
604 | }
|
---|
605 |
|
---|
606 | int closedir(DIR *dirp)
|
---|
607 | {
|
---|
608 | (void) close(dirp->fd);
|
---|
609 | free(dirp);
|
---|
610 | return 0;
|
---|
611 | }
|
---|
612 |
|
---|
613 | int mkdir(const char *path, mode_t mode)
|
---|
614 | {
|
---|
615 | sysarg_t rc;
|
---|
616 | aid_t req;
|
---|
617 |
|
---|
618 | size_t pa_size;
|
---|
619 | char *pa = absolutize(path, &pa_size);
|
---|
620 | if (!pa)
|
---|
621 | return ENOMEM;
|
---|
622 |
|
---|
623 | async_exch_t *exch = vfs_exchange_begin();
|
---|
624 |
|
---|
625 | req = async_send_1(exch, VFS_IN_MKDIR, mode, NULL);
|
---|
626 | rc = async_data_write_start(exch, pa, pa_size);
|
---|
627 | if (rc != EOK) {
|
---|
628 | vfs_exchange_end(exch);
|
---|
629 | free(pa);
|
---|
630 |
|
---|
631 | sysarg_t rc_orig;
|
---|
632 | async_wait_for(req, &rc_orig);
|
---|
633 |
|
---|
634 | if (rc_orig == EOK)
|
---|
635 | return (int) rc;
|
---|
636 | else
|
---|
637 | return (int) rc_orig;
|
---|
638 | }
|
---|
639 | vfs_exchange_end(exch);
|
---|
640 | free(pa);
|
---|
641 | async_wait_for(req, &rc);
|
---|
642 | return rc;
|
---|
643 | }
|
---|
644 |
|
---|
645 | static int _unlink(const char *path, int lflag)
|
---|
646 | {
|
---|
647 | sysarg_t rc;
|
---|
648 | aid_t req;
|
---|
649 |
|
---|
650 | size_t pa_size;
|
---|
651 | char *pa = absolutize(path, &pa_size);
|
---|
652 | if (!pa)
|
---|
653 | return ENOMEM;
|
---|
654 |
|
---|
655 | async_exch_t *exch = vfs_exchange_begin();
|
---|
656 |
|
---|
657 | req = async_send_1(exch, VFS_IN_UNLINK, lflag, NULL);
|
---|
658 | rc = async_data_write_start(exch, pa, pa_size);
|
---|
659 | if (rc != EOK) {
|
---|
660 | vfs_exchange_end(exch);
|
---|
661 | free(pa);
|
---|
662 |
|
---|
663 | sysarg_t rc_orig;
|
---|
664 | async_wait_for(req, &rc_orig);
|
---|
665 |
|
---|
666 | if (rc_orig == EOK)
|
---|
667 | return (int) rc;
|
---|
668 | else
|
---|
669 | return (int) rc_orig;
|
---|
670 | }
|
---|
671 | vfs_exchange_end(exch);
|
---|
672 | free(pa);
|
---|
673 | async_wait_for(req, &rc);
|
---|
674 | return rc;
|
---|
675 | }
|
---|
676 |
|
---|
677 | int unlink(const char *path)
|
---|
678 | {
|
---|
679 | return _unlink(path, L_NONE);
|
---|
680 | }
|
---|
681 |
|
---|
682 | int rmdir(const char *path)
|
---|
683 | {
|
---|
684 | return _unlink(path, L_DIRECTORY);
|
---|
685 | }
|
---|
686 |
|
---|
687 | int rename(const char *old, const char *new)
|
---|
688 | {
|
---|
689 | sysarg_t rc;
|
---|
690 | sysarg_t rc_orig;
|
---|
691 | aid_t req;
|
---|
692 |
|
---|
693 | size_t olda_size;
|
---|
694 | char *olda = absolutize(old, &olda_size);
|
---|
695 | if (!olda)
|
---|
696 | return ENOMEM;
|
---|
697 |
|
---|
698 | size_t newa_size;
|
---|
699 | char *newa = absolutize(new, &newa_size);
|
---|
700 | if (!newa) {
|
---|
701 | free(olda);
|
---|
702 | return ENOMEM;
|
---|
703 | }
|
---|
704 |
|
---|
705 | async_exch_t *exch = vfs_exchange_begin();
|
---|
706 |
|
---|
707 | req = async_send_0(exch, VFS_IN_RENAME, NULL);
|
---|
708 | rc = async_data_write_start(exch, olda, olda_size);
|
---|
709 | if (rc != EOK) {
|
---|
710 | vfs_exchange_end(exch);
|
---|
711 | free(olda);
|
---|
712 | free(newa);
|
---|
713 | async_wait_for(req, &rc_orig);
|
---|
714 | if (rc_orig == EOK)
|
---|
715 | return (int) rc;
|
---|
716 | else
|
---|
717 | return (int) rc_orig;
|
---|
718 | }
|
---|
719 | rc = async_data_write_start(exch, newa, newa_size);
|
---|
720 | if (rc != EOK) {
|
---|
721 | vfs_exchange_end(exch);
|
---|
722 | free(olda);
|
---|
723 | free(newa);
|
---|
724 | async_wait_for(req, &rc_orig);
|
---|
725 | if (rc_orig == EOK)
|
---|
726 | return (int) rc;
|
---|
727 | else
|
---|
728 | return (int) rc_orig;
|
---|
729 | }
|
---|
730 | vfs_exchange_end(exch);
|
---|
731 | free(olda);
|
---|
732 | free(newa);
|
---|
733 | async_wait_for(req, &rc);
|
---|
734 | return rc;
|
---|
735 | }
|
---|
736 |
|
---|
737 | int chdir(const char *path)
|
---|
738 | {
|
---|
739 | size_t abs_size;
|
---|
740 | char *abs = absolutize(path, &abs_size);
|
---|
741 | if (!abs)
|
---|
742 | return ENOMEM;
|
---|
743 |
|
---|
744 | int fd = open_internal(abs, abs_size, L_DIRECTORY, O_DESC);
|
---|
745 |
|
---|
746 | if (fd < 0) {
|
---|
747 | free(abs);
|
---|
748 | return ENOENT;
|
---|
749 | }
|
---|
750 |
|
---|
751 | fibril_mutex_lock(&cwd_mutex);
|
---|
752 |
|
---|
753 | if (cwd_fd >= 0)
|
---|
754 | close(cwd_fd);
|
---|
755 |
|
---|
756 |
|
---|
757 | if (cwd_path)
|
---|
758 | free(cwd_path);
|
---|
759 |
|
---|
760 | cwd_fd = fd;
|
---|
761 | cwd_path = abs;
|
---|
762 | cwd_size = abs_size;
|
---|
763 |
|
---|
764 | fibril_mutex_unlock(&cwd_mutex);
|
---|
765 | return EOK;
|
---|
766 | }
|
---|
767 |
|
---|
768 | char *getcwd(char *buf, size_t size)
|
---|
769 | {
|
---|
770 | if (size == 0)
|
---|
771 | return NULL;
|
---|
772 |
|
---|
773 | fibril_mutex_lock(&cwd_mutex);
|
---|
774 |
|
---|
775 | if ((cwd_size == 0) || (size < cwd_size + 1)) {
|
---|
776 | fibril_mutex_unlock(&cwd_mutex);
|
---|
777 | return NULL;
|
---|
778 | }
|
---|
779 |
|
---|
780 | str_cpy(buf, size, cwd_path);
|
---|
781 | fibril_mutex_unlock(&cwd_mutex);
|
---|
782 |
|
---|
783 | return buf;
|
---|
784 | }
|
---|
785 |
|
---|
786 | async_sess_t *fd_session(exch_mgmt_t mgmt, int fildes)
|
---|
787 | {
|
---|
788 | struct stat stat;
|
---|
789 | int rc = fstat(fildes, &stat);
|
---|
790 | if (rc != 0) {
|
---|
791 | errno = rc;
|
---|
792 | return NULL;
|
---|
793 | }
|
---|
794 |
|
---|
795 | if (!stat.service) {
|
---|
796 | errno = ENOENT;
|
---|
797 | return NULL;
|
---|
798 | }
|
---|
799 |
|
---|
800 | return loc_service_connect(mgmt, stat.service, 0);
|
---|
801 | }
|
---|
802 |
|
---|
803 | int dup2(int oldfd, int newfd)
|
---|
804 | {
|
---|
805 | async_exch_t *exch = vfs_exchange_begin();
|
---|
806 |
|
---|
807 | sysarg_t ret;
|
---|
808 | sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret);
|
---|
809 |
|
---|
810 | vfs_exchange_end(exch);
|
---|
811 |
|
---|
812 | if (rc == EOK)
|
---|
813 | return (int) ret;
|
---|
814 |
|
---|
815 | return (int) rc;
|
---|
816 | }
|
---|
817 |
|
---|
818 | int fd_wait(void)
|
---|
819 | {
|
---|
820 | async_exch_t *exch = vfs_exchange_begin();
|
---|
821 |
|
---|
822 | sysarg_t ret;
|
---|
823 | sysarg_t rc = async_req_0_1(exch, VFS_IN_WAIT_HANDLE, &ret);
|
---|
824 |
|
---|
825 | vfs_exchange_end(exch);
|
---|
826 |
|
---|
827 | if (rc == EOK)
|
---|
828 | return (int) ret;
|
---|
829 |
|
---|
830 | return (int) rc;
|
---|
831 | }
|
---|
832 |
|
---|
833 | int get_mtab_list(list_t *mtab_list)
|
---|
834 | {
|
---|
835 | sysarg_t rc;
|
---|
836 | aid_t req;
|
---|
837 | size_t i;
|
---|
838 | sysarg_t num_mounted_fs;
|
---|
839 |
|
---|
840 | async_exch_t *exch = vfs_exchange_begin();
|
---|
841 |
|
---|
842 | req = async_send_0(exch, VFS_IN_MTAB_GET, NULL);
|
---|
843 |
|
---|
844 | /* Ask VFS how many filesystems are mounted */
|
---|
845 | rc = async_req_0_1(exch, VFS_IN_PING, &num_mounted_fs);
|
---|
846 | if (rc != EOK)
|
---|
847 | goto exit;
|
---|
848 |
|
---|
849 | for (i = 0; i < num_mounted_fs; ++i) {
|
---|
850 | mtab_ent_t *mtab_ent;
|
---|
851 |
|
---|
852 | mtab_ent = malloc(sizeof(mtab_ent_t));
|
---|
853 | if (!mtab_ent) {
|
---|
854 | rc = ENOMEM;
|
---|
855 | goto exit;
|
---|
856 | }
|
---|
857 |
|
---|
858 | memset(mtab_ent, 0, sizeof(mtab_ent_t));
|
---|
859 |
|
---|
860 | rc = async_data_read_start(exch, (void *) mtab_ent->mp,
|
---|
861 | MAX_PATH_LEN);
|
---|
862 | if (rc != EOK)
|
---|
863 | goto exit;
|
---|
864 |
|
---|
865 | rc = async_data_read_start(exch, (void *) mtab_ent->opts,
|
---|
866 | MAX_MNTOPTS_LEN);
|
---|
867 | if (rc != EOK)
|
---|
868 | goto exit;
|
---|
869 |
|
---|
870 | rc = async_data_read_start(exch, (void *) mtab_ent->fs_name,
|
---|
871 | FS_NAME_MAXLEN);
|
---|
872 | if (rc != EOK)
|
---|
873 | goto exit;
|
---|
874 |
|
---|
875 | sysarg_t p[3];
|
---|
876 |
|
---|
877 | rc = async_req_0_3(exch, VFS_IN_PING, &p[0], &p[1], &p[2]);
|
---|
878 | if (rc != EOK)
|
---|
879 | goto exit;
|
---|
880 |
|
---|
881 | mtab_ent->flags = p[0];
|
---|
882 | mtab_ent->instance = p[1];
|
---|
883 | mtab_ent->fs_handle = p[2];
|
---|
884 |
|
---|
885 | link_initialize(&mtab_ent->link);
|
---|
886 | list_append(&mtab_ent->link, mtab_list);
|
---|
887 | }
|
---|
888 |
|
---|
889 | exit:
|
---|
890 | async_wait_for(req, &rc);
|
---|
891 | vfs_exchange_end(exch);
|
---|
892 | return rc;
|
---|
893 | }
|
---|
894 |
|
---|
895 | /** @}
|
---|
896 | */
|
---|