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