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