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