paul@90 | 1 | /* |
paul@90 | 2 | * Filesystem client functions. |
paul@90 | 3 | * |
paul@90 | 4 | * Copyright (C) 2018, 2019, 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@90 | 5 | * |
paul@90 | 6 | * This program is free software; you can redistribute it and/or |
paul@90 | 7 | * modify it under the terms of the GNU General Public License as |
paul@90 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@90 | 9 | * the License, or (at your option) any later version. |
paul@90 | 10 | * |
paul@90 | 11 | * This program is distributed in the hope that it will be useful, |
paul@90 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@90 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@90 | 14 | * GNU General Public License for more details. |
paul@90 | 15 | * |
paul@90 | 16 | * You should have received a copy of the GNU General Public License |
paul@90 | 17 | * along with this program; if not, write to the Free Software |
paul@90 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@90 | 19 | * Boston, MA 02110-1301, USA |
paul@90 | 20 | */ |
paul@90 | 21 | |
paul@90 | 22 | #include <l4/re/env.h> |
paul@90 | 23 | |
paul@90 | 24 | #include <stdio.h> |
paul@90 | 25 | #include <stdlib.h> |
paul@90 | 26 | |
paul@90 | 27 | #include "client.h" |
paul@90 | 28 | |
paul@90 | 29 | |
paul@90 | 30 | |
paul@90 | 31 | /* Default size of pipe regions. */ |
paul@90 | 32 | |
paul@90 | 33 | const offset_t DEFAULT_PIPE_SIZE = 4096; |
paul@90 | 34 | |
paul@90 | 35 | |
paul@90 | 36 | |
paul@117 | 37 | /* Access the given position and synchronise state with the file object. */ |
paul@117 | 38 | |
paul@117 | 39 | static long _access(file_t *file, offset_t position) |
paul@117 | 40 | { |
paul@117 | 41 | long err; |
paul@117 | 42 | |
paul@117 | 43 | if (file->can_mmap) |
paul@117 | 44 | { |
paul@117 | 45 | /* Where the position is outside the current region, re-map. */ |
paul@117 | 46 | |
paul@117 | 47 | if ((position < file->start_pos) || (position >= file->end_pos)) |
paul@117 | 48 | { |
paul@117 | 49 | if (file_mmap(file, position, file_span(file))) |
paul@117 | 50 | return -L4_EIO; |
paul@117 | 51 | } |
paul@117 | 52 | |
paul@117 | 53 | /* Otherwise, flush any written data in the current region and update the |
paul@117 | 54 | file size details. */ |
paul@117 | 55 | |
paul@117 | 56 | else |
paul@117 | 57 | { |
paul@117 | 58 | err = client_flush(file); |
paul@117 | 59 | if (err) |
paul@117 | 60 | return err; |
paul@117 | 61 | } |
paul@117 | 62 | } |
paul@117 | 63 | else |
paul@117 | 64 | { |
paul@117 | 65 | /* Strict conditions for region navigation in pipes. */ |
paul@117 | 66 | |
paul@117 | 67 | if ((position < file->start_pos) || (position > file->end_pos)) |
paul@117 | 68 | { |
paul@117 | 69 | return -L4_EIO; |
paul@117 | 70 | } |
paul@117 | 71 | |
paul@117 | 72 | /* The next region is only available at the end of the mapped memory. */ |
paul@117 | 73 | |
paul@117 | 74 | else if (position == file->end_pos) |
paul@117 | 75 | { |
paul@117 | 76 | err = client_next_region(file); |
paul@117 | 77 | if (err) |
paul@117 | 78 | return err; |
paul@117 | 79 | |
paul@117 | 80 | file->data_current = 0; |
paul@117 | 81 | return L4_EOK; |
paul@117 | 82 | } |
paul@117 | 83 | |
paul@117 | 84 | /* Within the current pipe region, synchronise with the pipe object. */ |
paul@117 | 85 | |
paul@117 | 86 | else |
paul@117 | 87 | { |
paul@117 | 88 | err = client_current_region(file); |
paul@117 | 89 | if (err) |
paul@117 | 90 | return err; |
paul@117 | 91 | } |
paul@117 | 92 | } |
paul@117 | 93 | |
paul@117 | 94 | /* Update the current data offset. */ |
paul@117 | 95 | |
paul@117 | 96 | file->data_current = position - file->start_pos; |
paul@117 | 97 | |
paul@117 | 98 | return L4_EOK; |
paul@117 | 99 | } |
paul@117 | 100 | |
paul@117 | 101 | |
paul@117 | 102 | |
paul@162 | 103 | /* Return whether an operation on file should block for more content or more |
paul@162 | 104 | space. */ |
paul@162 | 105 | |
paul@162 | 106 | static int _operation_blocking(file_t *file, int reading) |
paul@162 | 107 | { |
paul@162 | 108 | return (file->can_block && ( |
paul@162 | 109 | (reading && !file_data_available(file)) || |
paul@162 | 110 | (!reading && !file_data_space(file)))); |
paul@162 | 111 | } |
paul@162 | 112 | |
paul@162 | 113 | |
paul@162 | 114 | |
paul@117 | 115 | /* Return whether an access could occur, blocking if necessary. */ |
paul@117 | 116 | |
paul@162 | 117 | static int _access_blocking(file_t *file, offset_t position, int reading) |
paul@117 | 118 | { |
paul@117 | 119 | long err; |
paul@117 | 120 | |
paul@162 | 121 | /* Attempt to access the position, handling an error condition or a blocking |
paul@162 | 122 | condition. */ |
paul@162 | 123 | |
paul@162 | 124 | while ((err = _access(file, position)) || _operation_blocking(file, reading)) |
paul@117 | 125 | { |
paul@117 | 126 | /* Exit if blocking is not configured or suitable. */ |
paul@117 | 127 | |
paul@162 | 128 | if ((err && (err != -L4_EBUSY)) || !file->can_block) |
paul@117 | 129 | return 0; |
paul@117 | 130 | |
paul@117 | 131 | /* Handle an inability to access by blocking, exiting if waiting failed. */ |
paul@117 | 132 | |
paul@123 | 133 | if (client_wait_file(file)) |
paul@117 | 134 | return 0; |
paul@162 | 135 | |
paul@162 | 136 | /* Handle closure of the object. */ |
paul@162 | 137 | |
paul@162 | 138 | if (file->notifications & NOTIFY_PEER_CLOSED) |
paul@162 | 139 | return 0; |
paul@117 | 140 | } |
paul@117 | 141 | |
paul@117 | 142 | return 1; |
paul@117 | 143 | } |
paul@117 | 144 | |
paul@117 | 145 | |
paul@117 | 146 | |
paul@117 | 147 | /* Ensure that memory is mapped for accessing the given file, using the |
paul@117 | 148 | indicated count as a region size hint. */ |
paul@117 | 149 | |
paul@117 | 150 | static void *_map_memory(file_t *file, offset_t count) |
paul@117 | 151 | { |
paul@117 | 152 | if (file->memory == NULL) |
paul@117 | 153 | { |
paul@117 | 154 | if (file->can_mmap) |
paul@117 | 155 | return client_mmap(file, client_tell(file), count); |
paul@117 | 156 | else if (pipe_current(file)) |
paul@117 | 157 | return NULL; |
paul@117 | 158 | } |
paul@117 | 159 | |
paul@117 | 160 | return file->memory; |
paul@117 | 161 | } |
paul@117 | 162 | |
paul@117 | 163 | |
paul@117 | 164 | |
paul@144 | 165 | /* Open a file opening object. */ |
paul@144 | 166 | |
paul@150 | 167 | l4_cap_idx_t client_open_for_user(user_t user) |
paul@144 | 168 | { |
paul@144 | 169 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@144 | 170 | |
paul@150 | 171 | return client_open_for_user_using(user, server); |
paul@144 | 172 | } |
paul@144 | 173 | |
paul@144 | 174 | /* Open a file opening object via a named capability. */ |
paul@144 | 175 | |
paul@150 | 176 | l4_cap_idx_t client_open_for_user_using(user_t user, l4_cap_idx_t server) |
paul@144 | 177 | { |
paul@144 | 178 | if (l4_is_invalid_cap(server)) |
paul@144 | 179 | return L4_INVALID_CAP; |
paul@144 | 180 | |
paul@144 | 181 | l4_cap_idx_t opener; |
paul@150 | 182 | long err = file_open_for_user(user, server, &opener); |
paul@144 | 183 | |
paul@144 | 184 | if (err) |
paul@144 | 185 | return L4_INVALID_CAP; |
paul@144 | 186 | |
paul@144 | 187 | return opener; |
paul@144 | 188 | } |
paul@144 | 189 | |
paul@144 | 190 | |
paul@144 | 191 | |
paul@90 | 192 | /* Close a filesystem object. */ |
paul@90 | 193 | |
paul@90 | 194 | void client_close(file_t *file) |
paul@90 | 195 | { |
paul@90 | 196 | if (file == NULL) |
paul@90 | 197 | return; |
paul@90 | 198 | |
paul@159 | 199 | file_flush(file); |
paul@90 | 200 | file_close(file); |
paul@90 | 201 | free(file); |
paul@90 | 202 | } |
paul@90 | 203 | |
paul@90 | 204 | |
paul@90 | 205 | |
paul@90 | 206 | /* Open a filesystem object. */ |
paul@90 | 207 | |
paul@90 | 208 | file_t *client_open(const char *name, flags_t flags) |
paul@90 | 209 | { |
paul@144 | 210 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@144 | 211 | |
paul@144 | 212 | return client_open_using(name, flags, server); |
paul@103 | 213 | } |
paul@103 | 214 | |
paul@103 | 215 | /* Open a filesystem object via a named capability. */ |
paul@103 | 216 | |
paul@144 | 217 | file_t *client_open_using(const char *name, flags_t flags, l4_cap_idx_t server) |
paul@103 | 218 | { |
paul@144 | 219 | if (l4_is_invalid_cap(server)) |
paul@144 | 220 | return NULL; |
paul@144 | 221 | |
paul@90 | 222 | file_t *file = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 223 | |
paul@90 | 224 | if (file == NULL) |
paul@90 | 225 | return NULL; |
paul@90 | 226 | |
paul@90 | 227 | if (file_open(file, name, flags, server)) |
paul@90 | 228 | { |
paul@90 | 229 | free(file); |
paul@90 | 230 | return NULL; |
paul@90 | 231 | } |
paul@90 | 232 | |
paul@90 | 233 | return file; |
paul@90 | 234 | } |
paul@90 | 235 | |
paul@90 | 236 | |
paul@90 | 237 | |
paul@90 | 238 | /* Open a pipe object. */ |
paul@90 | 239 | |
paul@90 | 240 | long client_pipe(file_t **reader, file_t **writer) |
paul@90 | 241 | { |
paul@144 | 242 | l4_cap_idx_t server = l4re_env_get_cap("pipes"); |
paul@144 | 243 | |
paul@144 | 244 | return client_pipe_using(reader, writer, server); |
paul@135 | 245 | } |
paul@135 | 246 | |
paul@144 | 247 | long client_pipe_using(file_t **reader, file_t **writer, l4_cap_idx_t server) |
paul@135 | 248 | { |
paul@144 | 249 | if (l4_is_invalid_cap(server)) |
paul@144 | 250 | return -L4_EINVAL; |
paul@144 | 251 | |
paul@90 | 252 | *reader = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 253 | |
paul@90 | 254 | if (*reader == NULL) |
paul@90 | 255 | return -L4_ENOMEM; |
paul@90 | 256 | |
paul@90 | 257 | *writer = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 258 | |
paul@90 | 259 | if (*writer == NULL) |
paul@90 | 260 | { |
paul@90 | 261 | free(*reader); |
paul@90 | 262 | return -L4_ENOMEM; |
paul@90 | 263 | } |
paul@90 | 264 | |
paul@90 | 265 | long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); |
paul@90 | 266 | |
paul@90 | 267 | if (err) |
paul@90 | 268 | { |
paul@90 | 269 | free(*reader); |
paul@90 | 270 | free(*writer); |
paul@90 | 271 | } |
paul@90 | 272 | |
paul@90 | 273 | return err; |
paul@90 | 274 | } |
paul@90 | 275 | |
paul@90 | 276 | |
paul@90 | 277 | |
paul@90 | 278 | /* Flush data explicitly to the filesystem object. */ |
paul@90 | 279 | |
paul@90 | 280 | long client_flush(file_t *file) |
paul@90 | 281 | { |
paul@90 | 282 | if (file == NULL) |
paul@90 | 283 | return -L4_EINVAL; |
paul@90 | 284 | |
paul@90 | 285 | /* Flush and retain most buffer settings. */ |
paul@90 | 286 | |
paul@90 | 287 | return file_flush(file); |
paul@90 | 288 | } |
paul@90 | 289 | |
paul@90 | 290 | |
paul@90 | 291 | |
paul@90 | 292 | /* Map a memory region to a file. */ |
paul@90 | 293 | |
paul@90 | 294 | void *client_mmap(file_t *file, offset_t position, offset_t length) |
paul@90 | 295 | { |
paul@90 | 296 | if ((file == NULL) || (file_mmap(file, position, length))) |
paul@90 | 297 | return NULL; |
paul@90 | 298 | |
paul@90 | 299 | return file->memory; |
paul@90 | 300 | } |
paul@90 | 301 | |
paul@90 | 302 | |
paul@90 | 303 | |
paul@90 | 304 | /* Obtain the current region of a pipe. */ |
paul@90 | 305 | |
paul@116 | 306 | long client_current_region(file_t *file) |
paul@90 | 307 | { |
paul@116 | 308 | if (file == NULL) |
paul@116 | 309 | return -L4_EINVAL; |
paul@90 | 310 | |
paul@116 | 311 | return pipe_current(file); |
paul@90 | 312 | } |
paul@90 | 313 | |
paul@90 | 314 | |
paul@90 | 315 | |
paul@90 | 316 | /* Obtain the next region of a pipe. */ |
paul@90 | 317 | |
paul@116 | 318 | long client_next_region(file_t *file) |
paul@90 | 319 | { |
paul@116 | 320 | if (file == NULL) |
paul@116 | 321 | return -L4_EINVAL; |
paul@90 | 322 | |
paul@116 | 323 | return pipe_next(file); |
paul@90 | 324 | } |
paul@90 | 325 | |
paul@90 | 326 | |
paul@90 | 327 | |
paul@90 | 328 | /* Read from the filesystem object into the buffer provided. */ |
paul@90 | 329 | |
paul@90 | 330 | offset_t client_read(file_t *file, void *buf, offset_t count) |
paul@90 | 331 | { |
paul@90 | 332 | if (file == NULL) |
paul@90 | 333 | return 0; |
paul@90 | 334 | |
paul@100 | 335 | /* Map memory if none has been mapped so far. */ |
paul@100 | 336 | |
paul@108 | 337 | if (_map_memory(file, count) == NULL) |
paul@100 | 338 | return 0; |
paul@100 | 339 | |
paul@90 | 340 | /* Amount available in the descriptor buffer already. */ |
paul@90 | 341 | |
paul@90 | 342 | offset_t available = file_data_available(file); |
paul@90 | 343 | offset_t to_transfer, total = 0; |
paul@90 | 344 | |
paul@90 | 345 | while (count > 0) |
paul@90 | 346 | { |
paul@90 | 347 | /* If there is no data, try and obtain more data. */ |
paul@90 | 348 | |
paul@90 | 349 | if (!available) |
paul@90 | 350 | { |
paul@90 | 351 | /* Flush any unwritten data, preparing to read from the file position at |
paul@90 | 352 | the end of the data, and returning if no new data is available. */ |
paul@90 | 353 | |
paul@162 | 354 | if (!_access_blocking(file, file_data_end_position(file), 1)) |
paul@90 | 355 | break; |
paul@90 | 356 | |
paul@90 | 357 | available = file_data_available(file); |
paul@90 | 358 | |
paul@90 | 359 | if (!available) |
paul@90 | 360 | break; |
paul@90 | 361 | } |
paul@90 | 362 | |
paul@90 | 363 | /* Transfer data into the supplied buffer. */ |
paul@90 | 364 | |
paul@90 | 365 | to_transfer = available <= count ? available : count; |
paul@90 | 366 | |
paul@90 | 367 | file_data_read(file, (char *) buf, to_transfer); |
paul@90 | 368 | |
paul@90 | 369 | /* Update counters. */ |
paul@90 | 370 | |
paul@90 | 371 | available -= to_transfer; |
paul@90 | 372 | |
paul@90 | 373 | count -= to_transfer; |
paul@90 | 374 | total += to_transfer; |
paul@90 | 375 | |
paul@90 | 376 | buf = ((char *) buf + to_transfer); |
paul@90 | 377 | } |
paul@90 | 378 | |
paul@90 | 379 | return total; |
paul@90 | 380 | } |
paul@90 | 381 | |
paul@90 | 382 | |
paul@90 | 383 | |
paul@90 | 384 | /* Ensure that the buffer can provide the needed data. */ |
paul@90 | 385 | |
paul@90 | 386 | offset_t client_seek(file_t *file, offset_t offset, int whence) |
paul@90 | 387 | { |
paul@90 | 388 | if (file == NULL) |
paul@90 | 389 | return 0; |
paul@90 | 390 | |
paul@90 | 391 | offset_t position, current = file_data_current_position(file), change; |
paul@90 | 392 | |
paul@90 | 393 | switch (whence) |
paul@90 | 394 | { |
paul@90 | 395 | case SEEK_SET: |
paul@90 | 396 | position = offset; |
paul@90 | 397 | break; |
paul@90 | 398 | |
paul@90 | 399 | case SEEK_CUR: |
paul@90 | 400 | position = current + offset; |
paul@90 | 401 | break; |
paul@90 | 402 | |
paul@90 | 403 | case SEEK_END: |
paul@90 | 404 | position = file->size + offset; |
paul@90 | 405 | break; |
paul@90 | 406 | |
paul@90 | 407 | default: |
paul@90 | 408 | /* NOTE: Set errno to EINVAL. */ |
paul@117 | 409 | return current; |
paul@90 | 410 | } |
paul@90 | 411 | |
paul@90 | 412 | /* Retain the current position if unchanged. */ |
paul@90 | 413 | |
paul@90 | 414 | if (position == current) |
paul@90 | 415 | return position; |
paul@90 | 416 | |
paul@90 | 417 | /* Move forward in the file. */ |
paul@90 | 418 | |
paul@90 | 419 | if (position > current) |
paul@90 | 420 | { |
paul@90 | 421 | change = position - current; |
paul@90 | 422 | |
paul@90 | 423 | /* Move towards the end of available data. |
paul@90 | 424 | Request new data if not enough is available. */ |
paul@90 | 425 | |
paul@90 | 426 | if (change <= file_data_available(file)) |
paul@90 | 427 | { |
paul@90 | 428 | file->data_current += change; |
paul@90 | 429 | return position; |
paul@90 | 430 | } |
paul@90 | 431 | } |
paul@90 | 432 | |
paul@90 | 433 | /* Move backward in the file. */ |
paul@90 | 434 | |
paul@90 | 435 | else |
paul@90 | 436 | { |
paul@90 | 437 | change = current - position; |
paul@90 | 438 | |
paul@90 | 439 | /* Move towards the start of available data. |
paul@90 | 440 | Request new data if moving beyond the start of the data. */ |
paul@90 | 441 | |
paul@90 | 442 | if (change <= file->data_current) |
paul@90 | 443 | { |
paul@90 | 444 | file->data_current -= change; |
paul@90 | 445 | return position; |
paul@90 | 446 | } |
paul@90 | 447 | } |
paul@90 | 448 | |
paul@90 | 449 | /* Handle unwritten data and reset the buffer for reading. */ |
paul@90 | 450 | |
paul@117 | 451 | if (_access(file, position)) |
paul@117 | 452 | return current; |
paul@117 | 453 | |
paul@90 | 454 | return position; |
paul@90 | 455 | } |
paul@90 | 456 | |
paul@90 | 457 | |
paul@90 | 458 | |
paul@117 | 459 | /* Set or unset blocking access for a file. */ |
paul@117 | 460 | |
paul@122 | 461 | long client_set_blocking(file_t *file, notify_flags_t flags) |
paul@117 | 462 | { |
paul@117 | 463 | long err; |
paul@117 | 464 | |
paul@122 | 465 | if (file->can_block == flags) |
paul@117 | 466 | return L4_EOK; |
paul@117 | 467 | |
paul@117 | 468 | // NOTE: Set appropriate flags. |
paul@117 | 469 | |
paul@122 | 470 | if (flags) |
paul@122 | 471 | err = client_subscribe(file, flags); |
paul@117 | 472 | else |
paul@117 | 473 | err = client_unsubscribe(file); |
paul@117 | 474 | |
paul@117 | 475 | if (err) |
paul@117 | 476 | return err; |
paul@117 | 477 | |
paul@122 | 478 | file->can_block = flags; |
paul@117 | 479 | return L4_EOK; |
paul@117 | 480 | } |
paul@117 | 481 | |
paul@117 | 482 | |
paul@117 | 483 | |
paul@117 | 484 | /* Subscribe from events concerning a file. */ |
paul@117 | 485 | |
paul@121 | 486 | long client_subscribe(file_t *file, notify_flags_t flags) |
paul@117 | 487 | { |
paul@117 | 488 | if (file == NULL) |
paul@117 | 489 | return -L4_EINVAL; |
paul@117 | 490 | |
paul@117 | 491 | return file_notify_subscribe(file, flags); |
paul@117 | 492 | } |
paul@117 | 493 | |
paul@117 | 494 | |
paul@117 | 495 | |
paul@117 | 496 | /* Return the current position in the file. */ |
paul@117 | 497 | |
paul@90 | 498 | long client_tell(file_t *file) |
paul@90 | 499 | { |
paul@90 | 500 | if (file == NULL) |
paul@90 | 501 | return -L4_EINVAL; |
paul@90 | 502 | |
paul@90 | 503 | return file_data_current_position(file); |
paul@90 | 504 | } |
paul@90 | 505 | |
paul@90 | 506 | |
paul@90 | 507 | |
paul@117 | 508 | /* Unsubscribe from events concerning a file. */ |
paul@117 | 509 | |
paul@117 | 510 | long client_unsubscribe(file_t *file) |
paul@117 | 511 | { |
paul@117 | 512 | if (file == NULL) |
paul@117 | 513 | return -L4_EINVAL; |
paul@117 | 514 | |
paul@117 | 515 | return file_notify_unsubscribe(file); |
paul@117 | 516 | } |
paul@117 | 517 | |
paul@117 | 518 | |
paul@117 | 519 | |
paul@123 | 520 | /* Wait for events involving a specific file. */ |
paul@123 | 521 | |
paul@123 | 522 | long client_wait_file(file_t *file) |
paul@123 | 523 | { |
paul@123 | 524 | if (file == NULL) |
paul@123 | 525 | return -L4_EINVAL; |
paul@123 | 526 | |
paul@123 | 527 | return file_notify_wait_file(file); |
paul@123 | 528 | } |
paul@123 | 529 | |
paul@123 | 530 | /* Wait for events concerning files, referencing a file object if an event is |
paul@123 | 531 | delivered. */ |
paul@123 | 532 | |
paul@123 | 533 | long client_wait_files(file_t **file) |
paul@123 | 534 | { |
paul@123 | 535 | return file_notify_wait_files(file); |
paul@117 | 536 | } |
paul@117 | 537 | |
paul@117 | 538 | |
paul@117 | 539 | |
paul@90 | 540 | /* Write to the filesystem object from the buffer provided. */ |
paul@90 | 541 | |
paul@90 | 542 | offset_t client_write(file_t *file, const void *buf, offset_t count) |
paul@90 | 543 | { |
paul@90 | 544 | if (file == NULL) |
paul@90 | 545 | return 0; |
paul@90 | 546 | |
paul@100 | 547 | /* Map memory if none has been mapped so far. */ |
paul@100 | 548 | |
paul@108 | 549 | if (_map_memory(file, count) == NULL) |
paul@100 | 550 | return 0; |
paul@100 | 551 | |
paul@90 | 552 | /* Attempt to ensure that the file can accept the amount of data to be |
paul@90 | 553 | written. This may not resize to the needed amount if a file has a fixed |
paul@90 | 554 | size, but data will still be written to any available space. */ |
paul@90 | 555 | |
paul@90 | 556 | offset_t needed_size = file_data_current_position(file) + count; |
paul@90 | 557 | |
paul@108 | 558 | if (file->has_size) |
paul@90 | 559 | { |
paul@108 | 560 | if (file->size < needed_size) |
paul@108 | 561 | { |
paul@108 | 562 | file_resize(file, needed_size); |
paul@90 | 563 | |
paul@108 | 564 | if (file->size < needed_size) |
paul@108 | 565 | count = file->size - file_data_current_position(file); |
paul@108 | 566 | } |
paul@90 | 567 | } |
paul@90 | 568 | |
paul@90 | 569 | /* Space remaining in the descriptor buffer. */ |
paul@90 | 570 | |
paul@90 | 571 | offset_t space = file_data_space(file); |
paul@90 | 572 | offset_t to_transfer, total = 0; |
paul@90 | 573 | |
paul@90 | 574 | while (count > 0) |
paul@90 | 575 | { |
paul@90 | 576 | /* If no space is available, try and send data, reset the buffer. */ |
paul@90 | 577 | |
paul@90 | 578 | if (!space) |
paul@90 | 579 | { |
paul@90 | 580 | /* Flush any unwritten data and continue writing from the current data |
paul@90 | 581 | position. */ |
paul@90 | 582 | |
paul@162 | 583 | if (!_access_blocking(file, file_data_current_position(file), 0)) |
paul@90 | 584 | break; |
paul@90 | 585 | |
paul@90 | 586 | space = file_data_space(file); |
paul@90 | 587 | } |
paul@90 | 588 | |
paul@90 | 589 | /* Transfer data into the supplied buffer. */ |
paul@90 | 590 | |
paul@90 | 591 | to_transfer = space <= count ? space : count; |
paul@90 | 592 | |
paul@90 | 593 | file_data_write(file, (char *) buf, to_transfer); |
paul@90 | 594 | |
paul@90 | 595 | /* Update counters. */ |
paul@90 | 596 | |
paul@90 | 597 | space -= to_transfer; |
paul@90 | 598 | |
paul@90 | 599 | count -= to_transfer; |
paul@90 | 600 | total += to_transfer; |
paul@90 | 601 | |
paul@90 | 602 | buf = ((char *) buf + to_transfer); |
paul@90 | 603 | } |
paul@90 | 604 | |
paul@90 | 605 | return total; |
paul@90 | 606 | } |
paul@90 | 607 | |
paul@90 | 608 | // vim: tabstop=2 expandtab shiftwidth=2 |