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