source: mainline/uspace/lib/c/generic/vfs/vfs.c@ 354b642

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 354b642 was 354b642, checked in by Jakub Jermar <jakub@…>, 8 years ago

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5/ revisions 1932-1936

Original commit messages:

1936: Jiri Zarevucky 2013-08-05 Modifications to vfs_rdwr.
1935: Jiri Zarevucky 2013-08-05 Fix a bug in read/write.
1934: Jiri Zarevucky 2013-08-05 Fix a hidden bug in handle passing.
1933: Jiri Zarevucky 2013-08-05 Add VFS_IN_CLONE.
1932: Jiri Zarevucky 2013-08-05 Add functions for passing handles around.

Modifications:

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