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