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