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