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