1 /* 2 * File access convenience functions and types. 3 * 4 * Copyright (C) 2021, 2022, 2023, 2024 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 <sys/stat.h> 25 26 #include <l4/re/c/rm.h> 27 #include <l4/sys/types.h> 28 29 #include <systypes/base.h> 30 #include <systypes/user.h> 31 32 #include <fsclient/notify.h> 33 34 35 36 EXTERN_C_BEGIN 37 38 struct file_t; 39 40 /* File access abstraction compatible with notifiable_base_t. */ 41 42 typedef struct file_t 43 { 44 /* File object reference. */ 45 46 l4_cap_idx_t ref; 47 48 /* Notification structure. */ 49 50 notifiable_t notifiable; 51 52 /* Mapped memory accessing a file region. */ 53 54 char *memory; 55 56 /* File region parameters. */ 57 58 offset_t start_pos, end_pos; /* start and end positions of region */ 59 offset_t data_end; /* amount/extent of data in the region */ 60 offset_t data_current; /* client access offset */ 61 62 /* Total size of file. */ 63 64 offset_t size; 65 66 /* Support for arbitrary memory mapping and explicit object size. */ 67 68 object_flags_t object_flags; 69 70 /* Blocking accesses. */ 71 72 notify_flags_t can_block; 73 74 /* Flags indicated when opening the file. */ 75 76 flags_t flags; 77 78 /* Any failure or error condition on the file. */ 79 80 long error; 81 82 /* Metadata file object reference. */ 83 84 struct file_t *metadata; 85 86 } file_t; 87 88 89 90 /* Filesystem operations. */ 91 92 long file_open_for_user(user_t user, l4_cap_idx_t server, l4_cap_idx_t *opener); 93 94 /* File operations. */ 95 96 void file_close(file_t *file); 97 long file_mkdir(const char *filename, mode_t mode, l4_cap_idx_t server); 98 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server); 99 long file_remove(const char *filename, l4_cap_idx_t server); 100 long file_reopen(file_t *file, file_t *new_file, flags_t flags); 101 long file_rename(const char *source, const char *target, l4_cap_idx_t server); 102 long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server); 103 104 /* File lifecycle operations. */ 105 106 long file_context(file_t *file, l4_cap_idx_t server); 107 long file_context_mkdir(mode_t mode, file_t *context); 108 long file_context_open(file_t *file, flags_t flags, file_t *context); 109 long file_context_remove(file_t *context); 110 long file_context_rename(file_t *context); 111 long file_context_stat(struct stat *st, file_t *context); 112 void file_init(file_t *file); 113 114 /* File and region operations. */ 115 116 long file_flush(file_t *file); 117 long file_refresh(file_t *file); 118 119 long file_mmap(file_t *file, offset_t position, offset_t length, 120 offset_t start_visible, offset_t end_visible, 121 rm_flags_t region_flags); 122 long file_mmap_only(file_t *file, offset_t position, offset_t length, 123 offset_t start_visible, offset_t end_visible); 124 125 flags_t file_opening_flags(rm_flags_t rm_flags); 126 rm_flags_t file_region_flags(flags_t flags); 127 128 long file_resize(file_t *file, offset_t size); 129 130 /* File and region properties. */ 131 132 offset_t file_populated_span(file_t *file); 133 offset_t file_span(file_t *file); 134 135 /* Convenience functions. */ 136 137 char *file_string_get(file_t *file, offset_t offset); 138 int file_string_set(file_t *file, const char *data, offset_t offset, offset_t *written); 139 140 /* Client data functions. */ 141 142 offset_t file_data_available(file_t *file); 143 char *file_data_current(file_t *file); 144 offset_t file_data_current_position(file_t *file); 145 offset_t file_data_end_position(file_t *file); 146 offset_t file_data_space(file_t *file); 147 148 /* Client data transfer functions. */ 149 150 void file_data_read(file_t *file, char *buf, offset_t to_transfer); 151 void file_data_write(file_t *file, char *buf, offset_t to_transfer); 152 153 /* Notification functions. */ 154 155 notifiable_t *file_notifiable(file_t *file); 156 notify_flags_t file_notifications(file_t *file); 157 158 long file_notify_wait_file(file_t *file, notifier_t *notifier); 159 long file_notify_wait_files(file_t **file, notifier_t *notifier); 160 161 162 163 /* Pipe operations. */ 164 165 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server); 166 167 /* Pipe region operations. */ 168 169 long pipe_current(file_t *pipe); 170 long pipe_next(file_t *pipe); 171 long pipe_written(file_t *pipe, offset_t size); 172 173 174 175 /* Directory operations. */ 176 177 long directory_opendir(file_t *file, file_t *reader); 178 179 180 181 EXTERN_C_END 182 183 // vim: tabstop=2 expandtab shiftwidth=2