source: mainline/uspace/lib/c/generic/vfs/vfs.c@ 089385e8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 089385e8 was 089385e8, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Change to previous directory with `cd -' in bdsh

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