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