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 <systypes/fcntl.h> 25 #include <systypes/stat.h> 26 27 #include <stdio.h> 28 #include <string.h> 29 30 #include "dataspace_client.h" 31 #include "directory_client.h" 32 #include "file_client.h" 33 #include "filesystem_client.h" 34 #include "flush_client.h" 35 #include "opener_client.h" 36 #include "opener_context_client.h" 37 #include "pipe_client.h" 38 #include "pipe_opener_client.h" 39 #include "mapped_file_client.h" 40 41 #include "file.h" 42 #include "notifier.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. */ 104 105 void file_close(file_t *file) 106 { 107 if (l4_is_valid_cap(file->ref)) 108 { 109 ipc_cap_free_um(file->ref); 110 111 /* Only unsubscribe after actually closing the file and sending any 112 notifications. */ 113 114 notifier_get_task_notifier()->unsubscribe(file_notifiable(file)); 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 offset_t size; 272 map_flags_t flags; 273 long err; 274 275 file_init(file); 276 file->flags = O_RDWR; 277 278 err = opener.context(&file->ref); 279 if (err) 280 return err; 281 282 client_Dataspace context_ds(file->ref); 283 284 err = context_ds.info(&size, &flags); 285 if (err) 286 return err; 287 288 file->start_pos = 0; 289 file->end_pos = size; 290 291 return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); 292 } 293 294 /* Make a directory using the given context. */ 295 296 long file_context_mkdir(mode_t mode, file_t *context) 297 { 298 client_OpenerContext openercontext(context->ref); 299 return openercontext.mkdir(mode); 300 } 301 302 /* Open a file using the given structure and context. */ 303 304 long file_context_open(file_t *file, flags_t flags, file_t *context) 305 { 306 client_OpenerContext openercontext(context->ref); 307 file_init(file); 308 file->flags = flags; 309 return openercontext.open(flags, &file->size, &file->ref, &file->object_flags); 310 } 311 312 /* Remove a file using the given context. */ 313 314 long file_context_remove(file_t *context) 315 { 316 client_OpenerContext openercontext(context->ref); 317 return openercontext.remove(); 318 } 319 320 /* Rename a file using the given context. */ 321 322 long file_context_rename(file_t *context) 323 { 324 client_OpenerContext openercontext(context->ref); 325 return openercontext.rename(); 326 } 327 328 /* Obtain filesystem object statistics using the given context. */ 329 330 long file_context_stat(struct stat *st, file_t *context) 331 { 332 client_OpenerContext openercontext(context->ref); 333 long err = openercontext.stat(); 334 335 if (err) 336 return err; 337 338 /* Copy the stat structure manually in case of any divergence. */ 339 340 systypes_copy_from_sys_stat(st, (sys_stat_t *) context->memory); 341 342 return L4_EOK; 343 } 344 345 346 347 /* Flush populated data and obtain an updated file size and populated data 348 details. */ 349 350 long file_flush(file_t *file) 351 { 352 if (l4_is_invalid_cap(file->ref)) 353 return -L4_EINVAL; 354 355 client_Flush _file(file->ref); 356 long err = _file.flush(file->data_current, &file->size); 357 358 if (err) 359 return err; 360 361 _update_extent(file); 362 363 return L4_EOK; 364 } 365 366 /* Map a region of the given file to a memory region, obtaining an updated file 367 size and populated data details. Unmap any previously mapped region. */ 368 369 long file_mmap(file_t *file, offset_t position, offset_t length, 370 offset_t start_visible, offset_t end_visible, 371 l4re_rm_flags_t region_flags) 372 { 373 if (file->memory != NULL) 374 { 375 ipc_detach_dataspace(file->memory); 376 file->memory = NULL; 377 } 378 379 long err = file_mmap_only(file, position, length, start_visible, end_visible); 380 381 if (err) 382 return err; 383 384 err = ipc_attach_dataspace_align(file->ref, file_span(file), 385 L4RE_RM_F_SEARCH_ADDR | region_flags, 386 L4_PAGESHIFT, 387 (void **) &file->memory); 388 if (err) 389 return err; 390 391 return L4_EOK; 392 } 393 394 /* Request access to a region of the given file, obtaining an updated file size 395 and populated data details. The region is not mapped, however. */ 396 397 long file_mmap_only(file_t *file, offset_t position, offset_t length, 398 offset_t start_visible, offset_t end_visible) 399 { 400 client_MappedFile mapped_file(file->ref); 401 long err = mapped_file.mmap(position, length, start_visible, end_visible, 402 &file->start_pos, &file->end_pos, &file->size); 403 404 if (err) 405 return err; 406 407 _update_extent(file); 408 409 return L4_EOK; 410 } 411 412 /* Return opening flags compatible with the given region flags. */ 413 414 flags_t file_opening_flags(l4re_rm_flags_t rm_flags) 415 { 416 if ((rm_flags & L4RE_RM_F_RW) == L4RE_RM_F_RW) 417 return O_RDWR; 418 else if (rm_flags & L4RE_RM_F_W) 419 return O_WRONLY; 420 else 421 return O_RDONLY; 422 } 423 424 /* Return mmap flags corresponding to the file access flags. */ 425 426 l4re_rm_flags_t file_region_flags(flags_t flags) 427 { 428 l4re_rm_flags_t rm_flags; 429 430 switch (flags & 3) 431 { 432 case O_WRONLY: 433 rm_flags = L4RE_RM_F_W; 434 break; 435 436 case O_RDWR: 437 rm_flags = L4RE_RM_F_RW; 438 break; 439 440 default: 441 rm_flags = L4RE_RM_F_R; 442 break; 443 } 444 445 return rm_flags; 446 } 447 448 /* Resize a file, obtaining updated file size and populated data details. */ 449 450 long file_resize(file_t *file, offset_t size) 451 { 452 if (!(file->object_flags & OBJECT_HAS_SIZE)) 453 return -L4_EIO; 454 455 client_File _file(file->ref); 456 offset_t file_size = size; 457 long err = _file.resize(&file_size); 458 459 if (err) 460 return err; 461 462 file->size = file_size; 463 _update_extent(file); 464 return L4_EOK; 465 } 466 467 468 469 /* Return the amount of data in the mapped region for the given file. */ 470 471 offset_t file_populated_span(file_t *file) 472 { 473 offset_t size = file_span(file); 474 return (file->data_end < size) ? file->data_end : size; 475 } 476 477 /* Return the size of the mapped region for the given file. */ 478 479 offset_t file_span(file_t *file) 480 { 481 return file->end_pos - file->start_pos; 482 } 483 484 485 486 /* Get a pointer to any terminated string at the given offset or NULL if the 487 data from offset is not terminated. */ 488 489 char *file_string_get(file_t *file, offset_t offset) 490 { 491 offset_t limit = file_span(file) - offset; 492 493 if (strnlen(file->memory + offset, limit) < limit) 494 return file->memory + offset; 495 else 496 return NULL; 497 } 498 499 /* Copy a string to the mapped region at the given offset, returning 1 (true) 500 where all characters were copied, 0 (false) otherwise. The precise number of 501 characters copied, excluding the zero terminator is provided via the written 502 parameter if it is not specified as NULL. */ 503 504 int file_string_set(file_t *file, const char *data, offset_t offset, 505 offset_t *written) 506 { 507 offset_t i, pos, limit = file_span(file); 508 509 /* Do not attempt to copy data with an invalid offset. */ 510 511 if (offset >= limit) 512 { 513 if (written != NULL) 514 *written = 0; 515 return 0; 516 } 517 518 /* Copy the data to the given offset, stopping at the end of the region. */ 519 520 for (i = 0, pos = offset; pos < limit; i++, pos++) 521 { 522 file->memory[pos] = data[i]; 523 524 /* Terminator written, can return immediately. */ 525 526 if (!data[i]) 527 { 528 if (written != NULL) 529 *written = pos - offset; 530 return 1; 531 } 532 } 533 534 /* Terminate the incomplete string at the end of the region. */ 535 536 file->memory[limit - 1] = '\0'; 537 if (written != NULL) 538 *written = limit - 1 - offset; 539 return 0; 540 } 541 542 543 544 /* Return the number of remaining populated bytes in the region. */ 545 546 offset_t file_data_available(file_t *file) 547 { 548 return file_populated_span(file) - file->data_current; 549 } 550 551 /* Return the current data offset in the region. */ 552 553 char *file_data_current(file_t *file) 554 { 555 return file->memory + file->data_current; 556 } 557 558 /* Return the current access position in the file. */ 559 560 offset_t file_data_current_position(file_t *file) 561 { 562 return file->start_pos + file->data_current; 563 } 564 565 /* Return the position of the end of the populated bytes in the region. */ 566 567 offset_t file_data_end_position(file_t *file) 568 { 569 return file->start_pos + file->data_end; 570 } 571 572 /* Return the amount of remaining space in the region. */ 573 574 offset_t file_data_space(file_t *file) 575 { 576 return file_span(file) - file->data_current; 577 } 578 579 580 581 /* Copy data to the given buffer from the current data position, updating the 582 position. */ 583 584 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 585 { 586 memcpy(buf, file_data_current(file), to_transfer); 587 588 /* Update position details. */ 589 590 file->data_current += to_transfer; 591 } 592 593 /* Copy data from the given buffer to the current data position, updating the 594 position and the extent of populated data if this was exceeded. */ 595 596 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 597 { 598 memcpy(file_data_current(file), buf, to_transfer); 599 600 /* Update position details. */ 601 602 file->data_current += to_transfer; 603 604 if (file->data_current > file->data_end) 605 file->data_end = file->data_current; 606 } 607 608 609 610 /* Opaque notifier type for file_notifier_t. */ 611 612 struct file_notifier 613 { 614 ObjectNotifier *obj; 615 }; 616 617 /* Conversion to the generic notification types. */ 618 619 notifiable_t *file_notifiable(file_t *file) 620 { 621 return &file->notifiable; 622 } 623 624 /* Return the notification flags for a file. */ 625 626 notify_flags_t file_notifications(file_t *file) 627 { 628 return file->notifiable.notifications; 629 } 630 631 /* Close a notifier object. */ 632 633 void file_notify_close(file_notifier_t *notifier) 634 { 635 delete notifier->obj; 636 delete notifier; 637 } 638 639 /* Obtain a local notifier object. */ 640 641 file_notifier_t *file_notify_local() 642 { 643 file_notifier_t *notifier = new file_notifier_t; 644 645 notifier->obj = notifier_get_local_notifier(); 646 return notifier; 647 } 648 649 /* Obtain the task-wide notifier object. */ 650 651 file_notifier_t *file_notify_task() 652 { 653 file_notifier_t *notifier = new file_notifier_t; 654 655 notifier->obj = notifier_get_task_notifier(); 656 return notifier; 657 } 658 659 /* Subscribe to notification events on a file. */ 660 661 long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) 662 { 663 return notifier->obj->subscribe(file_notifiable(file), flags); 664 } 665 666 /* Unsubscribe from notification events on a file. */ 667 668 long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier) 669 { 670 return notifier->obj->unsubscribe(file_notifiable(file)); 671 } 672 673 /* Wait for a notification event on a file. */ 674 675 long file_notify_wait_file(file_t *file, file_notifier_t *notifier) 676 { 677 SpecificObjectNotifier *specific_notifier = dynamic_cast<SpecificObjectNotifier *>(notifier->obj); 678 long err = specific_notifier->wait_object(file_notifiable(file)); 679 680 /* Unsubscribe if a closure notification has been received. */ 681 682 if (!err && (file->notifiable.notifications & NOTIFY_PEER_CLOSED)) 683 file_notify_unsubscribe(file, notifier); 684 685 return err; 686 } 687 688 /* Wait for notification events on files. */ 689 690 long file_notify_wait_files(file_t **file, file_notifier_t *notifier) 691 { 692 GeneralObjectNotifier *general_notifier = dynamic_cast<GeneralObjectNotifier *>(notifier->obj); 693 notifiable_t *notifiable; 694 long err = general_notifier->wait(¬ifiable); 695 696 *file = (file_t *) notifiable->base; 697 698 /* Unsubscribe if a closure notification has been received. */ 699 700 if (!err && ((*file)->notifiable.notifications & NOTIFY_PEER_CLOSED)) 701 file_notify_unsubscribe(*file, notifier); 702 703 return err; 704 } 705 706 707 708 /* Open two pipe endpoints using the given pipe server. */ 709 710 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 711 { 712 if (l4_is_invalid_cap(server)) 713 return -L4_EINVAL; 714 715 client_PipeOpener opener(server); 716 717 file_init(reader); 718 file_init(writer); 719 reader->flags = O_RDONLY; 720 writer->flags = O_WRONLY; 721 722 return opener.pipe(size, &reader->ref, &writer->ref); 723 } 724 725 /* Access the current region for a pipe endpoint. */ 726 727 long pipe_current(file_t *pipe) 728 { 729 client_Pipe _pipe(pipe->ref); 730 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 731 732 if (err) 733 return err; 734 735 pipe->end_pos = pipe->size; 736 737 /* Attach memory if necessary. */ 738 739 if (pipe->memory == NULL) 740 { 741 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 742 if (err) 743 return err; 744 } 745 746 return L4_EOK; 747 } 748 749 /* Access the next region for a pipe endpoint, updating the eventual size of 750 the current region. */ 751 752 long pipe_next(file_t *pipe) 753 { 754 client_Pipe _pipe(pipe->ref); 755 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 756 757 if (err) 758 return err; 759 760 if (pipe->memory != NULL) 761 ipc_detach_dataspace(pipe->memory); 762 763 pipe->end_pos = pipe->size; 764 pipe->memory = NULL; 765 766 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 767 if (err) 768 return err; 769 770 return L4_EOK; 771 } 772 773 /* Set the size of the written region. */ 774 775 long pipe_written(file_t *pipe, offset_t size) 776 { 777 if (size <= pipe->size) 778 { 779 pipe->data_end = size; 780 return L4_EOK; 781 } 782 else 783 return -L4_EINVAL; 784 } 785 786 787 788 /* Obtain a directory listing stream from a directory. */ 789 790 long directory_opendir(file_t *file, file_t *reader) 791 { 792 client_Directory directory(file->ref); 793 file_init(reader); 794 reader->flags = O_RDONLY; 795 return directory.opendir(&reader->size, &reader->ref, &reader->object_flags); 796 } 797 798 // vim: tabstop=2 expandtab shiftwidth=2