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