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