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