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