1 /* 2 * File access convenience functions and types. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #pragma once 23 24 #include <l4/sys/types.h> 25 26 #include <systypes/base.h> 27 #include <systypes/user.h> 28 29 30 31 /* C compatibility types (defined in the implementation). */ 32 33 struct file_notifier; 34 35 typedef struct file_notifier file_notifier_t; 36 37 38 39 EXTERN_C_BEGIN 40 41 /* File access abstraction. */ 42 43 typedef struct 44 { 45 /* File object reference. */ 46 47 l4_cap_idx_t ref; 48 49 /* Mapped memory accessing a file region. */ 50 51 char *memory; 52 53 /* File region parameters. */ 54 55 offset_t start_pos, end_pos; /* start and end positions of region */ 56 offset_t data_end; /* amount/extent of data in the region */ 57 offset_t data_current; /* client access offset */ 58 59 /* Total size of file. */ 60 61 offset_t size; 62 63 /* Support for arbitrary memory mapping and explicit object size. */ 64 65 object_flags_t object_flags; 66 67 /* Blocking accesses. */ 68 69 notify_flags_t can_block; 70 71 /* Saved notifications. */ 72 73 notify_flags_t notifications; 74 75 } file_t; 76 77 78 79 /* Filesystem operations. */ 80 81 long file_open_for_user(user_t user, l4_cap_idx_t server, l4_cap_idx_t *opener); 82 83 /* File operations. */ 84 85 void file_close(file_t *file); 86 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server); 87 long file_remove(const char *filename, l4_cap_idx_t server); 88 89 /* File lifecycle operations. */ 90 91 long file_context(file_t *file, l4_cap_idx_t server); 92 long file_context_open(file_t *file, flags_t flags, file_t *context); 93 long file_context_remove(file_t *context); 94 void file_init(file_t *file); 95 96 /* File and region operations. */ 97 98 long file_flush(file_t *file); 99 long file_mmap(file_t *file, offset_t position, offset_t length); 100 long file_resize(file_t *file, offset_t size); 101 102 /* File and region properties. */ 103 104 offset_t file_populated_span(file_t *file); 105 offset_t file_span(file_t *file); 106 107 /* Convenience functions. */ 108 109 char *file_string_get(file_t *file, offset_t offset); 110 int file_string_set(file_t *file, const char *data, offset_t offset, offset_t *written); 111 112 /* Client data functions. */ 113 114 offset_t file_data_available(file_t *file); 115 char *file_data_current(file_t *file); 116 offset_t file_data_current_position(file_t *file); 117 offset_t file_data_end_position(file_t *file); 118 offset_t file_data_space(file_t *file); 119 120 /* Client data transfer functions. */ 121 122 void file_data_read(file_t *file, char *buf, offset_t to_transfer); 123 void file_data_write(file_t *file, char *buf, offset_t to_transfer); 124 125 /* Notification functions. */ 126 127 void file_notify_close(file_notifier_t *notifier); 128 file_notifier_t *file_notify_local(void); 129 file_notifier_t *file_notify_task(void); 130 131 long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier); 132 long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier); 133 long file_notify_wait_file(file_t *file, file_notifier_t *notifier); 134 long file_notify_wait_files(file_t **file, file_notifier_t *notifier); 135 136 137 138 /* Pipe operations. */ 139 140 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server); 141 142 /* Pipe region operations. */ 143 144 long pipe_current(file_t *pipe); 145 long pipe_next(file_t *pipe); 146 long pipe_written(file_t *pipe, offset_t size); 147 148 149 150 /* Directory operations. */ 151 152 long directory_opendir(file_t *file, file_t *reader); 153 154 155 156 EXTERN_C_END 157 158 // vim: tabstop=2 expandtab shiftwidth=2