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