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