paul@31 | 1 | /* |
paul@31 | 2 | * File access convenience functions. |
paul@31 | 3 | * |
paul@31 | 4 | * Copyright (C) 2021 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@31 | 24 | |
paul@46 | 25 | #include <string.h> |
paul@46 | 26 | |
paul@31 | 27 | #include "dataspace_client.h" |
paul@55 | 28 | #include "file_client.h" |
paul@31 | 29 | #include "opener_client.h" |
paul@31 | 30 | #include "opener_context_client.h" |
paul@65 | 31 | #include "pipe_client.h" |
paul@65 | 32 | #include "pipe_opener_client.h" |
paul@31 | 33 | #include "mapped_file_client.h" |
paul@31 | 34 | |
paul@31 | 35 | #include "file.h" |
paul@31 | 36 | |
paul@31 | 37 | |
paul@31 | 38 | |
paul@90 | 39 | /* Update the extent of the file in a region using the region start and end |
paul@90 | 40 | positions and the file size. */ |
paul@90 | 41 | |
paul@90 | 42 | static void _update_extent(file_t *file) |
paul@90 | 43 | { |
paul@90 | 44 | /* Handle files ending after or within the region. */ |
paul@90 | 45 | |
paul@90 | 46 | if (file->size > file->start_pos) |
paul@90 | 47 | { |
paul@90 | 48 | if (file->size > file->end_pos) |
paul@90 | 49 | file->data_end = file->end_pos - file->start_pos; |
paul@90 | 50 | else |
paul@90 | 51 | file->data_end = file->size - file->start_pos; |
paul@90 | 52 | } |
paul@90 | 53 | |
paul@90 | 54 | /* Handle files ending before the region. */ |
paul@90 | 55 | |
paul@90 | 56 | else |
paul@90 | 57 | file->data_end = 0; |
paul@90 | 58 | } |
paul@90 | 59 | |
paul@90 | 60 | |
paul@90 | 61 | |
paul@90 | 62 | /* Initialise the given file structure. */ |
paul@90 | 63 | |
paul@90 | 64 | void file_init(file_t *file) |
paul@90 | 65 | { |
paul@90 | 66 | file->memory = NULL; |
paul@90 | 67 | file->ref = L4_INVALID_CAP; |
paul@90 | 68 | file->start_pos = 0; |
paul@90 | 69 | file->end_pos = 0; |
paul@90 | 70 | file->data_end = 0; |
paul@90 | 71 | file->data_current = 0; |
paul@90 | 72 | file->can_mmap = 1; |
paul@90 | 73 | } |
paul@90 | 74 | |
paul@90 | 75 | |
paul@90 | 76 | |
paul@33 | 77 | /* Release resources for the given file. */ |
paul@31 | 78 | |
paul@31 | 79 | void file_close(file_t *file) |
paul@31 | 80 | { |
paul@31 | 81 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 82 | ipc_cap_free_um(file->ref); |
paul@31 | 83 | |
paul@31 | 84 | if (file->memory != NULL) |
paul@31 | 85 | ipc_detach_dataspace(file->memory); |
paul@31 | 86 | |
paul@31 | 87 | file_init(file); |
paul@31 | 88 | } |
paul@31 | 89 | |
paul@90 | 90 | /* Open a file using the given structure, indicating the filename and |
paul@90 | 91 | filesystem server. The file_mmap function should be used to obtain access to |
paul@90 | 92 | memory providing file data. This is a convenience function invoking |
paul@90 | 93 | file_context and file_context_open. */ |
paul@90 | 94 | |
paul@90 | 95 | long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) |
paul@90 | 96 | { |
paul@90 | 97 | file_t context; |
paul@90 | 98 | long err; |
paul@90 | 99 | |
paul@90 | 100 | err = file_context(&context, server); |
paul@90 | 101 | if (err) |
paul@90 | 102 | return err; |
paul@90 | 103 | |
paul@90 | 104 | if (!file_string_set(&context, filename, 0, NULL)) |
paul@90 | 105 | return -L4_ENOMEM; |
paul@90 | 106 | |
paul@90 | 107 | err = file_context_open(file, flags, &context); |
paul@90 | 108 | |
paul@90 | 109 | /* Close the context, although a separate mechanism could permit contexts to |
paul@90 | 110 | open several files. */ |
paul@90 | 111 | |
paul@90 | 112 | file_close(&context); |
paul@90 | 113 | return err; |
paul@90 | 114 | } |
paul@90 | 115 | |
paul@90 | 116 | |
paul@90 | 117 | |
paul@90 | 118 | /* Initialise a file structure for a context obtained from the given server |
paul@90 | 119 | attaching memory to communicate filename information. */ |
paul@31 | 120 | |
paul@33 | 121 | long file_context(file_t *file, l4_cap_idx_t server) |
paul@31 | 122 | { |
paul@85 | 123 | if (l4_is_invalid_cap(server)) |
paul@85 | 124 | return -L4_EINVAL; |
paul@85 | 125 | |
paul@31 | 126 | client_Opener opener(server); |
paul@31 | 127 | unsigned long size, flags; |
paul@31 | 128 | long err; |
paul@31 | 129 | |
paul@31 | 130 | file_init(file); |
paul@47 | 131 | |
paul@31 | 132 | err = opener.context(&file->ref); |
paul@31 | 133 | if (err) |
paul@31 | 134 | return err; |
paul@31 | 135 | |
paul@31 | 136 | client_Dataspace context_ds(file->ref); |
paul@31 | 137 | |
paul@31 | 138 | err = context_ds.info(&size, &flags); |
paul@31 | 139 | if (err) |
paul@31 | 140 | return err; |
paul@31 | 141 | |
paul@47 | 142 | file->start_pos = 0; |
paul@47 | 143 | file->end_pos = size; |
paul@47 | 144 | |
paul@31 | 145 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 146 | } |
paul@31 | 147 | |
paul@47 | 148 | /* Open a file using the given structure and context. */ |
paul@47 | 149 | |
paul@85 | 150 | long file_context_open(file_t *file, flags_t flags, file_t *context) |
paul@47 | 151 | { |
paul@47 | 152 | client_OpenerContext openercontext(context->ref); |
paul@47 | 153 | file_init(file); |
paul@85 | 154 | return openercontext.open(flags, &file->size, &file->ref); |
paul@47 | 155 | } |
paul@47 | 156 | |
paul@46 | 157 | |
paul@46 | 158 | |
paul@90 | 159 | /* Flush populated data and obtain an updated file size and populated data |
paul@90 | 160 | details. */ |
paul@33 | 161 | |
paul@90 | 162 | long file_flush(file_t *file) |
paul@33 | 163 | { |
paul@90 | 164 | client_File _file(file->ref); |
paul@90 | 165 | long err = _file.flush(file->data_current, &file->size); |
paul@33 | 166 | |
paul@33 | 167 | if (err) |
paul@33 | 168 | return err; |
paul@33 | 169 | |
paul@90 | 170 | _update_extent(file); |
paul@90 | 171 | |
paul@90 | 172 | return L4_EOK; |
paul@33 | 173 | } |
paul@33 | 174 | |
paul@90 | 175 | /* Map a region of the given file to a memory region, obtaining an updated file |
paul@90 | 176 | size and populated data details. Unmap any previously mapped region. */ |
paul@90 | 177 | |
paul@90 | 178 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@90 | 179 | { |
paul@90 | 180 | char *memory = file->memory; |
paul@90 | 181 | client_MappedFile mapped_file(file->ref); |
paul@90 | 182 | long err = mapped_file.mmap(position, length, &file->start_pos, |
paul@90 | 183 | &file->end_pos, &file->size); |
paul@90 | 184 | |
paul@90 | 185 | if (err) |
paul@90 | 186 | return err; |
paul@90 | 187 | |
paul@90 | 188 | _update_extent(file); |
paul@90 | 189 | |
paul@90 | 190 | err = ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); |
paul@90 | 191 | if (err) |
paul@90 | 192 | return err; |
paul@90 | 193 | |
paul@90 | 194 | if (memory != NULL) |
paul@90 | 195 | ipc_detach_dataspace(memory); |
paul@90 | 196 | |
paul@90 | 197 | return L4_EOK; |
paul@90 | 198 | } |
paul@90 | 199 | |
paul@90 | 200 | /* Resize a file, obtaining updated file size and populated data details. */ |
paul@55 | 201 | |
paul@55 | 202 | long file_resize(file_t *file, offset_t size) |
paul@55 | 203 | { |
paul@55 | 204 | client_File _file(file->ref); |
paul@55 | 205 | offset_t file_size = size; |
paul@55 | 206 | long err = _file.resize(&file_size); |
paul@55 | 207 | |
paul@90 | 208 | if (err) |
paul@90 | 209 | return err; |
paul@55 | 210 | |
paul@90 | 211 | file->size = file_size; |
paul@90 | 212 | _update_extent(file); |
paul@90 | 213 | return L4_EOK; |
paul@55 | 214 | } |
paul@55 | 215 | |
paul@55 | 216 | |
paul@55 | 217 | |
paul@33 | 218 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 219 | |
paul@45 | 220 | offset_t file_populated_span(file_t *file) |
paul@33 | 221 | { |
paul@45 | 222 | offset_t size = file_span(file); |
paul@33 | 223 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 224 | } |
paul@33 | 225 | |
paul@45 | 226 | /* Return the size of the mapped region for the given file. */ |
paul@45 | 227 | |
paul@45 | 228 | offset_t file_span(file_t *file) |
paul@45 | 229 | { |
paul@45 | 230 | return file->end_pos - file->start_pos; |
paul@45 | 231 | } |
paul@45 | 232 | |
paul@46 | 233 | |
paul@46 | 234 | |
paul@46 | 235 | /* Get a pointer to any terminated string at the given offset or NULL if the |
paul@46 | 236 | data from offset is not terminated. */ |
paul@46 | 237 | |
paul@46 | 238 | char *file_string_get(file_t *file, offset_t offset) |
paul@46 | 239 | { |
paul@46 | 240 | offset_t limit = file_span(file) - offset; |
paul@46 | 241 | |
paul@46 | 242 | if (strnlen(file->memory + offset, limit) < limit) |
paul@46 | 243 | return file->memory + offset; |
paul@46 | 244 | else |
paul@46 | 245 | return NULL; |
paul@46 | 246 | } |
paul@46 | 247 | |
paul@47 | 248 | /* Copy a string to the mapped region at the given offset, returning 1 (true) |
paul@47 | 249 | where all characters were copied, 0 (false) otherwise. The precise number of |
paul@47 | 250 | characters copied, excluding the zero terminator is provided via the written |
paul@47 | 251 | parameter if it is not specified as NULL. */ |
paul@46 | 252 | |
paul@47 | 253 | int file_string_set(file_t *file, const char *data, offset_t offset, |
paul@47 | 254 | offset_t *written) |
paul@46 | 255 | { |
paul@47 | 256 | offset_t i, pos, limit = file_span(file); |
paul@46 | 257 | |
paul@46 | 258 | /* Do not attempt to copy data with an invalid offset. */ |
paul@46 | 259 | |
paul@47 | 260 | if (offset >= limit) |
paul@47 | 261 | { |
paul@47 | 262 | if (written != NULL) |
paul@47 | 263 | *written = 0; |
paul@46 | 264 | return 0; |
paul@47 | 265 | } |
paul@46 | 266 | |
paul@47 | 267 | /* Copy the data to the given offset, stopping at the end of the region. */ |
paul@46 | 268 | |
paul@46 | 269 | for (i = 0, pos = offset; pos < limit; i++, pos++) |
paul@46 | 270 | { |
paul@46 | 271 | file->memory[pos] = data[i]; |
paul@46 | 272 | |
paul@46 | 273 | /* Terminator written, can return immediately. */ |
paul@46 | 274 | |
paul@46 | 275 | if (!data[i]) |
paul@47 | 276 | { |
paul@47 | 277 | if (written != NULL) |
paul@47 | 278 | *written = pos - offset; |
paul@47 | 279 | return 1; |
paul@47 | 280 | } |
paul@46 | 281 | } |
paul@46 | 282 | |
paul@46 | 283 | /* Terminate the incomplete string at the end of the region. */ |
paul@46 | 284 | |
paul@47 | 285 | file->memory[limit - 1] = '\0'; |
paul@47 | 286 | if (written != NULL) |
paul@47 | 287 | *written = limit - 1 - offset; |
paul@47 | 288 | return 0; |
paul@46 | 289 | } |
paul@46 | 290 | |
paul@65 | 291 | |
paul@65 | 292 | |
paul@90 | 293 | /* Return the number of remaining populated bytes in the region. */ |
paul@90 | 294 | |
paul@90 | 295 | offset_t file_data_available(file_t *file) |
paul@90 | 296 | { |
paul@90 | 297 | return file_populated_span(file) - file->data_current; |
paul@90 | 298 | } |
paul@90 | 299 | |
paul@90 | 300 | /* Return the current data offset in the region. */ |
paul@90 | 301 | |
paul@90 | 302 | char *file_data_current(file_t *file) |
paul@90 | 303 | { |
paul@90 | 304 | return file->memory + file->data_current; |
paul@90 | 305 | } |
paul@90 | 306 | |
paul@90 | 307 | /* Return the current access position in the file. */ |
paul@90 | 308 | |
paul@90 | 309 | offset_t file_data_current_position(file_t *file) |
paul@90 | 310 | { |
paul@90 | 311 | return file->start_pos + file->data_current; |
paul@90 | 312 | } |
paul@90 | 313 | |
paul@90 | 314 | /* Return the position of the end of the populated bytes in the region. */ |
paul@90 | 315 | |
paul@90 | 316 | offset_t file_data_end_position(file_t *file) |
paul@90 | 317 | { |
paul@90 | 318 | return file->start_pos + file->data_end; |
paul@90 | 319 | } |
paul@90 | 320 | |
paul@90 | 321 | /* Return the amount of remaining space in the region. */ |
paul@90 | 322 | |
paul@90 | 323 | offset_t file_data_space(file_t *file) |
paul@90 | 324 | { |
paul@90 | 325 | return file_span(file) - file->data_current; |
paul@90 | 326 | } |
paul@90 | 327 | |
paul@90 | 328 | |
paul@90 | 329 | |
paul@90 | 330 | /* Copy data to the given buffer from the current data position, updating the |
paul@90 | 331 | position. */ |
paul@90 | 332 | |
paul@90 | 333 | void file_data_read(file_t *file, char *buf, size_t to_transfer) |
paul@90 | 334 | { |
paul@90 | 335 | memcpy(buf, file_data_current(file), to_transfer); |
paul@90 | 336 | |
paul@90 | 337 | /* Update position details. */ |
paul@90 | 338 | |
paul@90 | 339 | file->data_current += to_transfer; |
paul@90 | 340 | } |
paul@90 | 341 | |
paul@90 | 342 | /* Copy data from the given buffer to the current data position, updating the |
paul@90 | 343 | position and the extent of populated data if this was exceeded. */ |
paul@90 | 344 | |
paul@90 | 345 | void file_data_write(file_t *file, char *buf, size_t to_transfer) |
paul@90 | 346 | { |
paul@90 | 347 | memcpy(file_data_current(file), buf, to_transfer); |
paul@90 | 348 | |
paul@90 | 349 | /* Update position details. */ |
paul@90 | 350 | |
paul@90 | 351 | file->data_current += to_transfer; |
paul@90 | 352 | |
paul@90 | 353 | if (file->data_current > file->data_end) |
paul@90 | 354 | file->data_end = file->data_current; |
paul@90 | 355 | } |
paul@90 | 356 | |
paul@90 | 357 | |
paul@90 | 358 | |
paul@65 | 359 | /* Open two pipe endpoints using the given pipe server. */ |
paul@65 | 360 | |
paul@66 | 361 | long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) |
paul@65 | 362 | { |
paul@85 | 363 | if (l4_is_invalid_cap(server)) |
paul@85 | 364 | return -L4_EINVAL; |
paul@85 | 365 | |
paul@65 | 366 | client_PipeOpener opener(server); |
paul@65 | 367 | |
paul@65 | 368 | file_init(reader); |
paul@65 | 369 | file_init(writer); |
paul@65 | 370 | |
paul@90 | 371 | /* Pipes can usually only be accessed via region navigation. */ |
paul@90 | 372 | |
paul@90 | 373 | reader->can_mmap = 0; |
paul@90 | 374 | writer->can_mmap = 0; |
paul@90 | 375 | |
paul@66 | 376 | long err = opener.pipe(size, &reader->ref, &writer->ref); |
paul@65 | 377 | if (err) |
paul@65 | 378 | return err; |
paul@65 | 379 | |
paul@65 | 380 | err = pipe_next(writer) || pipe_next(reader); |
paul@65 | 381 | |
paul@65 | 382 | if (err) |
paul@65 | 383 | { |
paul@65 | 384 | file_close(reader); |
paul@65 | 385 | file_close(writer); |
paul@65 | 386 | } |
paul@65 | 387 | |
paul@65 | 388 | return err; |
paul@65 | 389 | } |
paul@65 | 390 | |
paul@65 | 391 | /* Access the current region for a pipe endpoint. */ |
paul@65 | 392 | |
paul@65 | 393 | long pipe_current(file_t *pipe) |
paul@65 | 394 | { |
paul@65 | 395 | client_Pipe _pipe(pipe->ref); |
paul@65 | 396 | long err = _pipe.current_region(&pipe->data_end, &pipe->size); |
paul@65 | 397 | char *memory = pipe->memory; |
paul@65 | 398 | |
paul@65 | 399 | if (err) |
paul@65 | 400 | return err; |
paul@65 | 401 | |
paul@65 | 402 | pipe->end_pos = pipe->size; |
paul@65 | 403 | |
paul@65 | 404 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@65 | 405 | if (err) |
paul@65 | 406 | return err; |
paul@65 | 407 | |
paul@65 | 408 | if (memory != NULL) |
paul@65 | 409 | ipc_detach_dataspace(memory); |
paul@65 | 410 | |
paul@65 | 411 | return L4_EOK; |
paul@65 | 412 | } |
paul@65 | 413 | |
paul@65 | 414 | /* Access the next region for a pipe endpoint, updating the eventual size of |
paul@65 | 415 | the current region. */ |
paul@65 | 416 | |
paul@65 | 417 | long pipe_next(file_t *pipe) |
paul@65 | 418 | { |
paul@65 | 419 | client_Pipe _pipe(pipe->ref); |
paul@65 | 420 | long err = _pipe.next_region(&pipe->data_end, &pipe->size); |
paul@65 | 421 | char *memory = pipe->memory; |
paul@65 | 422 | |
paul@65 | 423 | if (err) |
paul@65 | 424 | return err; |
paul@65 | 425 | |
paul@65 | 426 | pipe->end_pos = pipe->size; |
paul@65 | 427 | |
paul@65 | 428 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@65 | 429 | if (err) |
paul@65 | 430 | return err; |
paul@65 | 431 | |
paul@65 | 432 | if (memory != NULL) |
paul@65 | 433 | ipc_detach_dataspace(memory); |
paul@65 | 434 | |
paul@65 | 435 | return L4_EOK; |
paul@65 | 436 | } |
paul@65 | 437 | |
paul@65 | 438 | /* Set the size of the written region. */ |
paul@65 | 439 | |
paul@65 | 440 | long pipe_written(file_t *pipe, offset_t size) |
paul@65 | 441 | { |
paul@65 | 442 | if (size <= pipe->size) |
paul@65 | 443 | { |
paul@65 | 444 | pipe->data_end = size; |
paul@65 | 445 | return L4_EOK; |
paul@65 | 446 | } |
paul@65 | 447 | else |
paul@65 | 448 | return -L4_EINVAL; |
paul@65 | 449 | } |
paul@65 | 450 | |
paul@31 | 451 | // vim: tabstop=2 expandtab shiftwidth=2 |