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