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