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