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