source: mainline/uspace/lib/c/generic/vfs/vfs.c@ 0ed9cb6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0ed9cb6 was 0ed9cb6, checked in by Ji?? Z?rev?cky <zarevucky.jiri@…>, 12 years ago

Implement stat() using walk() and fstat().

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