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