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