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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0ed9cb6 was 0ed9cb6, checked in by Ji?? Z?rev?cky <zarevucky.jiri@…>, 12 years ago

Implement stat() using walk() and fstat().

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