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 if (file->memory != NULL) 371 { 372 ipc_detach_dataspace(file->memory); 373 file->memory = NULL; 374 } 375 376 long err = file_mmap_only(file, position, length, start_visible, end_visible); 377 378 if (err) 379 return err; 380 381 err = ipc_attach_dataspace_align(file->ref, file_span(file), 382 L4RE_RM_F_SEARCH_ADDR | region_flags, 383 L4_PAGESHIFT, 384 (void **) &file->memory); 385 if (err) 386 return err; 387 388 return L4_EOK; 389 } 390 391 /* Request access to a region of the given file, obtaining an updated file size 392 and populated data details. The region is not mapped, however. */ 393 394 long file_mmap_only(file_t *file, offset_t position, offset_t length, 395 offset_t start_visible, offset_t end_visible) 396 { 397 client_MappedFile mapped_file(file->ref); 398 long err = mapped_file.mmap(position, length, start_visible, end_visible, 399 &file->start_pos, &file->end_pos, &file->size); 400 401 if (err) 402 return err; 403 404 _update_extent(file); 405 406 return L4_EOK; 407 } 408 409 /* Return opening flags compatible with the given region flags. */ 410 411 flags_t file_opening_flags(l4re_rm_flags_t rm_flags) 412 { 413 if ((rm_flags & L4RE_RM_F_RW) == L4RE_RM_F_RW) 414 return O_RDWR; 415 else if (rm_flags & L4RE_RM_F_W) 416 return O_WRONLY; 417 else 418 return O_RDONLY; 419 } 420 421 /* Return mmap flags corresponding to the file access flags. */ 422 423 l4re_rm_flags_t file_region_flags(flags_t flags) 424 { 425 l4re_rm_flags_t rm_flags; 426 427 switch (flags & 3) 428 { 429 case O_WRONLY: 430 rm_flags = L4RE_RM_F_W; 431 break; 432 433 case O_RDWR: 434 rm_flags = L4RE_RM_F_RW; 435 break; 436 437 default: 438 rm_flags = L4RE_RM_F_R; 439 break; 440 } 441 442 return rm_flags; 443 } 444 445 /* Resize a file, obtaining updated file size and populated data details. */ 446 447 long file_resize(file_t *file, offset_t size) 448 { 449 if (!(file->object_flags & OBJECT_HAS_SIZE)) 450 return -L4_EIO; 451 452 client_File _file(file->ref); 453 offset_t file_size = size; 454 long err = _file.resize(&file_size); 455 456 if (err) 457 return err; 458 459 file->size = file_size; 460 _update_extent(file); 461 return L4_EOK; 462 } 463 464 465 466 /* Return the amount of data in the mapped region for the given file. */ 467 468 offset_t file_populated_span(file_t *file) 469 { 470 offset_t size = file_span(file); 471 return (file->data_end < size) ? file->data_end : size; 472 } 473 474 /* Return the size of the mapped region for the given file. */ 475 476 offset_t file_span(file_t *file) 477 { 478 return file->end_pos - file->start_pos; 479 } 480 481 482 483 /* Get a pointer to any terminated string at the given offset or NULL if the 484 data from offset is not terminated. */ 485 486 char *file_string_get(file_t *file, offset_t offset) 487 { 488 offset_t limit = file_span(file) - offset; 489 490 if (strnlen(file->memory + offset, limit) < limit) 491 return file->memory + offset; 492 else 493 return NULL; 494 } 495 496 /* Copy a string to the mapped region at the given offset, returning 1 (true) 497 where all characters were copied, 0 (false) otherwise. The precise number of 498 characters copied, excluding the zero terminator is provided via the written 499 parameter if it is not specified as NULL. */ 500 501 int file_string_set(file_t *file, const char *data, offset_t offset, 502 offset_t *written) 503 { 504 offset_t i, pos, limit = file_span(file); 505 506 /* Do not attempt to copy data with an invalid offset. */ 507 508 if (offset >= limit) 509 { 510 if (written != NULL) 511 *written = 0; 512 return 0; 513 } 514 515 /* Copy the data to the given offset, stopping at the end of the region. */ 516 517 for (i = 0, pos = offset; pos < limit; i++, pos++) 518 { 519 file->memory[pos] = data[i]; 520 521 /* Terminator written, can return immediately. */ 522 523 if (!data[i]) 524 { 525 if (written != NULL) 526 *written = pos - offset; 527 return 1; 528 } 529 } 530 531 /* Terminate the incomplete string at the end of the region. */ 532 533 file->memory[limit - 1] = '\0'; 534 if (written != NULL) 535 *written = limit - 1 - offset; 536 return 0; 537 } 538 539 540 541 /* Return the number of remaining populated bytes in the region. */ 542 543 offset_t file_data_available(file_t *file) 544 { 545 return file_populated_span(file) - file->data_current; 546 } 547 548 /* Return the current data offset in the region. */ 549 550 char *file_data_current(file_t *file) 551 { 552 return file->memory + file->data_current; 553 } 554 555 /* Return the current access position in the file. */ 556 557 offset_t file_data_current_position(file_t *file) 558 { 559 return file->start_pos + file->data_current; 560 } 561 562 /* Return the position of the end of the populated bytes in the region. */ 563 564 offset_t file_data_end_position(file_t *file) 565 { 566 return file->start_pos + file->data_end; 567 } 568 569 /* Return the amount of remaining space in the region. */ 570 571 offset_t file_data_space(file_t *file) 572 { 573 return file_span(file) - file->data_current; 574 } 575 576 577 578 /* Copy data to the given buffer from the current data position, updating the 579 position. */ 580 581 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 582 { 583 memcpy(buf, file_data_current(file), to_transfer); 584 585 /* Update position details. */ 586 587 file->data_current += to_transfer; 588 } 589 590 /* Copy data from the given buffer to the current data position, updating the 591 position and the extent of populated data if this was exceeded. */ 592 593 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 594 { 595 memcpy(file_data_current(file), buf, to_transfer); 596 597 /* Update position details. */ 598 599 file->data_current += to_transfer; 600 601 if (file->data_current > file->data_end) 602 file->data_end = file->data_current; 603 } 604 605 606 607 /* Opaque notifier type for file_notifier_t. */ 608 609 struct file_notifier 610 { 611 ObjectNotifier *obj; 612 }; 613 614 /* Conversion to the generic notification types. */ 615 616 notifiable_t *file_notifiable(file_t *file) 617 { 618 return &file->notifiable; 619 } 620 621 /* Return the notification flags for a file. */ 622 623 notify_flags_t file_notifications(file_t *file) 624 { 625 return file->notifiable.notifications; 626 } 627 628 /* Close a notifier object. */ 629 630 void file_notify_close(file_notifier_t *notifier) 631 { 632 notifier->obj->stop(); 633 delete notifier->obj; 634 delete notifier; 635 } 636 637 /* Obtain a local notifier object. */ 638 639 file_notifier_t *file_notify_local() 640 { 641 file_notifier_t *notifier = new file_notifier_t; 642 643 notifier->obj = notifier_get_local_notifier(); 644 return notifier; 645 } 646 647 /* Obtain the task-wide notifier object. */ 648 649 file_notifier_t *file_notify_task() 650 { 651 file_notifier_t *notifier = new file_notifier_t; 652 653 notifier->obj = notifier_get_task_notifier(); 654 return notifier; 655 } 656 657 /* Subscribe to notification events on a file. */ 658 659 long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) 660 { 661 return notifier->obj->subscribe(file_notifiable(file), flags); 662 } 663 664 /* Unsubscribe from notification events on a file. */ 665 666 long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier) 667 { 668 return notifier->obj->unsubscribe(file_notifiable(file)); 669 } 670 671 /* Wait for a notification event on a file. */ 672 673 long file_notify_wait_file(file_t *file, file_notifier_t *notifier) 674 { 675 SpecificObjectNotifier *specific_notifier = dynamic_cast<SpecificObjectNotifier *>(notifier->obj); 676 long err = specific_notifier->wait_object(file_notifiable(file)); 677 678 /* Unsubscribe if a closure notification has been received. */ 679 680 if (!err && (file->notifiable.notifications & NOTIFY_PEER_CLOSED)) 681 file_notify_unsubscribe(file, notifier); 682 683 return err; 684 } 685 686 /* Wait for notification events on files. */ 687 688 long file_notify_wait_files(file_t **file, file_notifier_t *notifier) 689 { 690 GeneralObjectNotifier *general_notifier = dynamic_cast<GeneralObjectNotifier *>(notifier->obj); 691 notifiable_t *notifiable; 692 long err = general_notifier->wait(¬ifiable); 693 694 *file = (file_t *) notifiable->base; 695 696 /* Unsubscribe if a closure notification has been received. */ 697 698 if (!err && ((*file)->notifiable.notifications & NOTIFY_PEER_CLOSED)) 699 file_notify_unsubscribe(*file, notifier); 700 701 return err; 702 } 703 704 705 706 /* Open two pipe endpoints using the given pipe server. */ 707 708 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 709 { 710 if (l4_is_invalid_cap(server)) 711 return -L4_EINVAL; 712 713 client_PipeOpener opener(server); 714 715 file_init(reader); 716 file_init(writer); 717 reader->flags = O_RDONLY; 718 writer->flags = O_WRONLY; 719 720 return opener.pipe(size, &reader->ref, &writer->ref); 721 } 722 723 /* Access the current region for a pipe endpoint. */ 724 725 long pipe_current(file_t *pipe) 726 { 727 client_Pipe _pipe(pipe->ref); 728 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 729 730 if (err) 731 return err; 732 733 pipe->end_pos = pipe->size; 734 735 /* Attach memory if necessary. */ 736 737 if (pipe->memory == NULL) 738 { 739 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 740 if (err) 741 return err; 742 } 743 744 return L4_EOK; 745 } 746 747 /* Access the next region for a pipe endpoint, updating the eventual size of 748 the current region. */ 749 750 long pipe_next(file_t *pipe) 751 { 752 client_Pipe _pipe(pipe->ref); 753 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 754 755 if (err) 756 return err; 757 758 if (pipe->memory != NULL) 759 ipc_detach_dataspace(pipe->memory); 760 761 pipe->end_pos = pipe->size; 762 pipe->memory = NULL; 763 764 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 765 if (err) 766 return err; 767 768 return L4_EOK; 769 } 770 771 /* Set the size of the written region. */ 772 773 long pipe_written(file_t *pipe, offset_t size) 774 { 775 if (size <= pipe->size) 776 { 777 pipe->data_end = size; 778 return L4_EOK; 779 } 780 else 781 return -L4_EINVAL; 782 } 783 784 785 786 /* Obtain a directory listing stream from a directory. */ 787 788 long directory_opendir(file_t *file, file_t *reader) 789 { 790 client_Directory directory(file->ref); 791 file_init(reader); 792 reader->flags = O_RDONLY; 793 return directory.opendir(&reader->size, &reader->ref, &reader->object_flags); 794 } 795 796 // vim: tabstop=2 expandtab shiftwidth=2