source: mainline/uspace/lib/c/generic/vfs/vfs.c@ cc574511

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc574511 was 86ffa27f, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Merge mainline changes.

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