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