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