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