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_mtab.h>
|
---|
38 | #include <vfs/vfs_sess.h>
|
---|
39 | #include <macros.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <unistd.h>
|
---|
42 | #include <dirent.h>
|
---|
43 | #include <fcntl.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <sys/stat.h>
|
---|
46 | #include <sys/statfs.h>
|
---|
47 | #include <sys/types.h>
|
---|
48 | #include <ipc/services.h>
|
---|
49 | #include <ns.h>
|
---|
50 | #include <async.h>
|
---|
51 | #include <fibril_synch.h>
|
---|
52 | #include <errno.h>
|
---|
53 | #include <assert.h>
|
---|
54 | #include <str.h>
|
---|
55 | #include <loc.h>
|
---|
56 | #include <ipc/vfs.h>
|
---|
57 | #include <ipc/loc.h>
|
---|
58 |
|
---|
59 | static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
|
---|
60 | static async_sess_t *vfs_sess = NULL;
|
---|
61 |
|
---|
62 | static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
|
---|
63 |
|
---|
64 | static int cwd_fd = -1;
|
---|
65 | static char *cwd_path = NULL;
|
---|
66 | static size_t cwd_size = 0;
|
---|
67 |
|
---|
68 | static FIBRIL_MUTEX_INITIALIZE(root_mutex);
|
---|
69 | static int root_fd = -1;
|
---|
70 |
|
---|
71 | int vfs_root(void)
|
---|
72 | {
|
---|
73 | fibril_mutex_lock(&root_mutex);
|
---|
74 | int r;
|
---|
75 | if (root_fd < 0)
|
---|
76 | r = ENOENT;
|
---|
77 | else
|
---|
78 | r = vfs_clone(root_fd, true);
|
---|
79 | fibril_mutex_unlock(&root_mutex);
|
---|
80 | return r;
|
---|
81 | }
|
---|
82 |
|
---|
83 | void vfs_root_set(int nroot)
|
---|
84 | {
|
---|
85 | fibril_mutex_lock(&root_mutex);
|
---|
86 | if (root_fd >= 0)
|
---|
87 | close(root_fd);
|
---|
88 | root_fd = vfs_clone(nroot, true);
|
---|
89 | fibril_mutex_unlock(&root_mutex);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Start an async exchange on the VFS session.
|
---|
93 | *
|
---|
94 | * @return New exchange.
|
---|
95 | *
|
---|
96 | */
|
---|
97 | async_exch_t *vfs_exchange_begin(void)
|
---|
98 | {
|
---|
99 | fibril_mutex_lock(&vfs_mutex);
|
---|
100 |
|
---|
101 | while (vfs_sess == NULL) {
|
---|
102 | vfs_sess = service_connect_blocking(SERVICE_VFS, INTERFACE_VFS,
|
---|
103 | 0);
|
---|
104 | }
|
---|
105 |
|
---|
106 | fibril_mutex_unlock(&vfs_mutex);
|
---|
107 |
|
---|
108 | return async_exchange_begin(vfs_sess);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Finish an async exchange on the VFS session.
|
---|
112 | *
|
---|
113 | * @param exch Exchange to be finished.
|
---|
114 | *
|
---|
115 | */
|
---|
116 | void vfs_exchange_end(async_exch_t *exch)
|
---|
117 | {
|
---|
118 | async_exchange_end(exch);
|
---|
119 | }
|
---|
120 |
|
---|
121 | int _vfs_walk(int parent, const char *path, int flags)
|
---|
122 | {
|
---|
123 | async_exch_t *exch = vfs_exchange_begin();
|
---|
124 |
|
---|
125 | ipc_call_t answer;
|
---|
126 | aid_t req = async_send_2(exch, VFS_IN_WALK, parent, flags, &answer);
|
---|
127 | sysarg_t rc = async_data_write_start(exch, path, str_size(path));
|
---|
128 | vfs_exchange_end(exch);
|
---|
129 |
|
---|
130 | sysarg_t rc_orig;
|
---|
131 | async_wait_for(req, &rc_orig);
|
---|
132 |
|
---|
133 | if (rc_orig != EOK)
|
---|
134 | return (int) rc_orig;
|
---|
135 |
|
---|
136 | if (rc != EOK)
|
---|
137 | return (int) rc;
|
---|
138 |
|
---|
139 | return (int) IPC_GET_ARG1(answer);
|
---|
140 | }
|
---|
141 |
|
---|
142 | int vfs_lookup(const char *path, int flags)
|
---|
143 | {
|
---|
144 | size_t size;
|
---|
145 | char *p = vfs_absolutize(path, &size);
|
---|
146 | if (!p)
|
---|
147 | return ENOMEM;
|
---|
148 | int root = vfs_root();
|
---|
149 | if (root < 0) {
|
---|
150 | free(p);
|
---|
151 | return ENOENT;
|
---|
152 | }
|
---|
153 | int rc = _vfs_walk(root, p, flags);
|
---|
154 | close(root);
|
---|
155 | free(p);
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 | int _vfs_open(int fildes, int mode)
|
---|
160 | {
|
---|
161 | async_exch_t *exch = vfs_exchange_begin();
|
---|
162 | sysarg_t rc = async_req_2_0(exch, VFS_IN_OPEN2, fildes, mode);
|
---|
163 | vfs_exchange_end(exch);
|
---|
164 |
|
---|
165 | return (int) rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | char *vfs_absolutize(const char *path, size_t *retlen)
|
---|
169 | {
|
---|
170 | char *ncwd_path;
|
---|
171 | char *ncwd_path_nc;
|
---|
172 |
|
---|
173 | fibril_mutex_lock(&cwd_mutex);
|
---|
174 | size_t size = str_size(path);
|
---|
175 | if (*path != '/') {
|
---|
176 | if (cwd_path == NULL) {
|
---|
177 | fibril_mutex_unlock(&cwd_mutex);
|
---|
178 | return NULL;
|
---|
179 | }
|
---|
180 | ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
|
---|
181 | if (ncwd_path_nc == NULL) {
|
---|
182 | fibril_mutex_unlock(&cwd_mutex);
|
---|
183 | return NULL;
|
---|
184 | }
|
---|
185 | str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
|
---|
186 | ncwd_path_nc[cwd_size] = '/';
|
---|
187 | ncwd_path_nc[cwd_size + 1] = '\0';
|
---|
188 | } else {
|
---|
189 | ncwd_path_nc = malloc(size + 1);
|
---|
190 | if (ncwd_path_nc == NULL) {
|
---|
191 | fibril_mutex_unlock(&cwd_mutex);
|
---|
192 | return NULL;
|
---|
193 | }
|
---|
194 | ncwd_path_nc[0] = '\0';
|
---|
195 | }
|
---|
196 | str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
|
---|
197 | ncwd_path = canonify(ncwd_path_nc, retlen);
|
---|
198 | if (ncwd_path == NULL) {
|
---|
199 | fibril_mutex_unlock(&cwd_mutex);
|
---|
200 | free(ncwd_path_nc);
|
---|
201 | return NULL;
|
---|
202 | }
|
---|
203 | /*
|
---|
204 | * We need to clone ncwd_path because canonify() works in-place and thus
|
---|
205 | * the address in ncwd_path need not be the same as ncwd_path_nc, even
|
---|
206 | * though they both point into the same dynamically allocated buffer.
|
---|
207 | */
|
---|
208 | ncwd_path = str_dup(ncwd_path);
|
---|
209 | free(ncwd_path_nc);
|
---|
210 | if (ncwd_path == NULL) {
|
---|
211 | fibril_mutex_unlock(&cwd_mutex);
|
---|
212 | return NULL;
|
---|
213 | }
|
---|
214 | fibril_mutex_unlock(&cwd_mutex);
|
---|
215 | return ncwd_path;
|
---|
216 | }
|
---|
217 |
|
---|
218 | int vfs_mount(int mp, const char *fs_name, service_id_t serv, const char *opts,
|
---|
219 | unsigned int flags, unsigned int instance, int *mountedfd)
|
---|
220 | {
|
---|
221 | sysarg_t rc, rc1;
|
---|
222 |
|
---|
223 | if (!mountedfd)
|
---|
224 | flags |= VFS_MOUNT_NO_REF;
|
---|
225 | if (mp < 0)
|
---|
226 | flags |= VFS_MOUNT_CONNECT_ONLY;
|
---|
227 |
|
---|
228 | ipc_call_t answer;
|
---|
229 | async_exch_t *exch = vfs_exchange_begin();
|
---|
230 | aid_t req = async_send_4(exch, VFS_IN_MOUNT, mp, serv, flags, instance,
|
---|
231 | &answer);
|
---|
232 |
|
---|
233 | rc1 = async_data_write_start(exch, (void *) opts, str_size(opts));
|
---|
234 | if (rc1 == EOK) {
|
---|
235 | rc1 = async_data_write_start(exch, (void *) fs_name,
|
---|
236 | str_size(fs_name));
|
---|
237 | }
|
---|
238 |
|
---|
239 | vfs_exchange_end(exch);
|
---|
240 |
|
---|
241 | async_wait_for(req, &rc);
|
---|
242 |
|
---|
243 | if (mountedfd)
|
---|
244 | *mountedfd = (int) IPC_GET_ARG1(answer);
|
---|
245 |
|
---|
246 | if (rc != EOK)
|
---|
247 | return rc;
|
---|
248 | return rc1;
|
---|
249 | }
|
---|
250 |
|
---|
251 | int vfs_unmount(int mp)
|
---|
252 | {
|
---|
253 | async_exch_t *exch = vfs_exchange_begin();
|
---|
254 | int rc = async_req_1_0(exch, VFS_IN_UNMOUNT, mp);
|
---|
255 | vfs_exchange_end(exch);
|
---|
256 | return rc;
|
---|
257 | }
|
---|
258 |
|
---|
259 | int mount(const char *fs_name, const char *mp, const char *fqsn,
|
---|
260 | const char *opts, unsigned int flags, unsigned int instance)
|
---|
261 | {
|
---|
262 | int null_id = -1;
|
---|
263 | char null[LOC_NAME_MAXLEN];
|
---|
264 |
|
---|
265 | if (str_cmp(fqsn, "") == 0) {
|
---|
266 | /*
|
---|
267 | * No device specified, create a fresh null/%d device instead.
|
---|
268 | */
|
---|
269 | null_id = loc_null_create();
|
---|
270 |
|
---|
271 | if (null_id == -1)
|
---|
272 | return ENOMEM;
|
---|
273 |
|
---|
274 | snprintf(null, LOC_NAME_MAXLEN, "null/%d", null_id);
|
---|
275 | fqsn = null;
|
---|
276 | }
|
---|
277 |
|
---|
278 | if (flags & IPC_FLAG_BLOCKING)
|
---|
279 | flags = VFS_MOUNT_BLOCKING;
|
---|
280 | else
|
---|
281 | flags = 0;
|
---|
282 |
|
---|
283 | service_id_t service_id;
|
---|
284 | int res = loc_service_get_id(fqsn, &service_id, flags);
|
---|
285 | if (res != EOK) {
|
---|
286 | if (null_id != -1)
|
---|
287 | loc_null_destroy(null_id);
|
---|
288 |
|
---|
289 | return res;
|
---|
290 | }
|
---|
291 |
|
---|
292 | size_t mpa_size;
|
---|
293 | char *mpa = vfs_absolutize(mp, &mpa_size);
|
---|
294 | if (mpa == NULL) {
|
---|
295 | if (null_id != -1)
|
---|
296 | loc_null_destroy(null_id);
|
---|
297 |
|
---|
298 | return ENOMEM;
|
---|
299 | }
|
---|
300 |
|
---|
301 | fibril_mutex_lock(&root_mutex);
|
---|
302 |
|
---|
303 | int rc;
|
---|
304 |
|
---|
305 | if (str_cmp(mpa, "/") == 0) {
|
---|
306 | /* Mounting root. */
|
---|
307 |
|
---|
308 | if (root_fd >= 0) {
|
---|
309 | fibril_mutex_unlock(&root_mutex);
|
---|
310 | if (null_id != -1)
|
---|
311 | loc_null_destroy(null_id);
|
---|
312 | return EBUSY;
|
---|
313 | }
|
---|
314 |
|
---|
315 | int root;
|
---|
316 | rc = vfs_mount(-1, fs_name, service_id, opts, flags, instance,
|
---|
317 | &root);
|
---|
318 | if (rc == EOK)
|
---|
319 | root_fd = root;
|
---|
320 | } else {
|
---|
321 | if (root_fd < 0) {
|
---|
322 | fibril_mutex_unlock(&root_mutex);
|
---|
323 | if (null_id != -1)
|
---|
324 | loc_null_destroy(null_id);
|
---|
325 | return EINVAL;
|
---|
326 | }
|
---|
327 |
|
---|
328 | int mpfd = _vfs_walk(root_fd, mpa, WALK_DIRECTORY);
|
---|
329 | if (mpfd >= 0) {
|
---|
330 | rc = vfs_mount(mpfd, fs_name, service_id, opts, flags,
|
---|
331 | instance, NULL);
|
---|
332 | close(mpfd);
|
---|
333 | } else {
|
---|
334 | rc = mpfd;
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | fibril_mutex_unlock(&root_mutex);
|
---|
339 |
|
---|
340 | if ((rc != EOK) && (null_id != -1))
|
---|
341 | loc_null_destroy(null_id);
|
---|
342 |
|
---|
343 | return (int) rc;
|
---|
344 | }
|
---|
345 |
|
---|
346 | int unmount(const char *mpp)
|
---|
347 | {
|
---|
348 | int mp = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY);
|
---|
349 | if (mp < 0)
|
---|
350 | return mp;
|
---|
351 |
|
---|
352 | int rc = vfs_unmount(mp);
|
---|
353 | close(mp);
|
---|
354 | return rc;
|
---|
355 | }
|
---|
356 |
|
---|
357 | static int walk_flags(int oflags)
|
---|
358 | {
|
---|
359 | int flags = 0;
|
---|
360 | if (oflags & O_CREAT) {
|
---|
361 | if (oflags & O_EXCL)
|
---|
362 | flags |= WALK_MUST_CREATE;
|
---|
363 | else
|
---|
364 | flags |= WALK_MAY_CREATE;
|
---|
365 | }
|
---|
366 | return flags;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /** Open file.
|
---|
370 | *
|
---|
371 | * @param path File path
|
---|
372 | * @param oflag O_xxx flags
|
---|
373 | * @param mode File mode (only with O_CREAT)
|
---|
374 | *
|
---|
375 | * @return Nonnegative file descriptor on success. On error -1 is returned
|
---|
376 | * and errno is set.
|
---|
377 | */
|
---|
378 | int open(const char *path, int oflag, ...)
|
---|
379 | {
|
---|
380 | if (((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||
|
---|
381 | ((oflag & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||
|
---|
382 | ((oflag & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||
|
---|
383 | ((oflag & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {
|
---|
384 | errno = EINVAL;
|
---|
385 | return -1;
|
---|
386 | }
|
---|
387 |
|
---|
388 | int fd = vfs_lookup(path, walk_flags(oflag) | WALK_REGULAR);
|
---|
389 | if (fd < 0) {
|
---|
390 | errno = fd;
|
---|
391 | return -1;
|
---|
392 | }
|
---|
393 |
|
---|
394 | int mode =
|
---|
395 | ((oflag & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |
|
---|
396 | ((oflag & O_RDONLY) ? MODE_READ : 0) |
|
---|
397 | ((oflag & O_WRONLY) ? MODE_WRITE : 0) |
|
---|
398 | ((oflag & O_APPEND) ? MODE_APPEND : 0);
|
---|
399 |
|
---|
400 | int rc = _vfs_open(fd, mode);
|
---|
401 | if (rc < 0) {
|
---|
402 | close(fd);
|
---|
403 | errno = rc;
|
---|
404 | return -1;
|
---|
405 | }
|
---|
406 |
|
---|
407 | if (oflag & O_TRUNC) {
|
---|
408 | assert(oflag & O_WRONLY || oflag & O_RDWR);
|
---|
409 | assert(!(oflag & O_APPEND));
|
---|
410 |
|
---|
411 | (void) ftruncate(fd, 0);
|
---|
412 | }
|
---|
413 |
|
---|
414 | return fd;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /** Close file.
|
---|
418 | *
|
---|
419 | * @param fildes File descriptor
|
---|
420 | * @return Zero on success. On error -1 is returned and errno is set.
|
---|
421 | */
|
---|
422 | int close(int fildes)
|
---|
423 | {
|
---|
424 | sysarg_t rc;
|
---|
425 |
|
---|
426 | async_exch_t *exch = vfs_exchange_begin();
|
---|
427 | rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes);
|
---|
428 | vfs_exchange_end(exch);
|
---|
429 |
|
---|
430 | if (rc != EOK) {
|
---|
431 | errno = rc;
|
---|
432 | return -1;
|
---|
433 | }
|
---|
434 |
|
---|
435 | return 0;
|
---|
436 | }
|
---|
437 |
|
---|
438 | /** Read bytes from file.
|
---|
439 | *
|
---|
440 | * Read up to @a nbyte bytes from file. The actual number of bytes read
|
---|
441 | * may be lower, but greater than zero if there are any bytes available.
|
---|
442 | * If there are no bytes available for reading, then the function will
|
---|
443 | * return success with zero bytes read.
|
---|
444 | *
|
---|
445 | * @param fildes File descriptor
|
---|
446 | * @param pos Position to read from
|
---|
447 | * @param buf Buffer
|
---|
448 | * @param nbyte Maximum number of bytes to read
|
---|
449 | * @param nread Place to store actual number of bytes read (0 or more)
|
---|
450 | *
|
---|
451 | * @return EOK on success, non-zero error code on error.
|
---|
452 | */
|
---|
453 | static int _read_short(int fildes, aoff64_t pos, void *buf, size_t nbyte,
|
---|
454 | ssize_t *nread)
|
---|
455 | {
|
---|
456 | sysarg_t rc;
|
---|
457 | ipc_call_t answer;
|
---|
458 | aid_t req;
|
---|
459 |
|
---|
460 | if (nbyte > DATA_XFER_LIMIT)
|
---|
461 | nbyte = DATA_XFER_LIMIT;
|
---|
462 |
|
---|
463 | async_exch_t *exch = vfs_exchange_begin();
|
---|
464 |
|
---|
465 | req = async_send_3(exch, VFS_IN_READ, fildes, LOWER32(pos),
|
---|
466 | UPPER32(pos), &answer);
|
---|
467 | rc = async_data_read_start(exch, (void *) buf, nbyte);
|
---|
468 |
|
---|
469 | vfs_exchange_end(exch);
|
---|
470 |
|
---|
471 | if (rc == EOK)
|
---|
472 | async_wait_for(req, &rc);
|
---|
473 | else
|
---|
474 | async_forget(req);
|
---|
475 |
|
---|
476 | if (rc != EOK)
|
---|
477 | return rc;
|
---|
478 |
|
---|
479 | *nread = (ssize_t) IPC_GET_ARG1(answer);
|
---|
480 | return EOK;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /** Write bytes to file.
|
---|
484 | *
|
---|
485 | * Write up to @a nbyte bytes from file. The actual number of bytes written
|
---|
486 | * may be lower, but greater than zero.
|
---|
487 | *
|
---|
488 | * @param fildes File descriptor
|
---|
489 | * @param pos Position to write to
|
---|
490 | * @param buf Buffer
|
---|
491 | * @param nbyte Maximum number of bytes to write
|
---|
492 | * @param nread Place to store actual number of bytes written (0 or more)
|
---|
493 | *
|
---|
494 | * @return EOK on success, non-zero error code on error.
|
---|
495 | */
|
---|
496 | static int _write_short(int fildes, aoff64_t pos, const void *buf, size_t nbyte,
|
---|
497 | ssize_t *nwritten)
|
---|
498 | {
|
---|
499 | sysarg_t rc;
|
---|
500 | ipc_call_t answer;
|
---|
501 | aid_t req;
|
---|
502 |
|
---|
503 | if (nbyte > DATA_XFER_LIMIT)
|
---|
504 | nbyte = DATA_XFER_LIMIT;
|
---|
505 |
|
---|
506 | async_exch_t *exch = vfs_exchange_begin();
|
---|
507 |
|
---|
508 | req = async_send_3(exch, VFS_IN_WRITE, fildes, LOWER32(pos),
|
---|
509 | UPPER32(pos), &answer);
|
---|
510 | rc = async_data_write_start(exch, (void *) buf, nbyte);
|
---|
511 |
|
---|
512 | vfs_exchange_end(exch);
|
---|
513 |
|
---|
514 | if (rc == EOK)
|
---|
515 | async_wait_for(req, &rc);
|
---|
516 | else
|
---|
517 | async_forget(req);
|
---|
518 |
|
---|
519 | if (rc != EOK)
|
---|
520 | return rc;
|
---|
521 |
|
---|
522 | *nwritten = (ssize_t) IPC_GET_ARG1(answer);
|
---|
523 | return EOK;
|
---|
524 | }
|
---|
525 |
|
---|
526 | /** Read data.
|
---|
527 | *
|
---|
528 | * Read up to @a nbytes bytes from file if available. This function always reads
|
---|
529 | * all the available bytes up to @a nbytes.
|
---|
530 | *
|
---|
531 | * @param fildes File descriptor
|
---|
532 | * @param pos Pointer to position to read from
|
---|
533 | * @param buf Buffer, @a nbytes bytes long
|
---|
534 | * @param nbytes Number of bytes to read
|
---|
535 | *
|
---|
536 | * @return On success, nonnegative number of bytes read.
|
---|
537 | * On failure, -1 and sets errno.
|
---|
538 | */
|
---|
539 | ssize_t read(int fildes, aoff64_t *pos, void *buf, size_t nbyte)
|
---|
540 | {
|
---|
541 | ssize_t cnt = 0;
|
---|
542 | size_t nread = 0;
|
---|
543 | uint8_t *bp = (uint8_t *) buf;
|
---|
544 | int rc;
|
---|
545 |
|
---|
546 | do {
|
---|
547 | bp += cnt;
|
---|
548 | nread += cnt;
|
---|
549 | *pos += cnt;
|
---|
550 | rc = _read_short(fildes, *pos, bp, nbyte - nread, &cnt);
|
---|
551 | } while (rc == EOK && cnt > 0 && (nbyte - nread - cnt) > 0);
|
---|
552 |
|
---|
553 | if (rc != EOK) {
|
---|
554 | errno = rc;
|
---|
555 | return -1;
|
---|
556 | }
|
---|
557 |
|
---|
558 | *pos += cnt;
|
---|
559 | return nread + cnt;
|
---|
560 | }
|
---|
561 |
|
---|
562 | /** Write data.
|
---|
563 | *
|
---|
564 | * This function fails if it cannot write exactly @a len bytes to the file.
|
---|
565 | *
|
---|
566 | * @param fildes File descriptor
|
---|
567 | * @param pos Pointer to position to write to
|
---|
568 | * @param buf Data, @a nbytes bytes long
|
---|
569 | * @param nbytes Number of bytes to write
|
---|
570 | *
|
---|
571 | * @return On success, nonnegative number of bytes written.
|
---|
572 | * On failure, -1 and sets errno.
|
---|
573 | */
|
---|
574 | ssize_t write(int fildes, aoff64_t *pos, const void *buf, size_t nbyte)
|
---|
575 | {
|
---|
576 | ssize_t cnt = 0;
|
---|
577 | ssize_t nwritten = 0;
|
---|
578 | const uint8_t *bp = (uint8_t *) buf;
|
---|
579 | int rc;
|
---|
580 |
|
---|
581 | do {
|
---|
582 | bp += cnt;
|
---|
583 | nwritten += cnt;
|
---|
584 | *pos += cnt;
|
---|
585 | rc = _write_short(fildes, *pos, bp, nbyte - nwritten, &cnt);
|
---|
586 | } while (rc == EOK && ((ssize_t )nbyte - nwritten - cnt) > 0);
|
---|
587 |
|
---|
588 | if (rc != EOK) {
|
---|
589 | errno = rc;
|
---|
590 | return -1;
|
---|
591 | }
|
---|
592 |
|
---|
593 | *pos += cnt;
|
---|
594 | return nbyte;
|
---|
595 | }
|
---|
596 |
|
---|
597 | /** Synchronize file.
|
---|
598 | *
|
---|
599 | * @param fildes File descriptor
|
---|
600 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
601 | */
|
---|
602 | int fsync(int fildes)
|
---|
603 | {
|
---|
604 | async_exch_t *exch = vfs_exchange_begin();
|
---|
605 | sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes);
|
---|
606 | vfs_exchange_end(exch);
|
---|
607 |
|
---|
608 | if (rc != EOK) {
|
---|
609 | errno = rc;
|
---|
610 | return -1;
|
---|
611 | }
|
---|
612 |
|
---|
613 | return 0;
|
---|
614 | }
|
---|
615 |
|
---|
616 | /** Truncate file to a specified length.
|
---|
617 | *
|
---|
618 | * Truncate file so that its size is exactly @a length
|
---|
619 | *
|
---|
620 | * @param fildes File descriptor
|
---|
621 | * @param length Length
|
---|
622 | *
|
---|
623 | * @return 0 on success, -1 on error and sets errno.
|
---|
624 | */
|
---|
625 | int ftruncate(int fildes, aoff64_t length)
|
---|
626 | {
|
---|
627 | sysarg_t rc;
|
---|
628 |
|
---|
629 | async_exch_t *exch = vfs_exchange_begin();
|
---|
630 | rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes,
|
---|
631 | LOWER32(length), UPPER32(length));
|
---|
632 | vfs_exchange_end(exch);
|
---|
633 |
|
---|
634 | if (rc != EOK) {
|
---|
635 | errno = rc;
|
---|
636 | return -1;
|
---|
637 | }
|
---|
638 |
|
---|
639 | return 0;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /** Get file status.
|
---|
643 | *
|
---|
644 | * @param fildes File descriptor
|
---|
645 | * @param stat Place to store file information
|
---|
646 | *
|
---|
647 | * @return 0 on success, -1 on error and sets errno.
|
---|
648 | */
|
---|
649 | int fstat(int fildes, struct stat *stat)
|
---|
650 | {
|
---|
651 | sysarg_t rc;
|
---|
652 | aid_t req;
|
---|
653 |
|
---|
654 | async_exch_t *exch = vfs_exchange_begin();
|
---|
655 |
|
---|
656 | req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL);
|
---|
657 | rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
|
---|
658 | if (rc != EOK) {
|
---|
659 | vfs_exchange_end(exch);
|
---|
660 |
|
---|
661 | sysarg_t rc_orig;
|
---|
662 | async_wait_for(req, &rc_orig);
|
---|
663 |
|
---|
664 | if (rc_orig != EOK)
|
---|
665 | rc = rc_orig;
|
---|
666 | if (rc != EOK) {
|
---|
667 | errno = rc;
|
---|
668 | return -1;
|
---|
669 | }
|
---|
670 |
|
---|
671 | return 0;
|
---|
672 | }
|
---|
673 |
|
---|
674 | vfs_exchange_end(exch);
|
---|
675 | async_wait_for(req, &rc);
|
---|
676 |
|
---|
677 | if (rc != EOK) {
|
---|
678 | errno = rc;
|
---|
679 | return -1;
|
---|
680 | }
|
---|
681 |
|
---|
682 | return 0;
|
---|
683 | }
|
---|
684 |
|
---|
685 | /** Get file status.
|
---|
686 | *
|
---|
687 | * @param path Path to file
|
---|
688 | * @param stat Place to store file information
|
---|
689 | *
|
---|
690 | * @return 0 on success, -1 on error and sets errno.
|
---|
691 | */
|
---|
692 | int stat(const char *path, struct stat *stat)
|
---|
693 | {
|
---|
694 | int fd = vfs_lookup(path, 0);
|
---|
695 | if (fd < 0) {
|
---|
696 | errno = fd;
|
---|
697 | return -1;
|
---|
698 | }
|
---|
699 |
|
---|
700 | int rc = fstat(fd, stat);
|
---|
701 | if (rc != EOK) {
|
---|
702 | close(fd);
|
---|
703 | errno = rc;
|
---|
704 | rc = -1;
|
---|
705 | } else
|
---|
706 | rc = close(fd);
|
---|
707 |
|
---|
708 | return rc;
|
---|
709 | }
|
---|
710 |
|
---|
711 | /** Open directory.
|
---|
712 | *
|
---|
713 | * @param dirname Directory pathname
|
---|
714 | *
|
---|
715 | * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
|
---|
716 | */
|
---|
717 | DIR *opendir(const char *dirname)
|
---|
718 | {
|
---|
719 | DIR *dirp = malloc(sizeof(DIR));
|
---|
720 | if (!dirp) {
|
---|
721 | errno = ENOMEM;
|
---|
722 | return NULL;
|
---|
723 | }
|
---|
724 |
|
---|
725 | int fd = vfs_lookup(dirname, WALK_DIRECTORY);
|
---|
726 | if (fd < 0) {
|
---|
727 | free(dirp);
|
---|
728 | errno = fd;
|
---|
729 | return NULL;
|
---|
730 | }
|
---|
731 |
|
---|
732 | int rc = _vfs_open(fd, MODE_READ);
|
---|
733 | if (rc < 0) {
|
---|
734 | free(dirp);
|
---|
735 | close(fd);
|
---|
736 | errno = rc;
|
---|
737 | return NULL;
|
---|
738 | }
|
---|
739 |
|
---|
740 | dirp->fd = fd;
|
---|
741 | dirp->pos = 0;
|
---|
742 | return dirp;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /** Read directory entry.
|
---|
746 | *
|
---|
747 | * @param dirp Open directory
|
---|
748 | * @return Non-NULL pointer to directory entry on success. On error returns
|
---|
749 | * @c NULL and sets errno.
|
---|
750 | */
|
---|
751 | struct dirent *readdir(DIR *dirp)
|
---|
752 | {
|
---|
753 | int rc;
|
---|
754 | ssize_t len;
|
---|
755 |
|
---|
756 | rc = _read_short(dirp->fd, dirp->pos, &dirp->res.d_name[0],
|
---|
757 | NAME_MAX + 1, &len);
|
---|
758 | if (rc != EOK) {
|
---|
759 | errno = rc;
|
---|
760 | return NULL;
|
---|
761 | }
|
---|
762 |
|
---|
763 | dirp->pos += len;
|
---|
764 |
|
---|
765 | return &dirp->res;
|
---|
766 | }
|
---|
767 |
|
---|
768 | /** Rewind directory position to the beginning.
|
---|
769 | *
|
---|
770 | * @param dirp Open directory
|
---|
771 | */
|
---|
772 | void rewinddir(DIR *dirp)
|
---|
773 | {
|
---|
774 | dirp->pos = 0;
|
---|
775 | }
|
---|
776 |
|
---|
777 | /** Close directory.
|
---|
778 | *
|
---|
779 | * @param dirp Open directory
|
---|
780 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
781 | */
|
---|
782 | int closedir(DIR *dirp)
|
---|
783 | {
|
---|
784 | int rc;
|
---|
785 |
|
---|
786 | rc = close(dirp->fd);
|
---|
787 | free(dirp);
|
---|
788 |
|
---|
789 | /* On error errno was set by close() */
|
---|
790 | return rc;
|
---|
791 | }
|
---|
792 |
|
---|
793 | /** Create directory.
|
---|
794 | *
|
---|
795 | * @param path Path
|
---|
796 | * @param mode File mode
|
---|
797 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
798 | */
|
---|
799 | int mkdir(const char *path, mode_t mode)
|
---|
800 | {
|
---|
801 | int fd = vfs_lookup(path, WALK_MUST_CREATE | WALK_DIRECTORY);
|
---|
802 | if (fd < 0) {
|
---|
803 | errno = fd;
|
---|
804 | return -1;
|
---|
805 | }
|
---|
806 |
|
---|
807 | return close(fd);
|
---|
808 | }
|
---|
809 |
|
---|
810 | static int _vfs_unlink2(int parent, const char *path, int expect, int wflag)
|
---|
811 | {
|
---|
812 | sysarg_t rc;
|
---|
813 | aid_t req;
|
---|
814 |
|
---|
815 | async_exch_t *exch = vfs_exchange_begin();
|
---|
816 |
|
---|
817 | req = async_send_3(exch, VFS_IN_UNLINK2, parent, expect, wflag, NULL);
|
---|
818 | rc = async_data_write_start(exch, path, str_size(path));
|
---|
819 |
|
---|
820 | vfs_exchange_end(exch);
|
---|
821 |
|
---|
822 | sysarg_t rc_orig;
|
---|
823 | async_wait_for(req, &rc_orig);
|
---|
824 |
|
---|
825 | if (rc_orig != EOK)
|
---|
826 | return (int) rc_orig;
|
---|
827 | return rc;
|
---|
828 | }
|
---|
829 |
|
---|
830 | /** Unlink file or directory.
|
---|
831 | *
|
---|
832 | * @param path Path
|
---|
833 | * @return EOk on success, error code on error
|
---|
834 | */
|
---|
835 | int unlink(const char *path)
|
---|
836 | {
|
---|
837 | size_t pa_size;
|
---|
838 | char *pa = vfs_absolutize(path, &pa_size);
|
---|
839 | if (!pa) {
|
---|
840 | errno = ENOMEM;
|
---|
841 | return -1;
|
---|
842 | }
|
---|
843 |
|
---|
844 | int root = vfs_root();
|
---|
845 | if (root < 0) {
|
---|
846 | free(pa);
|
---|
847 | errno = ENOENT;
|
---|
848 | return -1;
|
---|
849 | }
|
---|
850 |
|
---|
851 | int rc = _vfs_unlink2(root, pa, -1, 0);
|
---|
852 |
|
---|
853 | if (rc != EOK) {
|
---|
854 | errno = rc;
|
---|
855 | rc = -1;
|
---|
856 | }
|
---|
857 |
|
---|
858 | free(pa);
|
---|
859 | close(root);
|
---|
860 | return rc;
|
---|
861 | }
|
---|
862 |
|
---|
863 | /** Remove empty directory.
|
---|
864 | *
|
---|
865 | * @param path Path
|
---|
866 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
867 | */
|
---|
868 | int rmdir(const char *path)
|
---|
869 | {
|
---|
870 | size_t pa_size;
|
---|
871 | char *pa = vfs_absolutize(path, &pa_size);
|
---|
872 | if (!pa) {
|
---|
873 | errno = ENOMEM;
|
---|
874 | return -1;
|
---|
875 | }
|
---|
876 |
|
---|
877 | int root = vfs_root();
|
---|
878 | if (root < 0) {
|
---|
879 | free(pa);
|
---|
880 | errno = ENOENT;
|
---|
881 | return -1;
|
---|
882 | }
|
---|
883 |
|
---|
884 | int rc = _vfs_unlink2(root, pa, -1, WALK_DIRECTORY);
|
---|
885 | if (rc != EOK) {
|
---|
886 | errno = rc;
|
---|
887 | rc = -1;
|
---|
888 | }
|
---|
889 |
|
---|
890 | free(pa);
|
---|
891 | close(root);
|
---|
892 | return rc;
|
---|
893 | }
|
---|
894 |
|
---|
895 | /** Rename directory entry.
|
---|
896 | *
|
---|
897 | * @param old Old name
|
---|
898 | * @param new New name
|
---|
899 | *
|
---|
900 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
901 | */
|
---|
902 | int rename(const char *old, const char *new)
|
---|
903 | {
|
---|
904 | sysarg_t rc;
|
---|
905 | sysarg_t rc_orig;
|
---|
906 | aid_t req;
|
---|
907 |
|
---|
908 | size_t olda_size;
|
---|
909 | char *olda = vfs_absolutize(old, &olda_size);
|
---|
910 | if (olda == NULL) {
|
---|
911 | errno = ENOMEM;
|
---|
912 | return -1;
|
---|
913 | }
|
---|
914 |
|
---|
915 | size_t newa_size;
|
---|
916 | char *newa = vfs_absolutize(new, &newa_size);
|
---|
917 | if (newa == NULL) {
|
---|
918 | free(olda);
|
---|
919 | errno = ENOMEM;
|
---|
920 | return -1;
|
---|
921 | }
|
---|
922 |
|
---|
923 | async_exch_t *exch = vfs_exchange_begin();
|
---|
924 | int root = vfs_root();
|
---|
925 | if (root < 0) {
|
---|
926 | free(olda);
|
---|
927 | free(newa);
|
---|
928 | errno = ENOENT;
|
---|
929 | return -1;
|
---|
930 | }
|
---|
931 |
|
---|
932 | req = async_send_1(exch, VFS_IN_RENAME, root, NULL);
|
---|
933 | rc = async_data_write_start(exch, olda, olda_size);
|
---|
934 | if (rc != EOK) {
|
---|
935 | vfs_exchange_end(exch);
|
---|
936 | free(olda);
|
---|
937 | free(newa);
|
---|
938 | close(root);
|
---|
939 | async_wait_for(req, &rc_orig);
|
---|
940 | if (rc_orig != EOK)
|
---|
941 | rc = rc_orig;
|
---|
942 | if (rc != EOK) {
|
---|
943 | errno = rc;
|
---|
944 | return -1;
|
---|
945 | }
|
---|
946 | return 0;
|
---|
947 | }
|
---|
948 | rc = async_data_write_start(exch, newa, newa_size);
|
---|
949 | if (rc != EOK) {
|
---|
950 | vfs_exchange_end(exch);
|
---|
951 | free(olda);
|
---|
952 | free(newa);
|
---|
953 | close(root);
|
---|
954 | async_wait_for(req, &rc_orig);
|
---|
955 | if (rc_orig != EOK)
|
---|
956 | rc = rc_orig;
|
---|
957 | if (rc != EOK) {
|
---|
958 | errno = rc;
|
---|
959 | return -1;
|
---|
960 | }
|
---|
961 | return 0;
|
---|
962 | }
|
---|
963 | vfs_exchange_end(exch);
|
---|
964 | free(olda);
|
---|
965 | free(newa);
|
---|
966 | close(root);
|
---|
967 | async_wait_for(req, &rc);
|
---|
968 |
|
---|
969 | if (rc != EOK) {
|
---|
970 | errno = rc;
|
---|
971 | return -1;
|
---|
972 | }
|
---|
973 |
|
---|
974 | return 0;
|
---|
975 | }
|
---|
976 |
|
---|
977 | /** Remove directory entry.
|
---|
978 | *
|
---|
979 | * @param path Path
|
---|
980 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
981 | */
|
---|
982 | int remove(const char *path)
|
---|
983 | {
|
---|
984 | return unlink(path);
|
---|
985 | }
|
---|
986 |
|
---|
987 | /** Change working directory.
|
---|
988 | *
|
---|
989 | * @param path Path
|
---|
990 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
991 | */
|
---|
992 | int chdir(const char *path)
|
---|
993 | {
|
---|
994 | size_t abs_size;
|
---|
995 | char *abs = vfs_absolutize(path, &abs_size);
|
---|
996 | if (!abs) {
|
---|
997 | errno = ENOMEM;
|
---|
998 | return -1;
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | int fd = vfs_lookup(abs, WALK_DIRECTORY);
|
---|
1002 | if (fd < 0) {
|
---|
1003 | free(abs);
|
---|
1004 | errno = fd;
|
---|
1005 | return -1;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | fibril_mutex_lock(&cwd_mutex);
|
---|
1009 |
|
---|
1010 | if (cwd_fd >= 0)
|
---|
1011 | close(cwd_fd);
|
---|
1012 |
|
---|
1013 | if (cwd_path)
|
---|
1014 | free(cwd_path);
|
---|
1015 |
|
---|
1016 | cwd_fd = fd;
|
---|
1017 | cwd_path = abs;
|
---|
1018 | cwd_size = abs_size;
|
---|
1019 |
|
---|
1020 | fibril_mutex_unlock(&cwd_mutex);
|
---|
1021 | return 0;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | /** Get current working directory path.
|
---|
1025 | *
|
---|
1026 | * @param buf Buffer
|
---|
1027 | * @param size Size of @a buf
|
---|
1028 | * @return On success returns @a buf. On failure returns @c NULL and sets errno.
|
---|
1029 | */
|
---|
1030 | char *getcwd(char *buf, size_t size)
|
---|
1031 | {
|
---|
1032 | if (size == 0) {
|
---|
1033 | errno = EINVAL;
|
---|
1034 | return NULL;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | fibril_mutex_lock(&cwd_mutex);
|
---|
1038 |
|
---|
1039 | if ((cwd_size == 0) || (size < cwd_size + 1)) {
|
---|
1040 | fibril_mutex_unlock(&cwd_mutex);
|
---|
1041 | errno = ERANGE;
|
---|
1042 | return NULL;
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | str_cpy(buf, size, cwd_path);
|
---|
1046 | fibril_mutex_unlock(&cwd_mutex);
|
---|
1047 |
|
---|
1048 | return buf;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | /** Open session to service represented by a special file.
|
---|
1052 | *
|
---|
1053 | * Given that the file referred to by @a fildes represents a service,
|
---|
1054 | * open a session to that service.
|
---|
1055 | *
|
---|
1056 | * @param fildes File descriptor
|
---|
1057 | * @param iface Interface to connect to (XXX Should be automatic)
|
---|
1058 | * @return On success returns session pointer. On error returns @c NULL.
|
---|
1059 | */
|
---|
1060 | async_sess_t *vfs_fd_session(int fildes, iface_t iface)
|
---|
1061 | {
|
---|
1062 | struct stat stat;
|
---|
1063 | int rc = fstat(fildes, &stat);
|
---|
1064 | if (rc != 0)
|
---|
1065 | return NULL;
|
---|
1066 |
|
---|
1067 | if (stat.service == 0)
|
---|
1068 | return NULL;
|
---|
1069 |
|
---|
1070 | return loc_service_connect(stat.service, iface, 0);
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | /** Duplicate open file.
|
---|
1074 | *
|
---|
1075 | * Duplicate open file under a new file descriptor.
|
---|
1076 | *
|
---|
1077 | * @param oldfd Old file descriptor
|
---|
1078 | * @param newfd New file descriptor
|
---|
1079 | * @return 0 on success. On error -1 is returned and errno is set
|
---|
1080 | */
|
---|
1081 | int dup2(int oldfd, int newfd)
|
---|
1082 | {
|
---|
1083 | async_exch_t *exch = vfs_exchange_begin();
|
---|
1084 |
|
---|
1085 | sysarg_t ret;
|
---|
1086 | sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret);
|
---|
1087 |
|
---|
1088 | vfs_exchange_end(exch);
|
---|
1089 |
|
---|
1090 | if (rc == EOK)
|
---|
1091 | rc = ret;
|
---|
1092 |
|
---|
1093 | if (rc != EOK) {
|
---|
1094 | errno = rc;
|
---|
1095 | return -1;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | return 0;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | static void process_mp(const char *path, struct stat *stat, list_t *mtab_list)
|
---|
1102 | {
|
---|
1103 | mtab_ent_t *ent;
|
---|
1104 |
|
---|
1105 | ent = (mtab_ent_t *) malloc(sizeof(mtab_ent_t));
|
---|
1106 | if (!ent)
|
---|
1107 | return;
|
---|
1108 |
|
---|
1109 | link_initialize(&ent->link);
|
---|
1110 | str_cpy(ent->mp, sizeof(ent->mp), path);
|
---|
1111 | ent->service_id = stat->service_id;
|
---|
1112 |
|
---|
1113 | struct statfs stfs;
|
---|
1114 | if (statfs(path, &stfs) == EOK)
|
---|
1115 | str_cpy(ent->fs_name, sizeof(ent->fs_name), stfs.fs_name);
|
---|
1116 | else
|
---|
1117 | str_cpy(ent->fs_name, sizeof(ent->fs_name), "?");
|
---|
1118 |
|
---|
1119 | list_append(&ent->link, mtab_list);
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | static int vfs_get_mtab_visit(const char *path, list_t *mtab_list,
|
---|
1123 | fs_handle_t fs_handle, service_id_t service_id)
|
---|
1124 | {
|
---|
1125 | DIR *dir;
|
---|
1126 | struct dirent *dirent;
|
---|
1127 |
|
---|
1128 | dir = opendir(path);
|
---|
1129 | if (!dir)
|
---|
1130 | return ENOENT;
|
---|
1131 |
|
---|
1132 | while ((dirent = readdir(dir)) != NULL) {
|
---|
1133 | char *child;
|
---|
1134 | struct stat st;
|
---|
1135 | int rc;
|
---|
1136 |
|
---|
1137 | rc = asprintf(&child, "%s/%s", path, dirent->d_name);
|
---|
1138 | if (rc < 0) {
|
---|
1139 | closedir(dir);
|
---|
1140 | return rc;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | char *pa = vfs_absolutize(child, NULL);
|
---|
1144 | if (!pa) {
|
---|
1145 | free(child);
|
---|
1146 | closedir(dir);
|
---|
1147 | return ENOMEM;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | free(child);
|
---|
1151 | child = pa;
|
---|
1152 |
|
---|
1153 | rc = stat(child, &st);
|
---|
1154 | if (rc != 0) {
|
---|
1155 | free(child);
|
---|
1156 | closedir(dir);
|
---|
1157 | return rc;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | if (st.fs_handle != fs_handle || st.service_id != service_id) {
|
---|
1161 | /*
|
---|
1162 | * We have discovered a mountpoint.
|
---|
1163 | */
|
---|
1164 | process_mp(child, &st, mtab_list);
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | if (st.is_directory) {
|
---|
1168 | (void) vfs_get_mtab_visit(child, mtab_list,
|
---|
1169 | st.fs_handle, st.service_id);
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | free(child);
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | closedir(dir);
|
---|
1176 | return EOK;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | int vfs_get_mtab_list(list_t *mtab_list)
|
---|
1180 | {
|
---|
1181 | struct stat st;
|
---|
1182 |
|
---|
1183 | int rc = stat("/", &st);
|
---|
1184 | if (rc != 0)
|
---|
1185 | return rc;
|
---|
1186 |
|
---|
1187 | process_mp("/", &st, mtab_list);
|
---|
1188 |
|
---|
1189 | return vfs_get_mtab_visit("/", mtab_list, st.fs_handle, st.service_id);
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /** Get filesystem statistics.
|
---|
1193 | *
|
---|
1194 | * @param path Mount point path
|
---|
1195 | * @param st Buffer for storing information
|
---|
1196 | * @return 0 on success. On error -1 is returned and errno is set.
|
---|
1197 | */
|
---|
1198 | int statfs(const char *path, struct statfs *st)
|
---|
1199 | {
|
---|
1200 | int fd = vfs_lookup(path, 0);
|
---|
1201 | if (fd < 0) {
|
---|
1202 | errno = fd;
|
---|
1203 | return -1;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | sysarg_t rc, ret;
|
---|
1207 | aid_t req;
|
---|
1208 |
|
---|
1209 | async_exch_t *exch = vfs_exchange_begin();
|
---|
1210 |
|
---|
1211 | req = async_send_1(exch, VFS_IN_STATFS, fd, NULL);
|
---|
1212 | rc = async_data_read_start(exch, (void *) st, sizeof(*st));
|
---|
1213 |
|
---|
1214 | vfs_exchange_end(exch);
|
---|
1215 | async_wait_for(req, &ret);
|
---|
1216 | close(fd);
|
---|
1217 |
|
---|
1218 | rc = (ret != EOK ? ret : rc);
|
---|
1219 | if (rc != EOK) {
|
---|
1220 | errno = rc;
|
---|
1221 | return -1;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | return 0;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | int vfs_pass_handle(async_exch_t *vfs_exch, int file, async_exch_t *exch)
|
---|
1228 | {
|
---|
1229 | return async_state_change_start(exch, VFS_PASS_HANDLE, (sysarg_t) file,
|
---|
1230 | 0, vfs_exch);
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | int vfs_receive_handle(bool high_descriptor)
|
---|
1234 | {
|
---|
1235 | ipc_callid_t callid;
|
---|
1236 | if (!async_state_change_receive(&callid, NULL, NULL, NULL)) {
|
---|
1237 | async_answer_0(callid, EINVAL);
|
---|
1238 | return EINVAL;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | async_exch_t *vfs_exch = vfs_exchange_begin();
|
---|
1242 |
|
---|
1243 | async_state_change_finalize(callid, vfs_exch);
|
---|
1244 |
|
---|
1245 | sysarg_t ret;
|
---|
1246 | sysarg_t rc = async_req_1_1(vfs_exch, VFS_IN_WAIT_HANDLE,
|
---|
1247 | high_descriptor, &ret);
|
---|
1248 |
|
---|
1249 | async_exchange_end(vfs_exch);
|
---|
1250 |
|
---|
1251 | if (rc != EOK)
|
---|
1252 | return rc;
|
---|
1253 | return ret;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | int vfs_clone(int file, bool high_descriptor)
|
---|
1257 | {
|
---|
1258 | async_exch_t *vfs_exch = vfs_exchange_begin();
|
---|
1259 | int rc = async_req_2_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file,
|
---|
1260 | (sysarg_t) high_descriptor);
|
---|
1261 | vfs_exchange_end(vfs_exch);
|
---|
1262 | return rc;
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | /** @}
|
---|
1266 | */
|
---|