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