paul@31 | 1 | /* |
paul@31 | 2 | * File access convenience functions. |
paul@31 | 3 | * |
paul@31 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@31 | 5 | * |
paul@31 | 6 | * This program is free software; you can redistribute it and/or |
paul@31 | 7 | * modify it under the terms of the GNU General Public License as |
paul@31 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@31 | 9 | * the License, or (at your option) any later version. |
paul@31 | 10 | * |
paul@31 | 11 | * This program is distributed in the hope that it will be useful, |
paul@31 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@31 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@31 | 14 | * GNU General Public License for more details. |
paul@31 | 15 | * |
paul@31 | 16 | * You should have received a copy of the GNU General Public License |
paul@31 | 17 | * along with this program; if not, write to the Free Software |
paul@31 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@31 | 19 | * Boston, MA 02110-1301, USA |
paul@31 | 20 | */ |
paul@31 | 21 | |
paul@31 | 22 | #include <ipc/cap_alloc.h> |
paul@31 | 23 | #include <ipc/mem_ipc.h> |
paul@31 | 24 | |
paul@46 | 25 | #include <string.h> |
paul@46 | 26 | |
paul@31 | 27 | #include "dataspace_client.h" |
paul@55 | 28 | #include "file_client.h" |
paul@144 | 29 | #include "filesystem_client.h" |
paul@117 | 30 | #include "flush_client.h" |
paul@31 | 31 | #include "opener_client.h" |
paul@31 | 32 | #include "opener_context_client.h" |
paul@65 | 33 | #include "pipe_client.h" |
paul@65 | 34 | #include "pipe_opener_client.h" |
paul@31 | 35 | #include "mapped_file_client.h" |
paul@31 | 36 | |
paul@31 | 37 | #include "file.h" |
paul@124 | 38 | #include "notifier.h" |
paul@31 | 39 | |
paul@31 | 40 | |
paul@31 | 41 | |
paul@90 | 42 | /* Update the extent of the file in a region using the region start and end |
paul@90 | 43 | positions and the file size. */ |
paul@90 | 44 | |
paul@90 | 45 | static void _update_extent(file_t *file) |
paul@90 | 46 | { |
paul@90 | 47 | /* Handle files ending after or within the region. */ |
paul@90 | 48 | |
paul@90 | 49 | if (file->size > file->start_pos) |
paul@90 | 50 | { |
paul@90 | 51 | if (file->size > file->end_pos) |
paul@90 | 52 | file->data_end = file->end_pos - file->start_pos; |
paul@90 | 53 | else |
paul@90 | 54 | file->data_end = file->size - file->start_pos; |
paul@90 | 55 | } |
paul@90 | 56 | |
paul@90 | 57 | /* Handle files ending before the region. */ |
paul@90 | 58 | |
paul@90 | 59 | else |
paul@90 | 60 | file->data_end = 0; |
paul@90 | 61 | } |
paul@90 | 62 | |
paul@90 | 63 | |
paul@90 | 64 | |
paul@144 | 65 | /* Obtain a reference to a file opener for the given user. */ |
paul@144 | 66 | |
paul@150 | 67 | long file_open_for_user(user_t user, l4_cap_idx_t server, l4_cap_idx_t *opener) |
paul@144 | 68 | { |
paul@144 | 69 | client_Filesystem filesystem(server); |
paul@144 | 70 | |
paul@150 | 71 | return filesystem.open_for_user(user, opener); |
paul@144 | 72 | } |
paul@144 | 73 | |
paul@144 | 74 | |
paul@144 | 75 | |
paul@90 | 76 | /* Initialise the given file structure. */ |
paul@90 | 77 | |
paul@90 | 78 | void file_init(file_t *file) |
paul@90 | 79 | { |
paul@90 | 80 | file->memory = NULL; |
paul@90 | 81 | file->ref = L4_INVALID_CAP; |
paul@90 | 82 | file->start_pos = 0; |
paul@90 | 83 | file->end_pos = 0; |
paul@90 | 84 | file->data_end = 0; |
paul@90 | 85 | file->data_current = 0; |
paul@171 | 86 | file->object_flags = 0; |
paul@117 | 87 | file->can_block = 0; |
paul@124 | 88 | file->notifications = 0; |
paul@90 | 89 | } |
paul@90 | 90 | |
paul@90 | 91 | |
paul@90 | 92 | |
paul@33 | 93 | /* Release resources for the given file. */ |
paul@31 | 94 | |
paul@31 | 95 | void file_close(file_t *file) |
paul@31 | 96 | { |
paul@31 | 97 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 98 | ipc_cap_free_um(file->ref); |
paul@31 | 99 | |
paul@177 | 100 | /* Only unsubscribe after actually closing the file and sending any |
paul@178 | 101 | notifications. This depends on a valid reference, so it is also tested |
paul@178 | 102 | here. */ |
paul@177 | 103 | |
paul@178 | 104 | if (l4_is_valid_cap(file->ref)) |
paul@180 | 105 | notifier_get_task_notifier()->unsubscribe(file); |
paul@117 | 106 | |
paul@31 | 107 | if (file->memory != NULL) |
paul@31 | 108 | ipc_detach_dataspace(file->memory); |
paul@31 | 109 | |
paul@31 | 110 | file_init(file); |
paul@31 | 111 | } |
paul@31 | 112 | |
paul@90 | 113 | /* Open a file using the given structure, indicating the filename and |
paul@90 | 114 | filesystem server. The file_mmap function should be used to obtain access to |
paul@90 | 115 | memory providing file data. This is a convenience function invoking |
paul@90 | 116 | file_context and file_context_open. */ |
paul@90 | 117 | |
paul@90 | 118 | long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) |
paul@90 | 119 | { |
paul@90 | 120 | file_t context; |
paul@90 | 121 | long err; |
paul@90 | 122 | |
paul@90 | 123 | err = file_context(&context, server); |
paul@90 | 124 | if (err) |
paul@90 | 125 | return err; |
paul@90 | 126 | |
paul@90 | 127 | if (!file_string_set(&context, filename, 0, NULL)) |
paul@90 | 128 | return -L4_ENOMEM; |
paul@90 | 129 | |
paul@90 | 130 | err = file_context_open(file, flags, &context); |
paul@90 | 131 | |
paul@90 | 132 | /* Close the context, although a separate mechanism could permit contexts to |
paul@90 | 133 | open several files. */ |
paul@90 | 134 | |
paul@90 | 135 | file_close(&context); |
paul@90 | 136 | return err; |
paul@90 | 137 | } |
paul@90 | 138 | |
paul@90 | 139 | |
paul@90 | 140 | |
paul@90 | 141 | /* Initialise a file structure for a context obtained from the given server |
paul@90 | 142 | attaching memory to communicate filename information. */ |
paul@31 | 143 | |
paul@33 | 144 | long file_context(file_t *file, l4_cap_idx_t server) |
paul@31 | 145 | { |
paul@85 | 146 | if (l4_is_invalid_cap(server)) |
paul@85 | 147 | return -L4_EINVAL; |
paul@85 | 148 | |
paul@31 | 149 | client_Opener opener(server); |
paul@92 | 150 | offset_t size; |
paul@92 | 151 | flags_t flags; |
paul@31 | 152 | long err; |
paul@31 | 153 | |
paul@31 | 154 | file_init(file); |
paul@47 | 155 | |
paul@31 | 156 | err = opener.context(&file->ref); |
paul@31 | 157 | if (err) |
paul@31 | 158 | return err; |
paul@31 | 159 | |
paul@31 | 160 | client_Dataspace context_ds(file->ref); |
paul@31 | 161 | |
paul@31 | 162 | err = context_ds.info(&size, &flags); |
paul@31 | 163 | if (err) |
paul@31 | 164 | return err; |
paul@31 | 165 | |
paul@47 | 166 | file->start_pos = 0; |
paul@47 | 167 | file->end_pos = size; |
paul@47 | 168 | |
paul@31 | 169 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 170 | } |
paul@31 | 171 | |
paul@47 | 172 | /* Open a file using the given structure and context. */ |
paul@47 | 173 | |
paul@85 | 174 | long file_context_open(file_t *file, flags_t flags, file_t *context) |
paul@47 | 175 | { |
paul@47 | 176 | client_OpenerContext openercontext(context->ref); |
paul@47 | 177 | file_init(file); |
paul@171 | 178 | return openercontext.open(flags, &file->size, &file->ref, &file->object_flags); |
paul@47 | 179 | } |
paul@47 | 180 | |
paul@46 | 181 | |
paul@46 | 182 | |
paul@90 | 183 | /* Flush populated data and obtain an updated file size and populated data |
paul@90 | 184 | details. */ |
paul@33 | 185 | |
paul@90 | 186 | long file_flush(file_t *file) |
paul@33 | 187 | { |
paul@178 | 188 | if (l4_is_invalid_cap(file->ref)) |
paul@178 | 189 | return -L4_EINVAL; |
paul@178 | 190 | |
paul@117 | 191 | client_Flush _file(file->ref); |
paul@90 | 192 | long err = _file.flush(file->data_current, &file->size); |
paul@33 | 193 | |
paul@33 | 194 | if (err) |
paul@33 | 195 | return err; |
paul@33 | 196 | |
paul@90 | 197 | _update_extent(file); |
paul@90 | 198 | |
paul@90 | 199 | return L4_EOK; |
paul@33 | 200 | } |
paul@33 | 201 | |
paul@90 | 202 | /* Map a region of the given file to a memory region, obtaining an updated file |
paul@90 | 203 | size and populated data details. Unmap any previously mapped region. */ |
paul@90 | 204 | |
paul@90 | 205 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@90 | 206 | { |
paul@90 | 207 | char *memory = file->memory; |
paul@90 | 208 | client_MappedFile mapped_file(file->ref); |
paul@90 | 209 | long err = mapped_file.mmap(position, length, &file->start_pos, |
paul@90 | 210 | &file->end_pos, &file->size); |
paul@90 | 211 | |
paul@90 | 212 | if (err) |
paul@90 | 213 | return err; |
paul@90 | 214 | |
paul@90 | 215 | _update_extent(file); |
paul@90 | 216 | |
paul@90 | 217 | err = ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); |
paul@90 | 218 | if (err) |
paul@90 | 219 | return err; |
paul@90 | 220 | |
paul@90 | 221 | if (memory != NULL) |
paul@90 | 222 | ipc_detach_dataspace(memory); |
paul@90 | 223 | |
paul@90 | 224 | return L4_EOK; |
paul@90 | 225 | } |
paul@90 | 226 | |
paul@90 | 227 | /* Resize a file, obtaining updated file size and populated data details. */ |
paul@55 | 228 | |
paul@55 | 229 | long file_resize(file_t *file, offset_t size) |
paul@55 | 230 | { |
paul@176 | 231 | if (!(file->object_flags & OBJECT_HAS_SIZE)) |
paul@108 | 232 | return -L4_EIO; |
paul@108 | 233 | |
paul@55 | 234 | client_File _file(file->ref); |
paul@55 | 235 | offset_t file_size = size; |
paul@55 | 236 | long err = _file.resize(&file_size); |
paul@55 | 237 | |
paul@90 | 238 | if (err) |
paul@90 | 239 | return err; |
paul@55 | 240 | |
paul@90 | 241 | file->size = file_size; |
paul@90 | 242 | _update_extent(file); |
paul@90 | 243 | return L4_EOK; |
paul@55 | 244 | } |
paul@55 | 245 | |
paul@55 | 246 | |
paul@55 | 247 | |
paul@33 | 248 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 249 | |
paul@45 | 250 | offset_t file_populated_span(file_t *file) |
paul@33 | 251 | { |
paul@45 | 252 | offset_t size = file_span(file); |
paul@33 | 253 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 254 | } |
paul@33 | 255 | |
paul@45 | 256 | /* Return the size of the mapped region for the given file. */ |
paul@45 | 257 | |
paul@45 | 258 | offset_t file_span(file_t *file) |
paul@45 | 259 | { |
paul@45 | 260 | return file->end_pos - file->start_pos; |
paul@45 | 261 | } |
paul@45 | 262 | |
paul@46 | 263 | |
paul@46 | 264 | |
paul@46 | 265 | /* Get a pointer to any terminated string at the given offset or NULL if the |
paul@46 | 266 | data from offset is not terminated. */ |
paul@46 | 267 | |
paul@46 | 268 | char *file_string_get(file_t *file, offset_t offset) |
paul@46 | 269 | { |
paul@46 | 270 | offset_t limit = file_span(file) - offset; |
paul@46 | 271 | |
paul@46 | 272 | if (strnlen(file->memory + offset, limit) < limit) |
paul@46 | 273 | return file->memory + offset; |
paul@46 | 274 | else |
paul@46 | 275 | return NULL; |
paul@46 | 276 | } |
paul@46 | 277 | |
paul@47 | 278 | /* Copy a string to the mapped region at the given offset, returning 1 (true) |
paul@47 | 279 | where all characters were copied, 0 (false) otherwise. The precise number of |
paul@47 | 280 | characters copied, excluding the zero terminator is provided via the written |
paul@47 | 281 | parameter if it is not specified as NULL. */ |
paul@46 | 282 | |
paul@47 | 283 | int file_string_set(file_t *file, const char *data, offset_t offset, |
paul@47 | 284 | offset_t *written) |
paul@46 | 285 | { |
paul@47 | 286 | offset_t i, pos, limit = file_span(file); |
paul@46 | 287 | |
paul@46 | 288 | /* Do not attempt to copy data with an invalid offset. */ |
paul@46 | 289 | |
paul@47 | 290 | if (offset >= limit) |
paul@47 | 291 | { |
paul@47 | 292 | if (written != NULL) |
paul@47 | 293 | *written = 0; |
paul@46 | 294 | return 0; |
paul@47 | 295 | } |
paul@46 | 296 | |
paul@47 | 297 | /* Copy the data to the given offset, stopping at the end of the region. */ |
paul@46 | 298 | |
paul@46 | 299 | for (i = 0, pos = offset; pos < limit; i++, pos++) |
paul@46 | 300 | { |
paul@46 | 301 | file->memory[pos] = data[i]; |
paul@46 | 302 | |
paul@46 | 303 | /* Terminator written, can return immediately. */ |
paul@46 | 304 | |
paul@46 | 305 | if (!data[i]) |
paul@47 | 306 | { |
paul@47 | 307 | if (written != NULL) |
paul@47 | 308 | *written = pos - offset; |
paul@47 | 309 | return 1; |
paul@47 | 310 | } |
paul@46 | 311 | } |
paul@46 | 312 | |
paul@46 | 313 | /* Terminate the incomplete string at the end of the region. */ |
paul@46 | 314 | |
paul@47 | 315 | file->memory[limit - 1] = '\0'; |
paul@47 | 316 | if (written != NULL) |
paul@47 | 317 | *written = limit - 1 - offset; |
paul@47 | 318 | return 0; |
paul@46 | 319 | } |
paul@46 | 320 | |
paul@65 | 321 | |
paul@65 | 322 | |
paul@90 | 323 | /* Return the number of remaining populated bytes in the region. */ |
paul@90 | 324 | |
paul@90 | 325 | offset_t file_data_available(file_t *file) |
paul@90 | 326 | { |
paul@90 | 327 | return file_populated_span(file) - file->data_current; |
paul@90 | 328 | } |
paul@90 | 329 | |
paul@90 | 330 | /* Return the current data offset in the region. */ |
paul@90 | 331 | |
paul@90 | 332 | char *file_data_current(file_t *file) |
paul@90 | 333 | { |
paul@90 | 334 | return file->memory + file->data_current; |
paul@90 | 335 | } |
paul@90 | 336 | |
paul@90 | 337 | /* Return the current access position in the file. */ |
paul@90 | 338 | |
paul@90 | 339 | offset_t file_data_current_position(file_t *file) |
paul@90 | 340 | { |
paul@90 | 341 | return file->start_pos + file->data_current; |
paul@90 | 342 | } |
paul@90 | 343 | |
paul@90 | 344 | /* Return the position of the end of the populated bytes in the region. */ |
paul@90 | 345 | |
paul@90 | 346 | offset_t file_data_end_position(file_t *file) |
paul@90 | 347 | { |
paul@90 | 348 | return file->start_pos + file->data_end; |
paul@90 | 349 | } |
paul@90 | 350 | |
paul@90 | 351 | /* Return the amount of remaining space in the region. */ |
paul@90 | 352 | |
paul@90 | 353 | offset_t file_data_space(file_t *file) |
paul@90 | 354 | { |
paul@90 | 355 | return file_span(file) - file->data_current; |
paul@90 | 356 | } |
paul@90 | 357 | |
paul@90 | 358 | |
paul@90 | 359 | |
paul@90 | 360 | /* Copy data to the given buffer from the current data position, updating the |
paul@90 | 361 | position. */ |
paul@90 | 362 | |
paul@98 | 363 | void file_data_read(file_t *file, char *buf, offset_t to_transfer) |
paul@90 | 364 | { |
paul@90 | 365 | memcpy(buf, file_data_current(file), to_transfer); |
paul@90 | 366 | |
paul@90 | 367 | /* Update position details. */ |
paul@90 | 368 | |
paul@90 | 369 | file->data_current += to_transfer; |
paul@90 | 370 | } |
paul@90 | 371 | |
paul@90 | 372 | /* Copy data from the given buffer to the current data position, updating the |
paul@90 | 373 | position and the extent of populated data if this was exceeded. */ |
paul@90 | 374 | |
paul@98 | 375 | void file_data_write(file_t *file, char *buf, offset_t to_transfer) |
paul@90 | 376 | { |
paul@90 | 377 | memcpy(file_data_current(file), buf, to_transfer); |
paul@90 | 378 | |
paul@90 | 379 | /* Update position details. */ |
paul@90 | 380 | |
paul@90 | 381 | file->data_current += to_transfer; |
paul@90 | 382 | |
paul@90 | 383 | if (file->data_current > file->data_end) |
paul@90 | 384 | file->data_end = file->data_current; |
paul@90 | 385 | } |
paul@90 | 386 | |
paul@90 | 387 | |
paul@90 | 388 | |
paul@180 | 389 | /* Opaque notifier type for file_notifier_t. */ |
paul@180 | 390 | |
paul@180 | 391 | struct file_notifier |
paul@180 | 392 | { |
paul@180 | 393 | FileNotifier *obj; |
paul@180 | 394 | }; |
paul@180 | 395 | |
paul@180 | 396 | |
paul@180 | 397 | |
paul@180 | 398 | /* Close a notifier object. */ |
paul@180 | 399 | |
paul@180 | 400 | void file_notify_close(file_notifier_t *notifier) |
paul@180 | 401 | { |
paul@180 | 402 | delete notifier->obj; |
paul@180 | 403 | delete notifier; |
paul@180 | 404 | } |
paul@180 | 405 | |
paul@180 | 406 | /* Obtain a local notifier object. */ |
paul@180 | 407 | |
paul@180 | 408 | file_notifier_t *file_notify_local() |
paul@180 | 409 | { |
paul@180 | 410 | file_notifier_t *notifier = new file_notifier_t; |
paul@180 | 411 | |
paul@180 | 412 | notifier->obj = notifier_get_local_notifier(); |
paul@180 | 413 | return notifier; |
paul@180 | 414 | } |
paul@180 | 415 | |
paul@180 | 416 | /* Obtain the task-wide notifier object. */ |
paul@180 | 417 | |
paul@180 | 418 | file_notifier_t *file_notify_task() |
paul@180 | 419 | { |
paul@180 | 420 | file_notifier_t *notifier = new file_notifier_t; |
paul@180 | 421 | |
paul@180 | 422 | notifier->obj = notifier_get_task_notifier(); |
paul@180 | 423 | return notifier; |
paul@180 | 424 | } |
paul@180 | 425 | |
paul@117 | 426 | /* Subscribe to notification events on a file. */ |
paul@117 | 427 | |
paul@180 | 428 | long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) |
paul@117 | 429 | { |
paul@180 | 430 | return notifier->obj->subscribe(file, flags); |
paul@117 | 431 | } |
paul@117 | 432 | |
paul@117 | 433 | /* Unsubscribe from notification events on a file. */ |
paul@117 | 434 | |
paul@180 | 435 | long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier) |
paul@117 | 436 | { |
paul@180 | 437 | return notifier->obj->unsubscribe(file); |
paul@123 | 438 | } |
paul@123 | 439 | |
paul@123 | 440 | /* Wait for a notification event on a file. */ |
paul@123 | 441 | |
paul@180 | 442 | long file_notify_wait_file(file_t *file, file_notifier_t *notifier) |
paul@117 | 443 | { |
paul@180 | 444 | SpecificFileNotifier *specific_notifier = dynamic_cast<SpecificFileNotifier *>(notifier->obj); |
paul@180 | 445 | long err = specific_notifier->wait_file(file); |
paul@117 | 446 | |
paul@179 | 447 | /* Unsubscribe if a closure notification has been received. */ |
paul@179 | 448 | |
paul@179 | 449 | if (!err && (file->notifications & NOTIFY_PEER_CLOSED)) |
paul@180 | 450 | file_notify_unsubscribe(file, notifier); |
paul@179 | 451 | |
paul@179 | 452 | return err; |
paul@123 | 453 | } |
paul@123 | 454 | |
paul@123 | 455 | /* Wait for notification events on files. */ |
paul@117 | 456 | |
paul@180 | 457 | long file_notify_wait_files(file_t **file, file_notifier_t *notifier) |
paul@123 | 458 | { |
paul@180 | 459 | GeneralFileNotifier *general_notifier = dynamic_cast<GeneralFileNotifier *>(notifier->obj); |
paul@180 | 460 | long err = general_notifier->wait(file); |
paul@123 | 461 | |
paul@179 | 462 | /* Unsubscribe if a closure notification has been received. */ |
paul@179 | 463 | |
paul@179 | 464 | if (!err && ((*file)->notifications & NOTIFY_PEER_CLOSED)) |
paul@180 | 465 | file_notify_unsubscribe(*file, notifier); |
paul@179 | 466 | |
paul@179 | 467 | return err; |
paul@117 | 468 | } |
paul@117 | 469 | |
paul@117 | 470 | |
paul@117 | 471 | |
paul@65 | 472 | /* Open two pipe endpoints using the given pipe server. */ |
paul@65 | 473 | |
paul@66 | 474 | long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) |
paul@65 | 475 | { |
paul@85 | 476 | if (l4_is_invalid_cap(server)) |
paul@85 | 477 | return -L4_EINVAL; |
paul@85 | 478 | |
paul@65 | 479 | client_PipeOpener opener(server); |
paul@65 | 480 | |
paul@65 | 481 | file_init(reader); |
paul@65 | 482 | file_init(writer); |
paul@65 | 483 | |
paul@192 | 484 | return opener.pipe(size, &reader->ref, &writer->ref); |
paul@65 | 485 | } |
paul@65 | 486 | |
paul@65 | 487 | /* Access the current region for a pipe endpoint. */ |
paul@65 | 488 | |
paul@65 | 489 | long pipe_current(file_t *pipe) |
paul@65 | 490 | { |
paul@65 | 491 | client_Pipe _pipe(pipe->ref); |
paul@65 | 492 | long err = _pipe.current_region(&pipe->data_end, &pipe->size); |
paul@65 | 493 | |
paul@65 | 494 | if (err) |
paul@65 | 495 | return err; |
paul@65 | 496 | |
paul@65 | 497 | pipe->end_pos = pipe->size; |
paul@65 | 498 | |
paul@117 | 499 | /* Attach memory if necessary. */ |
paul@65 | 500 | |
paul@117 | 501 | if (pipe->memory == NULL) |
paul@117 | 502 | { |
paul@117 | 503 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@117 | 504 | if (err) |
paul@117 | 505 | return err; |
paul@117 | 506 | } |
paul@65 | 507 | |
paul@65 | 508 | return L4_EOK; |
paul@65 | 509 | } |
paul@65 | 510 | |
paul@65 | 511 | /* Access the next region for a pipe endpoint, updating the eventual size of |
paul@65 | 512 | the current region. */ |
paul@65 | 513 | |
paul@65 | 514 | long pipe_next(file_t *pipe) |
paul@65 | 515 | { |
paul@65 | 516 | client_Pipe _pipe(pipe->ref); |
paul@65 | 517 | long err = _pipe.next_region(&pipe->data_end, &pipe->size); |
paul@65 | 518 | char *memory = pipe->memory; |
paul@65 | 519 | |
paul@65 | 520 | if (err) |
paul@65 | 521 | return err; |
paul@65 | 522 | |
paul@65 | 523 | pipe->end_pos = pipe->size; |
paul@65 | 524 | |
paul@65 | 525 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@65 | 526 | if (err) |
paul@65 | 527 | return err; |
paul@65 | 528 | |
paul@65 | 529 | if (memory != NULL) |
paul@65 | 530 | ipc_detach_dataspace(memory); |
paul@65 | 531 | |
paul@65 | 532 | return L4_EOK; |
paul@65 | 533 | } |
paul@65 | 534 | |
paul@65 | 535 | /* Set the size of the written region. */ |
paul@65 | 536 | |
paul@65 | 537 | long pipe_written(file_t *pipe, offset_t size) |
paul@65 | 538 | { |
paul@65 | 539 | if (size <= pipe->size) |
paul@65 | 540 | { |
paul@65 | 541 | pipe->data_end = size; |
paul@65 | 542 | return L4_EOK; |
paul@65 | 543 | } |
paul@65 | 544 | else |
paul@65 | 545 | return -L4_EINVAL; |
paul@65 | 546 | } |
paul@65 | 547 | |
paul@31 | 548 | // vim: tabstop=2 expandtab shiftwidth=2 |