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 unsigned long size, flags; 128 long err; 129 130 file_init(file); 131 132 err = opener.context(&file->ref); 133 if (err) 134 return err; 135 136 client_Dataspace context_ds(file->ref); 137 138 err = context_ds.info(&size, &flags); 139 if (err) 140 return err; 141 142 file->start_pos = 0; 143 file->end_pos = size; 144 145 return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); 146 } 147 148 /* Open a file using the given structure and context. */ 149 150 long file_context_open(file_t *file, flags_t flags, file_t *context) 151 { 152 client_OpenerContext openercontext(context->ref); 153 file_init(file); 154 return openercontext.open(flags, &file->size, &file->ref); 155 } 156 157 158 159 /* Flush populated data and obtain an updated file size and populated data 160 details. */ 161 162 long file_flush(file_t *file) 163 { 164 client_File _file(file->ref); 165 long err = _file.flush(file->data_current, &file->size); 166 167 if (err) 168 return err; 169 170 _update_extent(file); 171 172 return L4_EOK; 173 } 174 175 /* Map a region of the given file to a memory region, obtaining an updated file 176 size and populated data details. Unmap any previously mapped region. */ 177 178 long file_mmap(file_t *file, offset_t position, offset_t length) 179 { 180 char *memory = file->memory; 181 client_MappedFile mapped_file(file->ref); 182 long err = mapped_file.mmap(position, length, &file->start_pos, 183 &file->end_pos, &file->size); 184 185 if (err) 186 return err; 187 188 _update_extent(file); 189 190 err = ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); 191 if (err) 192 return err; 193 194 if (memory != NULL) 195 ipc_detach_dataspace(memory); 196 197 return L4_EOK; 198 } 199 200 /* Resize a file, obtaining updated file size and populated data details. */ 201 202 long file_resize(file_t *file, offset_t size) 203 { 204 client_File _file(file->ref); 205 offset_t file_size = size; 206 long err = _file.resize(&file_size); 207 208 if (err) 209 return err; 210 211 file->size = file_size; 212 _update_extent(file); 213 return L4_EOK; 214 } 215 216 217 218 /* Return the amount of data in the mapped region for the given file. */ 219 220 offset_t file_populated_span(file_t *file) 221 { 222 offset_t size = file_span(file); 223 return (file->data_end < size) ? file->data_end : size; 224 } 225 226 /* Return the size of the mapped region for the given file. */ 227 228 offset_t file_span(file_t *file) 229 { 230 return file->end_pos - file->start_pos; 231 } 232 233 234 235 /* Get a pointer to any terminated string at the given offset or NULL if the 236 data from offset is not terminated. */ 237 238 char *file_string_get(file_t *file, offset_t offset) 239 { 240 offset_t limit = file_span(file) - offset; 241 242 if (strnlen(file->memory + offset, limit) < limit) 243 return file->memory + offset; 244 else 245 return NULL; 246 } 247 248 /* Copy a string to the mapped region at the given offset, returning 1 (true) 249 where all characters were copied, 0 (false) otherwise. The precise number of 250 characters copied, excluding the zero terminator is provided via the written 251 parameter if it is not specified as NULL. */ 252 253 int file_string_set(file_t *file, const char *data, offset_t offset, 254 offset_t *written) 255 { 256 offset_t i, pos, limit = file_span(file); 257 258 /* Do not attempt to copy data with an invalid offset. */ 259 260 if (offset >= limit) 261 { 262 if (written != NULL) 263 *written = 0; 264 return 0; 265 } 266 267 /* Copy the data to the given offset, stopping at the end of the region. */ 268 269 for (i = 0, pos = offset; pos < limit; i++, pos++) 270 { 271 file->memory[pos] = data[i]; 272 273 /* Terminator written, can return immediately. */ 274 275 if (!data[i]) 276 { 277 if (written != NULL) 278 *written = pos - offset; 279 return 1; 280 } 281 } 282 283 /* Terminate the incomplete string at the end of the region. */ 284 285 file->memory[limit - 1] = '\0'; 286 if (written != NULL) 287 *written = limit - 1 - offset; 288 return 0; 289 } 290 291 292 293 /* Return the number of remaining populated bytes in the region. */ 294 295 offset_t file_data_available(file_t *file) 296 { 297 return file_populated_span(file) - file->data_current; 298 } 299 300 /* Return the current data offset in the region. */ 301 302 char *file_data_current(file_t *file) 303 { 304 return file->memory + file->data_current; 305 } 306 307 /* Return the current access position in the file. */ 308 309 offset_t file_data_current_position(file_t *file) 310 { 311 return file->start_pos + file->data_current; 312 } 313 314 /* Return the position of the end of the populated bytes in the region. */ 315 316 offset_t file_data_end_position(file_t *file) 317 { 318 return file->start_pos + file->data_end; 319 } 320 321 /* Return the amount of remaining space in the region. */ 322 323 offset_t file_data_space(file_t *file) 324 { 325 return file_span(file) - file->data_current; 326 } 327 328 329 330 /* Copy data to the given buffer from the current data position, updating the 331 position. */ 332 333 void file_data_read(file_t *file, char *buf, size_t to_transfer) 334 { 335 memcpy(buf, file_data_current(file), to_transfer); 336 337 /* Update position details. */ 338 339 file->data_current += to_transfer; 340 } 341 342 /* Copy data from the given buffer to the current data position, updating the 343 position and the extent of populated data if this was exceeded. */ 344 345 void file_data_write(file_t *file, char *buf, size_t to_transfer) 346 { 347 memcpy(file_data_current(file), buf, to_transfer); 348 349 /* Update position details. */ 350 351 file->data_current += to_transfer; 352 353 if (file->data_current > file->data_end) 354 file->data_end = file->data_current; 355 } 356 357 358 359 /* Open two pipe endpoints using the given pipe server. */ 360 361 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 362 { 363 if (l4_is_invalid_cap(server)) 364 return -L4_EINVAL; 365 366 client_PipeOpener opener(server); 367 368 file_init(reader); 369 file_init(writer); 370 371 /* Pipes can usually only be accessed via region navigation. */ 372 373 reader->can_mmap = 0; 374 writer->can_mmap = 0; 375 376 long err = opener.pipe(size, &reader->ref, &writer->ref); 377 if (err) 378 return err; 379 380 err = pipe_next(writer) || pipe_next(reader); 381 382 if (err) 383 { 384 file_close(reader); 385 file_close(writer); 386 } 387 388 return err; 389 } 390 391 /* Access the current region for a pipe endpoint. */ 392 393 long pipe_current(file_t *pipe) 394 { 395 client_Pipe _pipe(pipe->ref); 396 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 397 char *memory = pipe->memory; 398 399 if (err) 400 return err; 401 402 pipe->end_pos = pipe->size; 403 404 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 405 if (err) 406 return err; 407 408 if (memory != NULL) 409 ipc_detach_dataspace(memory); 410 411 return L4_EOK; 412 } 413 414 /* Access the next region for a pipe endpoint, updating the eventual size of 415 the current region. */ 416 417 long pipe_next(file_t *pipe) 418 { 419 client_Pipe _pipe(pipe->ref); 420 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 421 char *memory = pipe->memory; 422 423 if (err) 424 return err; 425 426 pipe->end_pos = pipe->size; 427 428 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 429 if (err) 430 return err; 431 432 if (memory != NULL) 433 ipc_detach_dataspace(memory); 434 435 return L4_EOK; 436 } 437 438 /* Set the size of the written region. */ 439 440 long pipe_written(file_t *pipe, offset_t size) 441 { 442 if (size <= pipe->size) 443 { 444 pipe->data_end = size; 445 return L4_EOK; 446 } 447 else 448 return -L4_EINVAL; 449 } 450 451 // vim: tabstop=2 expandtab shiftwidth=2