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