paul@90 | 1 | /* |
paul@90 | 2 | * Filesystem client functions. |
paul@90 | 3 | * |
paul@607 | 4 | * Copyright (C) 2018-2024 Paul Boddie <paul@boddie.org.uk> |
paul@90 | 5 | * |
paul@90 | 6 | * This program is free software; you can redistribute it and/or |
paul@90 | 7 | * modify it under the terms of the GNU General Public License as |
paul@90 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@90 | 9 | * the License, or (at your option) any later version. |
paul@90 | 10 | * |
paul@90 | 11 | * This program is distributed in the hope that it will be useful, |
paul@90 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@90 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@90 | 14 | * GNU General Public License for more details. |
paul@90 | 15 | * |
paul@90 | 16 | * You should have received a copy of the GNU General Public License |
paul@90 | 17 | * along with this program; if not, write to the Free Software |
paul@90 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@90 | 19 | * Boston, MA 02110-1301, USA |
paul@90 | 20 | */ |
paul@90 | 21 | |
paul@90 | 22 | #include <l4/re/env.h> |
paul@90 | 23 | |
paul@90 | 24 | #include <stdio.h> |
paul@90 | 25 | #include <stdlib.h> |
paul@174 | 26 | #include <string.h> |
paul@90 | 27 | |
paul@521 | 28 | #include <mem/memory_utils.h> |
paul@507 | 29 | #include <systypes/env.h> |
paul@175 | 30 | #include <systypes/fcntl.h> |
paul@175 | 31 | |
paul@90 | 32 | #include "client.h" |
paul@90 | 33 | |
paul@90 | 34 | |
paul@90 | 35 | |
paul@90 | 36 | /* Default size of pipe regions. */ |
paul@90 | 37 | |
paul@646 | 38 | static const offset_t DEFAULT_PIPE_SIZE = 4096; |
paul@90 | 39 | |
paul@174 | 40 | /* Size of the core member region of a directory entry structure. */ |
paul@174 | 41 | |
paul@646 | 42 | static const offset_t DIRENT_CORE_SIZE = (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name)); |
paul@174 | 43 | |
paul@90 | 44 | |
paul@90 | 45 | |
paul@330 | 46 | /* Merging of region flags from protection and access flags. */ |
paul@330 | 47 | |
paul@634 | 48 | static rm_flags_t _combine_region_flags(rm_flags_t region_flags, flags_t flags) |
paul@330 | 49 | { |
paul@330 | 50 | return region_flags & (file_region_flags(flags) | L4RE_RM_F_X); |
paul@330 | 51 | } |
paul@330 | 52 | |
paul@330 | 53 | /* Conversion of protection and access flags to region flags. */ |
paul@330 | 54 | |
paul@634 | 55 | rm_flags_t client_region_flags(prot_t prot, flags_t flags) |
paul@330 | 56 | { |
paul@634 | 57 | rm_flags_t rm_flags = 0; |
paul@330 | 58 | |
paul@330 | 59 | if (prot & PROT_READ) |
paul@330 | 60 | rm_flags |= L4RE_RM_F_R; |
paul@330 | 61 | if (prot & PROT_WRITE) |
paul@330 | 62 | rm_flags |= L4RE_RM_F_W; |
paul@330 | 63 | if (prot & PROT_EXEC) |
paul@330 | 64 | rm_flags |= L4RE_RM_F_X; |
paul@330 | 65 | |
paul@330 | 66 | return _combine_region_flags(rm_flags, flags); |
paul@330 | 67 | } |
paul@330 | 68 | |
paul@330 | 69 | |
paul@330 | 70 | |
paul@169 | 71 | /* Access the given position and synchronise state with the file object. Pipe |
paul@169 | 72 | objects may return busy conditions indicating that the desired access cannot |
paul@169 | 73 | yet be fulfilled. */ |
paul@117 | 74 | |
paul@117 | 75 | static long _access(file_t *file, offset_t position) |
paul@117 | 76 | { |
paul@117 | 77 | long err; |
paul@117 | 78 | |
paul@171 | 79 | if (file->object_flags & OBJECT_SUPPORTS_MMAP) |
paul@117 | 80 | { |
paul@521 | 81 | offset_t adjusted_position = position; |
paul@521 | 82 | |
paul@117 | 83 | /* Where the position is outside the current region, re-map. */ |
paul@117 | 84 | |
paul@117 | 85 | if ((position < file->start_pos) || (position >= file->end_pos)) |
paul@117 | 86 | { |
paul@521 | 87 | offset_t length = file_span(file); |
paul@521 | 88 | |
paul@521 | 89 | if (!length) |
paul@521 | 90 | length = PAGE_SIZE; |
paul@521 | 91 | |
paul@521 | 92 | /* Avoid growth of the mapped region when the end of the region is |
paul@521 | 93 | calculated to be position + span, which is then rounded up, whereas the |
paul@521 | 94 | start of the region is rounded down. */ |
paul@521 | 95 | |
paul@521 | 96 | adjusted_position = trunc(position, PAGE_SIZE); |
paul@521 | 97 | |
paul@521 | 98 | if (file_mmap(file, adjusted_position, length, 0, 0, |
paul@330 | 99 | file_region_flags(file->flags))) |
paul@117 | 100 | return -L4_EIO; |
paul@117 | 101 | } |
paul@117 | 102 | |
paul@117 | 103 | /* Otherwise, flush any written data in the current region and update the |
paul@117 | 104 | file size details. */ |
paul@117 | 105 | |
paul@117 | 106 | else |
paul@117 | 107 | { |
paul@117 | 108 | err = client_flush(file); |
paul@117 | 109 | if (err) |
paul@117 | 110 | return err; |
paul@117 | 111 | } |
paul@165 | 112 | |
paul@165 | 113 | /* Update the current data offset. */ |
paul@165 | 114 | |
paul@165 | 115 | file->data_current = position - file->start_pos; |
paul@165 | 116 | |
paul@165 | 117 | return L4_EOK; |
paul@117 | 118 | } |
paul@117 | 119 | else |
paul@117 | 120 | { |
paul@643 | 121 | bool initial = (file->memory == NULL); |
paul@643 | 122 | |
paul@192 | 123 | /* Handle the initial condition with no current region. */ |
paul@192 | 124 | |
paul@643 | 125 | if (initial) |
paul@192 | 126 | { |
paul@192 | 127 | err = client_current_region(file); |
paul@192 | 128 | if (err) |
paul@192 | 129 | return err; |
paul@192 | 130 | } |
paul@192 | 131 | |
paul@117 | 132 | /* Strict conditions for region navigation in pipes. */ |
paul@117 | 133 | |
paul@117 | 134 | if ((position < file->start_pos) || (position > file->end_pos)) |
paul@117 | 135 | { |
paul@117 | 136 | return -L4_EIO; |
paul@117 | 137 | } |
paul@117 | 138 | |
paul@117 | 139 | /* The next region is only available at the end of the mapped memory. */ |
paul@117 | 140 | |
paul@117 | 141 | else if (position == file->end_pos) |
paul@117 | 142 | { |
paul@117 | 143 | err = client_next_region(file); |
paul@117 | 144 | if (err) |
paul@117 | 145 | return err; |
paul@117 | 146 | |
paul@117 | 147 | file->data_current = 0; |
paul@117 | 148 | return L4_EOK; |
paul@117 | 149 | } |
paul@117 | 150 | |
paul@117 | 151 | /* Within the current pipe region, synchronise with the pipe object. */ |
paul@117 | 152 | |
paul@117 | 153 | else |
paul@402 | 154 | { |
paul@643 | 155 | if (initial) |
paul@643 | 156 | return L4_EOK; |
paul@643 | 157 | else |
paul@643 | 158 | return client_current_region(file); |
paul@402 | 159 | } |
paul@117 | 160 | } |
paul@117 | 161 | } |
paul@117 | 162 | |
paul@117 | 163 | |
paul@117 | 164 | |
paul@162 | 165 | /* Return whether an operation on file should block for more content or more |
paul@169 | 166 | space. A file must be configured for blocking, not be closed, and must either |
paul@169 | 167 | be lacking content (if reading) or space (if writing). */ |
paul@162 | 168 | |
paul@162 | 169 | static int _operation_blocking(file_t *file, int reading) |
paul@162 | 170 | { |
paul@479 | 171 | return (file->can_block && !(file->notifiable.notifications & NOTIFY_PEER_CLOSED) && ( |
paul@402 | 172 | (reading && !file_data_available(file)) || |
paul@402 | 173 | (!reading && !file_data_space(file)))); |
paul@162 | 174 | } |
paul@162 | 175 | |
paul@162 | 176 | |
paul@162 | 177 | |
paul@117 | 178 | /* Return whether an access could occur, blocking if necessary. */ |
paul@117 | 179 | |
paul@162 | 180 | static int _access_blocking(file_t *file, offset_t position, int reading) |
paul@117 | 181 | { |
paul@117 | 182 | long err; |
paul@117 | 183 | |
paul@162 | 184 | /* Attempt to access the position, handling an error condition or a blocking |
paul@162 | 185 | condition. */ |
paul@162 | 186 | |
paul@162 | 187 | while ((err = _access(file, position)) || _operation_blocking(file, reading)) |
paul@117 | 188 | { |
paul@165 | 189 | position = file->data_current; |
paul@165 | 190 | |
paul@117 | 191 | /* Exit if blocking is not configured or suitable. */ |
paul@117 | 192 | |
paul@162 | 193 | if ((err && (err != -L4_EBUSY)) || !file->can_block) |
paul@117 | 194 | return 0; |
paul@117 | 195 | |
paul@117 | 196 | /* Handle an inability to access by blocking, exiting if waiting failed. */ |
paul@117 | 197 | |
paul@180 | 198 | if (client_wait_file(file, client_notifier_task())) |
paul@117 | 199 | return 0; |
paul@117 | 200 | } |
paul@117 | 201 | |
paul@117 | 202 | return 1; |
paul@117 | 203 | } |
paul@117 | 204 | |
paul@117 | 205 | |
paul@117 | 206 | |
paul@117 | 207 | /* Ensure that memory is mapped for accessing the given file, using the |
paul@117 | 208 | indicated count as a region size hint. */ |
paul@117 | 209 | |
paul@117 | 210 | static void *_map_memory(file_t *file, offset_t count) |
paul@117 | 211 | { |
paul@117 | 212 | if (file->memory == NULL) |
paul@117 | 213 | { |
paul@171 | 214 | if (file->object_flags & OBJECT_SUPPORTS_MMAP) |
paul@339 | 215 | return client_mmap(file, client_tell(file), count, 0, 0, |
paul@330 | 216 | file_region_flags(file->flags)); |
paul@117 | 217 | else if (pipe_current(file)) |
paul@117 | 218 | return NULL; |
paul@117 | 219 | } |
paul@117 | 220 | |
paul@117 | 221 | return file->memory; |
paul@117 | 222 | } |
paul@117 | 223 | |
paul@117 | 224 | |
paul@117 | 225 | |
paul@144 | 226 | /* Open a file opening object. */ |
paul@144 | 227 | |
paul@150 | 228 | l4_cap_idx_t client_open_for_user(user_t user) |
paul@144 | 229 | { |
paul@507 | 230 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@144 | 231 | |
paul@150 | 232 | return client_open_for_user_using(user, server); |
paul@144 | 233 | } |
paul@144 | 234 | |
paul@144 | 235 | /* Open a file opening object via a named capability. */ |
paul@144 | 236 | |
paul@150 | 237 | l4_cap_idx_t client_open_for_user_using(user_t user, l4_cap_idx_t server) |
paul@144 | 238 | { |
paul@144 | 239 | if (l4_is_invalid_cap(server)) |
paul@144 | 240 | return L4_INVALID_CAP; |
paul@144 | 241 | |
paul@144 | 242 | l4_cap_idx_t opener; |
paul@150 | 243 | long err = file_open_for_user(user, server, &opener); |
paul@144 | 244 | |
paul@144 | 245 | if (err) |
paul@144 | 246 | return L4_INVALID_CAP; |
paul@144 | 247 | |
paul@144 | 248 | return opener; |
paul@144 | 249 | } |
paul@144 | 250 | |
paul@144 | 251 | |
paul@144 | 252 | |
paul@90 | 253 | /* Close a filesystem object. */ |
paul@90 | 254 | |
paul@90 | 255 | void client_close(file_t *file) |
paul@90 | 256 | { |
paul@90 | 257 | if (file == NULL) |
paul@90 | 258 | return; |
paul@90 | 259 | |
paul@159 | 260 | file_flush(file); |
paul@90 | 261 | file_close(file); |
paul@90 | 262 | free(file); |
paul@90 | 263 | } |
paul@90 | 264 | |
paul@90 | 265 | |
paul@90 | 266 | |
paul@584 | 267 | /* Obtain a stream from the environment. */ |
paul@584 | 268 | |
paul@584 | 269 | file_t *client_get_stream(const char *name, flags_t flags) |
paul@584 | 270 | { |
paul@613 | 271 | l4_cap_idx_t ref = l4re_env_get_cap(name); |
paul@613 | 272 | file_t *stream; |
paul@613 | 273 | |
paul@613 | 274 | if (l4_is_invalid_cap(ref)) |
paul@613 | 275 | return NULL; |
paul@613 | 276 | |
paul@613 | 277 | stream = (file_t *) malloc(sizeof(file_t)); |
paul@584 | 278 | |
paul@584 | 279 | file_init(stream); |
paul@584 | 280 | stream->flags = flags; |
paul@613 | 281 | stream->ref = ref; |
paul@584 | 282 | |
paul@614 | 283 | /* Test for pipe-based access, switching to memory mapped access if not |
paul@614 | 284 | supported. */ |
paul@614 | 285 | |
paul@614 | 286 | long err = client_current_region(stream); |
paul@614 | 287 | |
paul@614 | 288 | if (err == -L4_EBADPROTO) |
paul@614 | 289 | stream->object_flags |= OBJECT_SUPPORTS_MMAP; |
paul@614 | 290 | |
paul@584 | 291 | /* Enforce blocking if necessary. |
paul@584 | 292 | NOTE: Ignoring any event subscription error. */ |
paul@584 | 293 | |
paul@584 | 294 | if (!(flags & O_NONBLOCK)) |
paul@584 | 295 | { |
paul@584 | 296 | notify_flags_t nflags = 0; |
paul@584 | 297 | |
paul@584 | 298 | if ((flags & O_WRONLY) || (flags & O_RDWR)) |
paul@584 | 299 | nflags |= NOTIFY_SPACE_AVAILABLE; |
paul@584 | 300 | |
paul@584 | 301 | if ((flags & O_RDONLY) || (flags & O_RDWR)) |
paul@584 | 302 | nflags |= NOTIFY_CONTENT_AVAILABLE; |
paul@584 | 303 | |
paul@584 | 304 | client_set_blocking(stream, nflags | NOTIFY_PEER_CLOSED); |
paul@584 | 305 | } |
paul@584 | 306 | |
paul@584 | 307 | return stream; |
paul@584 | 308 | } |
paul@584 | 309 | |
paul@584 | 310 | |
paul@584 | 311 | |
paul@90 | 312 | /* Open a filesystem object. */ |
paul@90 | 313 | |
paul@90 | 314 | file_t *client_open(const char *name, flags_t flags) |
paul@90 | 315 | { |
paul@507 | 316 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@144 | 317 | |
paul@144 | 318 | return client_open_using(name, flags, server); |
paul@103 | 319 | } |
paul@103 | 320 | |
paul@103 | 321 | /* Open a filesystem object via a named capability. */ |
paul@103 | 322 | |
paul@144 | 323 | file_t *client_open_using(const char *name, flags_t flags, l4_cap_idx_t server) |
paul@103 | 324 | { |
paul@144 | 325 | if (l4_is_invalid_cap(server)) |
paul@144 | 326 | return NULL; |
paul@144 | 327 | |
paul@90 | 328 | file_t *file = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 329 | |
paul@90 | 330 | if (file == NULL) |
paul@90 | 331 | return NULL; |
paul@90 | 332 | |
paul@402 | 333 | /* Return any allocated structure even if an error occurs. */ |
paul@90 | 334 | |
paul@402 | 335 | file->error = file_open(file, name, flags, server); |
paul@90 | 336 | return file; |
paul@90 | 337 | } |
paul@90 | 338 | |
paul@90 | 339 | |
paul@90 | 340 | |
paul@204 | 341 | /* Open a directory listing stream via the given named directory. */ |
paul@204 | 342 | |
paul@204 | 343 | file_t *client_opendir(const char *name) |
paul@204 | 344 | { |
paul@507 | 345 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@204 | 346 | |
paul@204 | 347 | return client_opendir_using(name, server); |
paul@204 | 348 | } |
paul@204 | 349 | |
paul@204 | 350 | /* Open a directory listing stream via the given named directory and a named |
paul@204 | 351 | capability. */ |
paul@204 | 352 | |
paul@204 | 353 | file_t *client_opendir_using(const char *name, l4_cap_idx_t server) |
paul@204 | 354 | { |
paul@204 | 355 | file_t *file = client_open_using(name, O_DIRECTORY, server); |
paul@204 | 356 | |
paul@204 | 357 | if (file == NULL) |
paul@204 | 358 | return NULL; |
paul@204 | 359 | |
paul@437 | 360 | /* Return the directory structure itself for error handling. */ |
paul@437 | 361 | |
paul@437 | 362 | if (!client_opened(file)) |
paul@437 | 363 | return file; |
paul@437 | 364 | |
paul@204 | 365 | file_t *reader = client_opendir_at(file); |
paul@204 | 366 | |
paul@402 | 367 | /* Release the directory and return the reader even if an error occurs. */ |
paul@204 | 368 | |
paul@204 | 369 | client_close(file); |
paul@204 | 370 | return reader; |
paul@204 | 371 | } |
paul@204 | 372 | |
paul@204 | 373 | |
paul@204 | 374 | |
paul@202 | 375 | /* Open a directory listing stream via the given directory. */ |
paul@175 | 376 | |
paul@204 | 377 | file_t *client_opendir_at(file_t *file) |
paul@175 | 378 | { |
paul@202 | 379 | file_t *reader = (file_t *) malloc(sizeof(file_t)); |
paul@175 | 380 | |
paul@202 | 381 | if (reader == NULL) |
paul@202 | 382 | return NULL; |
paul@175 | 383 | |
paul@402 | 384 | /* Return any allocated structure even if an error occurs. */ |
paul@202 | 385 | |
paul@402 | 386 | reader->error = directory_opendir(file, reader); |
paul@175 | 387 | |
paul@175 | 388 | /* Set blocking read mode to be able to conveniently read directory entries |
paul@402 | 389 | from the stream. If this fails, the error is set on the structure, but the |
paul@402 | 390 | stream will be open. */ |
paul@175 | 391 | |
paul@402 | 392 | if (!reader->error) |
paul@402 | 393 | reader->error = client_set_blocking(reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@175 | 394 | |
paul@202 | 395 | return reader; |
paul@175 | 396 | } |
paul@175 | 397 | |
paul@175 | 398 | |
paul@175 | 399 | |
paul@475 | 400 | /* Open another instance of an opened filesystem object. */ |
paul@475 | 401 | |
paul@475 | 402 | file_t *client_reopen(file_t *file, flags_t flags) |
paul@475 | 403 | { |
paul@475 | 404 | if (file == NULL) |
paul@475 | 405 | return NULL; |
paul@475 | 406 | |
paul@475 | 407 | file_t *new_file = (file_t *) malloc(sizeof(file_t)); |
paul@475 | 408 | |
paul@475 | 409 | if (new_file == NULL) |
paul@475 | 410 | return NULL; |
paul@475 | 411 | |
paul@475 | 412 | /* Return any allocated structure even if an error occurs. */ |
paul@475 | 413 | |
paul@475 | 414 | new_file->error = file_reopen(file, new_file, flags); |
paul@475 | 415 | return new_file; |
paul@475 | 416 | } |
paul@475 | 417 | |
paul@475 | 418 | |
paul@475 | 419 | |
paul@402 | 420 | /* Open a pipe object, returning any error condition. */ |
paul@90 | 421 | |
paul@178 | 422 | long client_pipe(file_t **reader, file_t **writer, flags_t flags) |
paul@90 | 423 | { |
paul@507 | 424 | l4_cap_idx_t server = l4re_env_get_cap(ENV_PIPE_SERVER_NAME); |
paul@144 | 425 | |
paul@178 | 426 | return client_pipe_using(reader, writer, flags, server); |
paul@135 | 427 | } |
paul@135 | 428 | |
paul@178 | 429 | long client_pipe_using(file_t **reader, file_t **writer, flags_t flags, l4_cap_idx_t server) |
paul@135 | 430 | { |
paul@402 | 431 | *reader = NULL; |
paul@402 | 432 | *writer = NULL; |
paul@402 | 433 | |
paul@144 | 434 | if (l4_is_invalid_cap(server)) |
paul@144 | 435 | return -L4_EINVAL; |
paul@144 | 436 | |
paul@90 | 437 | *reader = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 438 | |
paul@90 | 439 | if (*reader == NULL) |
paul@90 | 440 | return -L4_ENOMEM; |
paul@90 | 441 | |
paul@90 | 442 | *writer = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 443 | |
paul@90 | 444 | if (*writer == NULL) |
paul@90 | 445 | { |
paul@90 | 446 | free(*reader); |
paul@90 | 447 | return -L4_ENOMEM; |
paul@90 | 448 | } |
paul@90 | 449 | |
paul@90 | 450 | long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); |
paul@90 | 451 | |
paul@178 | 452 | /* Set blocking if successful and non-blocking is not indicated. */ |
paul@178 | 453 | |
paul@178 | 454 | if (!err && !(flags & O_NONBLOCK)) |
paul@178 | 455 | { |
paul@178 | 456 | err = client_set_blocking(*reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@178 | 457 | if (!err) |
paul@178 | 458 | err = client_set_blocking(*writer, NOTIFY_SPACE_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@178 | 459 | } |
paul@178 | 460 | |
paul@90 | 461 | if (err) |
paul@90 | 462 | { |
paul@90 | 463 | free(*reader); |
paul@90 | 464 | free(*writer); |
paul@90 | 465 | } |
paul@90 | 466 | |
paul@90 | 467 | return err; |
paul@90 | 468 | } |
paul@90 | 469 | |
paul@90 | 470 | |
paul@90 | 471 | |
paul@402 | 472 | /* Determine whether a file has been successfully opened. */ |
paul@402 | 473 | |
paul@402 | 474 | int client_opened(file_t *file) |
paul@402 | 475 | { |
paul@402 | 476 | return (file != NULL) && !file->error; |
paul@402 | 477 | } |
paul@402 | 478 | |
paul@402 | 479 | |
paul@402 | 480 | |
paul@271 | 481 | /* Make a directory in the filesystem. */ |
paul@271 | 482 | |
paul@271 | 483 | long client_mkdir(const char *path, mode_t mode) |
paul@271 | 484 | { |
paul@507 | 485 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@271 | 486 | |
paul@271 | 487 | return client_mkdir_using(path, mode, server); |
paul@271 | 488 | } |
paul@271 | 489 | |
paul@271 | 490 | /* Make a directory in the filesystem via a named capability. */ |
paul@271 | 491 | |
paul@271 | 492 | long client_mkdir_using(const char *path, mode_t mode, l4_cap_idx_t server) |
paul@271 | 493 | { |
paul@271 | 494 | return file_mkdir(path, mode, server); |
paul@271 | 495 | } |
paul@271 | 496 | |
paul@271 | 497 | |
paul@271 | 498 | |
paul@232 | 499 | /* Remove a file from the filesystem. */ |
paul@232 | 500 | |
paul@266 | 501 | long client_remove(const char *path) |
paul@232 | 502 | { |
paul@507 | 503 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@232 | 504 | |
paul@266 | 505 | return client_remove_using(path, server); |
paul@232 | 506 | } |
paul@232 | 507 | |
paul@232 | 508 | /* Remove a file from the filesystem via a named capability. */ |
paul@232 | 509 | |
paul@266 | 510 | long client_remove_using(const char *path, l4_cap_idx_t server) |
paul@232 | 511 | { |
paul@266 | 512 | return file_remove(path, server); |
paul@232 | 513 | } |
paul@232 | 514 | |
paul@232 | 515 | |
paul@232 | 516 | |
paul@236 | 517 | /* Rename a file in the filesystem. */ |
paul@236 | 518 | |
paul@236 | 519 | long client_rename(const char *source, const char *target) |
paul@236 | 520 | { |
paul@507 | 521 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@236 | 522 | |
paul@236 | 523 | return client_rename_using(source, target, server); |
paul@236 | 524 | } |
paul@236 | 525 | |
paul@236 | 526 | /* Rename a file in the filesystem via a named capability. */ |
paul@236 | 527 | |
paul@236 | 528 | long client_rename_using(const char *source, const char *target, l4_cap_idx_t server) |
paul@236 | 529 | { |
paul@236 | 530 | return file_rename(source, target, server); |
paul@236 | 531 | } |
paul@236 | 532 | |
paul@236 | 533 | |
paul@236 | 534 | |
paul@266 | 535 | /* Obtain filesystem object statistics. */ |
paul@266 | 536 | |
paul@266 | 537 | long client_stat(const char *path, struct stat *st) |
paul@266 | 538 | { |
paul@507 | 539 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@266 | 540 | |
paul@266 | 541 | return client_stat_using(path, st, server); |
paul@266 | 542 | } |
paul@266 | 543 | |
paul@266 | 544 | /* Obtain object statistics from the filesystem via a named capability. */ |
paul@266 | 545 | |
paul@266 | 546 | long client_stat_using(const char *path, struct stat *st, l4_cap_idx_t server) |
paul@266 | 547 | { |
paul@266 | 548 | return file_stat(path, st, server); |
paul@266 | 549 | } |
paul@266 | 550 | |
paul@266 | 551 | |
paul@266 | 552 | |
paul@174 | 553 | /* Obtain the current region of a pipe. */ |
paul@174 | 554 | |
paul@174 | 555 | long client_current_region(file_t *file) |
paul@174 | 556 | { |
paul@402 | 557 | if (!client_opened(file)) |
paul@174 | 558 | return -L4_EINVAL; |
paul@174 | 559 | |
paul@643 | 560 | long err = pipe_current(file); |
paul@643 | 561 | |
paul@643 | 562 | if (err) |
paul@643 | 563 | return err; |
paul@643 | 564 | |
paul@643 | 565 | /* Handle any case where the current region has been exhausted. */ |
paul@643 | 566 | |
paul@643 | 567 | if (!file_populated_span(file)) |
paul@643 | 568 | file->data_current = 0; |
paul@643 | 569 | |
paul@643 | 570 | return L4_EOK; |
paul@174 | 571 | } |
paul@174 | 572 | |
paul@174 | 573 | |
paul@174 | 574 | |
paul@90 | 575 | /* Flush data explicitly to the filesystem object. */ |
paul@90 | 576 | |
paul@90 | 577 | long client_flush(file_t *file) |
paul@90 | 578 | { |
paul@402 | 579 | if (!client_opened(file)) |
paul@90 | 580 | return -L4_EINVAL; |
paul@90 | 581 | |
paul@90 | 582 | /* Flush and retain most buffer settings. */ |
paul@90 | 583 | |
paul@90 | 584 | return file_flush(file); |
paul@90 | 585 | } |
paul@90 | 586 | |
paul@90 | 587 | |
paul@90 | 588 | |
paul@90 | 589 | /* Map a memory region to a file. */ |
paul@90 | 590 | |
paul@339 | 591 | void *client_mmap(file_t *file, offset_t position, offset_t length, |
paul@339 | 592 | offset_t start_visible, offset_t end_visible, |
paul@634 | 593 | rm_flags_t region_flags) |
paul@90 | 594 | { |
paul@402 | 595 | if (!client_opened(file) || file_mmap(file, position, length, start_visible, |
paul@402 | 596 | end_visible, region_flags)) |
paul@90 | 597 | return NULL; |
paul@90 | 598 | |
paul@90 | 599 | return file->memory; |
paul@90 | 600 | } |
paul@90 | 601 | |
paul@90 | 602 | |
paul@90 | 603 | |
paul@90 | 604 | /* Obtain the next region of a pipe. */ |
paul@90 | 605 | |
paul@116 | 606 | long client_next_region(file_t *file) |
paul@90 | 607 | { |
paul@402 | 608 | if (!client_opened(file)) |
paul@116 | 609 | return -L4_EINVAL; |
paul@90 | 610 | |
paul@116 | 611 | return pipe_next(file); |
paul@90 | 612 | } |
paul@90 | 613 | |
paul@90 | 614 | |
paul@90 | 615 | |
paul@180 | 616 | /* Close a notifier object. */ |
paul@180 | 617 | |
paul@575 | 618 | void client_notifier_close(notifier_t *notifier) |
paul@180 | 619 | { |
paul@575 | 620 | notify_close(notifier); |
paul@180 | 621 | } |
paul@180 | 622 | |
paul@180 | 623 | /* Obtain a local notifier object. */ |
paul@180 | 624 | |
paul@575 | 625 | notifier_t *client_notifier_local() |
paul@180 | 626 | { |
paul@575 | 627 | return notify_get_local(); |
paul@180 | 628 | } |
paul@180 | 629 | |
paul@180 | 630 | /* Obtain a task-wide notifier object. */ |
paul@180 | 631 | |
paul@575 | 632 | notifier_t *client_notifier_task() |
paul@180 | 633 | { |
paul@575 | 634 | return notify_get_task(); |
paul@180 | 635 | } |
paul@180 | 636 | |
paul@180 | 637 | |
paul@180 | 638 | |
paul@174 | 639 | /* Read a directory entry. This must be freed by the caller after use. */ |
paul@174 | 640 | |
paul@174 | 641 | struct dirent *client_readdir(file_t *file) |
paul@174 | 642 | { |
paul@174 | 643 | char buffer[DIRENT_CORE_SIZE]; |
paul@174 | 644 | offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE); |
paul@174 | 645 | |
paul@174 | 646 | /* Stop if no new structure can be successfully read. */ |
paul@174 | 647 | |
paul@174 | 648 | if (nread != DIRENT_CORE_SIZE) |
paul@174 | 649 | return NULL; |
paul@174 | 650 | |
paul@174 | 651 | struct dirent *dirent = (struct dirent *) buffer; |
paul@174 | 652 | offset_t remaining = dirent->d_reclen - DIRENT_CORE_SIZE; |
paul@174 | 653 | |
paul@174 | 654 | /* Allocate a buffer for the complete structure. */ |
paul@174 | 655 | |
paul@174 | 656 | char *entry = (char *) calloc(DIRENT_CORE_SIZE + remaining, sizeof(char)); |
paul@174 | 657 | |
paul@174 | 658 | if (entry == NULL) |
paul@174 | 659 | return NULL; |
paul@174 | 660 | |
paul@174 | 661 | /* Copy the start of the entry into a new buffer. */ |
paul@174 | 662 | |
paul@174 | 663 | memcpy(entry, buffer, DIRENT_CORE_SIZE); |
paul@174 | 664 | |
paul@174 | 665 | /* Append to the entry buffer. */ |
paul@174 | 666 | |
paul@174 | 667 | char *current = entry + DIRENT_CORE_SIZE; |
paul@174 | 668 | |
paul@174 | 669 | nread = client_read(file, current, remaining); |
paul@174 | 670 | |
paul@174 | 671 | /* Stop if no complete structure can be successfully read. */ |
paul@174 | 672 | |
paul@174 | 673 | if (nread != remaining) |
paul@174 | 674 | { |
paul@174 | 675 | free(entry); |
paul@174 | 676 | return NULL; |
paul@174 | 677 | } |
paul@174 | 678 | |
paul@174 | 679 | return (struct dirent *) entry; |
paul@174 | 680 | } |
paul@174 | 681 | |
paul@174 | 682 | |
paul@174 | 683 | |
paul@90 | 684 | /* Read from the filesystem object into the buffer provided. */ |
paul@90 | 685 | |
paul@90 | 686 | offset_t client_read(file_t *file, void *buf, offset_t count) |
paul@90 | 687 | { |
paul@402 | 688 | if (!client_opened(file)) |
paul@90 | 689 | return 0; |
paul@90 | 690 | |
paul@100 | 691 | /* Map memory if none has been mapped so far. */ |
paul@100 | 692 | |
paul@108 | 693 | if (_map_memory(file, count) == NULL) |
paul@100 | 694 | return 0; |
paul@100 | 695 | |
paul@90 | 696 | /* Amount available in the descriptor buffer already. */ |
paul@90 | 697 | |
paul@90 | 698 | offset_t available = file_data_available(file); |
paul@90 | 699 | offset_t to_transfer, total = 0; |
paul@90 | 700 | |
paul@90 | 701 | while (count > 0) |
paul@90 | 702 | { |
paul@90 | 703 | /* If there is no data, try and obtain more data. */ |
paul@90 | 704 | |
paul@90 | 705 | if (!available) |
paul@90 | 706 | { |
paul@90 | 707 | /* Flush any unwritten data, preparing to read from the file position at |
paul@90 | 708 | the end of the data, and returning if no new data is available. */ |
paul@90 | 709 | |
paul@162 | 710 | if (!_access_blocking(file, file_data_end_position(file), 1)) |
paul@90 | 711 | break; |
paul@90 | 712 | |
paul@90 | 713 | available = file_data_available(file); |
paul@90 | 714 | |
paul@90 | 715 | if (!available) |
paul@90 | 716 | break; |
paul@90 | 717 | } |
paul@90 | 718 | |
paul@90 | 719 | /* Transfer data into the supplied buffer. */ |
paul@90 | 720 | |
paul@90 | 721 | to_transfer = available <= count ? available : count; |
paul@90 | 722 | |
paul@90 | 723 | file_data_read(file, (char *) buf, to_transfer); |
paul@90 | 724 | |
paul@90 | 725 | /* Update counters. */ |
paul@90 | 726 | |
paul@90 | 727 | available -= to_transfer; |
paul@90 | 728 | |
paul@90 | 729 | count -= to_transfer; |
paul@90 | 730 | total += to_transfer; |
paul@90 | 731 | |
paul@90 | 732 | buf = ((char *) buf + to_transfer); |
paul@90 | 733 | } |
paul@90 | 734 | |
paul@90 | 735 | return total; |
paul@90 | 736 | } |
paul@90 | 737 | |
paul@90 | 738 | |
paul@90 | 739 | |
paul@90 | 740 | /* Ensure that the buffer can provide the needed data. */ |
paul@90 | 741 | |
paul@90 | 742 | offset_t client_seek(file_t *file, offset_t offset, int whence) |
paul@90 | 743 | { |
paul@402 | 744 | if (!client_opened(file)) |
paul@90 | 745 | return 0; |
paul@90 | 746 | |
paul@90 | 747 | offset_t position, current = file_data_current_position(file), change; |
paul@90 | 748 | |
paul@90 | 749 | switch (whence) |
paul@90 | 750 | { |
paul@90 | 751 | case SEEK_SET: |
paul@90 | 752 | position = offset; |
paul@90 | 753 | break; |
paul@90 | 754 | |
paul@90 | 755 | case SEEK_CUR: |
paul@90 | 756 | position = current + offset; |
paul@90 | 757 | break; |
paul@90 | 758 | |
paul@90 | 759 | case SEEK_END: |
paul@90 | 760 | position = file->size + offset; |
paul@90 | 761 | break; |
paul@90 | 762 | |
paul@90 | 763 | default: |
paul@90 | 764 | /* NOTE: Set errno to EINVAL. */ |
paul@117 | 765 | return current; |
paul@90 | 766 | } |
paul@90 | 767 | |
paul@90 | 768 | /* Retain the current position if unchanged. */ |
paul@90 | 769 | |
paul@90 | 770 | if (position == current) |
paul@90 | 771 | return position; |
paul@90 | 772 | |
paul@90 | 773 | /* Move forward in the file. */ |
paul@90 | 774 | |
paul@90 | 775 | if (position > current) |
paul@90 | 776 | { |
paul@90 | 777 | change = position - current; |
paul@90 | 778 | |
paul@90 | 779 | /* Move towards the end of available data. |
paul@90 | 780 | Request new data if not enough is available. */ |
paul@90 | 781 | |
paul@90 | 782 | if (change <= file_data_available(file)) |
paul@90 | 783 | { |
paul@90 | 784 | file->data_current += change; |
paul@90 | 785 | return position; |
paul@90 | 786 | } |
paul@90 | 787 | } |
paul@90 | 788 | |
paul@90 | 789 | /* Move backward in the file. */ |
paul@90 | 790 | |
paul@90 | 791 | else |
paul@90 | 792 | { |
paul@90 | 793 | change = current - position; |
paul@90 | 794 | |
paul@90 | 795 | /* Move towards the start of available data. |
paul@90 | 796 | Request new data if moving beyond the start of the data. */ |
paul@90 | 797 | |
paul@90 | 798 | if (change <= file->data_current) |
paul@90 | 799 | { |
paul@90 | 800 | file->data_current -= change; |
paul@90 | 801 | return position; |
paul@90 | 802 | } |
paul@90 | 803 | } |
paul@90 | 804 | |
paul@90 | 805 | /* Handle unwritten data and reset the buffer for reading. */ |
paul@90 | 806 | |
paul@117 | 807 | if (_access(file, position)) |
paul@117 | 808 | return current; |
paul@117 | 809 | |
paul@90 | 810 | return position; |
paul@90 | 811 | } |
paul@90 | 812 | |
paul@90 | 813 | |
paul@90 | 814 | |
paul@117 | 815 | /* Set or unset blocking access for a file. */ |
paul@117 | 816 | |
paul@122 | 817 | long client_set_blocking(file_t *file, notify_flags_t flags) |
paul@117 | 818 | { |
paul@117 | 819 | long err; |
paul@117 | 820 | |
paul@122 | 821 | if (file->can_block == flags) |
paul@117 | 822 | return L4_EOK; |
paul@117 | 823 | |
paul@177 | 824 | /* Since blocking access is used with specific file notifications, the |
paul@177 | 825 | per-task notifier is used. */ |
paul@117 | 826 | |
paul@575 | 827 | notifier_t *notifier = client_notifier_task(); |
paul@180 | 828 | |
paul@122 | 829 | if (flags) |
paul@180 | 830 | err = client_subscribe(file, flags, notifier); |
paul@117 | 831 | else |
paul@180 | 832 | err = client_unsubscribe(file, notifier); |
paul@117 | 833 | |
paul@117 | 834 | if (err) |
paul@117 | 835 | return err; |
paul@117 | 836 | |
paul@122 | 837 | file->can_block = flags; |
paul@117 | 838 | return L4_EOK; |
paul@117 | 839 | } |
paul@117 | 840 | |
paul@117 | 841 | |
paul@117 | 842 | |
paul@607 | 843 | /* Subscribe to events concerning a file. */ |
paul@117 | 844 | |
paul@575 | 845 | long client_subscribe(file_t *file, notify_flags_t flags, notifier_t *notifier) |
paul@117 | 846 | { |
paul@402 | 847 | if (!client_opened(file)) |
paul@117 | 848 | return -L4_EINVAL; |
paul@117 | 849 | |
paul@575 | 850 | return notify_subscribe(file_notifiable(file), flags, notifier); |
paul@117 | 851 | } |
paul@117 | 852 | |
paul@117 | 853 | |
paul@117 | 854 | |
paul@117 | 855 | /* Return the current position in the file. */ |
paul@117 | 856 | |
paul@411 | 857 | offset_t client_tell(file_t *file) |
paul@90 | 858 | { |
paul@402 | 859 | if (!client_opened(file)) |
paul@90 | 860 | return -L4_EINVAL; |
paul@90 | 861 | |
paul@90 | 862 | return file_data_current_position(file); |
paul@90 | 863 | } |
paul@90 | 864 | |
paul@90 | 865 | |
paul@90 | 866 | |
paul@117 | 867 | /* Unsubscribe from events concerning a file. */ |
paul@117 | 868 | |
paul@575 | 869 | long client_unsubscribe(file_t *file, notifier_t *notifier) |
paul@117 | 870 | { |
paul@402 | 871 | if (!client_opened(file)) |
paul@117 | 872 | return -L4_EINVAL; |
paul@117 | 873 | |
paul@575 | 874 | return notify_unsubscribe(file_notifiable(file), notifier); |
paul@117 | 875 | } |
paul@117 | 876 | |
paul@117 | 877 | |
paul@117 | 878 | |
paul@123 | 879 | /* Wait for events involving a specific file. */ |
paul@123 | 880 | |
paul@575 | 881 | long client_wait_file(file_t *file, notifier_t *notifier) |
paul@123 | 882 | { |
paul@402 | 883 | if (!client_opened(file)) |
paul@123 | 884 | return -L4_EINVAL; |
paul@123 | 885 | |
paul@180 | 886 | return file_notify_wait_file(file, notifier); |
paul@123 | 887 | } |
paul@123 | 888 | |
paul@123 | 889 | /* Wait for events concerning files, referencing a file object if an event is |
paul@123 | 890 | delivered. */ |
paul@123 | 891 | |
paul@575 | 892 | long client_wait_files(file_t **file, notifier_t *notifier) |
paul@123 | 893 | { |
paul@180 | 894 | return file_notify_wait_files(file, notifier); |
paul@117 | 895 | } |
paul@117 | 896 | |
paul@117 | 897 | |
paul@117 | 898 | |
paul@90 | 899 | /* Write to the filesystem object from the buffer provided. */ |
paul@90 | 900 | |
paul@90 | 901 | offset_t client_write(file_t *file, const void *buf, offset_t count) |
paul@90 | 902 | { |
paul@402 | 903 | if (!client_opened(file)) |
paul@90 | 904 | return 0; |
paul@90 | 905 | |
paul@100 | 906 | /* Map memory if none has been mapped so far. */ |
paul@100 | 907 | |
paul@108 | 908 | if (_map_memory(file, count) == NULL) |
paul@100 | 909 | return 0; |
paul@100 | 910 | |
paul@90 | 911 | /* Attempt to ensure that the file can accept the amount of data to be |
paul@90 | 912 | written. This may not resize to the needed amount if a file has a fixed |
paul@90 | 913 | size, but data will still be written to any available space. */ |
paul@90 | 914 | |
paul@90 | 915 | offset_t needed_size = file_data_current_position(file) + count; |
paul@90 | 916 | |
paul@171 | 917 | if (file->object_flags & OBJECT_HAS_SIZE) |
paul@90 | 918 | { |
paul@108 | 919 | if (file->size < needed_size) |
paul@108 | 920 | { |
paul@108 | 921 | file_resize(file, needed_size); |
paul@90 | 922 | |
paul@108 | 923 | if (file->size < needed_size) |
paul@108 | 924 | count = file->size - file_data_current_position(file); |
paul@108 | 925 | } |
paul@90 | 926 | } |
paul@90 | 927 | |
paul@90 | 928 | /* Space remaining in the descriptor buffer. */ |
paul@90 | 929 | |
paul@90 | 930 | offset_t space = file_data_space(file); |
paul@90 | 931 | offset_t to_transfer, total = 0; |
paul@90 | 932 | |
paul@90 | 933 | while (count > 0) |
paul@90 | 934 | { |
paul@90 | 935 | /* If no space is available, try and send data, reset the buffer. */ |
paul@90 | 936 | |
paul@90 | 937 | if (!space) |
paul@90 | 938 | { |
paul@90 | 939 | /* Flush any unwritten data and continue writing from the current data |
paul@90 | 940 | position. */ |
paul@90 | 941 | |
paul@162 | 942 | if (!_access_blocking(file, file_data_current_position(file), 0)) |
paul@90 | 943 | break; |
paul@90 | 944 | |
paul@90 | 945 | space = file_data_space(file); |
paul@90 | 946 | } |
paul@90 | 947 | |
paul@90 | 948 | /* Transfer data into the supplied buffer. */ |
paul@90 | 949 | |
paul@90 | 950 | to_transfer = space <= count ? space : count; |
paul@90 | 951 | |
paul@90 | 952 | file_data_write(file, (char *) buf, to_transfer); |
paul@90 | 953 | |
paul@90 | 954 | /* Update counters. */ |
paul@90 | 955 | |
paul@90 | 956 | space -= to_transfer; |
paul@90 | 957 | |
paul@90 | 958 | count -= to_transfer; |
paul@90 | 959 | total += to_transfer; |
paul@90 | 960 | |
paul@90 | 961 | buf = ((char *) buf + to_transfer); |
paul@90 | 962 | } |
paul@90 | 963 | |
paul@90 | 964 | return total; |
paul@90 | 965 | } |
paul@90 | 966 | |
paul@90 | 967 | // vim: tabstop=2 expandtab shiftwidth=2 |