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