[2f02aa17] | 1 | /*
|
---|
[222e57c] | 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/vfs.h>
|
---|
| 36 | #include <vfs/canonify.h>
|
---|
[d0dc74ae] | 37 | #include <stdlib.h>
|
---|
[449c246] | 38 | #include <unistd.h>
|
---|
[d0dc74ae] | 39 | #include <dirent.h>
|
---|
[449c246] | 40 | #include <fcntl.h>
|
---|
[923c39e] | 41 | #include <stdio.h>
|
---|
[852b801] | 42 | #include <sys/stat.h>
|
---|
[72bde81] | 43 | #include <sys/types.h>
|
---|
[2f02aa17] | 44 | #include <ipc/ipc.h>
|
---|
| 45 | #include <ipc/services.h>
|
---|
| 46 | #include <async.h>
|
---|
| 47 | #include <atomic.h>
|
---|
| 48 | #include <futex.h>
|
---|
| 49 | #include <errno.h>
|
---|
| 50 | #include <string.h>
|
---|
[1090b8c] | 51 | #include <devmap.h>
|
---|
[2595dab] | 52 | #include <ipc/vfs.h>
|
---|
| 53 | #include <ipc/devmap.h>
|
---|
[2f02aa17] | 54 |
|
---|
[2595dab] | 55 | static int vfs_phone = -1;
|
---|
| 56 | static futex_t vfs_phone_futex = FUTEX_INITIALIZER;
|
---|
| 57 | static futex_t cwd_futex = FUTEX_INITIALIZER;
|
---|
[5fec355] | 58 |
|
---|
[2b88074b] | 59 | static int cwd_fd = -1;
|
---|
| 60 | static char *cwd_path = NULL;
|
---|
| 61 | static size_t cwd_size = 0;
|
---|
[5fec355] | 62 |
|
---|
[17b2aac] | 63 | char *absolutize(const char *path, size_t *retlen)
|
---|
[5fec355] | 64 | {
|
---|
| 65 | char *ncwd_path;
|
---|
[34a74ab] | 66 | char *ncwd_path_nc;
|
---|
[5fec355] | 67 |
|
---|
| 68 | futex_down(&cwd_futex);
|
---|
[9eb3623] | 69 | size_t size = str_size(path);
|
---|
[5fec355] | 70 | if (*path != '/') {
|
---|
| 71 | if (!cwd_path) {
|
---|
| 72 | futex_up(&cwd_futex);
|
---|
| 73 | return NULL;
|
---|
| 74 | }
|
---|
[9eb3623] | 75 | ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
|
---|
[34a74ab] | 76 | if (!ncwd_path_nc) {
|
---|
[5fec355] | 77 | futex_up(&cwd_futex);
|
---|
| 78 | return NULL;
|
---|
| 79 | }
|
---|
[6eb2e96] | 80 | str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
|
---|
[9eb3623] | 81 | ncwd_path_nc[cwd_size] = '/';
|
---|
| 82 | ncwd_path_nc[cwd_size + 1] = '\0';
|
---|
[5fec355] | 83 | } else {
|
---|
[9eb3623] | 84 | ncwd_path_nc = malloc(size + 1);
|
---|
[34a74ab] | 85 | if (!ncwd_path_nc) {
|
---|
[5fec355] | 86 | futex_up(&cwd_futex);
|
---|
| 87 | return NULL;
|
---|
| 88 | }
|
---|
[34a74ab] | 89 | ncwd_path_nc[0] = '\0';
|
---|
[5fec355] | 90 | }
|
---|
[4482bc7] | 91 | str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
|
---|
[34a74ab] | 92 | ncwd_path = canonify(ncwd_path_nc, retlen);
|
---|
| 93 | if (!ncwd_path) {
|
---|
| 94 | futex_up(&cwd_futex);
|
---|
| 95 | free(ncwd_path_nc);
|
---|
| 96 | return NULL;
|
---|
| 97 | }
|
---|
| 98 | /*
|
---|
| 99 | * We need to clone ncwd_path because canonify() works in-place and thus
|
---|
| 100 | * the address in ncwd_path need not be the same as ncwd_path_nc, even
|
---|
| 101 | * though they both point into the same dynamically allocated buffer.
|
---|
| 102 | */
|
---|
[095003a8] | 103 | ncwd_path = str_dup(ncwd_path);
|
---|
[34a74ab] | 104 | free(ncwd_path_nc);
|
---|
| 105 | if (!ncwd_path) {
|
---|
[923c39e] | 106 | futex_up(&cwd_futex);
|
---|
| 107 | return NULL;
|
---|
| 108 | }
|
---|
[5fec355] | 109 | futex_up(&cwd_futex);
|
---|
| 110 | return ncwd_path;
|
---|
| 111 | }
|
---|
[2f02aa17] | 112 |
|
---|
[19b28b0] | 113 | static void vfs_connect(void)
|
---|
[2f02aa17] | 114 | {
|
---|
[19b28b0] | 115 | while (vfs_phone < 0)
|
---|
| 116 | vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0);
|
---|
[2f02aa17] | 117 | }
|
---|
| 118 |
|
---|
[19b28b0] | 119 | int mount(const char *fs_name, const char *mp, const char *dev,
|
---|
[1090b8c] | 120 | const char *opts, unsigned int flags)
|
---|
[2f02aa17] | 121 | {
|
---|
| 122 | int res;
|
---|
| 123 | ipcarg_t rc;
|
---|
[3734106] | 124 | ipcarg_t rc_orig;
|
---|
[2f02aa17] | 125 | aid_t req;
|
---|
[82405266] | 126 | dev_handle_t dev_handle;
|
---|
| 127 |
|
---|
[1090b8c] | 128 | res = devmap_device_get_handle(dev, &dev_handle, flags);
|
---|
[82405266] | 129 | if (res != EOK)
|
---|
| 130 | return res;
|
---|
| 131 |
|
---|
[9eb3623] | 132 | size_t mpa_size;
|
---|
| 133 | char *mpa = absolutize(mp, &mpa_size);
|
---|
[5fec355] | 134 | if (!mpa)
|
---|
| 135 | return ENOMEM;
|
---|
[19b28b0] | 136 |
|
---|
[2f02aa17] | 137 | futex_down(&vfs_phone_futex);
|
---|
| 138 | async_serialize_start();
|
---|
[19b28b0] | 139 | vfs_connect();
|
---|
| 140 |
|
---|
[4198f9c3] | 141 | req = async_send_2(vfs_phone, VFS_IN_MOUNT, dev_handle, flags, NULL);
|
---|
[0da4e41] | 142 | rc = async_data_write_start(vfs_phone, (void *) mpa, mpa_size);
|
---|
[12fc042] | 143 | if (rc != EOK) {
|
---|
[3734106] | 144 | async_wait_for(req, &rc_orig);
|
---|
[12fc042] | 145 | async_serialize_end();
|
---|
| 146 | futex_up(&vfs_phone_futex);
|
---|
| 147 | free(mpa);
|
---|
[3734106] | 148 | if (rc_orig == EOK)
|
---|
| 149 | return (int) rc;
|
---|
| 150 | else
|
---|
| 151 | return (int) rc_orig;
|
---|
[12fc042] | 152 | }
|
---|
[19b28b0] | 153 |
|
---|
[0da4e41] | 154 | rc = async_data_write_start(vfs_phone, (void *) opts, str_size(opts));
|
---|
[594303b] | 155 | if (rc != EOK) {
|
---|
[3734106] | 156 | async_wait_for(req, &rc_orig);
|
---|
[594303b] | 157 | async_serialize_end();
|
---|
| 158 | futex_up(&vfs_phone_futex);
|
---|
| 159 | free(mpa);
|
---|
[3734106] | 160 | if (rc_orig == EOK)
|
---|
| 161 | return (int) rc;
|
---|
| 162 | else
|
---|
| 163 | return (int) rc_orig;
|
---|
[594303b] | 164 | }
|
---|
| 165 |
|
---|
[0da4e41] | 166 | rc = async_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name));
|
---|
[2f02aa17] | 167 | if (rc != EOK) {
|
---|
[3734106] | 168 | async_wait_for(req, &rc_orig);
|
---|
[2f02aa17] | 169 | async_serialize_end();
|
---|
| 170 | futex_up(&vfs_phone_futex);
|
---|
[5fec355] | 171 | free(mpa);
|
---|
[3734106] | 172 | if (rc_orig == EOK)
|
---|
| 173 | return (int) rc;
|
---|
| 174 | else
|
---|
| 175 | return (int) rc_orig;
|
---|
[2f02aa17] | 176 | }
|
---|
[c08c355] | 177 |
|
---|
| 178 | /* Ask VFS whether it likes fs_name. */
|
---|
| 179 | rc = async_req_0_0(vfs_phone, IPC_M_PING);
|
---|
| 180 | if (rc != EOK) {
|
---|
[3734106] | 181 | async_wait_for(req, &rc_orig);
|
---|
[c08c355] | 182 | async_serialize_end();
|
---|
| 183 | futex_up(&vfs_phone_futex);
|
---|
| 184 | free(mpa);
|
---|
[3734106] | 185 | if (rc_orig == EOK)
|
---|
| 186 | return (int) rc;
|
---|
| 187 | else
|
---|
| 188 | return (int) rc_orig;
|
---|
[c08c355] | 189 | }
|
---|
[19b28b0] | 190 |
|
---|
[2f02aa17] | 191 | async_wait_for(req, &rc);
|
---|
| 192 | async_serialize_end();
|
---|
| 193 | futex_up(&vfs_phone_futex);
|
---|
[5fec355] | 194 | free(mpa);
|
---|
[19b28b0] | 195 |
|
---|
[2f02aa17] | 196 | return (int) rc;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[2b88074b] | 199 | static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag)
|
---|
[2f02aa17] | 200 | {
|
---|
| 201 | futex_down(&vfs_phone_futex);
|
---|
| 202 | async_serialize_start();
|
---|
[19b28b0] | 203 | vfs_connect();
|
---|
| 204 |
|
---|
[2b88074b] | 205 | ipc_call_t answer;
|
---|
| 206 | aid_t req = async_send_3(vfs_phone, VFS_IN_OPEN, lflag, oflag, 0, &answer);
|
---|
| 207 | ipcarg_t rc = async_data_write_start(vfs_phone, abs, abs_size);
|
---|
| 208 |
|
---|
[2f02aa17] | 209 | if (rc != EOK) {
|
---|
[3734106] | 210 | ipcarg_t rc_orig;
|
---|
| 211 | async_wait_for(req, &rc_orig);
|
---|
[2b88074b] | 212 |
|
---|
[2f02aa17] | 213 | async_serialize_end();
|
---|
| 214 | futex_up(&vfs_phone_futex);
|
---|
[2b88074b] | 215 |
|
---|
[3734106] | 216 | if (rc_orig == EOK)
|
---|
| 217 | return (int) rc;
|
---|
| 218 | else
|
---|
| 219 | return (int) rc_orig;
|
---|
[2f02aa17] | 220 | }
|
---|
[2b88074b] | 221 |
|
---|
[2f02aa17] | 222 | async_wait_for(req, &rc);
|
---|
| 223 | async_serialize_end();
|
---|
| 224 | futex_up(&vfs_phone_futex);
|
---|
[2595dab] | 225 |
|
---|
[57b4f46] | 226 | if (rc != EOK)
|
---|
| 227 | return (int) rc;
|
---|
[2595dab] | 228 |
|
---|
[2f02aa17] | 229 | return (int) IPC_GET_ARG1(answer);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[ae78b530] | 232 | int open(const char *path, int oflag, ...)
|
---|
| 233 | {
|
---|
[2b88074b] | 234 | size_t abs_size;
|
---|
| 235 | char *abs = absolutize(path, &abs_size);
|
---|
| 236 | if (!abs)
|
---|
| 237 | return ENOMEM;
|
---|
| 238 |
|
---|
| 239 | int ret = open_internal(abs, abs_size, L_FILE, oflag);
|
---|
| 240 | free(abs);
|
---|
| 241 |
|
---|
| 242 | return ret;
|
---|
[ae78b530] | 243 | }
|
---|
| 244 |
|
---|
[99272a3] | 245 | int open_node(fdi_node_t *node, int oflag)
|
---|
[2595dab] | 246 | {
|
---|
| 247 | futex_down(&vfs_phone_futex);
|
---|
| 248 | async_serialize_start();
|
---|
| 249 | vfs_connect();
|
---|
| 250 |
|
---|
| 251 | ipc_call_t answer;
|
---|
[4198f9c3] | 252 | aid_t req = async_send_4(vfs_phone, VFS_IN_OPEN_NODE, node->fs_handle,
|
---|
[2595dab] | 253 | node->dev_handle, node->index, oflag, &answer);
|
---|
| 254 |
|
---|
| 255 | ipcarg_t rc;
|
---|
| 256 | async_wait_for(req, &rc);
|
---|
| 257 | async_serialize_end();
|
---|
| 258 | futex_up(&vfs_phone_futex);
|
---|
| 259 |
|
---|
| 260 | if (rc != EOK)
|
---|
[3734106] | 261 | return (int) rc;
|
---|
[2595dab] | 262 |
|
---|
| 263 | return (int) IPC_GET_ARG1(answer);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[72bde81] | 266 | int close(int fildes)
|
---|
| 267 | {
|
---|
[e704503] | 268 | ipcarg_t rc;
|
---|
[19b28b0] | 269 |
|
---|
[e704503] | 270 | futex_down(&vfs_phone_futex);
|
---|
| 271 | async_serialize_start();
|
---|
[19b28b0] | 272 | vfs_connect();
|
---|
| 273 |
|
---|
[4198f9c3] | 274 | rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes);
|
---|
[19b28b0] | 275 |
|
---|
[e704503] | 276 | async_serialize_end();
|
---|
| 277 | futex_up(&vfs_phone_futex);
|
---|
| 278 |
|
---|
| 279 | return (int)rc;
|
---|
[72bde81] | 280 | }
|
---|
| 281 |
|
---|
[2f02aa17] | 282 | ssize_t read(int fildes, void *buf, size_t nbyte)
|
---|
| 283 | {
|
---|
| 284 | ipcarg_t rc;
|
---|
| 285 | ipc_call_t answer;
|
---|
| 286 | aid_t req;
|
---|
| 287 |
|
---|
| 288 | futex_down(&vfs_phone_futex);
|
---|
| 289 | async_serialize_start();
|
---|
[19b28b0] | 290 | vfs_connect();
|
---|
| 291 |
|
---|
[4198f9c3] | 292 | req = async_send_1(vfs_phone, VFS_IN_READ, fildes, &answer);
|
---|
[0da4e41] | 293 | rc = async_data_read_start(vfs_phone, (void *)buf, nbyte);
|
---|
[07e01e6] | 294 | if (rc != EOK) {
|
---|
[3734106] | 295 | ipcarg_t rc_orig;
|
---|
| 296 |
|
---|
| 297 | async_wait_for(req, &rc_orig);
|
---|
[2f02aa17] | 298 | async_serialize_end();
|
---|
| 299 | futex_up(&vfs_phone_futex);
|
---|
[3734106] | 300 | if (rc_orig == EOK)
|
---|
| 301 | return (ssize_t) rc;
|
---|
| 302 | else
|
---|
| 303 | return (ssize_t) rc_orig;
|
---|
[2f02aa17] | 304 | }
|
---|
| 305 | async_wait_for(req, &rc);
|
---|
| 306 | async_serialize_end();
|
---|
| 307 | futex_up(&vfs_phone_futex);
|
---|
[f7017572] | 308 | if (rc == EOK)
|
---|
| 309 | return (ssize_t) IPC_GET_ARG1(answer);
|
---|
| 310 | else
|
---|
[25becee8] | 311 | return rc;
|
---|
[2f02aa17] | 312 | }
|
---|
| 313 |
|
---|
[449c246] | 314 | ssize_t write(int fildes, const void *buf, size_t nbyte)
|
---|
| 315 | {
|
---|
| 316 | ipcarg_t rc;
|
---|
| 317 | ipc_call_t answer;
|
---|
| 318 | aid_t req;
|
---|
| 319 |
|
---|
| 320 | futex_down(&vfs_phone_futex);
|
---|
| 321 | async_serialize_start();
|
---|
[19b28b0] | 322 | vfs_connect();
|
---|
| 323 |
|
---|
[4198f9c3] | 324 | req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer);
|
---|
[0da4e41] | 325 | rc = async_data_write_start(vfs_phone, (void *)buf, nbyte);
|
---|
[07e01e6] | 326 | if (rc != EOK) {
|
---|
[3734106] | 327 | ipcarg_t rc_orig;
|
---|
| 328 |
|
---|
| 329 | async_wait_for(req, &rc_orig);
|
---|
[449c246] | 330 | async_serialize_end();
|
---|
| 331 | futex_up(&vfs_phone_futex);
|
---|
[3734106] | 332 | if (rc_orig == EOK)
|
---|
| 333 | return (ssize_t) rc;
|
---|
| 334 | else
|
---|
| 335 | return (ssize_t) rc_orig;
|
---|
[449c246] | 336 | }
|
---|
| 337 | async_wait_for(req, &rc);
|
---|
| 338 | async_serialize_end();
|
---|
| 339 | futex_up(&vfs_phone_futex);
|
---|
[f7017572] | 340 | if (rc == EOK)
|
---|
| 341 | return (ssize_t) IPC_GET_ARG1(answer);
|
---|
| 342 | else
|
---|
| 343 | return -1;
|
---|
[449c246] | 344 | }
|
---|
[222e57c] | 345 |
|
---|
[2595dab] | 346 | int fsync(int fildes)
|
---|
| 347 | {
|
---|
| 348 | futex_down(&vfs_phone_futex);
|
---|
| 349 | async_serialize_start();
|
---|
| 350 | vfs_connect();
|
---|
| 351 |
|
---|
[4198f9c3] | 352 | ipcarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes);
|
---|
[2595dab] | 353 |
|
---|
| 354 | async_serialize_end();
|
---|
| 355 | futex_up(&vfs_phone_futex);
|
---|
| 356 |
|
---|
| 357 | return (int) rc;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[222e57c] | 360 | off_t lseek(int fildes, off_t offset, int whence)
|
---|
| 361 | {
|
---|
| 362 | ipcarg_t rc;
|
---|
| 363 |
|
---|
| 364 | futex_down(&vfs_phone_futex);
|
---|
| 365 | async_serialize_start();
|
---|
[19b28b0] | 366 | vfs_connect();
|
---|
| 367 |
|
---|
[c61d34b] | 368 | ipcarg_t newoffs;
|
---|
[4198f9c3] | 369 | rc = async_req_3_1(vfs_phone, VFS_IN_SEEK, fildes, offset, whence,
|
---|
[c61d34b] | 370 | &newoffs);
|
---|
[222e57c] | 371 |
|
---|
| 372 | async_serialize_end();
|
---|
| 373 | futex_up(&vfs_phone_futex);
|
---|
| 374 |
|
---|
| 375 | if (rc != EOK)
|
---|
| 376 | return (off_t) -1;
|
---|
| 377 |
|
---|
[c61d34b] | 378 | return (off_t) newoffs;
|
---|
[222e57c] | 379 | }
|
---|
| 380 |
|
---|
[0ee4322] | 381 | int ftruncate(int fildes, off_t length)
|
---|
| 382 | {
|
---|
| 383 | ipcarg_t rc;
|
---|
| 384 |
|
---|
| 385 | futex_down(&vfs_phone_futex);
|
---|
| 386 | async_serialize_start();
|
---|
[19b28b0] | 387 | vfs_connect();
|
---|
| 388 |
|
---|
[4198f9c3] | 389 | rc = async_req_2_0(vfs_phone, VFS_IN_TRUNCATE, fildes, length);
|
---|
[0ee4322] | 390 | async_serialize_end();
|
---|
| 391 | futex_up(&vfs_phone_futex);
|
---|
| 392 | return (int) rc;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[852b801] | 395 | int fstat(int fildes, struct stat *stat)
|
---|
| 396 | {
|
---|
| 397 | ipcarg_t rc;
|
---|
| 398 | aid_t req;
|
---|
| 399 |
|
---|
| 400 | futex_down(&vfs_phone_futex);
|
---|
| 401 | async_serialize_start();
|
---|
| 402 | vfs_connect();
|
---|
| 403 |
|
---|
| 404 | req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL);
|
---|
[0da4e41] | 405 | rc = async_data_read_start(vfs_phone, (void *)stat, sizeof(struct stat));
|
---|
[852b801] | 406 | if (rc != EOK) {
|
---|
[3734106] | 407 | ipcarg_t rc_orig;
|
---|
| 408 |
|
---|
| 409 | async_wait_for(req, &rc_orig);
|
---|
[852b801] | 410 | async_serialize_end();
|
---|
| 411 | futex_up(&vfs_phone_futex);
|
---|
[3734106] | 412 | if (rc_orig == EOK)
|
---|
| 413 | return (ssize_t) rc;
|
---|
| 414 | else
|
---|
| 415 | return (ssize_t) rc_orig;
|
---|
[852b801] | 416 | }
|
---|
| 417 | async_wait_for(req, &rc);
|
---|
| 418 | async_serialize_end();
|
---|
| 419 | futex_up(&vfs_phone_futex);
|
---|
| 420 |
|
---|
| 421 | return rc;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[415c7e0d] | 424 | int stat(const char *path, struct stat *stat)
|
---|
| 425 | {
|
---|
| 426 | ipcarg_t rc;
|
---|
[3734106] | 427 | ipcarg_t rc_orig;
|
---|
[415c7e0d] | 428 | aid_t req;
|
---|
| 429 |
|
---|
| 430 | size_t pa_size;
|
---|
| 431 | char *pa = absolutize(path, &pa_size);
|
---|
| 432 | if (!pa)
|
---|
| 433 | return ENOMEM;
|
---|
| 434 |
|
---|
| 435 | futex_down(&vfs_phone_futex);
|
---|
| 436 | async_serialize_start();
|
---|
| 437 | vfs_connect();
|
---|
| 438 |
|
---|
| 439 | req = async_send_0(vfs_phone, VFS_IN_STAT, NULL);
|
---|
[0da4e41] | 440 | rc = async_data_write_start(vfs_phone, pa, pa_size);
|
---|
[415c7e0d] | 441 | if (rc != EOK) {
|
---|
[3734106] | 442 | async_wait_for(req, &rc_orig);
|
---|
[415c7e0d] | 443 | async_serialize_end();
|
---|
| 444 | futex_up(&vfs_phone_futex);
|
---|
| 445 | free(pa);
|
---|
[3734106] | 446 | if (rc_orig == EOK)
|
---|
| 447 | return (int) rc;
|
---|
| 448 | else
|
---|
| 449 | return (int) rc_orig;
|
---|
[415c7e0d] | 450 | }
|
---|
[0da4e41] | 451 | rc = async_data_read_start(vfs_phone, stat, sizeof(struct stat));
|
---|
[415c7e0d] | 452 | if (rc != EOK) {
|
---|
[3734106] | 453 | async_wait_for(req, &rc_orig);
|
---|
[415c7e0d] | 454 | async_serialize_end();
|
---|
| 455 | futex_up(&vfs_phone_futex);
|
---|
| 456 | free(pa);
|
---|
[3734106] | 457 | if (rc_orig == EOK)
|
---|
| 458 | return (int) rc;
|
---|
| 459 | else
|
---|
| 460 | return (int) rc_orig;
|
---|
[415c7e0d] | 461 | }
|
---|
| 462 | async_wait_for(req, &rc);
|
---|
| 463 | async_serialize_end();
|
---|
| 464 | futex_up(&vfs_phone_futex);
|
---|
| 465 | free(pa);
|
---|
| 466 | return rc;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[d0dc74ae] | 469 | DIR *opendir(const char *dirname)
|
---|
| 470 | {
|
---|
| 471 | DIR *dirp = malloc(sizeof(DIR));
|
---|
| 472 | if (!dirp)
|
---|
| 473 | return NULL;
|
---|
[2b88074b] | 474 |
|
---|
| 475 | size_t abs_size;
|
---|
| 476 | char *abs = absolutize(dirname, &abs_size);
|
---|
| 477 | if (!abs) {
|
---|
| 478 | free(dirp);
|
---|
| 479 | return ENOMEM;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | int ret = open_internal(abs, abs_size, L_DIRECTORY, 0);
|
---|
| 483 | free(abs);
|
---|
| 484 |
|
---|
| 485 | if (ret < 0) {
|
---|
[d0dc74ae] | 486 | free(dirp);
|
---|
| 487 | return NULL;
|
---|
| 488 | }
|
---|
[2b88074b] | 489 |
|
---|
| 490 | dirp->fd = ret;
|
---|
[d0dc74ae] | 491 | return dirp;
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | struct dirent *readdir(DIR *dirp)
|
---|
| 495 | {
|
---|
[5973fd0] | 496 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
|
---|
| 497 | if (len <= 0)
|
---|
| 498 | return NULL;
|
---|
| 499 | return &dirp->res;
|
---|
[d0dc74ae] | 500 | }
|
---|
| 501 |
|
---|
| 502 | void rewinddir(DIR *dirp)
|
---|
| 503 | {
|
---|
[5973fd0] | 504 | (void) lseek(dirp->fd, 0, SEEK_SET);
|
---|
[d0dc74ae] | 505 | }
|
---|
| 506 |
|
---|
| 507 | int closedir(DIR *dirp)
|
---|
| 508 | {
|
---|
| 509 | (void) close(dirp->fd);
|
---|
| 510 | free(dirp);
|
---|
| 511 | return 0;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[72bde81] | 514 | int mkdir(const char *path, mode_t mode)
|
---|
[d0dc74ae] | 515 | {
|
---|
[72bde81] | 516 | ipcarg_t rc;
|
---|
| 517 | aid_t req;
|
---|
| 518 |
|
---|
[9eb3623] | 519 | size_t pa_size;
|
---|
| 520 | char *pa = absolutize(path, &pa_size);
|
---|
[5fec355] | 521 | if (!pa)
|
---|
| 522 | return ENOMEM;
|
---|
[19b28b0] | 523 |
|
---|
[72bde81] | 524 | futex_down(&vfs_phone_futex);
|
---|
| 525 | async_serialize_start();
|
---|
[19b28b0] | 526 | vfs_connect();
|
---|
| 527 |
|
---|
[4198f9c3] | 528 | req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL);
|
---|
[0da4e41] | 529 | rc = async_data_write_start(vfs_phone, pa, pa_size);
|
---|
[72bde81] | 530 | if (rc != EOK) {
|
---|
[3734106] | 531 | ipcarg_t rc_orig;
|
---|
| 532 |
|
---|
| 533 | async_wait_for(req, &rc_orig);
|
---|
[72bde81] | 534 | async_serialize_end();
|
---|
| 535 | futex_up(&vfs_phone_futex);
|
---|
[5fec355] | 536 | free(pa);
|
---|
[3734106] | 537 | if (rc_orig == EOK)
|
---|
| 538 | return (int) rc;
|
---|
| 539 | else
|
---|
| 540 | return (int) rc_orig;
|
---|
[72bde81] | 541 | }
|
---|
| 542 | async_wait_for(req, &rc);
|
---|
| 543 | async_serialize_end();
|
---|
| 544 | futex_up(&vfs_phone_futex);
|
---|
[5fec355] | 545 | free(pa);
|
---|
[2595dab] | 546 | return rc;
|
---|
[d0dc74ae] | 547 | }
|
---|
| 548 |
|
---|
[f15cf1a6] | 549 | static int _unlink(const char *path, int lflag)
|
---|
| 550 | {
|
---|
| 551 | ipcarg_t rc;
|
---|
| 552 | aid_t req;
|
---|
| 553 |
|
---|
[9eb3623] | 554 | size_t pa_size;
|
---|
| 555 | char *pa = absolutize(path, &pa_size);
|
---|
[5fec355] | 556 | if (!pa)
|
---|
| 557 | return ENOMEM;
|
---|
[923c39e] | 558 |
|
---|
[f15cf1a6] | 559 | futex_down(&vfs_phone_futex);
|
---|
| 560 | async_serialize_start();
|
---|
[19b28b0] | 561 | vfs_connect();
|
---|
| 562 |
|
---|
[4198f9c3] | 563 | req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL);
|
---|
[0da4e41] | 564 | rc = async_data_write_start(vfs_phone, pa, pa_size);
|
---|
[f15cf1a6] | 565 | if (rc != EOK) {
|
---|
[3734106] | 566 | ipcarg_t rc_orig;
|
---|
| 567 |
|
---|
| 568 | async_wait_for(req, &rc_orig);
|
---|
[f15cf1a6] | 569 | async_serialize_end();
|
---|
| 570 | futex_up(&vfs_phone_futex);
|
---|
[5fec355] | 571 | free(pa);
|
---|
[3734106] | 572 | if (rc_orig == EOK)
|
---|
| 573 | return (int) rc;
|
---|
| 574 | else
|
---|
| 575 | return (int) rc_orig;
|
---|
[f15cf1a6] | 576 | }
|
---|
| 577 | async_wait_for(req, &rc);
|
---|
| 578 | async_serialize_end();
|
---|
| 579 | futex_up(&vfs_phone_futex);
|
---|
[5fec355] | 580 | free(pa);
|
---|
[2595dab] | 581 | return rc;
|
---|
[f15cf1a6] | 582 | }
|
---|
| 583 |
|
---|
| 584 | int unlink(const char *path)
|
---|
| 585 | {
|
---|
| 586 | return _unlink(path, L_NONE);
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | int rmdir(const char *path)
|
---|
| 590 | {
|
---|
| 591 | return _unlink(path, L_DIRECTORY);
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[a8e9ab8d] | 594 | int rename(const char *old, const char *new)
|
---|
| 595 | {
|
---|
| 596 | ipcarg_t rc;
|
---|
[3734106] | 597 | ipcarg_t rc_orig;
|
---|
[a8e9ab8d] | 598 | aid_t req;
|
---|
| 599 |
|
---|
[9eb3623] | 600 | size_t olda_size;
|
---|
| 601 | char *olda = absolutize(old, &olda_size);
|
---|
[a8e9ab8d] | 602 | if (!olda)
|
---|
| 603 | return ENOMEM;
|
---|
[923c39e] | 604 |
|
---|
[9eb3623] | 605 | size_t newa_size;
|
---|
| 606 | char *newa = absolutize(new, &newa_size);
|
---|
[a8e9ab8d] | 607 | if (!newa) {
|
---|
| 608 | free(olda);
|
---|
| 609 | return ENOMEM;
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | futex_down(&vfs_phone_futex);
|
---|
| 613 | async_serialize_start();
|
---|
[19b28b0] | 614 | vfs_connect();
|
---|
| 615 |
|
---|
[4198f9c3] | 616 | req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL);
|
---|
[0da4e41] | 617 | rc = async_data_write_start(vfs_phone, olda, olda_size);
|
---|
[a8e9ab8d] | 618 | if (rc != EOK) {
|
---|
[3734106] | 619 | async_wait_for(req, &rc_orig);
|
---|
[a8e9ab8d] | 620 | async_serialize_end();
|
---|
| 621 | futex_up(&vfs_phone_futex);
|
---|
| 622 | free(olda);
|
---|
| 623 | free(newa);
|
---|
[3734106] | 624 | if (rc_orig == EOK)
|
---|
| 625 | return (int) rc;
|
---|
| 626 | else
|
---|
| 627 | return (int) rc_orig;
|
---|
[a8e9ab8d] | 628 | }
|
---|
[0da4e41] | 629 | rc = async_data_write_start(vfs_phone, newa, newa_size);
|
---|
[a8e9ab8d] | 630 | if (rc != EOK) {
|
---|
[3734106] | 631 | async_wait_for(req, &rc_orig);
|
---|
[a8e9ab8d] | 632 | async_serialize_end();
|
---|
| 633 | futex_up(&vfs_phone_futex);
|
---|
| 634 | free(olda);
|
---|
| 635 | free(newa);
|
---|
[3734106] | 636 | if (rc_orig == EOK)
|
---|
| 637 | return (int) rc;
|
---|
| 638 | else
|
---|
| 639 | return (int) rc_orig;
|
---|
[a8e9ab8d] | 640 | }
|
---|
| 641 | async_wait_for(req, &rc);
|
---|
| 642 | async_serialize_end();
|
---|
| 643 | futex_up(&vfs_phone_futex);
|
---|
| 644 | free(olda);
|
---|
| 645 | free(newa);
|
---|
| 646 | return rc;
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[5fec355] | 649 | int chdir(const char *path)
|
---|
| 650 | {
|
---|
[2b88074b] | 651 | size_t abs_size;
|
---|
| 652 | char *abs = absolutize(path, &abs_size);
|
---|
| 653 | if (!abs)
|
---|
[5fec355] | 654 | return ENOMEM;
|
---|
[2b88074b] | 655 |
|
---|
| 656 | int fd = open_internal(abs, abs_size, L_DIRECTORY, O_DESC);
|
---|
| 657 |
|
---|
| 658 | if (fd < 0) {
|
---|
| 659 | free(abs);
|
---|
[5fec355] | 660 | return ENOENT;
|
---|
| 661 | }
|
---|
[2b88074b] | 662 |
|
---|
[5fec355] | 663 | futex_down(&cwd_futex);
|
---|
[2b88074b] | 664 |
|
---|
| 665 | if (cwd_fd >= 0)
|
---|
| 666 | close(cwd_fd);
|
---|
| 667 |
|
---|
| 668 |
|
---|
| 669 | if (cwd_path)
|
---|
| 670 | free(cwd_path);
|
---|
| 671 |
|
---|
| 672 | cwd_fd = fd;
|
---|
| 673 | cwd_path = abs;
|
---|
| 674 | cwd_size = abs_size;
|
---|
| 675 |
|
---|
[5fec355] | 676 | futex_up(&cwd_futex);
|
---|
[0dd0f71f] | 677 | return EOK;
|
---|
[5fec355] | 678 | }
|
---|
| 679 |
|
---|
| 680 | char *getcwd(char *buf, size_t size)
|
---|
| 681 | {
|
---|
[2b88074b] | 682 | if (size == 0)
|
---|
[5fec355] | 683 | return NULL;
|
---|
[2b88074b] | 684 |
|
---|
[5fec355] | 685 | futex_down(&cwd_futex);
|
---|
[2b88074b] | 686 |
|
---|
| 687 | if ((cwd_size == 0) || (size < cwd_size + 1)) {
|
---|
[5fec355] | 688 | futex_up(&cwd_futex);
|
---|
| 689 | return NULL;
|
---|
| 690 | }
|
---|
[2b88074b] | 691 |
|
---|
[6eb2e96] | 692 | str_cpy(buf, size, cwd_path);
|
---|
[5fec355] | 693 | futex_up(&cwd_futex);
|
---|
[2b88074b] | 694 |
|
---|
[5fec355] | 695 | return buf;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
[852b801] | 698 | int fd_phone(int fildes)
|
---|
| 699 | {
|
---|
| 700 | struct stat stat;
|
---|
| 701 | int rc;
|
---|
| 702 |
|
---|
| 703 | rc = fstat(fildes, &stat);
|
---|
| 704 |
|
---|
| 705 | if (!stat.devfs_stat.device)
|
---|
| 706 | return -1;
|
---|
| 707 |
|
---|
| 708 | return devmap_device_connect(stat.devfs_stat.device, 0);
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | int fd_node(int fildes, fdi_node_t *node)
|
---|
| 712 | {
|
---|
| 713 | struct stat stat;
|
---|
| 714 | int rc;
|
---|
| 715 |
|
---|
| 716 | rc = fstat(fildes, &stat);
|
---|
| 717 |
|
---|
| 718 | if (rc == EOK) {
|
---|
| 719 | node->fs_handle = stat.fs_handle;
|
---|
| 720 | node->dev_handle = stat.dev_handle;
|
---|
| 721 | node->index = stat.index;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | return rc;
|
---|
| 725 | }
|
---|
| 726 |
|
---|
[2b88074b] | 727 | int dup2(int oldfd, int newfd)
|
---|
| 728 | {
|
---|
| 729 | futex_down(&vfs_phone_futex);
|
---|
| 730 | async_serialize_start();
|
---|
| 731 | vfs_connect();
|
---|
| 732 |
|
---|
| 733 | ipcarg_t ret;
|
---|
| 734 | ipcarg_t rc = async_req_2_1(vfs_phone, VFS_IN_DUP, oldfd, newfd, &ret);
|
---|
| 735 |
|
---|
| 736 | async_serialize_end();
|
---|
| 737 | futex_up(&vfs_phone_futex);
|
---|
| 738 |
|
---|
| 739 | if (rc == EOK)
|
---|
| 740 | return (int) ret;
|
---|
| 741 |
|
---|
| 742 | return (int) rc;
|
---|
| 743 | }
|
---|
| 744 |
|
---|
[2f02aa17] | 745 | /** @}
|
---|
| 746 | */
|
---|