1 /* 2 * File access convenience functions. 3 * 4 * Copyright (C) 2021, 2022, 2023 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 95 /* Initialise the notifiable section of the structure. */ 96 97 file->notifiable.notifications = 0; 98 file->notifiable.base = (notifiable_base_t *) file; 99 } 100 101 102 103 /* Release resources for the given file. Note that this function does not 104 deallocate the structure since it may be statically allocated. */ 105 106 void file_close(file_t *file) 107 { 108 if (l4_is_valid_cap(file->ref)) 109 { 110 notifier_get_task_notifier()->unsubscribe(file_notifiable(file)); 111 ipc_cap_free_um(file->ref); 112 } 113 114 if (file->memory != NULL) 115 ipc_detach_dataspace(file->memory); 116 117 file_init(file); 118 } 119 120 /* Make a directory in the filesystem. This is a convenience function invoking 121 file_context and file_context_mkdir. */ 122 123 long file_mkdir(const char *filename, mode_t mode, l4_cap_idx_t server) 124 { 125 file_t context; 126 long err; 127 128 err = file_context(&context, server); 129 if (err) 130 return err; 131 132 if (!file_string_set(&context, filename, 0, NULL)) 133 return -L4_ENOMEM; 134 135 err = file_context_mkdir(systypes_to_sys_mode(mode), &context); 136 137 /* Close the context, although a separate mechanism could permit contexts to 138 remove several files. */ 139 140 file_close(&context); 141 return err; 142 } 143 144 /* Open a file using the given structure, indicating the filename and 145 filesystem server. The file_mmap function should be used to obtain access to 146 memory providing file data. This is a convenience function invoking 147 file_context and file_context_open. */ 148 149 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) 150 { 151 file_t context; 152 long err; 153 154 err = file_context(&context, server); 155 if (err) 156 return err; 157 158 if (!file_string_set(&context, filename, 0, NULL)) 159 return -L4_ENOMEM; 160 161 err = file_context_open(file, flags, &context); 162 163 /* Close the context, although a separate mechanism could permit contexts to 164 open several files. */ 165 166 file_close(&context); 167 return err; 168 } 169 170 /* Remove a file from the filesystem. This is a convenience function invoking 171 file_context and file_context_remove. */ 172 173 long file_remove(const char *filename, l4_cap_idx_t server) 174 { 175 file_t context; 176 long err; 177 178 err = file_context(&context, server); 179 if (err) 180 return err; 181 182 if (!file_string_set(&context, filename, 0, NULL)) 183 return -L4_ENOMEM; 184 185 err = file_context_remove(&context); 186 187 /* Close the context, although a separate mechanism could permit contexts to 188 remove several files. */ 189 190 file_close(&context); 191 return err; 192 } 193 194 /* Open a new instance of a file using the given structure. */ 195 196 long file_reopen(file_t *file, file_t *new_file, flags_t flags) 197 { 198 client_File _file(file->ref); 199 file_init(new_file); 200 new_file->flags = flags; 201 return _file.reopen(flags, &new_file->size, &new_file->ref, &new_file->object_flags); 202 } 203 204 /* Rename an object in the filesystem. This is a convenience function invoking 205 file_context and file_context_rename. */ 206 207 long file_rename(const char *source, const char *target, l4_cap_idx_t server) 208 { 209 file_t context; 210 offset_t written; 211 long err; 212 213 err = file_context(&context, server); 214 if (err) 215 return err; 216 217 if (!file_string_set(&context, source, 0, &written)) 218 return -L4_ENOMEM; 219 220 if (!file_string_set(&context, target, written + 1, NULL)) 221 return -L4_ENOMEM; 222 223 err = file_context_rename(&context); 224 225 /* Close the context, although a separate mechanism could permit contexts to 226 rename several files. */ 227 228 file_close(&context); 229 return err; 230 } 231 232 /* Obtain filesystem object statistics. This is a convenience function invoking 233 file_context and file_context_stat. */ 234 235 long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server) 236 { 237 file_t context; 238 offset_t written; 239 long err; 240 241 err = file_context(&context, server); 242 if (err) 243 return err; 244 245 if (!file_string_set(&context, filename, 0, &written)) 246 return -L4_ENOMEM; 247 248 err = file_context_stat(st, &context); 249 250 /* Close the context, although a separate mechanism could permit contexts to 251 rename several files. */ 252 253 file_close(&context); 254 return err; 255 } 256 257 258 259 /* Initialise a file structure for a context obtained from the given server 260 attaching memory to communicate filename information. */ 261 262 long file_context(file_t *file, l4_cap_idx_t server) 263 { 264 if (l4_is_invalid_cap(server)) 265 return -L4_EINVAL; 266 267 client_Opener opener(server); 268 offset_t size; 269 map_flags_t flags; 270 long err; 271 272 file_init(file); 273 file->flags = O_RDWR; 274 275 err = opener.context(&file->ref); 276 if (err) 277 return err; 278 279 client_Dataspace context_ds(file->ref); 280 281 err = context_ds.info(&size, &flags); 282 if (err) 283 return err; 284 285 file->start_pos = 0; 286 file->end_pos = size; 287 288 return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); 289 } 290 291 /* Make a directory using the given context. */ 292 293 long file_context_mkdir(mode_t mode, file_t *context) 294 { 295 client_OpenerContext openercontext(context->ref); 296 return openercontext.mkdir(mode); 297 } 298 299 /* Open a file using the given structure and context. */ 300 301 long file_context_open(file_t *file, flags_t flags, file_t *context) 302 { 303 client_OpenerContext openercontext(context->ref); 304 file_init(file); 305 file->flags = flags; 306 return openercontext.open(flags, &file->size, &file->ref, &file->object_flags); 307 } 308 309 /* Remove a file using the given context. */ 310 311 long file_context_remove(file_t *context) 312 { 313 client_OpenerContext openercontext(context->ref); 314 return openercontext.remove(); 315 } 316 317 /* Rename a file using the given context. */ 318 319 long file_context_rename(file_t *context) 320 { 321 client_OpenerContext openercontext(context->ref); 322 return openercontext.rename(); 323 } 324 325 /* Obtain filesystem object statistics using the given context. */ 326 327 long file_context_stat(struct stat *st, file_t *context) 328 { 329 client_OpenerContext openercontext(context->ref); 330 long err = openercontext.stat(); 331 332 if (err) 333 return err; 334 335 /* Copy the stat structure manually in case of any divergence. */ 336 337 systypes_copy_from_sys_stat(st, (sys_stat_t *) context->memory); 338 339 return L4_EOK; 340 } 341 342 343 344 /* Flush populated data and obtain an updated file size and populated data 345 details. */ 346 347 long file_flush(file_t *file) 348 { 349 if (l4_is_invalid_cap(file->ref)) 350 return -L4_EINVAL; 351 352 client_Flush _file(file->ref); 353 long err = _file.flush(file->data_current, &file->size); 354 355 if (err) 356 return err; 357 358 _update_extent(file); 359 360 return L4_EOK; 361 } 362 363 /* Map a region of the given file to a memory region, obtaining an updated file 364 size and populated data details. Unmap any previously mapped region. */ 365 366 long file_mmap(file_t *file, offset_t position, offset_t length, 367 offset_t start_visible, offset_t end_visible, 368 l4re_rm_flags_t region_flags) 369 { 370 long err; 371 372 if (file->memory != NULL) 373 { 374 err = ipc_detach_dataspace(file->memory); 375 if (err) 376 return err; 377 378 file->memory = NULL; 379 } 380 381 err = file_mmap_only(file, position, length, start_visible, end_visible); 382 383 if (err) 384 return err; 385 386 err = ipc_attach_dataspace_align(file->ref, file_span(file), 387 L4RE_RM_F_SEARCH_ADDR | region_flags, 388 L4_PAGESHIFT, 389 (void **) &file->memory); 390 if (err) 391 return err; 392 393 return L4_EOK; 394 } 395 396 /* Request access to a region of the given file, obtaining an updated file size 397 and populated data details. The region is not mapped, however. */ 398 399 long file_mmap_only(file_t *file, offset_t position, offset_t length, 400 offset_t start_visible, offset_t end_visible) 401 { 402 client_MappedFile mapped_file(file->ref); 403 long err = mapped_file.mmap(position, length, start_visible, end_visible, 404 &file->start_pos, &file->end_pos, &file->size); 405 406 if (err) 407 return err; 408 409 _update_extent(file); 410 411 return L4_EOK; 412 } 413 414 /* Return opening flags compatible with the given region flags. */ 415 416 flags_t file_opening_flags(l4re_rm_flags_t rm_flags) 417 { 418 if ((rm_flags & L4RE_RM_F_RW) == L4RE_RM_F_RW) 419 return O_RDWR; 420 else if (rm_flags & L4RE_RM_F_W) 421 return O_WRONLY; 422 else 423 return O_RDONLY; 424 } 425 426 /* Return mmap flags corresponding to the file access flags. */ 427 428 l4re_rm_flags_t file_region_flags(flags_t flags) 429 { 430 l4re_rm_flags_t rm_flags; 431 432 switch (flags & 3) 433 { 434 case O_WRONLY: 435 rm_flags = L4RE_RM_F_W; 436 break; 437 438 case O_RDWR: 439 rm_flags = L4RE_RM_F_RW; 440 break; 441 442 default: 443 rm_flags = L4RE_RM_F_R; 444 break; 445 } 446 447 return rm_flags; 448 } 449 450 /* Resize a file, obtaining updated file size and populated data details. */ 451 452 long file_resize(file_t *file, offset_t size) 453 { 454 if (!(file->object_flags & OBJECT_HAS_SIZE)) 455 return -L4_EIO; 456 457 client_File _file(file->ref); 458 offset_t file_size = size; 459 long err = _file.resize(&file_size); 460 461 if (err) 462 return err; 463 464 file->size = file_size; 465 _update_extent(file); 466 return L4_EOK; 467 } 468 469 470 471 /* Return the amount of data in the mapped region for the given file. */ 472 473 offset_t file_populated_span(file_t *file) 474 { 475 offset_t size = file_span(file); 476 return (file->data_end < size) ? file->data_end : size; 477 } 478 479 /* Return the size of the mapped region for the given file. */ 480 481 offset_t file_span(file_t *file) 482 { 483 return file->end_pos - file->start_pos; 484 } 485 486 487 488 /* Get a pointer to any terminated string at the given offset or NULL if the 489 data from offset is not terminated. */ 490 491 char *file_string_get(file_t *file, offset_t offset) 492 { 493 offset_t limit = file_span(file) - offset; 494 495 if (strnlen(file->memory + offset, limit) < limit) 496 return file->memory + offset; 497 else 498 return NULL; 499 } 500 501 /* Copy a string to the mapped region at the given offset, returning 1 (true) 502 where all characters were copied, 0 (false) otherwise. The precise number of 503 characters copied, excluding the zero terminator is provided via the written 504 parameter if it is not specified as NULL. */ 505 506 int file_string_set(file_t *file, const char *data, offset_t offset, 507 offset_t *written) 508 { 509 offset_t i, pos, limit = file_span(file); 510 511 /* Do not attempt to copy data with an invalid offset. */ 512 513 if (offset >= limit) 514 { 515 if (written != NULL) 516 *written = 0; 517 return 0; 518 } 519 520 /* Copy the data to the given offset, stopping at the end of the region. */ 521 522 for (i = 0, pos = offset; pos < limit; i++, pos++) 523 { 524 file->memory[pos] = data[i]; 525 526 /* Terminator written, can return immediately. */ 527 528 if (!data[i]) 529 { 530 if (written != NULL) 531 *written = pos - offset; 532 return 1; 533 } 534 } 535 536 /* Terminate the incomplete string at the end of the region. */ 537 538 file->memory[limit - 1] = '\0'; 539 if (written != NULL) 540 *written = limit - 1 - offset; 541 return 0; 542 } 543 544 545 546 /* Return the number of remaining populated bytes in the region. */ 547 548 offset_t file_data_available(file_t *file) 549 { 550 return file_populated_span(file) - file->data_current; 551 } 552 553 /* Return the current data offset in the region. */ 554 555 char *file_data_current(file_t *file) 556 { 557 return file->memory + file->data_current; 558 } 559 560 /* Return the current access position in the file. */ 561 562 offset_t file_data_current_position(file_t *file) 563 { 564 return file->start_pos + file->data_current; 565 } 566 567 /* Return the position of the end of the populated bytes in the region. */ 568 569 offset_t file_data_end_position(file_t *file) 570 { 571 return file->start_pos + file->data_end; 572 } 573 574 /* Return the amount of remaining space in the region. */ 575 576 offset_t file_data_space(file_t *file) 577 { 578 return file_span(file) - file->data_current; 579 } 580 581 582 583 /* Copy data to the given buffer from the current data position, updating the 584 position. */ 585 586 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 587 { 588 memcpy(buf, file_data_current(file), to_transfer); 589 590 /* Update position details. */ 591 592 file->data_current += to_transfer; 593 } 594 595 /* Copy data from the given buffer to the current data position, updating the 596 position and the extent of populated data if this was exceeded. */ 597 598 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 599 { 600 memcpy(file_data_current(file), buf, to_transfer); 601 602 /* Update position details. */ 603 604 file->data_current += to_transfer; 605 606 if (file->data_current > file->data_end) 607 file->data_end = file->data_current; 608 } 609 610 611 612 /* Opaque notifier type for file_notifier_t. */ 613 614 struct file_notifier 615 { 616 ObjectNotifier *obj; 617 }; 618 619 /* Conversion to the generic notification types. */ 620 621 notifiable_t *file_notifiable(file_t *file) 622 { 623 return &file->notifiable; 624 } 625 626 /* Return the notification flags for a file. */ 627 628 notify_flags_t file_notifications(file_t *file) 629 { 630 return file->notifiable.notifications; 631 } 632 633 /* Close a notifier object. */ 634 635 void file_notify_close(file_notifier_t *notifier) 636 { 637 notifier->obj->stop(); 638 delete notifier->obj; 639 delete notifier; 640 } 641 642 /* Obtain a local notifier object. */ 643 644 file_notifier_t *file_notify_local() 645 { 646 file_notifier_t *notifier = new file_notifier_t; 647 648 notifier->obj = notifier_get_local_notifier(); 649 return notifier; 650 } 651 652 /* Obtain the task-wide notifier object. */ 653 654 file_notifier_t *file_notify_task() 655 { 656 file_notifier_t *notifier = new file_notifier_t; 657 658 notifier->obj = notifier_get_task_notifier(); 659 return notifier; 660 } 661 662 /* Subscribe to notification events on a file. */ 663 664 long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) 665 { 666 return notifier->obj->subscribe(file_notifiable(file), flags); 667 } 668 669 /* Unsubscribe from notification events on a file. */ 670 671 long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier) 672 { 673 return notifier->obj->unsubscribe(file_notifiable(file)); 674 } 675 676 /* Wait for a notification event on a file. */ 677 678 long file_notify_wait_file(file_t *file, file_notifier_t *notifier) 679 { 680 SpecificObjectNotifier *specific_notifier = dynamic_cast<SpecificObjectNotifier *>(notifier->obj); 681 long err = specific_notifier->wait_object(file_notifiable(file)); 682 683 /* Unsubscribe if a closure notification has been received. */ 684 685 if (!err && (file->notifiable.notifications & NOTIFY_PEER_CLOSED)) 686 file_notify_unsubscribe(file, notifier); 687 688 return err; 689 } 690 691 /* Wait for notification events on files. */ 692 693 long file_notify_wait_files(file_t **file, file_notifier_t *notifier) 694 { 695 GeneralObjectNotifier *general_notifier = dynamic_cast<GeneralObjectNotifier *>(notifier->obj); 696 notifiable_t *notifiable; 697 long err = general_notifier->wait(¬ifiable); 698 699 *file = (file_t *) notifiable->base; 700 701 /* Unsubscribe if a closure notification has been received. */ 702 703 if (!err && ((*file)->notifiable.notifications & NOTIFY_PEER_CLOSED)) 704 file_notify_unsubscribe(*file, notifier); 705 706 return err; 707 } 708 709 710 711 /* Open two pipe endpoints using the given pipe server. */ 712 713 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 714 { 715 if (l4_is_invalid_cap(server)) 716 return -L4_EINVAL; 717 718 client_PipeOpener opener(server); 719 720 file_init(reader); 721 file_init(writer); 722 reader->flags = O_RDONLY; 723 writer->flags = O_WRONLY; 724 725 return opener.pipe(size, &reader->ref, &writer->ref); 726 } 727 728 /* Access the current region for a pipe endpoint. */ 729 730 long pipe_current(file_t *pipe) 731 { 732 client_Pipe _pipe(pipe->ref); 733 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 734 735 if (err) 736 return err; 737 738 pipe->end_pos = pipe->size; 739 740 /* Attach memory if necessary. */ 741 742 if (pipe->memory == NULL) 743 { 744 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 745 if (err) 746 return err; 747 } 748 749 return L4_EOK; 750 } 751 752 /* Access the next region for a pipe endpoint, updating the eventual size of 753 the current region. */ 754 755 long pipe_next(file_t *pipe) 756 { 757 client_Pipe _pipe(pipe->ref); 758 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 759 760 if (err) 761 return err; 762 763 if (pipe->memory != NULL) 764 err = ipc_detach_dataspace(pipe->memory); 765 766 if (err) 767 return err; 768 769 pipe->end_pos = pipe->size; 770 pipe->memory = NULL; 771 772 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 773 if (err) 774 return err; 775 776 return L4_EOK; 777 } 778 779 /* Set the size of the written region. */ 780 781 long pipe_written(file_t *pipe, offset_t size) 782 { 783 if (size <= pipe->size) 784 { 785 pipe->data_end = size; 786 return L4_EOK; 787 } 788 else 789 return -L4_EINVAL; 790 } 791 792 793 794 /* Obtain a directory listing stream from a directory. */ 795 796 long directory_opendir(file_t *file, file_t *reader) 797 { 798 client_Directory directory(file->ref); 799 file_init(reader); 800 reader->flags = O_RDONLY; 801 return directory.opendir(&reader->size, &reader->ref, &reader->object_flags); 802 } 803 804 // vim: tabstop=2 expandtab shiftwidth=2