1 /* 2 * Filesystem client functions. 3 * 4 * Copyright (C) 2018, 2019, 2020, 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 <l4/re/env.h> 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 27 #include "client.h" 28 29 30 31 /* Default size of pipe regions. */ 32 33 const offset_t DEFAULT_PIPE_SIZE = 4096; 34 35 36 37 /* Close a filesystem object. */ 38 39 void client_close(file_t *file) 40 { 41 if (file == NULL) 42 return; 43 44 file_close(file); 45 free(file); 46 } 47 48 49 50 /* Open a filesystem object. */ 51 52 file_t *client_open(const char *name, flags_t flags) 53 { 54 file_t *file = (file_t *) malloc(sizeof(file_t)); 55 56 if (file == NULL) 57 return NULL; 58 59 l4_cap_idx_t server = l4re_env_get_cap("server"); 60 61 if (file_open(file, name, flags, server)) 62 { 63 free(file); 64 return NULL; 65 } 66 67 return file; 68 } 69 70 71 72 /* Open a pipe object. */ 73 74 long client_pipe(file_t **reader, file_t **writer) 75 { 76 *reader = (file_t *) malloc(sizeof(file_t)); 77 78 if (*reader == NULL) 79 return -L4_ENOMEM; 80 81 *writer = (file_t *) malloc(sizeof(file_t)); 82 83 if (*writer == NULL) 84 { 85 free(*reader); 86 return -L4_ENOMEM; 87 } 88 89 l4_cap_idx_t server = l4re_env_get_cap("pipes"); 90 91 long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); 92 93 if (err) 94 { 95 free(*reader); 96 free(*writer); 97 } 98 99 return err; 100 } 101 102 103 104 /* Flush data conditionally to the filesystem object. */ 105 106 static long _flush(file_t *file, offset_t position) 107 { 108 long err; 109 110 /* Where the position is outside the current region, re-map. */ 111 112 if ((position < file->start_pos) || (position >= file->end_pos)) 113 { 114 if (file->can_mmap) 115 { 116 if (file_mmap(file, position, file_span(file))) 117 return -L4_EIO; 118 } 119 120 /* Strict conditions for region navigation in pipes. */ 121 122 else if ((position != file->end_pos) || 123 (client_next_region(file) == NULL)) 124 return -L4_EIO; 125 } 126 127 /* Otherwise, flush any written data in the current region and update the 128 file size details. */ 129 130 else 131 { 132 err = client_flush(file); 133 134 if (err) 135 return err; 136 } 137 138 /* Update the current data offset. */ 139 140 file->data_current = position - file->start_pos; 141 return L4_EOK; 142 } 143 144 145 146 /* Flush data explicitly to the filesystem object. */ 147 148 long client_flush(file_t *file) 149 { 150 if (file == NULL) 151 return -L4_EINVAL; 152 153 /* Flush and retain most buffer settings. */ 154 155 return file_flush(file); 156 } 157 158 159 160 /* Map a memory region to a file. */ 161 162 void *client_mmap(file_t *file, offset_t position, offset_t length) 163 { 164 if ((file == NULL) || (file_mmap(file, position, length))) 165 return NULL; 166 167 return file->memory; 168 } 169 170 171 172 /* Obtain the current region of a pipe. */ 173 174 void *client_current_region(file_t *file) 175 { 176 if ((file == NULL) || (pipe_current(file))) 177 return NULL; 178 179 return file->memory; 180 } 181 182 183 184 /* Obtain the next region of a pipe. */ 185 186 void *client_next_region(file_t *file) 187 { 188 if ((file == NULL) || (pipe_next(file))) 189 return NULL; 190 191 return file->memory; 192 } 193 194 195 196 /* Read from the filesystem object into the buffer provided. */ 197 198 offset_t client_read(file_t *file, void *buf, offset_t count) 199 { 200 if (file == NULL) 201 return 0; 202 203 /* Amount available in the descriptor buffer already. */ 204 205 offset_t available = file_data_available(file); 206 offset_t to_transfer, total = 0; 207 208 while (count > 0) 209 { 210 /* If there is no data, try and obtain more data. */ 211 212 if (!available) 213 { 214 /* Flush any unwritten data, preparing to read from the file position at 215 the end of the data, and returning if no new data is available. */ 216 217 if (_flush(file, file_data_end_position(file))) 218 break; 219 220 available = file_data_available(file); 221 222 if (!available) 223 break; 224 } 225 226 /* Transfer data into the supplied buffer. */ 227 228 to_transfer = available <= count ? available : count; 229 230 file_data_read(file, (char *) buf, to_transfer); 231 232 /* Update counters. */ 233 234 available -= to_transfer; 235 236 count -= to_transfer; 237 total += to_transfer; 238 239 buf = ((char *) buf + to_transfer); 240 } 241 242 return total; 243 } 244 245 246 247 /* Ensure that the buffer can provide the needed data. */ 248 249 offset_t client_seek(file_t *file, offset_t offset, int whence) 250 { 251 if (file == NULL) 252 return 0; 253 254 offset_t position, current = file_data_current_position(file), change; 255 256 switch (whence) 257 { 258 case SEEK_SET: 259 position = offset; 260 break; 261 262 case SEEK_CUR: 263 position = current + offset; 264 break; 265 266 case SEEK_END: 267 position = file->size + offset; 268 break; 269 270 default: 271 /* NOTE: Set errno to EINVAL. */ 272 return -1; 273 } 274 275 /* Retain the current position if unchanged. */ 276 277 if (position == current) 278 return position; 279 280 /* Move forward in the file. */ 281 282 if (position > current) 283 { 284 change = position - current; 285 286 /* Move towards the end of available data. 287 Request new data if not enough is available. */ 288 289 if (change <= file_data_available(file)) 290 { 291 file->data_current += change; 292 return position; 293 } 294 } 295 296 /* Move backward in the file. */ 297 298 else 299 { 300 change = current - position; 301 302 /* Move towards the start of available data. 303 Request new data if moving beyond the start of the data. */ 304 305 if (change <= file->data_current) 306 { 307 file->data_current -= change; 308 return position; 309 } 310 } 311 312 /* Handle unwritten data and reset the buffer for reading. */ 313 314 _flush(file, position); 315 return position; 316 } 317 318 319 320 long client_tell(file_t *file) 321 { 322 if (file == NULL) 323 return -L4_EINVAL; 324 325 return file_data_current_position(file); 326 } 327 328 329 330 /* Write to the filesystem object from the buffer provided. */ 331 332 offset_t client_write(file_t *file, const void *buf, offset_t count) 333 { 334 if (file == NULL) 335 return 0; 336 337 /* Attempt to ensure that the file can accept the amount of data to be 338 written. This may not resize to the needed amount if a file has a fixed 339 size, but data will still be written to any available space. */ 340 341 offset_t needed_size = file_data_current_position(file) + count; 342 343 if (file->size < needed_size) 344 { 345 file_resize(file, needed_size); 346 347 if (file->size < needed_size) 348 count = file->size - file_data_current_position(file); 349 } 350 351 /* Space remaining in the descriptor buffer. */ 352 353 offset_t space = file_data_space(file); 354 offset_t to_transfer, total = 0; 355 356 while (count > 0) 357 { 358 /* If no space is available, try and send data, reset the buffer. */ 359 360 if (!space) 361 { 362 /* Flush any unwritten data and continue writing from the current data 363 position. */ 364 365 if (_flush(file, file_data_current_position(file))) 366 break; 367 368 space = file_data_space(file); 369 } 370 371 /* Transfer data into the supplied buffer. */ 372 373 to_transfer = space <= count ? space : count; 374 375 file_data_write(file, (char *) buf, to_transfer); 376 377 /* Update counters. */ 378 379 space -= to_transfer; 380 381 count -= to_transfer; 382 total += to_transfer; 383 384 buf = ((char *) buf + to_transfer); 385 } 386 387 return total; 388 } 389 390 // vim: tabstop=2 expandtab shiftwidth=2