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