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