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

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

Set errno and return -1 for standards-alluding interfaces

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