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/stat.h> 25 26 #include <string.h> 27 28 #include "dataspace_client.h" 29 #include "directory_client.h" 30 #include "file_client.h" 31 #include "filesystem_client.h" 32 #include "flush_client.h" 33 #include "opener_client.h" 34 #include "opener_context_client.h" 35 #include "pipe_client.h" 36 #include "pipe_opener_client.h" 37 #include "mapped_file_client.h" 38 39 #include "file.h" 40 #include "notifier.h" 41 42 43 44 /* Update the extent of the file in a region using the region start and end 45 positions and the file size. */ 46 47 static void _update_extent(file_t *file) 48 { 49 /* Handle files ending after or within the region. */ 50 51 if (file->size > file->start_pos) 52 { 53 if (file->size > file->end_pos) 54 file->data_end = file->end_pos - file->start_pos; 55 else 56 file->data_end = file->size - file->start_pos; 57 } 58 59 /* Handle files ending before the region. */ 60 61 else 62 file->data_end = 0; 63 } 64 65 66 67 /* Obtain a reference to a file opener for the given user. */ 68 69 long file_open_for_user(user_t user, l4_cap_idx_t server, l4_cap_idx_t *opener) 70 { 71 client_Filesystem filesystem(server); 72 73 return filesystem.open_for_user(user, opener); 74 } 75 76 77 78 /* Initialise the given file structure. */ 79 80 void file_init(file_t *file) 81 { 82 file->memory = NULL; 83 file->ref = L4_INVALID_CAP; 84 file->start_pos = 0; 85 file->end_pos = 0; 86 file->data_end = 0; 87 file->data_current = 0; 88 file->object_flags = 0; 89 file->can_block = 0; 90 file->notifications = 0; 91 } 92 93 94 95 /* Release resources for the given file. */ 96 97 void file_close(file_t *file) 98 { 99 if (l4_is_valid_cap(file->ref)) 100 { 101 ipc_cap_free_um(file->ref); 102 103 /* Only unsubscribe after actually closing the file and sending any 104 notifications. This depends on a valid reference, so it is also tested 105 here. */ 106 107 notifier_get_task_notifier()->unsubscribe(file); 108 } 109 110 if (file->memory != NULL) 111 ipc_detach_dataspace(file->memory); 112 113 file_init(file); 114 } 115 116 /* Make a directory in the filesystem. This is a convenience function invoking 117 file_context and file_context_mkdir. */ 118 119 long file_mkdir(const char *filename, mode_t mode, l4_cap_idx_t server) 120 { 121 file_t context; 122 long err; 123 124 err = file_context(&context, server); 125 if (err) 126 return err; 127 128 if (!file_string_set(&context, filename, 0, NULL)) 129 return -L4_ENOMEM; 130 131 err = file_context_mkdir(systypes_to_sys_mode(mode), &context); 132 133 /* Close the context, although a separate mechanism could permit contexts to 134 remove several files. */ 135 136 file_close(&context); 137 return err; 138 } 139 140 /* Open a file using the given structure, indicating the filename and 141 filesystem server. The file_mmap function should be used to obtain access to 142 memory providing file data. This is a convenience function invoking 143 file_context and file_context_open. */ 144 145 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) 146 { 147 file_t context; 148 long err; 149 150 err = file_context(&context, server); 151 if (err) 152 return err; 153 154 if (!file_string_set(&context, filename, 0, NULL)) 155 return -L4_ENOMEM; 156 157 err = file_context_open(file, flags, &context); 158 159 /* Close the context, although a separate mechanism could permit contexts to 160 open several files. */ 161 162 file_close(&context); 163 return err; 164 } 165 166 /* Remove a file from the filesystem. This is a convenience function invoking 167 file_context and file_context_remove. */ 168 169 long file_remove(const char *filename, l4_cap_idx_t server) 170 { 171 file_t context; 172 long err; 173 174 err = file_context(&context, server); 175 if (err) 176 return err; 177 178 if (!file_string_set(&context, filename, 0, NULL)) 179 return -L4_ENOMEM; 180 181 err = file_context_remove(&context); 182 183 /* Close the context, although a separate mechanism could permit contexts to 184 remove several files. */ 185 186 file_close(&context); 187 return err; 188 } 189 190 /* Rename an object in the filesystem. This is a convenience function invoking 191 file_context and file_context_rename. */ 192 193 long file_rename(const char *source, const char *target, l4_cap_idx_t server) 194 { 195 file_t context; 196 offset_t written; 197 long err; 198 199 err = file_context(&context, server); 200 if (err) 201 return err; 202 203 if (!file_string_set(&context, source, 0, &written)) 204 return -L4_ENOMEM; 205 206 if (!file_string_set(&context, target, written + 1, NULL)) 207 return -L4_ENOMEM; 208 209 err = file_context_rename(&context); 210 211 /* Close the context, although a separate mechanism could permit contexts to 212 rename several files. */ 213 214 file_close(&context); 215 return err; 216 } 217 218 /* Obtain filesystem object statistics. This is a convenience function invoking 219 file_context and file_context_stat. */ 220 221 long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server) 222 { 223 file_t context; 224 offset_t written; 225 long err; 226 227 err = file_context(&context, server); 228 if (err) 229 return err; 230 231 if (!file_string_set(&context, filename, 0, &written)) 232 return -L4_ENOMEM; 233 234 err = file_context_stat(st, &context); 235 236 /* Close the context, although a separate mechanism could permit contexts to 237 rename several files. */ 238 239 file_close(&context); 240 return err; 241 } 242 243 244 245 /* Initialise a file structure for a context obtained from the given server 246 attaching memory to communicate filename information. */ 247 248 long file_context(file_t *file, l4_cap_idx_t server) 249 { 250 if (l4_is_invalid_cap(server)) 251 return -L4_EINVAL; 252 253 client_Opener opener(server); 254 offset_t size; 255 flags_t flags; 256 long err; 257 258 file_init(file); 259 260 err = opener.context(&file->ref); 261 if (err) 262 return err; 263 264 client_Dataspace context_ds(file->ref); 265 266 err = context_ds.info(&size, &flags); 267 if (err) 268 return err; 269 270 file->start_pos = 0; 271 file->end_pos = size; 272 273 return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); 274 } 275 276 /* Make a directory using the given context. */ 277 278 long file_context_mkdir(mode_t mode, file_t *context) 279 { 280 client_OpenerContext openercontext(context->ref); 281 return openercontext.mkdir(mode); 282 } 283 284 /* Open a file using the given structure and context. */ 285 286 long file_context_open(file_t *file, flags_t flags, file_t *context) 287 { 288 client_OpenerContext openercontext(context->ref); 289 file_init(file); 290 return openercontext.open(flags, &file->size, &file->ref, &file->object_flags); 291 } 292 293 /* Remove a file using the given context. */ 294 295 long file_context_remove(file_t *context) 296 { 297 client_OpenerContext openercontext(context->ref); 298 return openercontext.remove(); 299 } 300 301 /* Rename a file using the given context. */ 302 303 long file_context_rename(file_t *context) 304 { 305 client_OpenerContext openercontext(context->ref); 306 return openercontext.rename(); 307 } 308 309 /* Obtain filesystem object statistics using the given context. */ 310 311 long file_context_stat(struct stat *st, file_t *context) 312 { 313 client_OpenerContext openercontext(context->ref); 314 long err = openercontext.stat(); 315 316 if (err) 317 return err; 318 319 /* Copy the stat structure manually in case of any divergence. */ 320 321 systypes_copy_from_sys_stat(st, (sys_stat_t *) context->memory); 322 323 return L4_EOK; 324 } 325 326 327 328 /* Flush populated data and obtain an updated file size and populated data 329 details. */ 330 331 long file_flush(file_t *file) 332 { 333 if (l4_is_invalid_cap(file->ref)) 334 return -L4_EINVAL; 335 336 client_Flush _file(file->ref); 337 long err = _file.flush(file->data_current, &file->size); 338 339 if (err) 340 return err; 341 342 _update_extent(file); 343 344 return L4_EOK; 345 } 346 347 /* Map a region of the given file to a memory region, obtaining an updated file 348 size and populated data details. Unmap any previously mapped region. */ 349 350 long file_mmap(file_t *file, offset_t position, offset_t length) 351 { 352 char *memory = file->memory; 353 client_MappedFile mapped_file(file->ref); 354 long err = mapped_file.mmap(position, length, &file->start_pos, 355 &file->end_pos, &file->size); 356 357 if (err) 358 return err; 359 360 _update_extent(file); 361 362 err = ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); 363 if (err) 364 return err; 365 366 if (memory != NULL) 367 ipc_detach_dataspace(memory); 368 369 return L4_EOK; 370 } 371 372 /* Resize a file, obtaining updated file size and populated data details. */ 373 374 long file_resize(file_t *file, offset_t size) 375 { 376 if (!(file->object_flags & OBJECT_HAS_SIZE)) 377 return -L4_EIO; 378 379 client_File _file(file->ref); 380 offset_t file_size = size; 381 long err = _file.resize(&file_size); 382 383 if (err) 384 return err; 385 386 file->size = file_size; 387 _update_extent(file); 388 return L4_EOK; 389 } 390 391 392 393 /* Return the amount of data in the mapped region for the given file. */ 394 395 offset_t file_populated_span(file_t *file) 396 { 397 offset_t size = file_span(file); 398 return (file->data_end < size) ? file->data_end : size; 399 } 400 401 /* Return the size of the mapped region for the given file. */ 402 403 offset_t file_span(file_t *file) 404 { 405 return file->end_pos - file->start_pos; 406 } 407 408 409 410 /* Get a pointer to any terminated string at the given offset or NULL if the 411 data from offset is not terminated. */ 412 413 char *file_string_get(file_t *file, offset_t offset) 414 { 415 offset_t limit = file_span(file) - offset; 416 417 if (strnlen(file->memory + offset, limit) < limit) 418 return file->memory + offset; 419 else 420 return NULL; 421 } 422 423 /* Copy a string to the mapped region at the given offset, returning 1 (true) 424 where all characters were copied, 0 (false) otherwise. The precise number of 425 characters copied, excluding the zero terminator is provided via the written 426 parameter if it is not specified as NULL. */ 427 428 int file_string_set(file_t *file, const char *data, offset_t offset, 429 offset_t *written) 430 { 431 offset_t i, pos, limit = file_span(file); 432 433 /* Do not attempt to copy data with an invalid offset. */ 434 435 if (offset >= limit) 436 { 437 if (written != NULL) 438 *written = 0; 439 return 0; 440 } 441 442 /* Copy the data to the given offset, stopping at the end of the region. */ 443 444 for (i = 0, pos = offset; pos < limit; i++, pos++) 445 { 446 file->memory[pos] = data[i]; 447 448 /* Terminator written, can return immediately. */ 449 450 if (!data[i]) 451 { 452 if (written != NULL) 453 *written = pos - offset; 454 return 1; 455 } 456 } 457 458 /* Terminate the incomplete string at the end of the region. */ 459 460 file->memory[limit - 1] = '\0'; 461 if (written != NULL) 462 *written = limit - 1 - offset; 463 return 0; 464 } 465 466 467 468 /* Return the number of remaining populated bytes in the region. */ 469 470 offset_t file_data_available(file_t *file) 471 { 472 return file_populated_span(file) - file->data_current; 473 } 474 475 /* Return the current data offset in the region. */ 476 477 char *file_data_current(file_t *file) 478 { 479 return file->memory + file->data_current; 480 } 481 482 /* Return the current access position in the file. */ 483 484 offset_t file_data_current_position(file_t *file) 485 { 486 return file->start_pos + file->data_current; 487 } 488 489 /* Return the position of the end of the populated bytes in the region. */ 490 491 offset_t file_data_end_position(file_t *file) 492 { 493 return file->start_pos + file->data_end; 494 } 495 496 /* Return the amount of remaining space in the region. */ 497 498 offset_t file_data_space(file_t *file) 499 { 500 return file_span(file) - file->data_current; 501 } 502 503 504 505 /* Copy data to the given buffer from the current data position, updating the 506 position. */ 507 508 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 509 { 510 memcpy(buf, file_data_current(file), to_transfer); 511 512 /* Update position details. */ 513 514 file->data_current += to_transfer; 515 } 516 517 /* Copy data from the given buffer to the current data position, updating the 518 position and the extent of populated data if this was exceeded. */ 519 520 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 521 { 522 memcpy(file_data_current(file), buf, to_transfer); 523 524 /* Update position details. */ 525 526 file->data_current += to_transfer; 527 528 if (file->data_current > file->data_end) 529 file->data_end = file->data_current; 530 } 531 532 533 534 /* Opaque notifier type for file_notifier_t. */ 535 536 struct file_notifier 537 { 538 FileNotifier *obj; 539 }; 540 541 542 543 /* Close a notifier object. */ 544 545 void file_notify_close(file_notifier_t *notifier) 546 { 547 delete notifier->obj; 548 delete notifier; 549 } 550 551 /* Obtain a local notifier object. */ 552 553 file_notifier_t *file_notify_local() 554 { 555 file_notifier_t *notifier = new file_notifier_t; 556 557 notifier->obj = notifier_get_local_notifier(); 558 return notifier; 559 } 560 561 /* Obtain the task-wide notifier object. */ 562 563 file_notifier_t *file_notify_task() 564 { 565 file_notifier_t *notifier = new file_notifier_t; 566 567 notifier->obj = notifier_get_task_notifier(); 568 return notifier; 569 } 570 571 /* Subscribe to notification events on a file. */ 572 573 long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) 574 { 575 return notifier->obj->subscribe(file, flags); 576 } 577 578 /* Unsubscribe from notification events on a file. */ 579 580 long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier) 581 { 582 return notifier->obj->unsubscribe(file); 583 } 584 585 /* Wait for a notification event on a file. */ 586 587 long file_notify_wait_file(file_t *file, file_notifier_t *notifier) 588 { 589 SpecificFileNotifier *specific_notifier = dynamic_cast<SpecificFileNotifier *>(notifier->obj); 590 long err = specific_notifier->wait_file(file); 591 592 /* Unsubscribe if a closure notification has been received. */ 593 594 if (!err && (file->notifications & NOTIFY_PEER_CLOSED)) 595 file_notify_unsubscribe(file, notifier); 596 597 return err; 598 } 599 600 /* Wait for notification events on files. */ 601 602 long file_notify_wait_files(file_t **file, file_notifier_t *notifier) 603 { 604 GeneralFileNotifier *general_notifier = dynamic_cast<GeneralFileNotifier *>(notifier->obj); 605 long err = general_notifier->wait(file); 606 607 /* Unsubscribe if a closure notification has been received. */ 608 609 if (!err && ((*file)->notifications & NOTIFY_PEER_CLOSED)) 610 file_notify_unsubscribe(*file, notifier); 611 612 return err; 613 } 614 615 616 617 /* Open two pipe endpoints using the given pipe server. */ 618 619 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 620 { 621 if (l4_is_invalid_cap(server)) 622 return -L4_EINVAL; 623 624 client_PipeOpener opener(server); 625 626 file_init(reader); 627 file_init(writer); 628 629 return opener.pipe(size, &reader->ref, &writer->ref); 630 } 631 632 /* Access the current region for a pipe endpoint. */ 633 634 long pipe_current(file_t *pipe) 635 { 636 client_Pipe _pipe(pipe->ref); 637 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 638 639 if (err) 640 return err; 641 642 pipe->end_pos = pipe->size; 643 644 /* Attach memory if necessary. */ 645 646 if (pipe->memory == NULL) 647 { 648 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 649 if (err) 650 return err; 651 } 652 653 return L4_EOK; 654 } 655 656 /* Access the next region for a pipe endpoint, updating the eventual size of 657 the current region. */ 658 659 long pipe_next(file_t *pipe) 660 { 661 client_Pipe _pipe(pipe->ref); 662 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 663 char *memory = pipe->memory; 664 665 if (err) 666 return err; 667 668 pipe->end_pos = pipe->size; 669 670 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 671 if (err) 672 return err; 673 674 if (memory != NULL) 675 ipc_detach_dataspace(memory); 676 677 return L4_EOK; 678 } 679 680 /* Set the size of the written region. */ 681 682 long pipe_written(file_t *pipe, offset_t size) 683 { 684 if (size <= pipe->size) 685 { 686 pipe->data_end = size; 687 return L4_EOK; 688 } 689 else 690 return -L4_EINVAL; 691 } 692 693 694 695 /* Obtain a directory listing stream from a directory. */ 696 697 long directory_opendir(file_t *file, file_t *reader) 698 { 699 client_Directory directory(file->ref); 700 file_init(reader); 701 return directory.opendir(&reader->size, &reader->ref, &reader->object_flags); 702 } 703 704 // vim: tabstop=2 expandtab shiftwidth=2