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