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.pending_notifications = 0; 99 file->notifiable.base = (notifiable_base_t *) file; 100 file->notifiable.handler = NULL; 101 } 102 103 104 105 /* Release resources for the given file. Note that this function does not 106 deallocate the structure since it may be statically allocated. */ 107 108 void file_close(file_t *file) 109 { 110 if (l4_is_valid_cap(file->ref)) 111 { 112 notifier_get_task_notifier()->unsubscribe(file_notifiable(file)); 113 ipc_cap_free_um(file->ref); 114 } 115 116 if (file->memory != NULL) 117 ipc_detach_dataspace(file->memory); 118 119 file_init(file); 120 } 121 122 /* Make a directory in the filesystem. This is a convenience function invoking 123 file_context and file_context_mkdir. */ 124 125 long file_mkdir(const char *filename, mode_t mode, l4_cap_idx_t server) 126 { 127 file_t context; 128 long err; 129 130 err = file_context(&context, server); 131 if (err) 132 return err; 133 134 if (!file_string_set(&context, filename, 0, NULL)) 135 return -L4_ENOMEM; 136 137 err = file_context_mkdir(systypes_to_sys_mode(mode), &context); 138 139 /* Close the context, although a separate mechanism could permit contexts to 140 remove several files. */ 141 142 file_close(&context); 143 return err; 144 } 145 146 /* Open a file using the given structure, indicating the filename and 147 filesystem server. The file_mmap function should be used to obtain access to 148 memory providing file data. This is a convenience function invoking 149 file_context and file_context_open. */ 150 151 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) 152 { 153 file_t context; 154 long err; 155 156 err = file_context(&context, server); 157 if (err) 158 return err; 159 160 if (!file_string_set(&context, filename, 0, NULL)) 161 return -L4_ENOMEM; 162 163 err = file_context_open(file, flags, &context); 164 165 /* Close the context, although a separate mechanism could permit contexts to 166 open several files. */ 167 168 file_close(&context); 169 return err; 170 } 171 172 /* Remove a file from the filesystem. This is a convenience function invoking 173 file_context and file_context_remove. */ 174 175 long file_remove(const char *filename, l4_cap_idx_t server) 176 { 177 file_t context; 178 long err; 179 180 err = file_context(&context, server); 181 if (err) 182 return err; 183 184 if (!file_string_set(&context, filename, 0, NULL)) 185 return -L4_ENOMEM; 186 187 err = file_context_remove(&context); 188 189 /* Close the context, although a separate mechanism could permit contexts to 190 remove several files. */ 191 192 file_close(&context); 193 return err; 194 } 195 196 /* Open a new instance of a file using the given structure. */ 197 198 long file_reopen(file_t *file, file_t *new_file, flags_t flags) 199 { 200 client_File _file(file->ref); 201 file_init(new_file); 202 new_file->flags = flags; 203 return _file.reopen(flags, &new_file->size, &new_file->ref, &new_file->object_flags); 204 } 205 206 /* Rename an object in the filesystem. This is a convenience function invoking 207 file_context and file_context_rename. */ 208 209 long file_rename(const char *source, const char *target, l4_cap_idx_t server) 210 { 211 file_t context; 212 offset_t written; 213 long err; 214 215 err = file_context(&context, server); 216 if (err) 217 return err; 218 219 if (!file_string_set(&context, source, 0, &written)) 220 return -L4_ENOMEM; 221 222 if (!file_string_set(&context, target, written + 1, NULL)) 223 return -L4_ENOMEM; 224 225 err = file_context_rename(&context); 226 227 /* Close the context, although a separate mechanism could permit contexts to 228 rename several files. */ 229 230 file_close(&context); 231 return err; 232 } 233 234 /* Obtain filesystem object statistics. This is a convenience function invoking 235 file_context and file_context_stat. */ 236 237 long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server) 238 { 239 file_t context; 240 offset_t written; 241 long err; 242 243 err = file_context(&context, server); 244 if (err) 245 return err; 246 247 if (!file_string_set(&context, filename, 0, &written)) 248 return -L4_ENOMEM; 249 250 err = file_context_stat(st, &context); 251 252 /* Close the context, although a separate mechanism could permit contexts to 253 rename several files. */ 254 255 file_close(&context); 256 return err; 257 } 258 259 260 261 /* Initialise a file structure for a context obtained from the given server 262 attaching memory to communicate filename information. */ 263 264 long file_context(file_t *file, l4_cap_idx_t server) 265 { 266 if (l4_is_invalid_cap(server)) 267 return -L4_EINVAL; 268 269 client_Opener opener(server); 270 offset_t size; 271 map_flags_t flags; 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(&size, &flags); 284 if (err) 285 return err; 286 287 file->start_pos = 0; 288 file->end_pos = size; 289 290 return ipc_attach_dataspace(file->ref, 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 l4re_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(l4re_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 l4re_rm_flags_t file_region_flags(flags_t flags) 431 { 432 l4re_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 return file_populated_span(file) - file->data_current; 553 } 554 555 /* Return the current data offset in the region. */ 556 557 char *file_data_current(file_t *file) 558 { 559 return file->memory + file->data_current; 560 } 561 562 /* Return the current access position in the file. */ 563 564 offset_t file_data_current_position(file_t *file) 565 { 566 return file->start_pos + file->data_current; 567 } 568 569 /* Return the position of the end of the populated bytes in the region. */ 570 571 offset_t file_data_end_position(file_t *file) 572 { 573 return file->start_pos + file->data_end; 574 } 575 576 /* Return the amount of remaining space in the region. */ 577 578 offset_t file_data_space(file_t *file) 579 { 580 return file_span(file) - file->data_current; 581 } 582 583 584 585 /* Copy data to the given buffer from the current data position, updating the 586 position. */ 587 588 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 589 { 590 memcpy(buf, file_data_current(file), to_transfer); 591 592 /* Update position details. */ 593 594 file->data_current += to_transfer; 595 } 596 597 /* Copy data from the given buffer to the current data position, updating the 598 position and the extent of populated data if this was exceeded. */ 599 600 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 601 { 602 memcpy(file_data_current(file), buf, to_transfer); 603 604 /* Update position details. */ 605 606 file->data_current += to_transfer; 607 608 if (file->data_current > file->data_end) 609 file->data_end = file->data_current; 610 } 611 612 613 614 /* Opaque notifier type for file_notifier_t. */ 615 616 struct file_notifier 617 { 618 ObjectNotifier *obj; 619 }; 620 621 /* Conversion to the generic notification types. */ 622 623 notifiable_t *file_notifiable(file_t *file) 624 { 625 return &file->notifiable; 626 } 627 628 /* Return the notification flags for a file. */ 629 630 notify_flags_t file_notifications(file_t *file) 631 { 632 return file->notifiable.notifications; 633 } 634 635 /* Close a notifier object. */ 636 637 void file_notify_close(file_notifier_t *notifier) 638 { 639 delete notifier->obj; 640 delete notifier; 641 } 642 643 /* Obtain a local notifier object. */ 644 645 file_notifier_t *file_notify_local() 646 { 647 file_notifier_t *notifier = new file_notifier_t; 648 649 notifier->obj = notifier_get_local_notifier(); 650 return notifier; 651 } 652 653 /* Obtain the task-wide notifier object. */ 654 655 file_notifier_t *file_notify_task() 656 { 657 file_notifier_t *notifier = new file_notifier_t; 658 659 notifier->obj = notifier_get_task_notifier(); 660 return notifier; 661 } 662 663 /* Subscribe to notification events on a file. */ 664 665 long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) 666 { 667 return notifier->obj->subscribe(file_notifiable(file), flags); 668 } 669 670 /* Unsubscribe from notification events on a file. */ 671 672 long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier) 673 { 674 return notifier->obj->unsubscribe(file_notifiable(file)); 675 } 676 677 /* Wait for a notification event on a file. */ 678 679 long file_notify_wait_file(file_t *file, file_notifier_t *notifier) 680 { 681 long err = notifier->obj->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 notifiable_t *notifiable; 696 long err = notifier->obj->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 err = ipc_detach_dataspace(pipe->memory); 764 765 if (err) 766 return err; 767 768 pipe->end_pos = pipe->size; 769 pipe->memory = NULL; 770 771 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 772 if (err) 773 return err; 774 775 return L4_EOK; 776 } 777 778 /* Set the size of the written region. */ 779 780 long pipe_written(file_t *pipe, offset_t size) 781 { 782 if (size <= pipe->size) 783 { 784 pipe->data_end = size; 785 return L4_EOK; 786 } 787 else 788 return -L4_EINVAL; 789 } 790 791 792 793 /* Obtain a directory listing stream from a directory. */ 794 795 long directory_opendir(file_t *file, file_t *reader) 796 { 797 client_Directory directory(file->ref); 798 file_init(reader); 799 reader->flags = O_RDONLY; 800 return directory.opendir(&reader->size, &reader->ref, &reader->object_flags); 801 } 802 803 // vim: tabstop=2 expandtab shiftwidth=2