1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <vfs/canonify.h>
|
---|
36 | #include <vfs/vfs.h>
|
---|
37 | #include <vfs/vfs_sess.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <unistd.h>
|
---|
41 | #include <dirent.h>
|
---|
42 | #include <fcntl.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <sys/stat.h>
|
---|
45 | #include <sys/types.h>
|
---|
46 | #include <ipc/services.h>
|
---|
47 | #include <ns.h>
|
---|
48 | #include <async.h>
|
---|
49 | #include <fibril_synch.h>
|
---|
50 | #include <errno.h>
|
---|
51 | #include <assert.h>
|
---|
52 | #include <str.h>
|
---|
53 | #include <loc.h>
|
---|
54 | #include <ipc/vfs.h>
|
---|
55 | #include <ipc/loc.h>
|
---|
56 |
|
---|
57 | static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
|
---|
58 | static async_sess_t *vfs_sess = NULL;
|
---|
59 |
|
---|
60 | static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
|
---|
61 |
|
---|
62 | static int cwd_fd = -1;
|
---|
63 | static char *cwd_path = NULL;
|
---|
64 | static size_t cwd_size = 0;
|
---|
65 |
|
---|
66 | /** Start an async exchange on the VFS session.
|
---|
67 | *
|
---|
68 | * @return New exchange.
|
---|
69 | *
|
---|
70 | */
|
---|
71 | 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 | */
|
---|
89 | void vfs_exchange_end(async_exch_t *exch)
|
---|
90 | {
|
---|
91 | async_exchange_end(exch);
|
---|
92 | }
|
---|
93 |
|
---|
94 | char *absolutize(const char *path, size_t *retlen)
|
---|
95 | {
|
---|
96 | char *ncwd_path;
|
---|
97 | char *ncwd_path_nc;
|
---|
98 |
|
---|
99 | fibril_mutex_lock(&cwd_mutex);
|
---|
100 | size_t size = str_size(path);
|
---|
101 | if (*path != '/') {
|
---|
102 | if (!cwd_path) {
|
---|
103 | fibril_mutex_unlock(&cwd_mutex);
|
---|
104 | return NULL;
|
---|
105 | }
|
---|
106 | ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
|
---|
107 | if (!ncwd_path_nc) {
|
---|
108 | fibril_mutex_unlock(&cwd_mutex);
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 | str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
|
---|
112 | ncwd_path_nc[cwd_size] = '/';
|
---|
113 | ncwd_path_nc[cwd_size + 1] = '\0';
|
---|
114 | } else {
|
---|
115 | ncwd_path_nc = malloc(size + 1);
|
---|
116 | if (!ncwd_path_nc) {
|
---|
117 | fibril_mutex_unlock(&cwd_mutex);
|
---|
118 | return NULL;
|
---|
119 | }
|
---|
120 | ncwd_path_nc[0] = '\0';
|
---|
121 | }
|
---|
122 | str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
|
---|
123 | ncwd_path = canonify(ncwd_path_nc, retlen);
|
---|
124 | if (!ncwd_path) {
|
---|
125 | fibril_mutex_unlock(&cwd_mutex);
|
---|
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 | */
|
---|
134 | ncwd_path = str_dup(ncwd_path);
|
---|
135 | free(ncwd_path_nc);
|
---|
136 | if (!ncwd_path) {
|
---|
137 | fibril_mutex_unlock(&cwd_mutex);
|
---|
138 | return NULL;
|
---|
139 | }
|
---|
140 | fibril_mutex_unlock(&cwd_mutex);
|
---|
141 | return ncwd_path;
|
---|
142 | }
|
---|
143 |
|
---|
144 | int mount(const char *fs_name, const char *mp, const char *fqsn,
|
---|
145 | const char *opts, unsigned int flags)
|
---|
146 | {
|
---|
147 | int null_id = -1;
|
---|
148 | char null[LOC_NAME_MAXLEN];
|
---|
149 |
|
---|
150 | if (str_cmp(fqsn, "") == 0) {
|
---|
151 | /* No device specified, create a fresh
|
---|
152 | null/%d device instead */
|
---|
153 | null_id = loc_null_create();
|
---|
154 |
|
---|
155 | if (null_id == -1)
|
---|
156 | return ENOMEM;
|
---|
157 |
|
---|
158 | snprintf(null, LOC_NAME_MAXLEN, "null/%d", null_id);
|
---|
159 | fqsn = null;
|
---|
160 | }
|
---|
161 |
|
---|
162 | service_id_t service_id;
|
---|
163 | int res = loc_service_get_id(fqsn, &service_id, flags);
|
---|
164 | if (res != EOK) {
|
---|
165 | if (null_id != -1)
|
---|
166 | loc_null_destroy(null_id);
|
---|
167 |
|
---|
168 | return res;
|
---|
169 | }
|
---|
170 |
|
---|
171 | size_t mpa_size;
|
---|
172 | char *mpa = absolutize(mp, &mpa_size);
|
---|
173 | if (!mpa) {
|
---|
174 | if (null_id != -1)
|
---|
175 | loc_null_destroy(null_id);
|
---|
176 |
|
---|
177 | return ENOMEM;
|
---|
178 | }
|
---|
179 |
|
---|
180 | async_exch_t *exch = vfs_exchange_begin();
|
---|
181 |
|
---|
182 | sysarg_t rc_orig;
|
---|
183 | aid_t req = async_send_2(exch, VFS_IN_MOUNT, service_id, flags, NULL);
|
---|
184 | sysarg_t rc = async_data_write_start(exch, (void *) mpa, mpa_size);
|
---|
185 | if (rc != EOK) {
|
---|
186 | vfs_exchange_end(exch);
|
---|
187 | free(mpa);
|
---|
188 | async_wait_for(req, &rc_orig);
|
---|
189 |
|
---|
190 | if (null_id != -1)
|
---|
191 | loc_null_destroy(null_id);
|
---|
192 |
|
---|
193 | if (rc_orig == EOK)
|
---|
194 | return (int) rc;
|
---|
195 | else
|
---|
196 | return (int) rc_orig;
|
---|
197 | }
|
---|
198 |
|
---|
199 | rc = async_data_write_start(exch, (void *) opts, str_size(opts));
|
---|
200 | if (rc != EOK) {
|
---|
201 | vfs_exchange_end(exch);
|
---|
202 | free(mpa);
|
---|
203 | async_wait_for(req, &rc_orig);
|
---|
204 |
|
---|
205 | if (null_id != -1)
|
---|
206 | loc_null_destroy(null_id);
|
---|
207 |
|
---|
208 | if (rc_orig == EOK)
|
---|
209 | return (int) rc;
|
---|
210 | else
|
---|
211 | return (int) rc_orig;
|
---|
212 | }
|
---|
213 |
|
---|
214 | rc = async_data_write_start(exch, (void *) fs_name, str_size(fs_name));
|
---|
215 | if (rc != EOK) {
|
---|
216 | vfs_exchange_end(exch);
|
---|
217 | free(mpa);
|
---|
218 | async_wait_for(req, &rc_orig);
|
---|
219 |
|
---|
220 | if (null_id != -1)
|
---|
221 | loc_null_destroy(null_id);
|
---|
222 |
|
---|
223 | if (rc_orig == EOK)
|
---|
224 | return (int) rc;
|
---|
225 | else
|
---|
226 | return (int) rc_orig;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /* Ask VFS whether it likes fs_name. */
|
---|
230 | rc = async_req_0_0(exch, VFS_IN_PING);
|
---|
231 | if (rc != EOK) {
|
---|
232 | vfs_exchange_end(exch);
|
---|
233 | free(mpa);
|
---|
234 | async_wait_for(req, &rc_orig);
|
---|
235 |
|
---|
236 | if (null_id != -1)
|
---|
237 | loc_null_destroy(null_id);
|
---|
238 |
|
---|
239 | if (rc_orig == EOK)
|
---|
240 | return (int) rc;
|
---|
241 | else
|
---|
242 | return (int) rc_orig;
|
---|
243 | }
|
---|
244 |
|
---|
245 | vfs_exchange_end(exch);
|
---|
246 | free(mpa);
|
---|
247 | async_wait_for(req, &rc);
|
---|
248 |
|
---|
249 | if ((rc != EOK) && (null_id != -1))
|
---|
250 | loc_null_destroy(null_id);
|
---|
251 |
|
---|
252 | return (int) rc;
|
---|
253 | }
|
---|
254 |
|
---|
255 | int unmount(const char *mp)
|
---|
256 | {
|
---|
257 | sysarg_t rc;
|
---|
258 | sysarg_t rc_orig;
|
---|
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 |
|
---|
267 | async_exch_t *exch = vfs_exchange_begin();
|
---|
268 |
|
---|
269 | req = async_send_0(exch, VFS_IN_UNMOUNT, NULL);
|
---|
270 | rc = async_data_write_start(exch, (void *) mpa, mpa_size);
|
---|
271 | if (rc != EOK) {
|
---|
272 | vfs_exchange_end(exch);
|
---|
273 | free(mpa);
|
---|
274 | async_wait_for(req, &rc_orig);
|
---|
275 | if (rc_orig == EOK)
|
---|
276 | return (int) rc;
|
---|
277 | else
|
---|
278 | return (int) rc_orig;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | vfs_exchange_end(exch);
|
---|
283 | free(mpa);
|
---|
284 | async_wait_for(req, &rc);
|
---|
285 |
|
---|
286 | return (int) rc;
|
---|
287 | }
|
---|
288 |
|
---|
289 | static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag)
|
---|
290 | {
|
---|
291 | async_exch_t *exch = vfs_exchange_begin();
|
---|
292 |
|
---|
293 | ipc_call_t answer;
|
---|
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);
|
---|
296 |
|
---|
297 | if (rc != EOK) {
|
---|
298 | vfs_exchange_end(exch);
|
---|
299 |
|
---|
300 | sysarg_t rc_orig;
|
---|
301 | async_wait_for(req, &rc_orig);
|
---|
302 |
|
---|
303 | if (rc_orig == EOK)
|
---|
304 | return (int) rc;
|
---|
305 | else
|
---|
306 | return (int) rc_orig;
|
---|
307 | }
|
---|
308 |
|
---|
309 | vfs_exchange_end(exch);
|
---|
310 | async_wait_for(req, &rc);
|
---|
311 |
|
---|
312 | if (rc != EOK)
|
---|
313 | return (int) rc;
|
---|
314 |
|
---|
315 | return (int) IPC_GET_ARG1(answer);
|
---|
316 | }
|
---|
317 |
|
---|
318 | int open(const char *path, int oflag, ...)
|
---|
319 | {
|
---|
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;
|
---|
329 | }
|
---|
330 |
|
---|
331 | int close(int fildes)
|
---|
332 | {
|
---|
333 | sysarg_t rc;
|
---|
334 |
|
---|
335 | async_exch_t *exch = vfs_exchange_begin();
|
---|
336 | rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes);
|
---|
337 | vfs_exchange_end(exch);
|
---|
338 |
|
---|
339 | return (int) rc;
|
---|
340 | }
|
---|
341 |
|
---|
342 | ssize_t read(int fildes, void *buf, size_t nbyte)
|
---|
343 | {
|
---|
344 | sysarg_t rc;
|
---|
345 | ipc_call_t answer;
|
---|
346 | aid_t req;
|
---|
347 |
|
---|
348 | async_exch_t *exch = vfs_exchange_begin();
|
---|
349 |
|
---|
350 | req = async_send_1(exch, VFS_IN_READ, fildes, &answer);
|
---|
351 | rc = async_data_read_start(exch, (void *)buf, nbyte);
|
---|
352 | if (rc != EOK) {
|
---|
353 | vfs_exchange_end(exch);
|
---|
354 |
|
---|
355 | sysarg_t rc_orig;
|
---|
356 | async_wait_for(req, &rc_orig);
|
---|
357 |
|
---|
358 | if (rc_orig == EOK)
|
---|
359 | return (ssize_t) rc;
|
---|
360 | else
|
---|
361 | return (ssize_t) rc_orig;
|
---|
362 | }
|
---|
363 | vfs_exchange_end(exch);
|
---|
364 | async_wait_for(req, &rc);
|
---|
365 | if (rc == EOK)
|
---|
366 | return (ssize_t) IPC_GET_ARG1(answer);
|
---|
367 | else
|
---|
368 | return rc;
|
---|
369 | }
|
---|
370 |
|
---|
371 | ssize_t write(int fildes, const void *buf, size_t nbyte)
|
---|
372 | {
|
---|
373 | sysarg_t rc;
|
---|
374 | ipc_call_t answer;
|
---|
375 | aid_t req;
|
---|
376 |
|
---|
377 | async_exch_t *exch = vfs_exchange_begin();
|
---|
378 |
|
---|
379 | req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer);
|
---|
380 | rc = async_data_write_start(exch, (void *)buf, nbyte);
|
---|
381 | if (rc != EOK) {
|
---|
382 | vfs_exchange_end(exch);
|
---|
383 |
|
---|
384 | sysarg_t rc_orig;
|
---|
385 | async_wait_for(req, &rc_orig);
|
---|
386 |
|
---|
387 | if (rc_orig == EOK)
|
---|
388 | return (ssize_t) rc;
|
---|
389 | else
|
---|
390 | return (ssize_t) rc_orig;
|
---|
391 | }
|
---|
392 | vfs_exchange_end(exch);
|
---|
393 | async_wait_for(req, &rc);
|
---|
394 | if (rc == EOK)
|
---|
395 | return (ssize_t) IPC_GET_ARG1(answer);
|
---|
396 | else
|
---|
397 | return -1;
|
---|
398 | }
|
---|
399 |
|
---|
400 | /** Read entire buffer.
|
---|
401 | *
|
---|
402 | * In face of short reads this function continues reading until either
|
---|
403 | * the entire buffer is read or no more data is available (at end of file).
|
---|
404 | *
|
---|
405 | * @param fildes File descriptor
|
---|
406 | * @param buf Buffer, @a nbytes bytes long
|
---|
407 | * @param nbytes Number of bytes to read
|
---|
408 | *
|
---|
409 | * @return On success, positive number of bytes read.
|
---|
410 | * On failure, negative error code from read().
|
---|
411 | */
|
---|
412 | ssize_t read_all(int fildes, void *buf, size_t nbyte)
|
---|
413 | {
|
---|
414 | ssize_t cnt = 0;
|
---|
415 | size_t nread = 0;
|
---|
416 | uint8_t *bp = (uint8_t *) buf;
|
---|
417 |
|
---|
418 | do {
|
---|
419 | bp += cnt;
|
---|
420 | nread += cnt;
|
---|
421 | cnt = read(fildes, bp, nbyte - nread);
|
---|
422 | } while (cnt > 0 && (nbyte - nread - cnt) > 0);
|
---|
423 |
|
---|
424 | if (cnt < 0)
|
---|
425 | return cnt;
|
---|
426 |
|
---|
427 | return nread + cnt;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /** Write entire buffer.
|
---|
431 | *
|
---|
432 | * This function fails if it cannot write exactly @a len bytes to the file.
|
---|
433 | *
|
---|
434 | * @param fildes File descriptor
|
---|
435 | * @param buf Data, @a nbytes bytes long
|
---|
436 | * @param nbytes Number of bytes to write
|
---|
437 | *
|
---|
438 | * @return EOK on error, return value from write() if writing
|
---|
439 | * failed.
|
---|
440 | */
|
---|
441 | ssize_t write_all(int fildes, const void *buf, size_t nbyte)
|
---|
442 | {
|
---|
443 | ssize_t cnt = 0;
|
---|
444 | ssize_t nwritten = 0;
|
---|
445 | const uint8_t *bp = (uint8_t *) buf;
|
---|
446 |
|
---|
447 | do {
|
---|
448 | bp += cnt;
|
---|
449 | nwritten += cnt;
|
---|
450 | cnt = write(fildes, bp, nbyte - nwritten);
|
---|
451 | } while (cnt > 0 && ((ssize_t )nbyte - nwritten - cnt) > 0);
|
---|
452 |
|
---|
453 | if (cnt < 0)
|
---|
454 | return cnt;
|
---|
455 |
|
---|
456 | if ((ssize_t)nbyte - nwritten - cnt > 0)
|
---|
457 | return EIO;
|
---|
458 |
|
---|
459 | return nbyte;
|
---|
460 | }
|
---|
461 |
|
---|
462 | int fsync(int fildes)
|
---|
463 | {
|
---|
464 | async_exch_t *exch = vfs_exchange_begin();
|
---|
465 | sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes);
|
---|
466 | vfs_exchange_end(exch);
|
---|
467 |
|
---|
468 | return (int) rc;
|
---|
469 | }
|
---|
470 |
|
---|
471 | off64_t lseek(int fildes, off64_t offset, int whence)
|
---|
472 | {
|
---|
473 | async_exch_t *exch = vfs_exchange_begin();
|
---|
474 |
|
---|
475 | sysarg_t newoff_lo;
|
---|
476 | sysarg_t newoff_hi;
|
---|
477 | sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes,
|
---|
478 | LOWER32(offset), UPPER32(offset), whence,
|
---|
479 | &newoff_lo, &newoff_hi);
|
---|
480 |
|
---|
481 | vfs_exchange_end(exch);
|
---|
482 |
|
---|
483 | if (rc != EOK)
|
---|
484 | return (off64_t) -1;
|
---|
485 |
|
---|
486 | return (off64_t) MERGE_LOUP32(newoff_lo, newoff_hi);
|
---|
487 | }
|
---|
488 |
|
---|
489 | int ftruncate(int fildes, aoff64_t length)
|
---|
490 | {
|
---|
491 | sysarg_t rc;
|
---|
492 |
|
---|
493 | async_exch_t *exch = vfs_exchange_begin();
|
---|
494 | rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes,
|
---|
495 | LOWER32(length), UPPER32(length));
|
---|
496 | vfs_exchange_end(exch);
|
---|
497 |
|
---|
498 | return (int) rc;
|
---|
499 | }
|
---|
500 |
|
---|
501 | int fstat(int fildes, struct stat *stat)
|
---|
502 | {
|
---|
503 | sysarg_t rc;
|
---|
504 | aid_t req;
|
---|
505 |
|
---|
506 | async_exch_t *exch = vfs_exchange_begin();
|
---|
507 |
|
---|
508 | req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL);
|
---|
509 | rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
|
---|
510 | if (rc != EOK) {
|
---|
511 | vfs_exchange_end(exch);
|
---|
512 |
|
---|
513 | sysarg_t rc_orig;
|
---|
514 | async_wait_for(req, &rc_orig);
|
---|
515 |
|
---|
516 | if (rc_orig == EOK)
|
---|
517 | return (ssize_t) rc;
|
---|
518 | else
|
---|
519 | return (ssize_t) rc_orig;
|
---|
520 | }
|
---|
521 | vfs_exchange_end(exch);
|
---|
522 | async_wait_for(req, &rc);
|
---|
523 |
|
---|
524 | return rc;
|
---|
525 | }
|
---|
526 |
|
---|
527 | int stat(const char *path, struct stat *stat)
|
---|
528 | {
|
---|
529 | sysarg_t rc;
|
---|
530 | sysarg_t rc_orig;
|
---|
531 | aid_t req;
|
---|
532 |
|
---|
533 | size_t pa_size;
|
---|
534 | char *pa = absolutize(path, &pa_size);
|
---|
535 | if (!pa)
|
---|
536 | return ENOMEM;
|
---|
537 |
|
---|
538 | async_exch_t *exch = vfs_exchange_begin();
|
---|
539 |
|
---|
540 | req = async_send_0(exch, VFS_IN_STAT, NULL);
|
---|
541 | rc = async_data_write_start(exch, pa, pa_size);
|
---|
542 | if (rc != EOK) {
|
---|
543 | vfs_exchange_end(exch);
|
---|
544 | free(pa);
|
---|
545 | async_wait_for(req, &rc_orig);
|
---|
546 | if (rc_orig == EOK)
|
---|
547 | return (int) rc;
|
---|
548 | else
|
---|
549 | return (int) rc_orig;
|
---|
550 | }
|
---|
551 | rc = async_data_read_start(exch, stat, sizeof(struct stat));
|
---|
552 | if (rc != EOK) {
|
---|
553 | vfs_exchange_end(exch);
|
---|
554 | free(pa);
|
---|
555 | async_wait_for(req, &rc_orig);
|
---|
556 | if (rc_orig == EOK)
|
---|
557 | return (int) rc;
|
---|
558 | else
|
---|
559 | return (int) rc_orig;
|
---|
560 | }
|
---|
561 | vfs_exchange_end(exch);
|
---|
562 | free(pa);
|
---|
563 | async_wait_for(req, &rc);
|
---|
564 | return rc;
|
---|
565 | }
|
---|
566 |
|
---|
567 | DIR *opendir(const char *dirname)
|
---|
568 | {
|
---|
569 | DIR *dirp = malloc(sizeof(DIR));
|
---|
570 | if (!dirp)
|
---|
571 | return NULL;
|
---|
572 |
|
---|
573 | size_t abs_size;
|
---|
574 | char *abs = absolutize(dirname, &abs_size);
|
---|
575 | if (!abs) {
|
---|
576 | free(dirp);
|
---|
577 | return NULL;
|
---|
578 | }
|
---|
579 |
|
---|
580 | int ret = open_internal(abs, abs_size, L_DIRECTORY, 0);
|
---|
581 | free(abs);
|
---|
582 |
|
---|
583 | if (ret < 0) {
|
---|
584 | free(dirp);
|
---|
585 | return NULL;
|
---|
586 | }
|
---|
587 |
|
---|
588 | dirp->fd = ret;
|
---|
589 | return dirp;
|
---|
590 | }
|
---|
591 |
|
---|
592 | struct dirent *readdir(DIR *dirp)
|
---|
593 | {
|
---|
594 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
|
---|
595 | if (len <= 0)
|
---|
596 | return NULL;
|
---|
597 | return &dirp->res;
|
---|
598 | }
|
---|
599 |
|
---|
600 | void rewinddir(DIR *dirp)
|
---|
601 | {
|
---|
602 | (void) lseek(dirp->fd, 0, SEEK_SET);
|
---|
603 | }
|
---|
604 |
|
---|
605 | int closedir(DIR *dirp)
|
---|
606 | {
|
---|
607 | (void) close(dirp->fd);
|
---|
608 | free(dirp);
|
---|
609 | return 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 | int mkdir(const char *path, mode_t mode)
|
---|
613 | {
|
---|
614 | sysarg_t rc;
|
---|
615 | aid_t req;
|
---|
616 |
|
---|
617 | size_t pa_size;
|
---|
618 | char *pa = absolutize(path, &pa_size);
|
---|
619 | if (!pa)
|
---|
620 | return ENOMEM;
|
---|
621 |
|
---|
622 | async_exch_t *exch = vfs_exchange_begin();
|
---|
623 |
|
---|
624 | req = async_send_1(exch, VFS_IN_MKDIR, mode, NULL);
|
---|
625 | rc = async_data_write_start(exch, pa, pa_size);
|
---|
626 | if (rc != EOK) {
|
---|
627 | vfs_exchange_end(exch);
|
---|
628 | free(pa);
|
---|
629 |
|
---|
630 | sysarg_t rc_orig;
|
---|
631 | async_wait_for(req, &rc_orig);
|
---|
632 |
|
---|
633 | if (rc_orig == EOK)
|
---|
634 | return (int) rc;
|
---|
635 | else
|
---|
636 | return (int) rc_orig;
|
---|
637 | }
|
---|
638 | vfs_exchange_end(exch);
|
---|
639 | free(pa);
|
---|
640 | async_wait_for(req, &rc);
|
---|
641 | return rc;
|
---|
642 | }
|
---|
643 |
|
---|
644 | static int _unlink(const char *path, int lflag)
|
---|
645 | {
|
---|
646 | sysarg_t rc;
|
---|
647 | aid_t req;
|
---|
648 |
|
---|
649 | size_t pa_size;
|
---|
650 | char *pa = absolutize(path, &pa_size);
|
---|
651 | if (!pa)
|
---|
652 | return ENOMEM;
|
---|
653 |
|
---|
654 | async_exch_t *exch = vfs_exchange_begin();
|
---|
655 |
|
---|
656 | req = async_send_1(exch, VFS_IN_UNLINK, lflag, NULL);
|
---|
657 | rc = async_data_write_start(exch, pa, pa_size);
|
---|
658 | if (rc != EOK) {
|
---|
659 | vfs_exchange_end(exch);
|
---|
660 | free(pa);
|
---|
661 |
|
---|
662 | sysarg_t rc_orig;
|
---|
663 | async_wait_for(req, &rc_orig);
|
---|
664 |
|
---|
665 | if (rc_orig == EOK)
|
---|
666 | return (int) rc;
|
---|
667 | else
|
---|
668 | return (int) rc_orig;
|
---|
669 | }
|
---|
670 | vfs_exchange_end(exch);
|
---|
671 | free(pa);
|
---|
672 | async_wait_for(req, &rc);
|
---|
673 | return rc;
|
---|
674 | }
|
---|
675 |
|
---|
676 | int unlink(const char *path)
|
---|
677 | {
|
---|
678 | return _unlink(path, L_NONE);
|
---|
679 | }
|
---|
680 |
|
---|
681 | int rmdir(const char *path)
|
---|
682 | {
|
---|
683 | return _unlink(path, L_DIRECTORY);
|
---|
684 | }
|
---|
685 |
|
---|
686 | int rename(const char *old, const char *new)
|
---|
687 | {
|
---|
688 | sysarg_t rc;
|
---|
689 | sysarg_t rc_orig;
|
---|
690 | aid_t req;
|
---|
691 |
|
---|
692 | size_t olda_size;
|
---|
693 | char *olda = absolutize(old, &olda_size);
|
---|
694 | if (!olda)
|
---|
695 | return ENOMEM;
|
---|
696 |
|
---|
697 | size_t newa_size;
|
---|
698 | char *newa = absolutize(new, &newa_size);
|
---|
699 | if (!newa) {
|
---|
700 | free(olda);
|
---|
701 | return ENOMEM;
|
---|
702 | }
|
---|
703 |
|
---|
704 | async_exch_t *exch = vfs_exchange_begin();
|
---|
705 |
|
---|
706 | req = async_send_0(exch, VFS_IN_RENAME, NULL);
|
---|
707 | rc = async_data_write_start(exch, olda, olda_size);
|
---|
708 | if (rc != EOK) {
|
---|
709 | vfs_exchange_end(exch);
|
---|
710 | free(olda);
|
---|
711 | free(newa);
|
---|
712 | async_wait_for(req, &rc_orig);
|
---|
713 | if (rc_orig == EOK)
|
---|
714 | return (int) rc;
|
---|
715 | else
|
---|
716 | return (int) rc_orig;
|
---|
717 | }
|
---|
718 | rc = async_data_write_start(exch, newa, newa_size);
|
---|
719 | if (rc != EOK) {
|
---|
720 | vfs_exchange_end(exch);
|
---|
721 | free(olda);
|
---|
722 | free(newa);
|
---|
723 | async_wait_for(req, &rc_orig);
|
---|
724 | if (rc_orig == EOK)
|
---|
725 | return (int) rc;
|
---|
726 | else
|
---|
727 | return (int) rc_orig;
|
---|
728 | }
|
---|
729 | vfs_exchange_end(exch);
|
---|
730 | free(olda);
|
---|
731 | free(newa);
|
---|
732 | async_wait_for(req, &rc);
|
---|
733 | return rc;
|
---|
734 | }
|
---|
735 |
|
---|
736 | int chdir(const char *path)
|
---|
737 | {
|
---|
738 | size_t abs_size;
|
---|
739 | char *abs = absolutize(path, &abs_size);
|
---|
740 | if (!abs)
|
---|
741 | return ENOMEM;
|
---|
742 |
|
---|
743 | int fd = open_internal(abs, abs_size, L_DIRECTORY, O_DESC);
|
---|
744 |
|
---|
745 | if (fd < 0) {
|
---|
746 | free(abs);
|
---|
747 | return ENOENT;
|
---|
748 | }
|
---|
749 |
|
---|
750 | fibril_mutex_lock(&cwd_mutex);
|
---|
751 |
|
---|
752 | if (cwd_fd >= 0)
|
---|
753 | close(cwd_fd);
|
---|
754 |
|
---|
755 |
|
---|
756 | if (cwd_path)
|
---|
757 | free(cwd_path);
|
---|
758 |
|
---|
759 | cwd_fd = fd;
|
---|
760 | cwd_path = abs;
|
---|
761 | cwd_size = abs_size;
|
---|
762 |
|
---|
763 | fibril_mutex_unlock(&cwd_mutex);
|
---|
764 | return EOK;
|
---|
765 | }
|
---|
766 |
|
---|
767 | char *getcwd(char *buf, size_t size)
|
---|
768 | {
|
---|
769 | if (size == 0)
|
---|
770 | return NULL;
|
---|
771 |
|
---|
772 | fibril_mutex_lock(&cwd_mutex);
|
---|
773 |
|
---|
774 | if ((cwd_size == 0) || (size < cwd_size + 1)) {
|
---|
775 | fibril_mutex_unlock(&cwd_mutex);
|
---|
776 | return NULL;
|
---|
777 | }
|
---|
778 |
|
---|
779 | str_cpy(buf, size, cwd_path);
|
---|
780 | fibril_mutex_unlock(&cwd_mutex);
|
---|
781 |
|
---|
782 | return buf;
|
---|
783 | }
|
---|
784 |
|
---|
785 | async_sess_t *fd_session(exch_mgmt_t mgmt, int fildes)
|
---|
786 | {
|
---|
787 | struct stat stat;
|
---|
788 | int rc = fstat(fildes, &stat);
|
---|
789 | if (rc != 0) {
|
---|
790 | errno = rc;
|
---|
791 | return NULL;
|
---|
792 | }
|
---|
793 |
|
---|
794 | if (!stat.service) {
|
---|
795 | errno = ENOENT;
|
---|
796 | return NULL;
|
---|
797 | }
|
---|
798 |
|
---|
799 | return loc_service_connect(mgmt, stat.service, 0);
|
---|
800 | }
|
---|
801 |
|
---|
802 | int dup2(int oldfd, int newfd)
|
---|
803 | {
|
---|
804 | async_exch_t *exch = vfs_exchange_begin();
|
---|
805 |
|
---|
806 | sysarg_t ret;
|
---|
807 | sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret);
|
---|
808 |
|
---|
809 | vfs_exchange_end(exch);
|
---|
810 |
|
---|
811 | if (rc == EOK)
|
---|
812 | return (int) ret;
|
---|
813 |
|
---|
814 | return (int) rc;
|
---|
815 | }
|
---|
816 |
|
---|
817 | int fd_wait(void)
|
---|
818 | {
|
---|
819 | async_exch_t *exch = vfs_exchange_begin();
|
---|
820 |
|
---|
821 | sysarg_t ret;
|
---|
822 | sysarg_t rc = async_req_0_1(exch, VFS_IN_WAIT_HANDLE, &ret);
|
---|
823 |
|
---|
824 | vfs_exchange_end(exch);
|
---|
825 |
|
---|
826 | if (rc == EOK)
|
---|
827 | return (int) ret;
|
---|
828 |
|
---|
829 | return (int) rc;
|
---|
830 | }
|
---|
831 |
|
---|
832 | /** @}
|
---|
833 | */
|
---|