1 /* 2 * File access convenience functions. 3 * 4 * Copyright (C) 2021, 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <ipc/cap_alloc.h> 23 #include <ipc/mem_ipc.h> 24 #include <notifier/notifier.h> 25 #include <systypes/fcntl.h> 26 #include <systypes/stat.h> 27 28 #include <stdio.h> 29 #include <string.h> 30 31 #include "dataspace_client.h" 32 #include "directory_client.h" 33 #include "file_client.h" 34 #include "filesystem_client.h" 35 #include "flush_client.h" 36 #include "opener_client.h" 37 #include "opener_context_client.h" 38 #include "pipe_client.h" 39 #include "pipe_opener_client.h" 40 #include "mapped_file_client.h" 41 42 #include "file.h" 43 44 45 46 /* Update the extent of the file in a region using the region start and end 47 positions and the file size. */ 48 49 static void _update_extent(file_t *file) 50 { 51 /* Handle files ending after or within the region. */ 52 53 if (file->size > file->start_pos) 54 { 55 if (file->size > file->end_pos) 56 file->data_end = file->end_pos - file->start_pos; 57 else 58 file->data_end = file->size - file->start_pos; 59 } 60 61 /* Handle files ending before the region. */ 62 63 else 64 file->data_end = 0; 65 } 66 67 68 69 /* Obtain a reference to a file opener for the given user. */ 70 71 long file_open_for_user(user_t user, l4_cap_idx_t server, l4_cap_idx_t *opener) 72 { 73 client_Filesystem filesystem(server); 74 75 return filesystem.open_for_user(user, opener); 76 } 77 78 79 80 /* Initialise the given file structure. */ 81 82 void file_init(file_t *file) 83 { 84 file->memory = NULL; 85 file->ref = L4_INVALID_CAP; 86 file->start_pos = 0; 87 file->end_pos = 0; 88 file->data_end = 0; 89 file->data_current = 0; 90 file->object_flags = 0; 91 file->can_block = 0; 92 file->flags = 0; 93 file->error = 0; 94 file->metadata = NULL; 95 96 /* Initialise the notifiable section of the structure. */ 97 98 file->notifiable.notifications = 0; 99 file->notifiable.pending_notifications = 0; 100 file->notifiable.base = (notifiable_base_t *) file; 101 file->notifiable.handler = NULL; 102 } 103 104 105 106 /* Release resources for the given file. Note that this function does not 107 deallocate the structure since it may be statically allocated. */ 108 109 void file_close(file_t *file) 110 { 111 if (l4_is_valid_cap(file->ref)) 112 { 113 notifier_get_task_notifier()->unsubscribe(file_notifiable(file)); 114 ipc_cap_free_um(file->ref); 115 } 116 117 if (file->memory != NULL) 118 ipc_detach_dataspace(file->memory); 119 120 file_init(file); 121 } 122 123 /* Make a directory in the filesystem. This is a convenience function invoking 124 file_context and file_context_mkdir. */ 125 126 long file_mkdir(const char *filename, mode_t mode, l4_cap_idx_t server) 127 { 128 file_t context; 129 long err; 130 131 err = file_context(&context, server); 132 if (err) 133 return err; 134 135 if (!file_string_set(&context, filename, 0, NULL)) 136 return -L4_ENOMEM; 137 138 err = file_context_mkdir(systypes_to_sys_mode(mode), &context); 139 140 /* Close the context, although a separate mechanism could permit contexts to 141 remove several files. */ 142 143 file_close(&context); 144 return err; 145 } 146 147 /* Open a file using the given structure, indicating the filename and 148 filesystem server. The file_mmap function should be used to obtain access to 149 memory providing file data. This is a convenience function invoking 150 file_context and file_context_open. */ 151 152 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) 153 { 154 file_t context; 155 long err; 156 157 err = file_context(&context, server); 158 if (err) 159 return err; 160 161 if (!file_string_set(&context, filename, 0, NULL)) 162 return -L4_ENOMEM; 163 164 err = file_context_open(file, flags, &context); 165 166 /* Close the context, although a separate mechanism could permit contexts to 167 open several files. */ 168 169 file_close(&context); 170 return err; 171 } 172 173 /* Remove a file from the filesystem. This is a convenience function invoking 174 file_context and file_context_remove. */ 175 176 long file_remove(const char *filename, l4_cap_idx_t server) 177 { 178 file_t context; 179 long err; 180 181 err = file_context(&context, server); 182 if (err) 183 return err; 184 185 if (!file_string_set(&context, filename, 0, NULL)) 186 return -L4_ENOMEM; 187 188 err = file_context_remove(&context); 189 190 /* Close the context, although a separate mechanism could permit contexts to 191 remove several files. */ 192 193 file_close(&context); 194 return err; 195 } 196 197 /* Open a new instance of a file using the given structure. */ 198 199 long file_reopen(file_t *file, file_t *new_file, flags_t flags) 200 { 201 client_File _file(file->ref); 202 file_init(new_file); 203 new_file->flags = flags; 204 return _file.reopen(flags, &new_file->size, &new_file->ref, &new_file->object_flags); 205 } 206 207 /* Rename an object in the filesystem. This is a convenience function invoking 208 file_context and file_context_rename. */ 209 210 long file_rename(const char *source, const char *target, l4_cap_idx_t server) 211 { 212 file_t context; 213 offset_t written; 214 long err; 215 216 err = file_context(&context, server); 217 if (err) 218 return err; 219 220 if (!file_string_set(&context, source, 0, &written)) 221 return -L4_ENOMEM; 222 223 if (!file_string_set(&context, target, written + 1, NULL)) 224 return -L4_ENOMEM; 225 226 err = file_context_rename(&context); 227 228 /* Close the context, although a separate mechanism could permit contexts to 229 rename several files. */ 230 231 file_close(&context); 232 return err; 233 } 234 235 /* Obtain filesystem object statistics. This is a convenience function invoking 236 file_context and file_context_stat. */ 237 238 long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server) 239 { 240 file_t context; 241 offset_t written; 242 long err; 243 244 err = file_context(&context, server); 245 if (err) 246 return err; 247 248 if (!file_string_set(&context, filename, 0, &written)) 249 return -L4_ENOMEM; 250 251 err = file_context_stat(st, &context); 252 253 /* Close the context, although a separate mechanism could permit contexts to 254 rename several files. */ 255 256 file_close(&context); 257 return err; 258 } 259 260 261 262 /* Initialise a file structure for a context obtained from the given server 263 attaching memory to communicate filename information. */ 264 265 long file_context(file_t *file, l4_cap_idx_t server) 266 { 267 if (l4_is_invalid_cap(server)) 268 return -L4_EINVAL; 269 270 client_Opener opener(server); 271 ds_stats_t stats; 272 long err; 273 274 file_init(file); 275 file->flags = O_RDWR; 276 277 err = opener.context(&file->ref); 278 if (err) 279 return err; 280 281 client_Dataspace context_ds(file->ref); 282 283 err = context_ds.info(&stats); 284 if (err) 285 return err; 286 287 file->start_pos = 0; 288 file->end_pos = stats.size; 289 290 return ipc_attach_dataspace(file->ref, stats.size, (void **) &file->memory); 291 } 292 293 /* Make a directory using the given context. */ 294 295 long file_context_mkdir(mode_t mode, file_t *context) 296 { 297 client_OpenerContext openercontext(context->ref); 298 return openercontext.mkdir(mode); 299 } 300 301 /* Open a file using the given structure and context. */ 302 303 long file_context_open(file_t *file, flags_t flags, file_t *context) 304 { 305 client_OpenerContext openercontext(context->ref); 306 file_init(file); 307 file->flags = flags; 308 return openercontext.open(flags, &file->size, &file->ref, &file->object_flags); 309 } 310 311 /* Remove a file using the given context. */ 312 313 long file_context_remove(file_t *context) 314 { 315 client_OpenerContext openercontext(context->ref); 316 return openercontext.remove(); 317 } 318 319 /* Rename a file using the given context. */ 320 321 long file_context_rename(file_t *context) 322 { 323 client_OpenerContext openercontext(context->ref); 324 return openercontext.rename(); 325 } 326 327 /* Obtain filesystem object statistics using the given context. */ 328 329 long file_context_stat(struct stat *st, file_t *context) 330 { 331 client_OpenerContext openercontext(context->ref); 332 long err = openercontext.stat(); 333 334 if (err) 335 return err; 336 337 /* Copy the stat structure manually in case of any divergence. */ 338 339 systypes_copy_from_sys_stat(st, (sys_stat_t *) context->memory); 340 341 return L4_EOK; 342 } 343 344 345 346 /* Flush populated data and obtain an updated file size and populated data 347 details. */ 348 349 long file_flush(file_t *file) 350 { 351 if (l4_is_invalid_cap(file->ref)) 352 return -L4_EINVAL; 353 354 client_Flush _file(file->ref); 355 long err = _file.flush(file->data_current, &file->size); 356 357 if (err) 358 return err; 359 360 _update_extent(file); 361 362 return L4_EOK; 363 } 364 365 /* Map a region of the given file to a memory region, obtaining an updated file 366 size and populated data details. Unmap any previously mapped region. */ 367 368 long file_mmap(file_t *file, offset_t position, offset_t length, 369 offset_t start_visible, offset_t end_visible, 370 rm_flags_t region_flags) 371 { 372 long err; 373 374 if (file->memory != NULL) 375 { 376 err = ipc_detach_dataspace(file->memory); 377 if (err) 378 return err; 379 380 file->memory = NULL; 381 } 382 383 err = file_mmap_only(file, position, length, start_visible, end_visible); 384 385 if (err) 386 return err; 387 388 err = ipc_attach_dataspace_align(file->ref, file_span(file), 389 L4RE_RM_F_SEARCH_ADDR | region_flags, 390 L4_PAGESHIFT, 391 (void **) &file->memory); 392 if (err) 393 return err; 394 395 return L4_EOK; 396 } 397 398 /* Request access to a region of the given file, obtaining an updated file size 399 and populated data details. The region is not mapped, however. */ 400 401 long file_mmap_only(file_t *file, offset_t position, offset_t length, 402 offset_t start_visible, offset_t end_visible) 403 { 404 client_MappedFile mapped_file(file->ref); 405 long err = mapped_file.mmap(position, length, start_visible, end_visible, 406 &file->start_pos, &file->end_pos, &file->size); 407 408 if (err) 409 return err; 410 411 _update_extent(file); 412 413 return L4_EOK; 414 } 415 416 /* Return opening flags compatible with the given region flags. */ 417 418 flags_t file_opening_flags(rm_flags_t rm_flags) 419 { 420 if ((rm_flags & L4RE_RM_F_RW) == L4RE_RM_F_RW) 421 return O_RDWR; 422 else if (rm_flags & L4RE_RM_F_W) 423 return O_WRONLY; 424 else 425 return O_RDONLY; 426 } 427 428 /* Return mmap flags corresponding to the file access flags. */ 429 430 rm_flags_t file_region_flags(flags_t flags) 431 { 432 rm_flags_t rm_flags; 433 434 switch (flags & 3) 435 { 436 case O_WRONLY: 437 rm_flags = L4RE_RM_F_W; 438 break; 439 440 case O_RDWR: 441 rm_flags = L4RE_RM_F_RW; 442 break; 443 444 default: 445 rm_flags = L4RE_RM_F_R; 446 break; 447 } 448 449 return rm_flags; 450 } 451 452 /* Resize a file, obtaining updated file size and populated data details. */ 453 454 long file_resize(file_t *file, offset_t size) 455 { 456 if (!(file->object_flags & OBJECT_HAS_SIZE)) 457 return -L4_EIO; 458 459 client_File _file(file->ref); 460 offset_t file_size = size; 461 long err = _file.resize(&file_size); 462 463 if (err) 464 return err; 465 466 file->size = file_size; 467 _update_extent(file); 468 return L4_EOK; 469 } 470 471 472 473 /* Return the amount of data in the mapped region for the given file. */ 474 475 offset_t file_populated_span(file_t *file) 476 { 477 offset_t size = file_span(file); 478 return (file->data_end < size) ? file->data_end : size; 479 } 480 481 /* Return the size of the mapped region for the given file. */ 482 483 offset_t file_span(file_t *file) 484 { 485 return file->end_pos - file->start_pos; 486 } 487 488 489 490 /* Get a pointer to any terminated string at the given offset or NULL if the 491 data from offset is not terminated. */ 492 493 char *file_string_get(file_t *file, offset_t offset) 494 { 495 offset_t limit = file_span(file) - offset; 496 497 if (strnlen(file->memory + offset, limit) < limit) 498 return file->memory + offset; 499 else 500 return NULL; 501 } 502 503 /* Copy a string to the mapped region at the given offset, returning 1 (true) 504 where all characters were copied, 0 (false) otherwise. The precise number of 505 characters copied, excluding the zero terminator is provided via the written 506 parameter if it is not specified as NULL. */ 507 508 int file_string_set(file_t *file, const char *data, offset_t offset, 509 offset_t *written) 510 { 511 offset_t i, pos, limit = file_span(file); 512 513 /* Do not attempt to copy data with an invalid offset. */ 514 515 if (offset >= limit) 516 { 517 if (written != NULL) 518 *written = 0; 519 return 0; 520 } 521 522 /* Copy the data to the given offset, stopping at the end of the region. */ 523 524 for (i = 0, pos = offset; pos < limit; i++, pos++) 525 { 526 file->memory[pos] = data[i]; 527 528 /* Terminator written, can return immediately. */ 529 530 if (!data[i]) 531 { 532 if (written != NULL) 533 *written = pos - offset; 534 return 1; 535 } 536 } 537 538 /* Terminate the incomplete string at the end of the region. */ 539 540 file->memory[limit - 1] = '\0'; 541 if (written != NULL) 542 *written = limit - 1 - offset; 543 return 0; 544 } 545 546 547 548 /* Return the number of remaining populated bytes in the region. */ 549 550 offset_t file_data_available(file_t *file) 551 { 552 if (file_populated_span(file) > file->data_current) 553 return file_populated_span(file) - file->data_current; 554 else 555 return 0; 556 } 557 558 /* Return the current data offset in the region. */ 559 560 char *file_data_current(file_t *file) 561 { 562 return file->memory + file->data_current; 563 } 564 565 /* Return the current access position in the file. */ 566 567 offset_t file_data_current_position(file_t *file) 568 { 569 return file->start_pos + file->data_current; 570 } 571 572 /* Return the position of the end of the populated bytes in the region. */ 573 574 offset_t file_data_end_position(file_t *file) 575 { 576 return file->start_pos + file->data_end; 577 } 578 579 /* Return the amount of remaining space in the region. */ 580 581 offset_t file_data_space(file_t *file) 582 { 583 return file_span(file) - file->data_current; 584 } 585 586 587 588 /* Copy data to the given buffer from the current data position, updating the 589 position. */ 590 591 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 592 { 593 memcpy(buf, file_data_current(file), to_transfer); 594 595 /* Update position details. */ 596 597 file->data_current += to_transfer; 598 } 599 600 /* Copy data from the given buffer to the current data position, updating the 601 position and the extent of populated data if this was exceeded. */ 602 603 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 604 { 605 memcpy(file_data_current(file), buf, to_transfer); 606 607 /* Update position details. */ 608 609 file->data_current += to_transfer; 610 611 if (file->data_current > file->data_end) 612 file->data_end = file->data_current; 613 } 614 615 616 617 /* Conversion to the generic notification types. */ 618 619 notifiable_t *file_notifiable(file_t *file) 620 { 621 return notify_notifiable((notifiable_base_t *) file); 622 } 623 624 /* Return the notification flags for a file. */ 625 626 notify_flags_t file_notifications(file_t *file) 627 { 628 return notify_notifications((notifiable_base_t *) file); 629 } 630 631 /* Wait for a notification event on a file. */ 632 633 long file_notify_wait_file(file_t *file, notifier_t *notifier) 634 { 635 long err = notify_wait(file_notifiable(file), notifier); 636 637 /* Unsubscribe if a closure notification has been received. */ 638 639 if (!err && (file_notifications(file) & NOTIFY_PEER_CLOSED)) 640 notify_unsubscribe(file_notifiable(file), notifier); 641 642 return err; 643 } 644 645 /* Wait for notification events on files. */ 646 647 long file_notify_wait_files(file_t **file, notifier_t *notifier) 648 { 649 notifiable_t *notifiable; 650 long err = notify_wait_many(¬ifiable, notifier); 651 652 *file = (file_t *) notifiable->base; 653 654 /* Unsubscribe if a closure notification has been received. */ 655 656 if (!err && (notifiable->notifications & NOTIFY_PEER_CLOSED)) 657 notify_unsubscribe(notifiable, notifier); 658 659 return err; 660 } 661 662 663 664 /* Open two pipe endpoints using the given pipe server. */ 665 666 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 667 { 668 if (l4_is_invalid_cap(server)) 669 return -L4_EINVAL; 670 671 client_PipeOpener opener(server); 672 673 file_init(reader); 674 file_init(writer); 675 reader->flags = O_RDONLY; 676 writer->flags = O_WRONLY; 677 678 return opener.pipe(size, &reader->ref, &writer->ref); 679 } 680 681 /* Access the current region for a pipe endpoint. */ 682 683 long pipe_current(file_t *pipe, int sync) 684 { 685 client_Pipe _pipe(pipe->ref); 686 offset_t data_current; 687 long err = _pipe.current_region(&data_current, &pipe->data_end, &pipe->size); 688 689 if (err) 690 return err; 691 692 pipe->end_pos = pipe->size; 693 694 /* Obtain the current position if synchronising position state. */ 695 696 if (sync) 697 pipe->data_current = data_current; 698 699 /* Handle any case where the current region has been exhausted. */ 700 701 if (!file_populated_span(pipe)) 702 pipe->data_current = 0; 703 704 /* Attach memory if necessary. */ 705 706 if (pipe->memory == NULL) 707 { 708 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 709 if (err) 710 return err; 711 } 712 713 return L4_EOK; 714 } 715 716 /* Access the next region for a pipe endpoint, updating the eventual size of 717 the current region. */ 718 719 long pipe_next(file_t *pipe) 720 { 721 client_Pipe _pipe(pipe->ref); 722 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 723 724 if (err) 725 return err; 726 727 if (pipe->memory != NULL) 728 err = ipc_detach_dataspace(pipe->memory); 729 730 if (err) 731 return err; 732 733 pipe->end_pos = pipe->size; 734 pipe->data_current = 0; 735 pipe->memory = NULL; 736 737 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 738 if (err) 739 return err; 740 741 return L4_EOK; 742 } 743 744 /* Set the size of the written region. */ 745 746 long pipe_written(file_t *pipe, offset_t size) 747 { 748 if (size <= pipe->size) 749 { 750 pipe->data_end = size; 751 return L4_EOK; 752 } 753 else 754 return -L4_EINVAL; 755 } 756 757 758 759 /* Obtain a directory listing stream from a directory. */ 760 761 long directory_opendir(file_t *file, file_t *reader) 762 { 763 client_Directory directory(file->ref); 764 file_init(reader); 765 reader->flags = O_RDONLY; 766 return directory.opendir(&reader->size, &reader->ref, &reader->object_flags); 767 } 768 769 // vim: tabstop=2 expandtab shiftwidth=2