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