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 /* Release resources for the given file. */ 40 41 void file_close(file_t *file) 42 { 43 if (l4_is_valid_cap(file->ref)) 44 ipc_cap_free_um(file->ref); 45 46 if (file->memory != NULL) 47 ipc_detach_dataspace(file->memory); 48 49 file_init(file); 50 } 51 52 /* Initialise a file structure for a context obtained from the given server. */ 53 54 long file_context(file_t *file, l4_cap_idx_t server) 55 { 56 if (l4_is_invalid_cap(server)) 57 return -L4_EINVAL; 58 59 client_Opener opener(server); 60 unsigned long size, flags; 61 long err; 62 63 file_init(file); 64 65 err = opener.context(&file->ref); 66 if (err) 67 return err; 68 69 client_Dataspace context_ds(file->ref); 70 71 err = context_ds.info(&size, &flags); 72 if (err) 73 return err; 74 75 file->start_pos = 0; 76 file->end_pos = size; 77 78 return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); 79 } 80 81 /* Open a file using the given structure and context. */ 82 83 long file_context_open(file_t *file, flags_t flags, file_t *context) 84 { 85 client_OpenerContext openercontext(context->ref); 86 file_init(file); 87 return openercontext.open(flags, &file->size, &file->ref); 88 } 89 90 /* Initialise the given file structure. */ 91 92 void file_init(file_t *file) 93 { 94 file->memory = NULL; 95 file->ref = L4_INVALID_CAP; 96 file->start_pos = 0; 97 file->end_pos = 0; 98 file->data_end = 0; 99 } 100 101 /* Open a file using the given structure, indicating the filename and 102 filesystem server. This is a convenience function invoking file_context and 103 file_context_open. */ 104 105 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) 106 { 107 file_t context; 108 long err; 109 110 err = file_context(&context, server); 111 if (err) 112 return err; 113 114 if (!file_string_set(&context, filename, 0, NULL)) 115 return -L4_ENOMEM; 116 117 err = file_context_open(file, flags, &context); 118 file_close(&context); 119 return err; 120 } 121 122 123 124 /* Map a region of the given file to a memory region. */ 125 126 long file_mmap(file_t *file, offset_t position, offset_t length) 127 { 128 client_MappedFile mapped_file(file->ref); 129 long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); 130 131 if (err) 132 return err; 133 134 return ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); 135 } 136 137 /* Resize a file. */ 138 139 long file_resize(file_t *file, offset_t size) 140 { 141 client_File _file(file->ref); 142 offset_t file_size = size; 143 long err = _file.resize(&file_size); 144 145 if (!err) 146 { 147 /* Determine the extent of the file in this region. */ 148 149 if (file_size > file->end_pos) 150 file->data_end = file_span(file); 151 else 152 file->data_end = file_size - file->start_pos; 153 154 /* Update the file size locally. */ 155 156 file->size = file_size; 157 } 158 159 return err; 160 } 161 162 163 164 /* Return the amount of data in the mapped region for the given file. */ 165 166 offset_t file_populated_span(file_t *file) 167 { 168 offset_t size = file_span(file); 169 return (file->data_end < size) ? file->data_end : size; 170 } 171 172 /* Return the size of the mapped region for the given file. */ 173 174 offset_t file_span(file_t *file) 175 { 176 return file->end_pos - file->start_pos; 177 } 178 179 180 181 /* Get a pointer to any terminated string at the given offset or NULL if the 182 data from offset is not terminated. */ 183 184 char *file_string_get(file_t *file, offset_t offset) 185 { 186 offset_t limit = file_span(file) - offset; 187 188 if (strnlen(file->memory + offset, limit) < limit) 189 return file->memory + offset; 190 else 191 return NULL; 192 } 193 194 /* Copy a string to the mapped region at the given offset, returning 1 (true) 195 where all characters were copied, 0 (false) otherwise. The precise number of 196 characters copied, excluding the zero terminator is provided via the written 197 parameter if it is not specified as NULL. */ 198 199 int file_string_set(file_t *file, const char *data, offset_t offset, 200 offset_t *written) 201 { 202 offset_t i, pos, limit = file_span(file); 203 204 /* Do not attempt to copy data with an invalid offset. */ 205 206 if (offset >= limit) 207 { 208 if (written != NULL) 209 *written = 0; 210 return 0; 211 } 212 213 /* Copy the data to the given offset, stopping at the end of the region. */ 214 215 for (i = 0, pos = offset; pos < limit; i++, pos++) 216 { 217 file->memory[pos] = data[i]; 218 219 /* Terminator written, can return immediately. */ 220 221 if (!data[i]) 222 { 223 if (written != NULL) 224 *written = pos - offset; 225 return 1; 226 } 227 } 228 229 /* Terminate the incomplete string at the end of the region. */ 230 231 file->memory[limit - 1] = '\0'; 232 if (written != NULL) 233 *written = limit - 1 - offset; 234 return 0; 235 } 236 237 238 239 /* Open two pipe endpoints using the given pipe server. */ 240 241 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 242 { 243 if (l4_is_invalid_cap(server)) 244 return -L4_EINVAL; 245 246 client_PipeOpener opener(server); 247 248 file_init(reader); 249 file_init(writer); 250 251 long err = opener.pipe(size, &reader->ref, &writer->ref); 252 if (err) 253 return err; 254 255 err = pipe_next(writer) || pipe_next(reader); 256 257 if (err) 258 { 259 file_close(reader); 260 file_close(writer); 261 } 262 263 return err; 264 } 265 266 /* Access the current region for a pipe endpoint. */ 267 268 long pipe_current(file_t *pipe) 269 { 270 client_Pipe _pipe(pipe->ref); 271 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 272 char *memory = pipe->memory; 273 274 if (err) 275 return err; 276 277 pipe->end_pos = pipe->size; 278 279 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 280 if (err) 281 return err; 282 283 if (memory != NULL) 284 ipc_detach_dataspace(memory); 285 286 return L4_EOK; 287 } 288 289 /* Access the next region for a pipe endpoint, updating the eventual size of 290 the current region. */ 291 292 long pipe_next(file_t *pipe) 293 { 294 client_Pipe _pipe(pipe->ref); 295 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 296 char *memory = pipe->memory; 297 298 if (err) 299 return err; 300 301 pipe->end_pos = pipe->size; 302 303 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 304 if (err) 305 return err; 306 307 if (memory != NULL) 308 ipc_detach_dataspace(memory); 309 310 return L4_EOK; 311 } 312 313 /* Set the size of the written region. */ 314 315 long pipe_written(file_t *pipe, offset_t size) 316 { 317 if (size <= pipe->size) 318 { 319 pipe->data_end = size; 320 return L4_EOK; 321 } 322 else 323 return -L4_EINVAL; 324 } 325 326 // vim: tabstop=2 expandtab shiftwidth=2