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