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