source: mainline/uspace/lib/c/generic/vfs/vfs.c@ 1dff985

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

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5 up to revision 1926

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