paul@90 | 1 | /* |
paul@90 | 2 | * Filesystem client functions. |
paul@90 | 3 | * |
paul@236 | 4 | * Copyright (C) 2018, 2019, 2020, 2021, 2022 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@330 | 44 | /* Merging of region flags from protection and access flags. */ |
paul@330 | 45 | |
paul@330 | 46 | static l4re_rm_flags_t _combine_region_flags(l4re_rm_flags_t region_flags, |
paul@330 | 47 | flags_t flags) |
paul@330 | 48 | { |
paul@330 | 49 | return region_flags & (file_region_flags(flags) | L4RE_RM_F_X); |
paul@330 | 50 | } |
paul@330 | 51 | |
paul@330 | 52 | /* Conversion of protection and access flags to region flags. */ |
paul@330 | 53 | |
paul@330 | 54 | l4re_rm_flags_t client_region_flags(prot_t prot, flags_t flags) |
paul@330 | 55 | { |
paul@330 | 56 | l4re_rm_flags_t rm_flags = 0; |
paul@330 | 57 | |
paul@330 | 58 | if (prot & PROT_READ) |
paul@330 | 59 | rm_flags |= L4RE_RM_F_R; |
paul@330 | 60 | if (prot & PROT_WRITE) |
paul@330 | 61 | rm_flags |= L4RE_RM_F_W; |
paul@330 | 62 | if (prot & PROT_EXEC) |
paul@330 | 63 | rm_flags |= L4RE_RM_F_X; |
paul@330 | 64 | |
paul@330 | 65 | return _combine_region_flags(rm_flags, flags); |
paul@330 | 66 | } |
paul@330 | 67 | |
paul@330 | 68 | |
paul@330 | 69 | |
paul@169 | 70 | /* Access the given position and synchronise state with the file object. Pipe |
paul@169 | 71 | objects may return busy conditions indicating that the desired access cannot |
paul@169 | 72 | yet be fulfilled. */ |
paul@117 | 73 | |
paul@117 | 74 | static long _access(file_t *file, offset_t position) |
paul@117 | 75 | { |
paul@117 | 76 | long err; |
paul@117 | 77 | |
paul@171 | 78 | if (file->object_flags & OBJECT_SUPPORTS_MMAP) |
paul@117 | 79 | { |
paul@117 | 80 | /* Where the position is outside the current region, re-map. */ |
paul@117 | 81 | |
paul@117 | 82 | if ((position < file->start_pos) || (position >= file->end_pos)) |
paul@117 | 83 | { |
paul@339 | 84 | if (file_mmap(file, position, file_span(file), 0, 0, |
paul@330 | 85 | file_region_flags(file->flags))) |
paul@117 | 86 | return -L4_EIO; |
paul@117 | 87 | } |
paul@117 | 88 | |
paul@117 | 89 | /* Otherwise, flush any written data in the current region and update the |
paul@117 | 90 | file size details. */ |
paul@117 | 91 | |
paul@117 | 92 | else |
paul@117 | 93 | { |
paul@117 | 94 | err = client_flush(file); |
paul@117 | 95 | if (err) |
paul@117 | 96 | return err; |
paul@117 | 97 | } |
paul@165 | 98 | |
paul@165 | 99 | /* Update the current data offset. */ |
paul@165 | 100 | |
paul@165 | 101 | file->data_current = position - file->start_pos; |
paul@165 | 102 | |
paul@165 | 103 | return L4_EOK; |
paul@117 | 104 | } |
paul@117 | 105 | else |
paul@117 | 106 | { |
paul@192 | 107 | /* Handle the initial condition with no current region. */ |
paul@192 | 108 | |
paul@192 | 109 | if (file->memory == NULL) |
paul@192 | 110 | { |
paul@192 | 111 | err = client_current_region(file); |
paul@192 | 112 | if (err) |
paul@192 | 113 | return err; |
paul@192 | 114 | } |
paul@192 | 115 | |
paul@117 | 116 | /* Strict conditions for region navigation in pipes. */ |
paul@117 | 117 | |
paul@117 | 118 | if ((position < file->start_pos) || (position > file->end_pos)) |
paul@117 | 119 | { |
paul@117 | 120 | return -L4_EIO; |
paul@117 | 121 | } |
paul@117 | 122 | |
paul@117 | 123 | /* The next region is only available at the end of the mapped memory. */ |
paul@117 | 124 | |
paul@117 | 125 | else if (position == file->end_pos) |
paul@117 | 126 | { |
paul@117 | 127 | err = client_next_region(file); |
paul@117 | 128 | if (err) |
paul@117 | 129 | return err; |
paul@117 | 130 | |
paul@117 | 131 | file->data_current = 0; |
paul@117 | 132 | return L4_EOK; |
paul@117 | 133 | } |
paul@117 | 134 | |
paul@117 | 135 | /* Within the current pipe region, synchronise with the pipe object. */ |
paul@117 | 136 | |
paul@117 | 137 | else |
paul@402 | 138 | { |
paul@165 | 139 | return client_current_region(file); |
paul@402 | 140 | } |
paul@117 | 141 | } |
paul@117 | 142 | } |
paul@117 | 143 | |
paul@117 | 144 | |
paul@117 | 145 | |
paul@162 | 146 | /* Return whether an operation on file should block for more content or more |
paul@169 | 147 | space. A file must be configured for blocking, not be closed, and must either |
paul@169 | 148 | be lacking content (if reading) or space (if writing). */ |
paul@162 | 149 | |
paul@162 | 150 | static int _operation_blocking(file_t *file, int reading) |
paul@162 | 151 | { |
paul@165 | 152 | return (file->can_block && !(file->notifications & NOTIFY_PEER_CLOSED) && ( |
paul@402 | 153 | (reading && !file_data_available(file)) || |
paul@402 | 154 | (!reading && !file_data_space(file)))); |
paul@162 | 155 | } |
paul@162 | 156 | |
paul@162 | 157 | |
paul@162 | 158 | |
paul@117 | 159 | /* Return whether an access could occur, blocking if necessary. */ |
paul@117 | 160 | |
paul@162 | 161 | static int _access_blocking(file_t *file, offset_t position, int reading) |
paul@117 | 162 | { |
paul@117 | 163 | long err; |
paul@117 | 164 | |
paul@162 | 165 | /* Attempt to access the position, handling an error condition or a blocking |
paul@162 | 166 | condition. */ |
paul@162 | 167 | |
paul@162 | 168 | while ((err = _access(file, position)) || _operation_blocking(file, reading)) |
paul@117 | 169 | { |
paul@165 | 170 | position = file->data_current; |
paul@165 | 171 | |
paul@117 | 172 | /* Exit if blocking is not configured or suitable. */ |
paul@117 | 173 | |
paul@162 | 174 | if ((err && (err != -L4_EBUSY)) || !file->can_block) |
paul@117 | 175 | return 0; |
paul@117 | 176 | |
paul@117 | 177 | /* Handle an inability to access by blocking, exiting if waiting failed. */ |
paul@117 | 178 | |
paul@180 | 179 | if (client_wait_file(file, client_notifier_task())) |
paul@117 | 180 | return 0; |
paul@117 | 181 | } |
paul@117 | 182 | |
paul@117 | 183 | return 1; |
paul@117 | 184 | } |
paul@117 | 185 | |
paul@117 | 186 | |
paul@117 | 187 | |
paul@117 | 188 | /* Ensure that memory is mapped for accessing the given file, using the |
paul@117 | 189 | indicated count as a region size hint. */ |
paul@117 | 190 | |
paul@117 | 191 | static void *_map_memory(file_t *file, offset_t count) |
paul@117 | 192 | { |
paul@117 | 193 | if (file->memory == NULL) |
paul@117 | 194 | { |
paul@171 | 195 | if (file->object_flags & OBJECT_SUPPORTS_MMAP) |
paul@339 | 196 | return client_mmap(file, client_tell(file), count, 0, 0, |
paul@330 | 197 | file_region_flags(file->flags)); |
paul@117 | 198 | else if (pipe_current(file)) |
paul@117 | 199 | return NULL; |
paul@117 | 200 | } |
paul@117 | 201 | |
paul@117 | 202 | return file->memory; |
paul@117 | 203 | } |
paul@117 | 204 | |
paul@117 | 205 | |
paul@117 | 206 | |
paul@144 | 207 | /* Open a file opening object. */ |
paul@144 | 208 | |
paul@150 | 209 | l4_cap_idx_t client_open_for_user(user_t user) |
paul@144 | 210 | { |
paul@144 | 211 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@144 | 212 | |
paul@150 | 213 | return client_open_for_user_using(user, server); |
paul@144 | 214 | } |
paul@144 | 215 | |
paul@144 | 216 | /* Open a file opening object via a named capability. */ |
paul@144 | 217 | |
paul@150 | 218 | l4_cap_idx_t client_open_for_user_using(user_t user, l4_cap_idx_t server) |
paul@144 | 219 | { |
paul@144 | 220 | if (l4_is_invalid_cap(server)) |
paul@144 | 221 | return L4_INVALID_CAP; |
paul@144 | 222 | |
paul@144 | 223 | l4_cap_idx_t opener; |
paul@150 | 224 | long err = file_open_for_user(user, server, &opener); |
paul@144 | 225 | |
paul@144 | 226 | if (err) |
paul@144 | 227 | return L4_INVALID_CAP; |
paul@144 | 228 | |
paul@144 | 229 | return opener; |
paul@144 | 230 | } |
paul@144 | 231 | |
paul@144 | 232 | |
paul@144 | 233 | |
paul@90 | 234 | /* Close a filesystem object. */ |
paul@90 | 235 | |
paul@90 | 236 | void client_close(file_t *file) |
paul@90 | 237 | { |
paul@90 | 238 | if (file == NULL) |
paul@90 | 239 | return; |
paul@90 | 240 | |
paul@159 | 241 | file_flush(file); |
paul@90 | 242 | file_close(file); |
paul@90 | 243 | free(file); |
paul@90 | 244 | } |
paul@90 | 245 | |
paul@90 | 246 | |
paul@90 | 247 | |
paul@90 | 248 | /* Open a filesystem object. */ |
paul@90 | 249 | |
paul@90 | 250 | file_t *client_open(const char *name, flags_t flags) |
paul@90 | 251 | { |
paul@144 | 252 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@144 | 253 | |
paul@144 | 254 | return client_open_using(name, flags, server); |
paul@103 | 255 | } |
paul@103 | 256 | |
paul@103 | 257 | /* Open a filesystem object via a named capability. */ |
paul@103 | 258 | |
paul@144 | 259 | file_t *client_open_using(const char *name, flags_t flags, l4_cap_idx_t server) |
paul@103 | 260 | { |
paul@144 | 261 | if (l4_is_invalid_cap(server)) |
paul@144 | 262 | return NULL; |
paul@144 | 263 | |
paul@90 | 264 | file_t *file = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 265 | |
paul@90 | 266 | if (file == NULL) |
paul@90 | 267 | return NULL; |
paul@90 | 268 | |
paul@402 | 269 | /* Return any allocated structure even if an error occurs. */ |
paul@90 | 270 | |
paul@402 | 271 | file->error = file_open(file, name, flags, server); |
paul@90 | 272 | return file; |
paul@90 | 273 | } |
paul@90 | 274 | |
paul@90 | 275 | |
paul@90 | 276 | |
paul@204 | 277 | /* Open a directory listing stream via the given named directory. */ |
paul@204 | 278 | |
paul@204 | 279 | file_t *client_opendir(const char *name) |
paul@204 | 280 | { |
paul@204 | 281 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@204 | 282 | |
paul@204 | 283 | return client_opendir_using(name, server); |
paul@204 | 284 | } |
paul@204 | 285 | |
paul@204 | 286 | /* Open a directory listing stream via the given named directory and a named |
paul@204 | 287 | capability. */ |
paul@204 | 288 | |
paul@204 | 289 | file_t *client_opendir_using(const char *name, l4_cap_idx_t server) |
paul@204 | 290 | { |
paul@204 | 291 | file_t *file = client_open_using(name, O_DIRECTORY, server); |
paul@204 | 292 | |
paul@204 | 293 | if (file == NULL) |
paul@204 | 294 | return NULL; |
paul@204 | 295 | |
paul@437 | 296 | /* Return the directory structure itself for error handling. */ |
paul@437 | 297 | |
paul@437 | 298 | if (!client_opened(file)) |
paul@437 | 299 | return file; |
paul@437 | 300 | |
paul@204 | 301 | file_t *reader = client_opendir_at(file); |
paul@204 | 302 | |
paul@402 | 303 | /* Release the directory and return the reader even if an error occurs. */ |
paul@204 | 304 | |
paul@204 | 305 | client_close(file); |
paul@204 | 306 | return reader; |
paul@204 | 307 | } |
paul@204 | 308 | |
paul@204 | 309 | |
paul@204 | 310 | |
paul@202 | 311 | /* Open a directory listing stream via the given directory. */ |
paul@175 | 312 | |
paul@204 | 313 | file_t *client_opendir_at(file_t *file) |
paul@175 | 314 | { |
paul@202 | 315 | file_t *reader = (file_t *) malloc(sizeof(file_t)); |
paul@175 | 316 | |
paul@202 | 317 | if (reader == NULL) |
paul@202 | 318 | return NULL; |
paul@175 | 319 | |
paul@402 | 320 | /* Return any allocated structure even if an error occurs. */ |
paul@202 | 321 | |
paul@402 | 322 | reader->error = directory_opendir(file, reader); |
paul@175 | 323 | |
paul@175 | 324 | /* Set blocking read mode to be able to conveniently read directory entries |
paul@402 | 325 | from the stream. If this fails, the error is set on the structure, but the |
paul@402 | 326 | stream will be open. */ |
paul@175 | 327 | |
paul@402 | 328 | if (!reader->error) |
paul@402 | 329 | reader->error = client_set_blocking(reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@175 | 330 | |
paul@202 | 331 | return reader; |
paul@175 | 332 | } |
paul@175 | 333 | |
paul@175 | 334 | |
paul@175 | 335 | |
paul@402 | 336 | /* Open a pipe object, returning any error condition. */ |
paul@90 | 337 | |
paul@178 | 338 | long client_pipe(file_t **reader, file_t **writer, flags_t flags) |
paul@90 | 339 | { |
paul@144 | 340 | l4_cap_idx_t server = l4re_env_get_cap("pipes"); |
paul@144 | 341 | |
paul@178 | 342 | return client_pipe_using(reader, writer, flags, server); |
paul@135 | 343 | } |
paul@135 | 344 | |
paul@178 | 345 | long client_pipe_using(file_t **reader, file_t **writer, flags_t flags, l4_cap_idx_t server) |
paul@135 | 346 | { |
paul@402 | 347 | *reader = NULL; |
paul@402 | 348 | *writer = NULL; |
paul@402 | 349 | |
paul@144 | 350 | if (l4_is_invalid_cap(server)) |
paul@144 | 351 | return -L4_EINVAL; |
paul@144 | 352 | |
paul@90 | 353 | *reader = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 354 | |
paul@90 | 355 | if (*reader == NULL) |
paul@90 | 356 | return -L4_ENOMEM; |
paul@90 | 357 | |
paul@90 | 358 | *writer = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 359 | |
paul@90 | 360 | if (*writer == NULL) |
paul@90 | 361 | { |
paul@90 | 362 | free(*reader); |
paul@90 | 363 | return -L4_ENOMEM; |
paul@90 | 364 | } |
paul@90 | 365 | |
paul@90 | 366 | long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); |
paul@90 | 367 | |
paul@178 | 368 | /* Set blocking if successful and non-blocking is not indicated. */ |
paul@178 | 369 | |
paul@178 | 370 | if (!err && !(flags & O_NONBLOCK)) |
paul@178 | 371 | { |
paul@178 | 372 | err = client_set_blocking(*reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@178 | 373 | if (!err) |
paul@178 | 374 | err = client_set_blocking(*writer, NOTIFY_SPACE_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@178 | 375 | } |
paul@178 | 376 | |
paul@90 | 377 | if (err) |
paul@90 | 378 | { |
paul@90 | 379 | free(*reader); |
paul@90 | 380 | free(*writer); |
paul@90 | 381 | } |
paul@90 | 382 | |
paul@90 | 383 | return err; |
paul@90 | 384 | } |
paul@90 | 385 | |
paul@90 | 386 | |
paul@90 | 387 | |
paul@402 | 388 | /* Determine whether a file has been successfully opened. */ |
paul@402 | 389 | |
paul@402 | 390 | int client_opened(file_t *file) |
paul@402 | 391 | { |
paul@402 | 392 | return (file != NULL) && !file->error; |
paul@402 | 393 | } |
paul@402 | 394 | |
paul@402 | 395 | |
paul@402 | 396 | |
paul@271 | 397 | /* Make a directory in the filesystem. */ |
paul@271 | 398 | |
paul@271 | 399 | long client_mkdir(const char *path, mode_t mode) |
paul@271 | 400 | { |
paul@271 | 401 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@271 | 402 | |
paul@271 | 403 | return client_mkdir_using(path, mode, server); |
paul@271 | 404 | } |
paul@271 | 405 | |
paul@271 | 406 | /* Make a directory in the filesystem via a named capability. */ |
paul@271 | 407 | |
paul@271 | 408 | long client_mkdir_using(const char *path, mode_t mode, l4_cap_idx_t server) |
paul@271 | 409 | { |
paul@271 | 410 | return file_mkdir(path, mode, server); |
paul@271 | 411 | } |
paul@271 | 412 | |
paul@271 | 413 | |
paul@271 | 414 | |
paul@232 | 415 | /* Remove a file from the filesystem. */ |
paul@232 | 416 | |
paul@266 | 417 | long client_remove(const char *path) |
paul@232 | 418 | { |
paul@232 | 419 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@232 | 420 | |
paul@266 | 421 | return client_remove_using(path, server); |
paul@232 | 422 | } |
paul@232 | 423 | |
paul@232 | 424 | /* Remove a file from the filesystem via a named capability. */ |
paul@232 | 425 | |
paul@266 | 426 | long client_remove_using(const char *path, l4_cap_idx_t server) |
paul@232 | 427 | { |
paul@266 | 428 | return file_remove(path, server); |
paul@232 | 429 | } |
paul@232 | 430 | |
paul@232 | 431 | |
paul@232 | 432 | |
paul@236 | 433 | /* Rename a file in the filesystem. */ |
paul@236 | 434 | |
paul@236 | 435 | long client_rename(const char *source, const char *target) |
paul@236 | 436 | { |
paul@236 | 437 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@236 | 438 | |
paul@236 | 439 | return client_rename_using(source, target, server); |
paul@236 | 440 | } |
paul@236 | 441 | |
paul@236 | 442 | /* Rename a file in the filesystem via a named capability. */ |
paul@236 | 443 | |
paul@236 | 444 | long client_rename_using(const char *source, const char *target, l4_cap_idx_t server) |
paul@236 | 445 | { |
paul@236 | 446 | return file_rename(source, target, server); |
paul@236 | 447 | } |
paul@236 | 448 | |
paul@236 | 449 | |
paul@236 | 450 | |
paul@266 | 451 | /* Obtain filesystem object statistics. */ |
paul@266 | 452 | |
paul@266 | 453 | long client_stat(const char *path, struct stat *st) |
paul@266 | 454 | { |
paul@266 | 455 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@266 | 456 | |
paul@266 | 457 | return client_stat_using(path, st, server); |
paul@266 | 458 | } |
paul@266 | 459 | |
paul@266 | 460 | /* Obtain object statistics from the filesystem via a named capability. */ |
paul@266 | 461 | |
paul@266 | 462 | long client_stat_using(const char *path, struct stat *st, l4_cap_idx_t server) |
paul@266 | 463 | { |
paul@266 | 464 | return file_stat(path, st, server); |
paul@266 | 465 | } |
paul@266 | 466 | |
paul@266 | 467 | |
paul@266 | 468 | |
paul@174 | 469 | /* Obtain the current region of a pipe. */ |
paul@174 | 470 | |
paul@174 | 471 | long client_current_region(file_t *file) |
paul@174 | 472 | { |
paul@402 | 473 | if (!client_opened(file)) |
paul@174 | 474 | return -L4_EINVAL; |
paul@174 | 475 | |
paul@174 | 476 | return pipe_current(file); |
paul@174 | 477 | } |
paul@174 | 478 | |
paul@174 | 479 | |
paul@174 | 480 | |
paul@90 | 481 | /* Flush data explicitly to the filesystem object. */ |
paul@90 | 482 | |
paul@90 | 483 | long client_flush(file_t *file) |
paul@90 | 484 | { |
paul@402 | 485 | if (!client_opened(file)) |
paul@90 | 486 | return -L4_EINVAL; |
paul@90 | 487 | |
paul@90 | 488 | /* Flush and retain most buffer settings. */ |
paul@90 | 489 | |
paul@90 | 490 | return file_flush(file); |
paul@90 | 491 | } |
paul@90 | 492 | |
paul@90 | 493 | |
paul@90 | 494 | |
paul@90 | 495 | /* Map a memory region to a file. */ |
paul@90 | 496 | |
paul@339 | 497 | void *client_mmap(file_t *file, offset_t position, offset_t length, |
paul@339 | 498 | offset_t start_visible, offset_t end_visible, |
paul@330 | 499 | l4re_rm_flags_t region_flags) |
paul@90 | 500 | { |
paul@402 | 501 | if (!client_opened(file) || file_mmap(file, position, length, start_visible, |
paul@402 | 502 | end_visible, region_flags)) |
paul@90 | 503 | return NULL; |
paul@90 | 504 | |
paul@90 | 505 | return file->memory; |
paul@90 | 506 | } |
paul@90 | 507 | |
paul@90 | 508 | |
paul@90 | 509 | |
paul@90 | 510 | /* Obtain the next region of a pipe. */ |
paul@90 | 511 | |
paul@116 | 512 | long client_next_region(file_t *file) |
paul@90 | 513 | { |
paul@402 | 514 | if (!client_opened(file)) |
paul@116 | 515 | return -L4_EINVAL; |
paul@90 | 516 | |
paul@116 | 517 | return pipe_next(file); |
paul@90 | 518 | } |
paul@90 | 519 | |
paul@90 | 520 | |
paul@90 | 521 | |
paul@180 | 522 | /* Close a notifier object. */ |
paul@180 | 523 | |
paul@180 | 524 | void client_notifier_close(file_notifier_t *notifier) |
paul@180 | 525 | { |
paul@180 | 526 | file_notify_close(notifier); |
paul@180 | 527 | } |
paul@180 | 528 | |
paul@180 | 529 | /* Obtain a local notifier object. */ |
paul@180 | 530 | |
paul@180 | 531 | file_notifier_t *client_notifier_local() |
paul@180 | 532 | { |
paul@180 | 533 | return file_notify_local(); |
paul@180 | 534 | } |
paul@180 | 535 | |
paul@180 | 536 | /* Obtain a task-wide notifier object. */ |
paul@180 | 537 | |
paul@180 | 538 | file_notifier_t *client_notifier_task() |
paul@180 | 539 | { |
paul@180 | 540 | return file_notify_task(); |
paul@180 | 541 | } |
paul@180 | 542 | |
paul@180 | 543 | |
paul@180 | 544 | |
paul@174 | 545 | /* Read a directory entry. This must be freed by the caller after use. */ |
paul@174 | 546 | |
paul@174 | 547 | struct dirent *client_readdir(file_t *file) |
paul@174 | 548 | { |
paul@174 | 549 | char buffer[DIRENT_CORE_SIZE]; |
paul@174 | 550 | offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE); |
paul@174 | 551 | |
paul@174 | 552 | /* Stop if no new structure can be successfully read. */ |
paul@174 | 553 | |
paul@174 | 554 | if (nread != DIRENT_CORE_SIZE) |
paul@174 | 555 | return NULL; |
paul@174 | 556 | |
paul@174 | 557 | struct dirent *dirent = (struct dirent *) buffer; |
paul@174 | 558 | offset_t remaining = dirent->d_reclen - DIRENT_CORE_SIZE; |
paul@174 | 559 | |
paul@174 | 560 | /* Allocate a buffer for the complete structure. */ |
paul@174 | 561 | |
paul@174 | 562 | char *entry = (char *) calloc(DIRENT_CORE_SIZE + remaining, sizeof(char)); |
paul@174 | 563 | |
paul@174 | 564 | if (entry == NULL) |
paul@174 | 565 | return NULL; |
paul@174 | 566 | |
paul@174 | 567 | /* Copy the start of the entry into a new buffer. */ |
paul@174 | 568 | |
paul@174 | 569 | memcpy(entry, buffer, DIRENT_CORE_SIZE); |
paul@174 | 570 | |
paul@174 | 571 | /* Append to the entry buffer. */ |
paul@174 | 572 | |
paul@174 | 573 | char *current = entry + DIRENT_CORE_SIZE; |
paul@174 | 574 | |
paul@174 | 575 | nread = client_read(file, current, remaining); |
paul@174 | 576 | |
paul@174 | 577 | /* Stop if no complete structure can be successfully read. */ |
paul@174 | 578 | |
paul@174 | 579 | if (nread != remaining) |
paul@174 | 580 | { |
paul@174 | 581 | free(entry); |
paul@174 | 582 | return NULL; |
paul@174 | 583 | } |
paul@174 | 584 | |
paul@174 | 585 | return (struct dirent *) entry; |
paul@174 | 586 | } |
paul@174 | 587 | |
paul@174 | 588 | |
paul@174 | 589 | |
paul@90 | 590 | /* Read from the filesystem object into the buffer provided. */ |
paul@90 | 591 | |
paul@90 | 592 | offset_t client_read(file_t *file, void *buf, offset_t count) |
paul@90 | 593 | { |
paul@402 | 594 | if (!client_opened(file)) |
paul@90 | 595 | return 0; |
paul@90 | 596 | |
paul@100 | 597 | /* Map memory if none has been mapped so far. */ |
paul@100 | 598 | |
paul@108 | 599 | if (_map_memory(file, count) == NULL) |
paul@100 | 600 | return 0; |
paul@100 | 601 | |
paul@90 | 602 | /* Amount available in the descriptor buffer already. */ |
paul@90 | 603 | |
paul@90 | 604 | offset_t available = file_data_available(file); |
paul@90 | 605 | offset_t to_transfer, total = 0; |
paul@90 | 606 | |
paul@90 | 607 | while (count > 0) |
paul@90 | 608 | { |
paul@90 | 609 | /* If there is no data, try and obtain more data. */ |
paul@90 | 610 | |
paul@90 | 611 | if (!available) |
paul@90 | 612 | { |
paul@90 | 613 | /* Flush any unwritten data, preparing to read from the file position at |
paul@90 | 614 | the end of the data, and returning if no new data is available. */ |
paul@90 | 615 | |
paul@162 | 616 | if (!_access_blocking(file, file_data_end_position(file), 1)) |
paul@90 | 617 | break; |
paul@90 | 618 | |
paul@90 | 619 | available = file_data_available(file); |
paul@90 | 620 | |
paul@90 | 621 | if (!available) |
paul@90 | 622 | break; |
paul@90 | 623 | } |
paul@90 | 624 | |
paul@90 | 625 | /* Transfer data into the supplied buffer. */ |
paul@90 | 626 | |
paul@90 | 627 | to_transfer = available <= count ? available : count; |
paul@90 | 628 | |
paul@90 | 629 | file_data_read(file, (char *) buf, to_transfer); |
paul@90 | 630 | |
paul@90 | 631 | /* Update counters. */ |
paul@90 | 632 | |
paul@90 | 633 | available -= to_transfer; |
paul@90 | 634 | |
paul@90 | 635 | count -= to_transfer; |
paul@90 | 636 | total += to_transfer; |
paul@90 | 637 | |
paul@90 | 638 | buf = ((char *) buf + to_transfer); |
paul@90 | 639 | } |
paul@90 | 640 | |
paul@90 | 641 | return total; |
paul@90 | 642 | } |
paul@90 | 643 | |
paul@90 | 644 | |
paul@90 | 645 | |
paul@90 | 646 | /* Ensure that the buffer can provide the needed data. */ |
paul@90 | 647 | |
paul@90 | 648 | offset_t client_seek(file_t *file, offset_t offset, int whence) |
paul@90 | 649 | { |
paul@402 | 650 | if (!client_opened(file)) |
paul@90 | 651 | return 0; |
paul@90 | 652 | |
paul@90 | 653 | offset_t position, current = file_data_current_position(file), change; |
paul@90 | 654 | |
paul@90 | 655 | switch (whence) |
paul@90 | 656 | { |
paul@90 | 657 | case SEEK_SET: |
paul@90 | 658 | position = offset; |
paul@90 | 659 | break; |
paul@90 | 660 | |
paul@90 | 661 | case SEEK_CUR: |
paul@90 | 662 | position = current + offset; |
paul@90 | 663 | break; |
paul@90 | 664 | |
paul@90 | 665 | case SEEK_END: |
paul@90 | 666 | position = file->size + offset; |
paul@90 | 667 | break; |
paul@90 | 668 | |
paul@90 | 669 | default: |
paul@90 | 670 | /* NOTE: Set errno to EINVAL. */ |
paul@117 | 671 | return current; |
paul@90 | 672 | } |
paul@90 | 673 | |
paul@90 | 674 | /* Retain the current position if unchanged. */ |
paul@90 | 675 | |
paul@90 | 676 | if (position == current) |
paul@90 | 677 | return position; |
paul@90 | 678 | |
paul@90 | 679 | /* Move forward in the file. */ |
paul@90 | 680 | |
paul@90 | 681 | if (position > current) |
paul@90 | 682 | { |
paul@90 | 683 | change = position - current; |
paul@90 | 684 | |
paul@90 | 685 | /* Move towards the end of available data. |
paul@90 | 686 | Request new data if not enough is available. */ |
paul@90 | 687 | |
paul@90 | 688 | if (change <= file_data_available(file)) |
paul@90 | 689 | { |
paul@90 | 690 | file->data_current += change; |
paul@90 | 691 | return position; |
paul@90 | 692 | } |
paul@90 | 693 | } |
paul@90 | 694 | |
paul@90 | 695 | /* Move backward in the file. */ |
paul@90 | 696 | |
paul@90 | 697 | else |
paul@90 | 698 | { |
paul@90 | 699 | change = current - position; |
paul@90 | 700 | |
paul@90 | 701 | /* Move towards the start of available data. |
paul@90 | 702 | Request new data if moving beyond the start of the data. */ |
paul@90 | 703 | |
paul@90 | 704 | if (change <= file->data_current) |
paul@90 | 705 | { |
paul@90 | 706 | file->data_current -= change; |
paul@90 | 707 | return position; |
paul@90 | 708 | } |
paul@90 | 709 | } |
paul@90 | 710 | |
paul@90 | 711 | /* Handle unwritten data and reset the buffer for reading. */ |
paul@90 | 712 | |
paul@117 | 713 | if (_access(file, position)) |
paul@117 | 714 | return current; |
paul@117 | 715 | |
paul@90 | 716 | return position; |
paul@90 | 717 | } |
paul@90 | 718 | |
paul@90 | 719 | |
paul@90 | 720 | |
paul@117 | 721 | /* Set or unset blocking access for a file. */ |
paul@117 | 722 | |
paul@122 | 723 | long client_set_blocking(file_t *file, notify_flags_t flags) |
paul@117 | 724 | { |
paul@117 | 725 | long err; |
paul@117 | 726 | |
paul@122 | 727 | if (file->can_block == flags) |
paul@117 | 728 | return L4_EOK; |
paul@117 | 729 | |
paul@177 | 730 | /* Since blocking access is used with specific file notifications, the |
paul@177 | 731 | per-task notifier is used. */ |
paul@117 | 732 | |
paul@180 | 733 | file_notifier_t *notifier = client_notifier_task(); |
paul@180 | 734 | |
paul@122 | 735 | if (flags) |
paul@180 | 736 | err = client_subscribe(file, flags, notifier); |
paul@117 | 737 | else |
paul@180 | 738 | err = client_unsubscribe(file, notifier); |
paul@117 | 739 | |
paul@117 | 740 | if (err) |
paul@117 | 741 | return err; |
paul@117 | 742 | |
paul@122 | 743 | file->can_block = flags; |
paul@117 | 744 | return L4_EOK; |
paul@117 | 745 | } |
paul@117 | 746 | |
paul@117 | 747 | |
paul@117 | 748 | |
paul@117 | 749 | /* Subscribe from events concerning a file. */ |
paul@117 | 750 | |
paul@180 | 751 | long client_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) |
paul@117 | 752 | { |
paul@402 | 753 | if (!client_opened(file)) |
paul@117 | 754 | return -L4_EINVAL; |
paul@117 | 755 | |
paul@180 | 756 | return file_notify_subscribe(file, flags, notifier); |
paul@117 | 757 | } |
paul@117 | 758 | |
paul@117 | 759 | |
paul@117 | 760 | |
paul@117 | 761 | /* Return the current position in the file. */ |
paul@117 | 762 | |
paul@411 | 763 | offset_t client_tell(file_t *file) |
paul@90 | 764 | { |
paul@402 | 765 | if (!client_opened(file)) |
paul@90 | 766 | return -L4_EINVAL; |
paul@90 | 767 | |
paul@90 | 768 | return file_data_current_position(file); |
paul@90 | 769 | } |
paul@90 | 770 | |
paul@90 | 771 | |
paul@90 | 772 | |
paul@117 | 773 | /* Unsubscribe from events concerning a file. */ |
paul@117 | 774 | |
paul@180 | 775 | long client_unsubscribe(file_t *file, file_notifier_t *notifier) |
paul@117 | 776 | { |
paul@402 | 777 | if (!client_opened(file)) |
paul@117 | 778 | return -L4_EINVAL; |
paul@117 | 779 | |
paul@180 | 780 | return file_notify_unsubscribe(file, notifier); |
paul@117 | 781 | } |
paul@117 | 782 | |
paul@117 | 783 | |
paul@117 | 784 | |
paul@123 | 785 | /* Wait for events involving a specific file. */ |
paul@123 | 786 | |
paul@180 | 787 | long client_wait_file(file_t *file, file_notifier_t *notifier) |
paul@123 | 788 | { |
paul@402 | 789 | if (!client_opened(file)) |
paul@123 | 790 | return -L4_EINVAL; |
paul@123 | 791 | |
paul@180 | 792 | return file_notify_wait_file(file, notifier); |
paul@123 | 793 | } |
paul@123 | 794 | |
paul@123 | 795 | /* Wait for events concerning files, referencing a file object if an event is |
paul@123 | 796 | delivered. */ |
paul@123 | 797 | |
paul@180 | 798 | long client_wait_files(file_t **file, file_notifier_t *notifier) |
paul@123 | 799 | { |
paul@180 | 800 | return file_notify_wait_files(file, notifier); |
paul@117 | 801 | } |
paul@117 | 802 | |
paul@117 | 803 | |
paul@117 | 804 | |
paul@90 | 805 | /* Write to the filesystem object from the buffer provided. */ |
paul@90 | 806 | |
paul@90 | 807 | offset_t client_write(file_t *file, const void *buf, offset_t count) |
paul@90 | 808 | { |
paul@402 | 809 | if (!client_opened(file)) |
paul@90 | 810 | return 0; |
paul@90 | 811 | |
paul@100 | 812 | /* Map memory if none has been mapped so far. */ |
paul@100 | 813 | |
paul@108 | 814 | if (_map_memory(file, count) == NULL) |
paul@100 | 815 | return 0; |
paul@100 | 816 | |
paul@90 | 817 | /* Attempt to ensure that the file can accept the amount of data to be |
paul@90 | 818 | written. This may not resize to the needed amount if a file has a fixed |
paul@90 | 819 | size, but data will still be written to any available space. */ |
paul@90 | 820 | |
paul@90 | 821 | offset_t needed_size = file_data_current_position(file) + count; |
paul@90 | 822 | |
paul@171 | 823 | if (file->object_flags & OBJECT_HAS_SIZE) |
paul@90 | 824 | { |
paul@108 | 825 | if (file->size < needed_size) |
paul@108 | 826 | { |
paul@108 | 827 | file_resize(file, needed_size); |
paul@90 | 828 | |
paul@108 | 829 | if (file->size < needed_size) |
paul@108 | 830 | count = file->size - file_data_current_position(file); |
paul@108 | 831 | } |
paul@90 | 832 | } |
paul@90 | 833 | |
paul@90 | 834 | /* Space remaining in the descriptor buffer. */ |
paul@90 | 835 | |
paul@90 | 836 | offset_t space = file_data_space(file); |
paul@90 | 837 | offset_t to_transfer, total = 0; |
paul@90 | 838 | |
paul@90 | 839 | while (count > 0) |
paul@90 | 840 | { |
paul@90 | 841 | /* If no space is available, try and send data, reset the buffer. */ |
paul@90 | 842 | |
paul@90 | 843 | if (!space) |
paul@90 | 844 | { |
paul@90 | 845 | /* Flush any unwritten data and continue writing from the current data |
paul@90 | 846 | position. */ |
paul@90 | 847 | |
paul@162 | 848 | if (!_access_blocking(file, file_data_current_position(file), 0)) |
paul@90 | 849 | break; |
paul@90 | 850 | |
paul@90 | 851 | space = file_data_space(file); |
paul@90 | 852 | } |
paul@90 | 853 | |
paul@90 | 854 | /* Transfer data into the supplied buffer. */ |
paul@90 | 855 | |
paul@90 | 856 | to_transfer = space <= count ? space : count; |
paul@90 | 857 | |
paul@90 | 858 | file_data_write(file, (char *) buf, to_transfer); |
paul@90 | 859 | |
paul@90 | 860 | /* Update counters. */ |
paul@90 | 861 | |
paul@90 | 862 | space -= to_transfer; |
paul@90 | 863 | |
paul@90 | 864 | count -= to_transfer; |
paul@90 | 865 | total += to_transfer; |
paul@90 | 866 | |
paul@90 | 867 | buf = ((char *) buf + to_transfer); |
paul@90 | 868 | } |
paul@90 | 869 | |
paul@90 | 870 | return total; |
paul@90 | 871 | } |
paul@90 | 872 | |
paul@90 | 873 | // vim: tabstop=2 expandtab shiftwidth=2 |