[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>
|
---|
[8ffedd8] | 37 | #include <vfs/vfs_mtab.h>
|
---|
[79ae36dd] | 38 | #include <vfs/vfs_sess.h>
|
---|
[ed903174] | 39 | #include <macros.h>
|
---|
[d0dc74ae] | 40 | #include <stdlib.h>
|
---|
[449c246] | 41 | #include <unistd.h>
|
---|
[d0dc74ae] | 42 | #include <dirent.h>
|
---|
[449c246] | 43 | #include <fcntl.h>
|
---|
[923c39e] | 44 | #include <stdio.h>
|
---|
[852b801] | 45 | #include <sys/stat.h>
|
---|
[9dc6083] | 46 | #include <sys/statfs.h>
|
---|
[72bde81] | 47 | #include <sys/types.h>
|
---|
[2f02aa17] | 48 | #include <ipc/services.h>
|
---|
[79ae36dd] | 49 | #include <ns.h>
|
---|
[2f02aa17] | 50 | #include <async.h>
|
---|
[a28ab12] | 51 | #include <fibril_synch.h>
|
---|
[2f02aa17] | 52 | #include <errno.h>
|
---|
[a28ab12] | 53 | #include <assert.h>
|
---|
[19f857a] | 54 | #include <str.h>
|
---|
[15f3c3f] | 55 | #include <loc.h>
|
---|
[2595dab] | 56 | #include <ipc/vfs.h>
|
---|
[15f3c3f] | 57 | #include <ipc/loc.h>
|
---|
[2f02aa17] | 58 |
|
---|
[79ae36dd] | 59 | static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
|
---|
| 60 | static async_sess_t *vfs_sess = NULL;
|
---|
[a28ab12] | 61 |
|
---|
| 62 | static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
|
---|
[5fec355] | 63 |
|
---|
[2b88074b] | 64 | static int cwd_fd = -1;
|
---|
| 65 | static char *cwd_path = NULL;
|
---|
| 66 | static size_t cwd_size = 0;
|
---|
[5fec355] | 67 |
|
---|
[5126f80] | 68 | static FIBRIL_MUTEX_INITIALIZE(root_mutex);
|
---|
| 69 | static int root_fd = -1;
|
---|
| 70 |
|
---|
| 71 | int vfs_root(void)
|
---|
| 72 | {
|
---|
| 73 | fibril_mutex_lock(&root_mutex);
|
---|
| 74 | int r;
|
---|
| 75 | if (root_fd < 0) {
|
---|
| 76 | r = ENOENT;
|
---|
| 77 | } else {
|
---|
| 78 | r = vfs_clone(root_fd, true);
|
---|
| 79 | }
|
---|
| 80 | fibril_mutex_unlock(&root_mutex);
|
---|
| 81 | return r;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void vfs_root_set(int nroot)
|
---|
| 85 | {
|
---|
| 86 | fibril_mutex_lock(&root_mutex);
|
---|
| 87 | if (root_fd >= 0) {
|
---|
| 88 | close(root_fd);
|
---|
| 89 | }
|
---|
| 90 | root_fd = vfs_clone(nroot, true);
|
---|
| 91 | fibril_mutex_unlock(&root_mutex);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[79ae36dd] | 94 | /** Start an async exchange on the VFS session.
|
---|
| 95 | *
|
---|
| 96 | * @return New exchange.
|
---|
| 97 | *
|
---|
| 98 | */
|
---|
[866e627] | 99 | async_exch_t *vfs_exchange_begin(void)
|
---|
[79ae36dd] | 100 | {
|
---|
| 101 | fibril_mutex_lock(&vfs_mutex);
|
---|
| 102 |
|
---|
| 103 | while (vfs_sess == NULL)
|
---|
[f9b2cb4c] | 104 | vfs_sess = service_connect_blocking(SERVICE_VFS, INTERFACE_VFS,
|
---|
[2133e02] | 105 | 0);
|
---|
[79ae36dd] | 106 |
|
---|
| 107 | fibril_mutex_unlock(&vfs_mutex);
|
---|
| 108 |
|
---|
| 109 | return async_exchange_begin(vfs_sess);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /** Finish an async exchange on the VFS session.
|
---|
| 113 | *
|
---|
| 114 | * @param exch Exchange to be finished.
|
---|
| 115 | *
|
---|
| 116 | */
|
---|
[866e627] | 117 | void vfs_exchange_end(async_exch_t *exch)
|
---|
[79ae36dd] | 118 | {
|
---|
| 119 | async_exchange_end(exch);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[0b18364] | 122 | int _vfs_walk(int parent, const char *path, int flags)
|
---|
| 123 | {
|
---|
| 124 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 125 |
|
---|
| 126 | ipc_call_t answer;
|
---|
| 127 | aid_t req = async_send_2(exch, VFS_IN_WALK, parent, flags, &answer);
|
---|
[3ef62df] | 128 | sysarg_t rc = async_data_write_start(exch, path, str_size(path));
|
---|
[0b18364] | 129 | vfs_exchange_end(exch);
|
---|
| 130 |
|
---|
| 131 | sysarg_t rc_orig;
|
---|
| 132 | async_wait_for(req, &rc_orig);
|
---|
| 133 |
|
---|
| 134 | if (rc_orig != EOK) {
|
---|
| 135 | return (int) rc_orig;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | if (rc != EOK) {
|
---|
| 139 | return (int) rc;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | return (int) IPC_GET_ARG1(answer);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[5126f80] | 145 | int vfs_lookup(const char *path, int flags)
|
---|
[eca9fd0] | 146 | {
|
---|
| 147 | size_t size;
|
---|
| 148 | char *p = vfs_absolutize(path, &size);
|
---|
| 149 | if (!p) {
|
---|
| 150 | return ENOMEM;
|
---|
| 151 | }
|
---|
[5126f80] | 152 | int root = vfs_root();
|
---|
[c725619] | 153 | if (root < 0) {
|
---|
[5126f80] | 154 | free(p);
|
---|
| 155 | return ENOENT;
|
---|
| 156 | }
|
---|
| 157 | int rc = _vfs_walk(root, p, flags);
|
---|
| 158 | close(root);
|
---|
[eca9fd0] | 159 | free(p);
|
---|
| 160 | return rc;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[0b18364] | 163 | int _vfs_open(int fildes, int mode)
|
---|
| 164 | {
|
---|
| 165 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 166 | sysarg_t rc = async_req_2_0(exch, VFS_IN_OPEN2, fildes, mode);
|
---|
| 167 | vfs_exchange_end(exch);
|
---|
| 168 |
|
---|
| 169 | return (int) rc;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[6afc9d7] | 172 | char *vfs_absolutize(const char *path, size_t *retlen)
|
---|
[5fec355] | 173 | {
|
---|
| 174 | char *ncwd_path;
|
---|
[34a74ab] | 175 | char *ncwd_path_nc;
|
---|
[5fec355] | 176 |
|
---|
[a28ab12] | 177 | fibril_mutex_lock(&cwd_mutex);
|
---|
[9eb3623] | 178 | size_t size = str_size(path);
|
---|
[5fec355] | 179 | if (*path != '/') {
|
---|
[6afc9d7] | 180 | if (cwd_path == NULL) {
|
---|
[a28ab12] | 181 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[5fec355] | 182 | return NULL;
|
---|
| 183 | }
|
---|
[79ae36dd] | 184 | ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
|
---|
[6afc9d7] | 185 | if (ncwd_path_nc == NULL) {
|
---|
[a28ab12] | 186 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[5fec355] | 187 | return NULL;
|
---|
| 188 | }
|
---|
[79ae36dd] | 189 | str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
|
---|
[9eb3623] | 190 | ncwd_path_nc[cwd_size] = '/';
|
---|
| 191 | ncwd_path_nc[cwd_size + 1] = '\0';
|
---|
[5fec355] | 192 | } else {
|
---|
[79ae36dd] | 193 | ncwd_path_nc = malloc(size + 1);
|
---|
[6afc9d7] | 194 | if (ncwd_path_nc == NULL) {
|
---|
[a28ab12] | 195 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[5fec355] | 196 | return NULL;
|
---|
| 197 | }
|
---|
[34a74ab] | 198 | ncwd_path_nc[0] = '\0';
|
---|
[5fec355] | 199 | }
|
---|
[79ae36dd] | 200 | str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
|
---|
[34a74ab] | 201 | ncwd_path = canonify(ncwd_path_nc, retlen);
|
---|
[6afc9d7] | 202 | if (ncwd_path == NULL) {
|
---|
[a28ab12] | 203 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[34a74ab] | 204 | free(ncwd_path_nc);
|
---|
| 205 | return NULL;
|
---|
| 206 | }
|
---|
| 207 | /*
|
---|
| 208 | * We need to clone ncwd_path because canonify() works in-place and thus
|
---|
| 209 | * the address in ncwd_path need not be the same as ncwd_path_nc, even
|
---|
| 210 | * though they both point into the same dynamically allocated buffer.
|
---|
| 211 | */
|
---|
[095003a8] | 212 | ncwd_path = str_dup(ncwd_path);
|
---|
[34a74ab] | 213 | free(ncwd_path_nc);
|
---|
[6afc9d7] | 214 | if (ncwd_path == NULL) {
|
---|
[a28ab12] | 215 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[923c39e] | 216 | return NULL;
|
---|
| 217 | }
|
---|
[a28ab12] | 218 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[5fec355] | 219 | return ncwd_path;
|
---|
| 220 | }
|
---|
[2f02aa17] | 221 |
|
---|
[5126f80] | 222 | int vfs_mount(int mp, const char *fs_name, service_id_t serv, const char *opts,
|
---|
| 223 | unsigned int flags, unsigned int instance, int *mountedfd)
|
---|
| 224 | {
|
---|
| 225 | sysarg_t rc, rc1;
|
---|
| 226 |
|
---|
| 227 | if (!mountedfd) {
|
---|
| 228 | flags |= VFS_MOUNT_NO_REF;
|
---|
| 229 | }
|
---|
| 230 | if (mp < 0) {
|
---|
| 231 | flags |= VFS_MOUNT_CONNECT_ONLY;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | ipc_call_t answer;
|
---|
| 235 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 236 | aid_t req = async_send_4(exch, VFS_IN_MOUNT, mp, serv, flags, instance, &answer);
|
---|
| 237 |
|
---|
| 238 | rc1 = async_data_write_start(exch, (void *) opts, str_size(opts));
|
---|
| 239 |
|
---|
| 240 | if (rc1 == EOK) {
|
---|
| 241 | rc1 = async_data_write_start(exch, (void *) fs_name, str_size(fs_name));
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | vfs_exchange_end(exch);
|
---|
| 245 |
|
---|
| 246 | async_wait_for(req, &rc);
|
---|
| 247 |
|
---|
| 248 | if (mountedfd) {
|
---|
| 249 | *mountedfd = (int) IPC_GET_ARG1(answer);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | if (rc != EOK) {
|
---|
| 253 | return rc;
|
---|
| 254 | }
|
---|
| 255 | return rc1;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | int vfs_unmount(int mp)
|
---|
| 259 | {
|
---|
| 260 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 261 | int rc = async_req_1_0(exch, VFS_IN_UNMOUNT, mp);
|
---|
| 262 | vfs_exchange_end(exch);
|
---|
| 263 | return rc;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | int mount(const char *fs_name, const char *mp, const char *fqsn,
|
---|
[4979403] | 267 | const char *opts, unsigned int flags, unsigned int instance)
|
---|
[2f02aa17] | 268 | {
|
---|
[210e50a] | 269 | int null_id = -1;
|
---|
[15f3c3f] | 270 | char null[LOC_NAME_MAXLEN];
|
---|
[210e50a] | 271 |
|
---|
[15f3c3f] | 272 | if (str_cmp(fqsn, "") == 0) {
|
---|
[210e50a] | 273 | /* No device specified, create a fresh
|
---|
| 274 | null/%d device instead */
|
---|
[15f3c3f] | 275 | null_id = loc_null_create();
|
---|
[210e50a] | 276 |
|
---|
| 277 | if (null_id == -1)
|
---|
| 278 | return ENOMEM;
|
---|
| 279 |
|
---|
[15f3c3f] | 280 | snprintf(null, LOC_NAME_MAXLEN, "null/%d", null_id);
|
---|
| 281 | fqsn = null;
|
---|
[210e50a] | 282 | }
|
---|
[82405266] | 283 |
|
---|
[5126f80] | 284 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
| 285 | flags = VFS_MOUNT_BLOCKING;
|
---|
| 286 | } else {
|
---|
| 287 | flags = 0;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[15f3c3f] | 290 | service_id_t service_id;
|
---|
| 291 | int res = loc_service_get_id(fqsn, &service_id, flags);
|
---|
[210e50a] | 292 | if (res != EOK) {
|
---|
| 293 | if (null_id != -1)
|
---|
[15f3c3f] | 294 | loc_null_destroy(null_id);
|
---|
[210e50a] | 295 |
|
---|
[82405266] | 296 | return res;
|
---|
[210e50a] | 297 | }
|
---|
[82405266] | 298 |
|
---|
[9eb3623] | 299 | size_t mpa_size;
|
---|
[6afc9d7] | 300 | char *mpa = vfs_absolutize(mp, &mpa_size);
|
---|
| 301 | if (mpa == NULL) {
|
---|
[210e50a] | 302 | if (null_id != -1)
|
---|
[15f3c3f] | 303 | loc_null_destroy(null_id);
|
---|
[210e50a] | 304 |
|
---|
[5fec355] | 305 | return ENOMEM;
|
---|
[210e50a] | 306 | }
|
---|
[19b28b0] | 307 |
|
---|
[5126f80] | 308 | fibril_mutex_lock(&root_mutex);
|
---|
[19b28b0] | 309 |
|
---|
[5126f80] | 310 | int rc;
|
---|
[210e50a] | 311 |
|
---|
[5126f80] | 312 | if (str_cmp(mpa, "/") == 0) {
|
---|
| 313 | /* Mounting root. */
|
---|
[210e50a] | 314 |
|
---|
[5126f80] | 315 | if (root_fd >= 0) {
|
---|
| 316 | fibril_mutex_unlock(&root_mutex);
|
---|
| 317 | if (null_id != -1) {
|
---|
| 318 | loc_null_destroy(null_id);
|
---|
| 319 | }
|
---|
| 320 | return EBUSY;
|
---|
| 321 | }
|
---|
[210e50a] | 322 |
|
---|
[5126f80] | 323 | int root;
|
---|
| 324 | rc = vfs_mount(-1, fs_name, service_id, opts, flags, instance, &root);
|
---|
| 325 | if (rc == EOK) {
|
---|
| 326 | root_fd = root;
|
---|
| 327 | }
|
---|
| 328 | } else {
|
---|
| 329 | if (root_fd < 0) {
|
---|
| 330 | fibril_mutex_unlock(&root_mutex);
|
---|
| 331 | if (null_id != -1) {
|
---|
| 332 | loc_null_destroy(null_id);
|
---|
| 333 | }
|
---|
| 334 | return EINVAL;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | int mpfd = _vfs_walk(root_fd, mpa, WALK_DIRECTORY);
|
---|
| 338 | if (mpfd >= 0) {
|
---|
| 339 | rc = vfs_mount(mpfd, fs_name, service_id, opts, flags, instance, NULL);
|
---|
| 340 | close(mpfd);
|
---|
| 341 | } else {
|
---|
| 342 | rc = mpfd;
|
---|
| 343 | }
|
---|
[2f02aa17] | 344 | }
|
---|
[210e50a] | 345 |
|
---|
[5126f80] | 346 | fibril_mutex_unlock(&root_mutex);
|
---|
[19b28b0] | 347 |
|
---|
[210e50a] | 348 | if ((rc != EOK) && (null_id != -1))
|
---|
[15f3c3f] | 349 | loc_null_destroy(null_id);
|
---|
[210e50a] | 350 |
|
---|
[2f02aa17] | 351 | return (int) rc;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[5126f80] | 354 | int unmount(const char *mpp)
|
---|
[21f32ee1] | 355 | {
|
---|
[5126f80] | 356 | int mp = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY);
|
---|
| 357 | if (mp < 0) {
|
---|
| 358 | return mp;
|
---|
[b9067dfa] | 359 | }
|
---|
| 360 |
|
---|
[5126f80] | 361 | int rc = vfs_unmount(mp);
|
---|
| 362 | close(mp);
|
---|
| 363 | return rc;
|
---|
[21f32ee1] | 364 | }
|
---|
| 365 |
|
---|
[3ef62df] | 366 | static int walk_flags(int oflags)
|
---|
[2f02aa17] | 367 | {
|
---|
[3ef62df] | 368 | int flags = 0;
|
---|
| 369 | if (oflags & O_CREAT) {
|
---|
| 370 | if (oflags & O_EXCL) {
|
---|
| 371 | flags |= WALK_MUST_CREATE;
|
---|
| 372 | } else {
|
---|
| 373 | flags |= WALK_MAY_CREATE;
|
---|
| 374 | }
|
---|
[2f02aa17] | 375 | }
|
---|
[3ef62df] | 376 | return flags;
|
---|
[2f02aa17] | 377 | }
|
---|
| 378 |
|
---|
[6afc9d7] | 379 | /** Open file.
|
---|
| 380 | *
|
---|
| 381 | * @param path File path
|
---|
| 382 | * @param oflag O_xxx flags
|
---|
| 383 | * @param mode File mode (only with O_CREAT)
|
---|
| 384 | *
|
---|
| 385 | * @return Nonnegative file descriptor on success. On error -1 is returned
|
---|
| 386 | * and errno is set.
|
---|
| 387 | */
|
---|
[ae78b530] | 388 | int open(const char *path, int oflag, ...)
|
---|
| 389 | {
|
---|
[3ef62df] | 390 | // FIXME: Some applications call this incorrectly.
|
---|
| 391 | if ((oflag & (O_RDONLY|O_WRONLY|O_RDWR)) == 0) {
|
---|
| 392 | oflag |= O_RDWR;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | assert((((oflag & O_RDONLY) != 0) + ((oflag & O_WRONLY) != 0) + ((oflag & O_RDWR) != 0)) == 1);
|
---|
| 396 |
|
---|
[0b97336] | 397 | int fd = vfs_lookup(path, walk_flags(oflag) | WALK_REGULAR);
|
---|
| 398 | if (fd < 0) {
|
---|
| 399 | errno = fd;
|
---|
| 400 | return -1;
|
---|
[6afc9d7] | 401 | }
|
---|
[2b88074b] | 402 |
|
---|
[3ef62df] | 403 | int mode =
|
---|
| 404 | ((oflag & O_RDWR) ? MODE_READ|MODE_WRITE : 0) |
|
---|
| 405 | ((oflag & O_RDONLY) ? MODE_READ : 0) |
|
---|
| 406 | ((oflag & O_WRONLY) ? MODE_WRITE : 0) |
|
---|
| 407 | ((oflag & O_APPEND) ? MODE_APPEND : 0);
|
---|
[2b88074b] | 408 |
|
---|
[0b97336] | 409 | int rc = _vfs_open(fd, mode);
|
---|
[3ef62df] | 410 | if (rc < 0) {
|
---|
[0b97336] | 411 | close(fd);
|
---|
| 412 | errno = rc;
|
---|
| 413 | return -1;
|
---|
[6afc9d7] | 414 | }
|
---|
| 415 |
|
---|
[3ef62df] | 416 | if (oflag & O_TRUNC) {
|
---|
| 417 | assert(oflag & O_WRONLY || oflag & O_RDWR);
|
---|
| 418 | assert(!(oflag & O_APPEND));
|
---|
| 419 |
|
---|
[0b97336] | 420 | (void) ftruncate(fd, 0);
|
---|
[3ef62df] | 421 | }
|
---|
| 422 |
|
---|
[0b97336] | 423 | return fd;
|
---|
[ae78b530] | 424 | }
|
---|
| 425 |
|
---|
[6afc9d7] | 426 | /** Close file.
|
---|
| 427 | *
|
---|
| 428 | * @param fildes File descriptor
|
---|
| 429 | * @return Zero on success. On error -1 is returned and errno is set.
|
---|
| 430 | */
|
---|
[72bde81] | 431 | int close(int fildes)
|
---|
| 432 | {
|
---|
[96b02eb9] | 433 | sysarg_t rc;
|
---|
[19b28b0] | 434 |
|
---|
[79ae36dd] | 435 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 436 | rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes);
|
---|
| 437 | vfs_exchange_end(exch);
|
---|
[19b28b0] | 438 |
|
---|
[6afc9d7] | 439 | if (rc != EOK) {
|
---|
| 440 | errno = rc;
|
---|
| 441 | return -1;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | return 0;
|
---|
[72bde81] | 445 | }
|
---|
| 446 |
|
---|
[6afc9d7] | 447 | /** Read bytes from file.
|
---|
| 448 | *
|
---|
| 449 | * Read up to @a nbyte bytes from file. The actual number of bytes read
|
---|
| 450 | * may be lower, but greater than zero if there are any bytes available.
|
---|
| 451 | * If there are no bytes available for reading, then the function will
|
---|
| 452 | * return success with zero bytes read.
|
---|
| 453 | *
|
---|
| 454 | * @param fildes File descriptor
|
---|
| 455 | * @param buf Buffer
|
---|
| 456 | * @param nbyte Maximum number of bytes to read
|
---|
| 457 | * @param nread Place to store actual number of bytes read (0 or more)
|
---|
| 458 | *
|
---|
| 459 | * @return EOK on success, non-zero error code on error.
|
---|
| 460 | */
|
---|
| 461 | static int _read_short(int fildes, void *buf, size_t nbyte, ssize_t *nread)
|
---|
[2f02aa17] | 462 | {
|
---|
[96b02eb9] | 463 | sysarg_t rc;
|
---|
[2f02aa17] | 464 | ipc_call_t answer;
|
---|
| 465 | aid_t req;
|
---|
[19b28b0] | 466 |
|
---|
[fa24efa] | 467 | if (nbyte > DATA_XFER_LIMIT)
|
---|
| 468 | nbyte = DATA_XFER_LIMIT;
|
---|
| 469 |
|
---|
[79ae36dd] | 470 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 471 |
|
---|
| 472 | req = async_send_1(exch, VFS_IN_READ, fildes, &answer);
|
---|
[fa24efa] | 473 | rc = async_data_read_start(exch, (void *) buf, nbyte);
|
---|
[354b642] | 474 |
|
---|
[79ae36dd] | 475 | vfs_exchange_end(exch);
|
---|
[354b642] | 476 |
|
---|
| 477 | if (rc == EOK) {
|
---|
| 478 | async_wait_for(req, &rc);
|
---|
| 479 | } else {
|
---|
| 480 | async_forget(req);
|
---|
| 481 | }
|
---|
[6afc9d7] | 482 |
|
---|
| 483 | if (rc != EOK)
|
---|
[25becee8] | 484 | return rc;
|
---|
[6afc9d7] | 485 |
|
---|
| 486 | *nread = (ssize_t) IPC_GET_ARG1(answer);
|
---|
| 487 | return EOK;
|
---|
[2f02aa17] | 488 | }
|
---|
| 489 |
|
---|
[6afc9d7] | 490 | /** Write bytes to file.
|
---|
| 491 | *
|
---|
| 492 | * Write up to @a nbyte bytes from file. The actual number of bytes written
|
---|
| 493 | * may be lower, but greater than zero.
|
---|
| 494 | *
|
---|
| 495 | * @param fildes File descriptor
|
---|
| 496 | * @param buf Buffer
|
---|
| 497 | * @param nbyte Maximum number of bytes to write
|
---|
| 498 | * @param nread Place to store actual number of bytes written (0 or more)
|
---|
| 499 | *
|
---|
| 500 | * @return EOK on success, non-zero error code on error.
|
---|
| 501 | */
|
---|
| 502 | static int _write_short(int fildes, const void *buf, size_t nbyte,
|
---|
| 503 | ssize_t *nwritten)
|
---|
[449c246] | 504 | {
|
---|
[96b02eb9] | 505 | sysarg_t rc;
|
---|
[449c246] | 506 | ipc_call_t answer;
|
---|
| 507 | aid_t req;
|
---|
[19b28b0] | 508 |
|
---|
[fa24efa] | 509 | if (nbyte > DATA_XFER_LIMIT)
|
---|
| 510 | nbyte = DATA_XFER_LIMIT;
|
---|
| 511 |
|
---|
[79ae36dd] | 512 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 513 |
|
---|
| 514 | req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer);
|
---|
[fa24efa] | 515 | rc = async_data_write_start(exch, (void *) buf, nbyte);
|
---|
[6afc9d7] | 516 |
|
---|
[79ae36dd] | 517 | vfs_exchange_end(exch);
|
---|
[6afc9d7] | 518 |
|
---|
[354b642] | 519 | if (rc == EOK) {
|
---|
| 520 | async_wait_for(req, &rc);
|
---|
| 521 | } else {
|
---|
| 522 | async_forget(req);
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[6afc9d7] | 525 | if (rc != EOK)
|
---|
| 526 | return rc;
|
---|
| 527 |
|
---|
| 528 | *nwritten = (ssize_t) IPC_GET_ARG1(answer);
|
---|
| 529 | return EOK;
|
---|
[449c246] | 530 | }
|
---|
[222e57c] | 531 |
|
---|
[6afc9d7] | 532 | /** Read data.
|
---|
[8fd04ba9] | 533 | *
|
---|
[6afc9d7] | 534 | * Read up to @a nbytes bytes from file if available. This function always reads
|
---|
| 535 | * all the available bytes up to @a nbytes.
|
---|
[8fd04ba9] | 536 | *
|
---|
| 537 | * @param fildes File descriptor
|
---|
| 538 | * @param buf Buffer, @a nbytes bytes long
|
---|
| 539 | * @param nbytes Number of bytes to read
|
---|
| 540 | *
|
---|
[6afc9d7] | 541 | * @return On success, nonnegative number of bytes read.
|
---|
| 542 | * On failure, -1 and sets errno.
|
---|
[8fd04ba9] | 543 | */
|
---|
[6afc9d7] | 544 | ssize_t read(int fildes, void *buf, size_t nbyte)
|
---|
[8fd04ba9] | 545 | {
|
---|
| 546 | ssize_t cnt = 0;
|
---|
| 547 | size_t nread = 0;
|
---|
| 548 | uint8_t *bp = (uint8_t *) buf;
|
---|
[6afc9d7] | 549 | int rc;
|
---|
| 550 |
|
---|
[8fd04ba9] | 551 | do {
|
---|
| 552 | bp += cnt;
|
---|
| 553 | nread += cnt;
|
---|
[6afc9d7] | 554 | rc = _read_short(fildes, bp, nbyte - nread, &cnt);
|
---|
| 555 | } while (rc == EOK && cnt > 0 && (nbyte - nread - cnt) > 0);
|
---|
| 556 |
|
---|
| 557 | if (rc != EOK) {
|
---|
| 558 | errno = rc;
|
---|
| 559 | return -1;
|
---|
| 560 | }
|
---|
| 561 |
|
---|
[8fd04ba9] | 562 | return nread + cnt;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
[6afc9d7] | 565 | /** Write data.
|
---|
[8fd04ba9] | 566 | *
|
---|
| 567 | * This function fails if it cannot write exactly @a len bytes to the file.
|
---|
| 568 | *
|
---|
| 569 | * @param fildes File descriptor
|
---|
| 570 | * @param buf Data, @a nbytes bytes long
|
---|
| 571 | * @param nbytes Number of bytes to write
|
---|
| 572 | *
|
---|
[6afc9d7] | 573 | * @return On success, nonnegative number of bytes written.
|
---|
| 574 | * On failure, -1 and sets errno.
|
---|
[8fd04ba9] | 575 | */
|
---|
[6afc9d7] | 576 | ssize_t write(int fildes, const void *buf, size_t nbyte)
|
---|
[8fd04ba9] | 577 | {
|
---|
| 578 | ssize_t cnt = 0;
|
---|
| 579 | ssize_t nwritten = 0;
|
---|
| 580 | const uint8_t *bp = (uint8_t *) buf;
|
---|
[6afc9d7] | 581 | int rc;
|
---|
[8fd04ba9] | 582 |
|
---|
| 583 | do {
|
---|
| 584 | bp += cnt;
|
---|
| 585 | nwritten += cnt;
|
---|
[6afc9d7] | 586 | rc = _write_short(fildes, bp, nbyte - nwritten, &cnt);
|
---|
| 587 | } while (rc == EOK && ((ssize_t )nbyte - nwritten - cnt) > 0);
|
---|
[8fd04ba9] | 588 |
|
---|
[6afc9d7] | 589 | if (rc != EOK) {
|
---|
| 590 | errno = rc;
|
---|
| 591 | return -1;
|
---|
| 592 | }
|
---|
[8fd04ba9] | 593 |
|
---|
| 594 | return nbyte;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[6afc9d7] | 597 | /** Synchronize file.
|
---|
| 598 | *
|
---|
| 599 | * @param fildes File descriptor
|
---|
| 600 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 601 | */
|
---|
[2595dab] | 602 | int fsync(int fildes)
|
---|
| 603 | {
|
---|
[79ae36dd] | 604 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 605 | sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes);
|
---|
| 606 | vfs_exchange_end(exch);
|
---|
[2595dab] | 607 |
|
---|
[6afc9d7] | 608 | if (rc != EOK) {
|
---|
| 609 | errno = rc;
|
---|
| 610 | return -1;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | return 0;
|
---|
[2595dab] | 614 | }
|
---|
| 615 |
|
---|
[6afc9d7] | 616 | /** Seek to a position.
|
---|
| 617 | *
|
---|
| 618 | * @param fildes File descriptor
|
---|
| 619 | * @param offset Offset
|
---|
| 620 | * @param whence SEEK_SET, SEEK_CUR or SEEK_END
|
---|
| 621 | *
|
---|
| 622 | * @return On success the nonnegative offset from start of file. On error
|
---|
| 623 | * returns (off64_t)-1 and sets errno.
|
---|
| 624 | */
|
---|
[ed903174] | 625 | off64_t lseek(int fildes, off64_t offset, int whence)
|
---|
[222e57c] | 626 | {
|
---|
[79ae36dd] | 627 | async_exch_t *exch = vfs_exchange_begin();
|
---|
[19b28b0] | 628 |
|
---|
[96b02eb9] | 629 | sysarg_t newoff_lo;
|
---|
| 630 | sysarg_t newoff_hi;
|
---|
[79ae36dd] | 631 | sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes,
|
---|
[ed903174] | 632 | LOWER32(offset), UPPER32(offset), whence,
|
---|
| 633 | &newoff_lo, &newoff_hi);
|
---|
| 634 |
|
---|
[79ae36dd] | 635 | vfs_exchange_end(exch);
|
---|
[ed903174] | 636 |
|
---|
[6afc9d7] | 637 | if (rc != EOK) {
|
---|
| 638 | errno = rc;
|
---|
[ed903174] | 639 | return (off64_t) -1;
|
---|
[6afc9d7] | 640 | }
|
---|
[222e57c] | 641 |
|
---|
[ed903174] | 642 | return (off64_t) MERGE_LOUP32(newoff_lo, newoff_hi);
|
---|
[222e57c] | 643 | }
|
---|
| 644 |
|
---|
[6afc9d7] | 645 | /** Truncate file to a specified length.
|
---|
| 646 | *
|
---|
| 647 | * Truncate file so that its size is exactly @a length
|
---|
| 648 | *
|
---|
| 649 | * @param fildes File descriptor
|
---|
| 650 | * @param length Length
|
---|
| 651 | *
|
---|
| 652 | * @return 0 on success, -1 on error and sets errno.
|
---|
| 653 | */
|
---|
[ed903174] | 654 | int ftruncate(int fildes, aoff64_t length)
|
---|
[0ee4322] | 655 | {
|
---|
[96b02eb9] | 656 | sysarg_t rc;
|
---|
[0ee4322] | 657 |
|
---|
[79ae36dd] | 658 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 659 | rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes,
|
---|
[ed903174] | 660 | LOWER32(length), UPPER32(length));
|
---|
[79ae36dd] | 661 | vfs_exchange_end(exch);
|
---|
[ed903174] | 662 |
|
---|
[6afc9d7] | 663 | if (rc != EOK) {
|
---|
| 664 | errno = rc;
|
---|
| 665 | return -1;
|
---|
| 666 | }
|
---|
| 667 |
|
---|
| 668 | return 0;
|
---|
[0ee4322] | 669 | }
|
---|
| 670 |
|
---|
[6afc9d7] | 671 | /** Get file status.
|
---|
| 672 | *
|
---|
| 673 | * @param fildes File descriptor
|
---|
| 674 | * @param stat Place to store file information
|
---|
| 675 | *
|
---|
| 676 | * @return 0 on success, -1 on error and sets errno.
|
---|
| 677 | */
|
---|
[852b801] | 678 | int fstat(int fildes, struct stat *stat)
|
---|
| 679 | {
|
---|
[96b02eb9] | 680 | sysarg_t rc;
|
---|
[852b801] | 681 | aid_t req;
|
---|
| 682 |
|
---|
[79ae36dd] | 683 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 684 |
|
---|
| 685 | req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL);
|
---|
| 686 | rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
|
---|
[852b801] | 687 | if (rc != EOK) {
|
---|
[79ae36dd] | 688 | vfs_exchange_end(exch);
|
---|
[6afc9d7] | 689 |
|
---|
[96b02eb9] | 690 | sysarg_t rc_orig;
|
---|
[3734106] | 691 | async_wait_for(req, &rc_orig);
|
---|
[6afc9d7] | 692 |
|
---|
| 693 | if (rc_orig != EOK)
|
---|
| 694 | rc = rc_orig;
|
---|
| 695 | if (rc != EOK) {
|
---|
| 696 | errno = rc;
|
---|
| 697 | return -1;
|
---|
| 698 | }
|
---|
| 699 |
|
---|
| 700 | return 0;
|
---|
[852b801] | 701 | }
|
---|
[6afc9d7] | 702 |
|
---|
[79ae36dd] | 703 | vfs_exchange_end(exch);
|
---|
[852b801] | 704 | async_wait_for(req, &rc);
|
---|
[6afc9d7] | 705 |
|
---|
| 706 | if (rc != EOK) {
|
---|
| 707 | errno = rc;
|
---|
| 708 | return -1;
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | return 0;
|
---|
[852b801] | 712 | }
|
---|
| 713 |
|
---|
[6afc9d7] | 714 | /** Get file status.
|
---|
| 715 | *
|
---|
| 716 | * @param path Path to file
|
---|
| 717 | * @param stat Place to store file information
|
---|
| 718 | *
|
---|
| 719 | * @return 0 on success, -1 on error and sets errno.
|
---|
| 720 | */
|
---|
[415c7e0d] | 721 | int stat(const char *path, struct stat *stat)
|
---|
| 722 | {
|
---|
[5126f80] | 723 | int fd = vfs_lookup(path, 0);
|
---|
[0ed9cb6] | 724 | if (fd < 0) {
|
---|
[0b97336] | 725 | errno = fd;
|
---|
| 726 | return -1;
|
---|
[415c7e0d] | 727 | }
|
---|
[0ed9cb6] | 728 |
|
---|
| 729 | int rc = fstat(fd, stat);
|
---|
[0b97336] | 730 | if (rc != EOK) {
|
---|
| 731 | close(fd);
|
---|
| 732 | errno = rc;
|
---|
| 733 | rc = -1;
|
---|
| 734 | } else
|
---|
| 735 | rc = close(fd);
|
---|
| 736 |
|
---|
[415c7e0d] | 737 | return rc;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
[6afc9d7] | 740 | /** Open directory.
|
---|
| 741 | *
|
---|
| 742 | * @param dirname Directory pathname
|
---|
| 743 | *
|
---|
| 744 | * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
|
---|
| 745 | */
|
---|
[d0dc74ae] | 746 | DIR *opendir(const char *dirname)
|
---|
| 747 | {
|
---|
| 748 | DIR *dirp = malloc(sizeof(DIR));
|
---|
[9b48c06] | 749 | if (!dirp) {
|
---|
[6afc9d7] | 750 | errno = ENOMEM;
|
---|
[d0dc74ae] | 751 | return NULL;
|
---|
[6afc9d7] | 752 | }
|
---|
[2b88074b] | 753 |
|
---|
[0b97336] | 754 | int fd = vfs_lookup(dirname, WALK_DIRECTORY);
|
---|
| 755 | if (fd < 0) {
|
---|
[d0dc74ae] | 756 | free(dirp);
|
---|
[0b97336] | 757 | errno = fd;
|
---|
[9b48c06] | 758 | return NULL;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
[0b97336] | 761 | int rc = _vfs_open(fd, MODE_READ);
|
---|
[9b48c06] | 762 | if (rc < 0) {
|
---|
| 763 | free(dirp);
|
---|
[0b97336] | 764 | close(fd);
|
---|
[6afc9d7] | 765 | errno = rc;
|
---|
[d0dc74ae] | 766 | return NULL;
|
---|
| 767 | }
|
---|
[2b88074b] | 768 |
|
---|
[0b97336] | 769 | dirp->fd = fd;
|
---|
[d0dc74ae] | 770 | return dirp;
|
---|
| 771 | }
|
---|
| 772 |
|
---|
[6afc9d7] | 773 | /** Read directory entry.
|
---|
| 774 | *
|
---|
| 775 | * @param dirp Open directory
|
---|
| 776 | * @return Non-NULL pointer to directory entry on success. On error returns
|
---|
| 777 | * @c NULL and sets errno.
|
---|
| 778 | */
|
---|
[d0dc74ae] | 779 | struct dirent *readdir(DIR *dirp)
|
---|
| 780 | {
|
---|
[6afc9d7] | 781 | int rc;
|
---|
| 782 | ssize_t len;
|
---|
| 783 |
|
---|
| 784 | rc = _read_short(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1, &len);
|
---|
| 785 | if (rc != EOK) {
|
---|
| 786 | errno = rc;
|
---|
[5973fd0] | 787 | return NULL;
|
---|
[6afc9d7] | 788 | }
|
---|
| 789 |
|
---|
| 790 | (void) len;
|
---|
[5973fd0] | 791 | return &dirp->res;
|
---|
[d0dc74ae] | 792 | }
|
---|
| 793 |
|
---|
[6afc9d7] | 794 | /** Rewind directory position to the beginning.
|
---|
| 795 | *
|
---|
| 796 | * @param dirp Open directory
|
---|
| 797 | */
|
---|
[d0dc74ae] | 798 | void rewinddir(DIR *dirp)
|
---|
| 799 | {
|
---|
[5973fd0] | 800 | (void) lseek(dirp->fd, 0, SEEK_SET);
|
---|
[d0dc74ae] | 801 | }
|
---|
| 802 |
|
---|
[6afc9d7] | 803 | /** Close directory.
|
---|
| 804 | *
|
---|
| 805 | * @param dirp Open directory
|
---|
| 806 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 807 | */
|
---|
[d0dc74ae] | 808 | int closedir(DIR *dirp)
|
---|
| 809 | {
|
---|
[6afc9d7] | 810 | int rc;
|
---|
| 811 |
|
---|
| 812 | rc = close(dirp->fd);
|
---|
[d0dc74ae] | 813 | free(dirp);
|
---|
[6afc9d7] | 814 |
|
---|
| 815 | /* On error errno was set by close() */
|
---|
| 816 | return rc;
|
---|
[d0dc74ae] | 817 | }
|
---|
| 818 |
|
---|
[6afc9d7] | 819 | /** Create directory.
|
---|
| 820 | *
|
---|
| 821 | * @param path Path
|
---|
| 822 | * @param mode File mode
|
---|
| 823 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 824 | */
|
---|
[72bde81] | 825 | int mkdir(const char *path, mode_t mode)
|
---|
[d0dc74ae] | 826 | {
|
---|
[0b97336] | 827 | int fd = vfs_lookup(path, WALK_MUST_CREATE | WALK_DIRECTORY);
|
---|
| 828 | if (fd < 0) {
|
---|
| 829 | errno = fd;
|
---|
| 830 | return -1;
|
---|
[6afc9d7] | 831 | }
|
---|
| 832 |
|
---|
[0b97336] | 833 | return close(fd);
|
---|
[d0dc74ae] | 834 | }
|
---|
| 835 |
|
---|
[d18c404] | 836 | static int _vfs_unlink2(int parent, const char *path, int expect, int wflag)
|
---|
[f15cf1a6] | 837 | {
|
---|
[96b02eb9] | 838 | sysarg_t rc;
|
---|
[f15cf1a6] | 839 | aid_t req;
|
---|
| 840 |
|
---|
[79ae36dd] | 841 | async_exch_t *exch = vfs_exchange_begin();
|
---|
| 842 |
|
---|
[d18c404] | 843 | req = async_send_3(exch, VFS_IN_UNLINK2, parent, expect, wflag, NULL);
|
---|
| 844 | rc = async_data_write_start(exch, path, str_size(path));
|
---|
| 845 |
|
---|
[79ae36dd] | 846 | vfs_exchange_end(exch);
|
---|
[d18c404] | 847 |
|
---|
| 848 | sysarg_t rc_orig;
|
---|
| 849 | async_wait_for(req, &rc_orig);
|
---|
| 850 |
|
---|
| 851 | if (rc_orig != EOK) {
|
---|
| 852 | return (int) rc_orig;
|
---|
| 853 | }
|
---|
[2595dab] | 854 | return rc;
|
---|
[f15cf1a6] | 855 | }
|
---|
| 856 |
|
---|
[6afc9d7] | 857 | /** Unlink file or directory.
|
---|
| 858 | *
|
---|
| 859 | * @param path Path
|
---|
| 860 | * @return EOk on success, error code on error
|
---|
| 861 | */
|
---|
[f15cf1a6] | 862 | int unlink(const char *path)
|
---|
| 863 | {
|
---|
[d18c404] | 864 | size_t pa_size;
|
---|
[1dff985] | 865 | char *pa = vfs_absolutize(path, &pa_size);
|
---|
[d18c404] | 866 | if (!pa) {
|
---|
[0b97336] | 867 | errno = ENOMEM;
|
---|
| 868 | return -1;
|
---|
[6afc9d7] | 869 | }
|
---|
[d18c404] | 870 |
|
---|
[5126f80] | 871 | int root = vfs_root();
|
---|
| 872 | if (root < 0) {
|
---|
| 873 | free(pa);
|
---|
[0b97336] | 874 | errno = ENOENT;
|
---|
| 875 | return -1;
|
---|
[5126f80] | 876 | }
|
---|
| 877 |
|
---|
| 878 | int rc = _vfs_unlink2(root, pa, -1, 0);
|
---|
| 879 |
|
---|
[0b97336] | 880 | if (rc != EOK) {
|
---|
| 881 | errno = rc;
|
---|
| 882 | rc = -1;
|
---|
| 883 | }
|
---|
| 884 |
|
---|
[5126f80] | 885 | free(pa);
|
---|
| 886 | close(root);
|
---|
| 887 | return rc;
|
---|
[f15cf1a6] | 888 | }
|
---|
| 889 |
|
---|
[6afc9d7] | 890 | /** Remove empty directory.
|
---|
| 891 | *
|
---|
| 892 | * @param path Path
|
---|
| 893 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 894 | */
|
---|
[f15cf1a6] | 895 | int rmdir(const char *path)
|
---|
| 896 | {
|
---|
[d18c404] | 897 | size_t pa_size;
|
---|
[1dff985] | 898 | char *pa = vfs_absolutize(path, &pa_size);
|
---|
[d18c404] | 899 | if (!pa) {
|
---|
[0b97336] | 900 | errno = ENOMEM;
|
---|
| 901 | return -1;
|
---|
[6afc9d7] | 902 | }
|
---|
[d18c404] | 903 |
|
---|
[5126f80] | 904 | int root = vfs_root();
|
---|
| 905 | if (root < 0) {
|
---|
| 906 | free(pa);
|
---|
[0b97336] | 907 | errno = ENOENT;
|
---|
| 908 | return -1;
|
---|
[5126f80] | 909 | }
|
---|
| 910 |
|
---|
| 911 | int rc = _vfs_unlink2(root, pa, -1, WALK_DIRECTORY);
|
---|
[0b97336] | 912 |
|
---|
| 913 | if (rc != EOK) {
|
---|
| 914 | errno = rc;
|
---|
| 915 | rc = -1;
|
---|
| 916 | }
|
---|
[5126f80] | 917 |
|
---|
| 918 | free(pa);
|
---|
| 919 | close(root);
|
---|
| 920 | return rc;
|
---|
[f15cf1a6] | 921 | }
|
---|
| 922 |
|
---|
[6afc9d7] | 923 | /** Rename directory entry.
|
---|
| 924 | *
|
---|
| 925 | * @param old Old name
|
---|
| 926 | * @param new New name
|
---|
| 927 | *
|
---|
| 928 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 929 | */
|
---|
[a8e9ab8d] | 930 | int rename(const char *old, const char *new)
|
---|
| 931 | {
|
---|
[96b02eb9] | 932 | sysarg_t rc;
|
---|
| 933 | sysarg_t rc_orig;
|
---|
[a8e9ab8d] | 934 | aid_t req;
|
---|
| 935 |
|
---|
[9eb3623] | 936 | size_t olda_size;
|
---|
[6afc9d7] | 937 | char *olda = vfs_absolutize(old, &olda_size);
|
---|
| 938 | if (olda == NULL) {
|
---|
| 939 | errno = ENOMEM;
|
---|
| 940 | return -1;
|
---|
| 941 | }
|
---|
[923c39e] | 942 |
|
---|
[9eb3623] | 943 | size_t newa_size;
|
---|
[6afc9d7] | 944 | char *newa = vfs_absolutize(new, &newa_size);
|
---|
| 945 | if (newa == NULL) {
|
---|
[a8e9ab8d] | 946 | free(olda);
|
---|
[6afc9d7] | 947 | errno = ENOMEM;
|
---|
| 948 | return -1;
|
---|
[a8e9ab8d] | 949 | }
|
---|
[19b28b0] | 950 |
|
---|
[79ae36dd] | 951 | async_exch_t *exch = vfs_exchange_begin();
|
---|
[5126f80] | 952 | int root = vfs_root();
|
---|
| 953 | if (root < 0) {
|
---|
| 954 | free(olda);
|
---|
| 955 | free(newa);
|
---|
[0b97336] | 956 | errno = ENOENT;
|
---|
| 957 | return -1;
|
---|
[5126f80] | 958 | }
|
---|
[79ae36dd] | 959 |
|
---|
[5126f80] | 960 | req = async_send_1(exch, VFS_IN_RENAME, root, NULL);
|
---|
[79ae36dd] | 961 | rc = async_data_write_start(exch, olda, olda_size);
|
---|
[a8e9ab8d] | 962 | if (rc != EOK) {
|
---|
[79ae36dd] | 963 | vfs_exchange_end(exch);
|
---|
[a8e9ab8d] | 964 | free(olda);
|
---|
| 965 | free(newa);
|
---|
[5126f80] | 966 | close(root);
|
---|
[a28ab12] | 967 | async_wait_for(req, &rc_orig);
|
---|
[6afc9d7] | 968 | if (rc_orig != EOK)
|
---|
| 969 | rc = rc_orig;
|
---|
| 970 | if (rc != EOK) {
|
---|
| 971 | errno = rc;
|
---|
| 972 | return -1;
|
---|
| 973 | }
|
---|
| 974 | return 0;
|
---|
[a8e9ab8d] | 975 | }
|
---|
[79ae36dd] | 976 | rc = async_data_write_start(exch, newa, newa_size);
|
---|
[a8e9ab8d] | 977 | if (rc != EOK) {
|
---|
[79ae36dd] | 978 | vfs_exchange_end(exch);
|
---|
[a8e9ab8d] | 979 | free(olda);
|
---|
| 980 | free(newa);
|
---|
[5126f80] | 981 | close(root);
|
---|
[a28ab12] | 982 | async_wait_for(req, &rc_orig);
|
---|
[6afc9d7] | 983 | if (rc_orig != EOK)
|
---|
| 984 | rc = rc_orig;
|
---|
| 985 | if (rc != EOK) {
|
---|
| 986 | errno = rc;
|
---|
| 987 | return -1;
|
---|
| 988 | }
|
---|
| 989 | return 0;
|
---|
[a8e9ab8d] | 990 | }
|
---|
[79ae36dd] | 991 | vfs_exchange_end(exch);
|
---|
[a8e9ab8d] | 992 | free(olda);
|
---|
| 993 | free(newa);
|
---|
[5126f80] | 994 | close(root);
|
---|
[a28ab12] | 995 | async_wait_for(req, &rc);
|
---|
[6afc9d7] | 996 |
|
---|
| 997 | if (rc != EOK) {
|
---|
| 998 | errno = rc;
|
---|
| 999 | return -1;
|
---|
| 1000 | }
|
---|
| 1001 |
|
---|
| 1002 | return 0;
|
---|
[a8e9ab8d] | 1003 | }
|
---|
| 1004 |
|
---|
[6afc9d7] | 1005 | /** Remove directory entry.
|
---|
| 1006 | *
|
---|
| 1007 | * @param path Path
|
---|
| 1008 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 1009 | */
|
---|
[38db6288] | 1010 | int remove(const char *path)
|
---|
| 1011 | {
|
---|
| 1012 | return unlink(path);
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
[6afc9d7] | 1015 | /** Change working directory.
|
---|
| 1016 | *
|
---|
| 1017 | * @param path Path
|
---|
| 1018 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 1019 | */
|
---|
[5fec355] | 1020 | int chdir(const char *path)
|
---|
| 1021 | {
|
---|
[2b88074b] | 1022 | size_t abs_size;
|
---|
[6afc9d7] | 1023 | char *abs = vfs_absolutize(path, &abs_size);
|
---|
[0b97336] | 1024 | if (!abs) {
|
---|
| 1025 | errno = ENOMEM;
|
---|
| 1026 | return -1;
|
---|
| 1027 | }
|
---|
[2b88074b] | 1028 |
|
---|
[5126f80] | 1029 | int fd = vfs_lookup(abs, WALK_DIRECTORY);
|
---|
[2b88074b] | 1030 | if (fd < 0) {
|
---|
| 1031 | free(abs);
|
---|
[1dff985] | 1032 | errno = fd;
|
---|
[6afc9d7] | 1033 | return -1;
|
---|
[5fec355] | 1034 | }
|
---|
[2b88074b] | 1035 |
|
---|
[a28ab12] | 1036 | fibril_mutex_lock(&cwd_mutex);
|
---|
[2b88074b] | 1037 |
|
---|
[5126f80] | 1038 | if (cwd_fd >= 0) {
|
---|
[2b88074b] | 1039 | close(cwd_fd);
|
---|
[5126f80] | 1040 | }
|
---|
[2b88074b] | 1041 |
|
---|
[5126f80] | 1042 | if (cwd_path) {
|
---|
[2b88074b] | 1043 | free(cwd_path);
|
---|
[5126f80] | 1044 | }
|
---|
[2b88074b] | 1045 |
|
---|
| 1046 | cwd_fd = fd;
|
---|
| 1047 | cwd_path = abs;
|
---|
| 1048 | cwd_size = abs_size;
|
---|
| 1049 |
|
---|
[a28ab12] | 1050 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[6afc9d7] | 1051 | return 0;
|
---|
[5fec355] | 1052 | }
|
---|
| 1053 |
|
---|
[6afc9d7] | 1054 | /** Get current working directory path.
|
---|
| 1055 | *
|
---|
| 1056 | * @param buf Buffer
|
---|
| 1057 | * @param size Size of @a buf
|
---|
| 1058 | * @return On success returns @a buf. On failure returns @c NULL and sets errno.
|
---|
| 1059 | */
|
---|
[5fec355] | 1060 | char *getcwd(char *buf, size_t size)
|
---|
| 1061 | {
|
---|
[6afc9d7] | 1062 | if (size == 0) {
|
---|
| 1063 | errno = EINVAL;
|
---|
[5fec355] | 1064 | return NULL;
|
---|
[6afc9d7] | 1065 | }
|
---|
[2b88074b] | 1066 |
|
---|
[a28ab12] | 1067 | fibril_mutex_lock(&cwd_mutex);
|
---|
[2b88074b] | 1068 |
|
---|
| 1069 | if ((cwd_size == 0) || (size < cwd_size + 1)) {
|
---|
[a28ab12] | 1070 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[6afc9d7] | 1071 | errno = ERANGE;
|
---|
[5fec355] | 1072 | return NULL;
|
---|
| 1073 | }
|
---|
[2b88074b] | 1074 |
|
---|
[6eb2e96] | 1075 | str_cpy(buf, size, cwd_path);
|
---|
[a28ab12] | 1076 | fibril_mutex_unlock(&cwd_mutex);
|
---|
[2b88074b] | 1077 |
|
---|
[5fec355] | 1078 | return buf;
|
---|
| 1079 | }
|
---|
| 1080 |
|
---|
[6afc9d7] | 1081 | /** Open session to service represented by a special file.
|
---|
| 1082 | *
|
---|
| 1083 | * Given that the file referred to by @a fildes represents a service,
|
---|
| 1084 | * open a session to that service.
|
---|
| 1085 | *
|
---|
| 1086 | * @param fildes File descriptor
|
---|
| 1087 | * @param iface Interface to connect to (XXX Should be automatic)
|
---|
| 1088 | * @return On success returns session pointer. On error returns @c NULL.
|
---|
| 1089 | */
|
---|
| 1090 | async_sess_t *vfs_fd_session(int fildes, iface_t iface)
|
---|
[852b801] | 1091 | {
|
---|
| 1092 | struct stat stat;
|
---|
[8caaea7] | 1093 | int rc = fstat(fildes, &stat);
|
---|
[6afc9d7] | 1094 | if (rc != 0)
|
---|
[79ae36dd] | 1095 | return NULL;
|
---|
[8caaea7] | 1096 |
|
---|
[6afc9d7] | 1097 | if (stat.service == 0)
|
---|
[79ae36dd] | 1098 | return NULL;
|
---|
[852b801] | 1099 |
|
---|
[f9b2cb4c] | 1100 | return loc_service_connect(stat.service, iface, 0);
|
---|
[852b801] | 1101 | }
|
---|
| 1102 |
|
---|
[6afc9d7] | 1103 | /** Duplicate open file.
|
---|
| 1104 | *
|
---|
| 1105 | * Duplicate open file under a new file descriptor.
|
---|
| 1106 | *
|
---|
| 1107 | * @param oldfd Old file descriptor
|
---|
| 1108 | * @param newfd New file descriptor
|
---|
| 1109 | * @return 0 on success. On error -1 is returned and errno is set
|
---|
| 1110 | */
|
---|
[2b88074b] | 1111 | int dup2(int oldfd, int newfd)
|
---|
[852b801] | 1112 | {
|
---|
[79ae36dd] | 1113 | async_exch_t *exch = vfs_exchange_begin();
|
---|
[852b801] | 1114 |
|
---|
[96b02eb9] | 1115 | sysarg_t ret;
|
---|
[79ae36dd] | 1116 | sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret);
|
---|
[852b801] | 1117 |
|
---|
[79ae36dd] | 1118 | vfs_exchange_end(exch);
|
---|
[2b88074b] | 1119 |
|
---|
| 1120 | if (rc == EOK)
|
---|
[6afc9d7] | 1121 | rc = ret;
|
---|
[2b88074b] | 1122 |
|
---|
[6afc9d7] | 1123 | if (rc != EOK) {
|
---|
| 1124 | errno = rc;
|
---|
| 1125 | return -1;
|
---|
| 1126 | }
|
---|
| 1127 |
|
---|
| 1128 | return 0;
|
---|
[852b801] | 1129 | }
|
---|
| 1130 |
|
---|
[8ffedd8] | 1131 | static void process_mp(const char *path, struct stat *stat, list_t *mtab_list)
|
---|
[10e4cd7] | 1132 | {
|
---|
[8ffedd8] | 1133 | mtab_ent_t *ent;
|
---|
| 1134 |
|
---|
| 1135 | ent = (mtab_ent_t *) malloc(sizeof(mtab_ent_t));
|
---|
| 1136 | if (!ent)
|
---|
| 1137 | return;
|
---|
| 1138 |
|
---|
| 1139 | link_initialize(&ent->link);
|
---|
| 1140 | str_cpy(ent->mp, sizeof(ent->mp), path);
|
---|
| 1141 | ent->service_id = stat->service_id;
|
---|
[5a2b765] | 1142 |
|
---|
| 1143 | struct statfs stfs;
|
---|
| 1144 | if (statfs(path, &stfs) == EOK)
|
---|
| 1145 | str_cpy(ent->fs_name, sizeof(ent->fs_name), stfs.fs_name);
|
---|
| 1146 | else
|
---|
| 1147 | str_cpy(ent->fs_name, sizeof(ent->fs_name), "?");
|
---|
[10e4cd7] | 1148 |
|
---|
[8ffedd8] | 1149 | list_append(&ent->link, mtab_list);
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
| 1152 | static int vfs_get_mtab_visit(const char *path, list_t *mtab_list,
|
---|
| 1153 | fs_handle_t fs_handle, service_id_t service_id)
|
---|
| 1154 | {
|
---|
| 1155 | DIR *dir;
|
---|
| 1156 | struct dirent *dirent;
|
---|
| 1157 |
|
---|
| 1158 | dir = opendir(path);
|
---|
| 1159 | if (!dir)
|
---|
[0b97336] | 1160 | return ENOENT;
|
---|
[8ffedd8] | 1161 |
|
---|
| 1162 | while ((dirent = readdir(dir)) != NULL) {
|
---|
| 1163 | char *child;
|
---|
| 1164 | struct stat st;
|
---|
| 1165 | int rc;
|
---|
| 1166 |
|
---|
| 1167 | rc = asprintf(&child, "%s/%s", path, dirent->d_name);
|
---|
| 1168 | if (rc < 0) {
|
---|
| 1169 | closedir(dir);
|
---|
[0b97336] | 1170 | return rc;
|
---|
[8ffedd8] | 1171 | }
|
---|
| 1172 |
|
---|
| 1173 | rc = stat(child, &st);
|
---|
| 1174 | if (rc != 0) {
|
---|
| 1175 | free(child);
|
---|
| 1176 | closedir(dir);
|
---|
[0b97336] | 1177 | return rc;
|
---|
[8ffedd8] | 1178 | }
|
---|
| 1179 |
|
---|
| 1180 | if (st.fs_handle != fs_handle || st.service_id != service_id) {
|
---|
| 1181 | /*
|
---|
| 1182 | * We have discovered a mountpoint.
|
---|
| 1183 | */
|
---|
| 1184 | process_mp(child, &st, mtab_list);
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
| 1187 | if (st.is_directory) {
|
---|
| 1188 | (void) vfs_get_mtab_visit(child, mtab_list,
|
---|
| 1189 | st.fs_handle, st.service_id);
|
---|
| 1190 | }
|
---|
| 1191 |
|
---|
| 1192 | free(child);
|
---|
| 1193 | }
|
---|
| 1194 |
|
---|
| 1195 | closedir(dir);
|
---|
| 1196 | return EOK;
|
---|
| 1197 | }
|
---|
| 1198 |
|
---|
| 1199 | int vfs_get_mtab_list(list_t *mtab_list)
|
---|
| 1200 | {
|
---|
| 1201 | struct stat st;
|
---|
| 1202 |
|
---|
| 1203 | int rc = stat("/", &st);
|
---|
| 1204 | if (rc != 0)
|
---|
| 1205 | return rc;
|
---|
| 1206 |
|
---|
| 1207 | process_mp("/", &st, mtab_list);
|
---|
| 1208 |
|
---|
| 1209 | return vfs_get_mtab_visit("", mtab_list, st.fs_handle, st.service_id);
|
---|
[10e4cd7] | 1210 | }
|
---|
| 1211 |
|
---|
[6afc9d7] | 1212 | /** Get filesystem statistics.
|
---|
| 1213 | *
|
---|
| 1214 | * @param path Mount point path
|
---|
| 1215 | * @param st Buffer for storing information
|
---|
| 1216 | * @return 0 on success. On error -1 is returned and errno is set.
|
---|
| 1217 | */
|
---|
[73fbcbb] | 1218 | int statfs(const char *path, struct statfs *st)
|
---|
[66366470] | 1219 | {
|
---|
[5126f80] | 1220 | int fd = vfs_lookup(path, 0);
|
---|
[a737667e] | 1221 | if (fd < 0) {
|
---|
| 1222 | errno = fd;
|
---|
| 1223 | return -1;
|
---|
| 1224 | }
|
---|
| 1225 |
|
---|
| 1226 | sysarg_t rc, ret;
|
---|
| 1227 | aid_t req;
|
---|
[73fbcbb] | 1228 |
|
---|
[a737667e] | 1229 | async_exch_t *exch = vfs_exchange_begin();
|
---|
[73fbcbb] | 1230 |
|
---|
[a737667e] | 1231 | req = async_send_1(exch, VFS_IN_STATFS, fd, NULL);
|
---|
[73fbcbb] | 1232 | rc = async_data_read_start(exch, (void *) st, sizeof(*st));
|
---|
| 1233 |
|
---|
[66366470] | 1234 | vfs_exchange_end(exch);
|
---|
[a737667e] | 1235 | async_wait_for(req, &ret);
|
---|
| 1236 | close(fd);
|
---|
[6afc9d7] | 1237 |
|
---|
[a737667e] | 1238 | rc = (ret != EOK ? ret : rc);
|
---|
[6afc9d7] | 1239 | if (rc != EOK) {
|
---|
| 1240 | errno = rc;
|
---|
| 1241 | return -1;
|
---|
| 1242 | }
|
---|
| 1243 |
|
---|
| 1244 | return 0;
|
---|
[66366470] | 1245 | }
|
---|
| 1246 |
|
---|
[354b642] | 1247 | int vfs_pass_handle(async_exch_t *vfs_exch, int file, async_exch_t *exch)
|
---|
| 1248 | {
|
---|
| 1249 | return async_state_change_start(exch, VFS_PASS_HANDLE, (sysarg_t)file, 0, vfs_exch);
|
---|
| 1250 | }
|
---|
| 1251 |
|
---|
[bb9ec2d] | 1252 | int vfs_receive_handle(bool high_descriptor)
|
---|
[354b642] | 1253 | {
|
---|
| 1254 | ipc_callid_t callid;
|
---|
| 1255 | if (!async_state_change_receive(&callid, NULL, NULL, NULL)) {
|
---|
| 1256 | async_answer_0(callid, EINVAL);
|
---|
| 1257 | return EINVAL;
|
---|
| 1258 | }
|
---|
| 1259 |
|
---|
| 1260 | async_exch_t *vfs_exch = vfs_exchange_begin();
|
---|
| 1261 |
|
---|
| 1262 | async_state_change_finalize(callid, vfs_exch);
|
---|
| 1263 |
|
---|
| 1264 | sysarg_t ret;
|
---|
[bb9ec2d] | 1265 | sysarg_t rc = async_req_1_1(vfs_exch, VFS_IN_WAIT_HANDLE, high_descriptor, &ret);
|
---|
[354b642] | 1266 |
|
---|
| 1267 | async_exchange_end(vfs_exch);
|
---|
| 1268 |
|
---|
| 1269 | if (rc != EOK) {
|
---|
| 1270 | return rc;
|
---|
| 1271 | }
|
---|
| 1272 | return ret;
|
---|
| 1273 | }
|
---|
| 1274 |
|
---|
| 1275 | int vfs_clone(int file, bool high_descriptor)
|
---|
| 1276 | {
|
---|
| 1277 | async_exch_t *vfs_exch = vfs_exchange_begin();
|
---|
| 1278 | int rc = async_req_2_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file, (sysarg_t) high_descriptor);
|
---|
| 1279 | vfs_exchange_end(vfs_exch);
|
---|
| 1280 | return rc;
|
---|
| 1281 | }
|
---|
| 1282 |
|
---|
[2f02aa17] | 1283 | /** @}
|
---|
| 1284 | */
|
---|