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