1 /* 2 * File access convenience functions and types. 3 * 4 * Copyright (C) 2021, 2022 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/sys/types.h> 27 28 #include <systypes/base.h> 29 #include <systypes/user.h> 30 31 32 33 /* C compatibility types (defined in the implementation). */ 34 35 struct file_notifier; 36 37 typedef struct file_notifier file_notifier_t; 38 39 40 41 EXTERN_C_BEGIN 42 43 /* File access abstraction. */ 44 45 typedef struct 46 { 47 /* File object reference. */ 48 49 l4_cap_idx_t ref; 50 51 /* Mapped memory accessing a file region. */ 52 53 char *memory; 54 55 /* File region parameters. */ 56 57 offset_t start_pos, end_pos; /* start and end positions of region */ 58 offset_t data_end; /* amount/extent of data in the region */ 59 offset_t data_current; /* client access offset */ 60 61 /* Total size of file. */ 62 63 offset_t size; 64 65 /* Support for arbitrary memory mapping and explicit object size. */ 66 67 object_flags_t object_flags; 68 69 /* Blocking accesses. */ 70 71 notify_flags_t can_block; 72 73 /* Saved notifications. */ 74 75 notify_flags_t notifications; 76 77 } file_t; 78 79 80 81 /* Filesystem operations. */ 82 83 long file_open_for_user(user_t user, l4_cap_idx_t server, l4_cap_idx_t *opener); 84 85 /* File operations. */ 86 87 void file_close(file_t *file); 88 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server); 89 long file_remove(const char *filename, l4_cap_idx_t server); 90 long file_rename(const char *source, const char *target, l4_cap_idx_t server); 91 long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server); 92 93 /* File lifecycle operations. */ 94 95 long file_context(file_t *file, l4_cap_idx_t server); 96 long file_context_open(file_t *file, flags_t flags, file_t *context); 97 long file_context_remove(file_t *context); 98 long file_context_rename(file_t *context); 99 long file_context_stat(struct stat *st, file_t *context); 100 void file_init(file_t *file); 101 102 /* File and region operations. */ 103 104 long file_flush(file_t *file); 105 long file_mmap(file_t *file, offset_t position, offset_t length); 106 long file_resize(file_t *file, offset_t size); 107 108 /* File and region properties. */ 109 110 offset_t file_populated_span(file_t *file); 111 offset_t file_span(file_t *file); 112 113 /* Convenience functions. */ 114 115 char *file_string_get(file_t *file, offset_t offset); 116 int file_string_set(file_t *file, const char *data, offset_t offset, offset_t *written); 117 118 /* Client data functions. */ 119 120 offset_t file_data_available(file_t *file); 121 char *file_data_current(file_t *file); 122 offset_t file_data_current_position(file_t *file); 123 offset_t file_data_end_position(file_t *file); 124 offset_t file_data_space(file_t *file); 125 126 /* Client data transfer functions. */ 127 128 void file_data_read(file_t *file, char *buf, offset_t to_transfer); 129 void file_data_write(file_t *file, char *buf, offset_t to_transfer); 130 131 /* Notification functions. */ 132 133 void file_notify_close(file_notifier_t *notifier); 134 file_notifier_t *file_notify_local(void); 135 file_notifier_t *file_notify_task(void); 136 137 long file_notify_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier); 138 long file_notify_unsubscribe(file_t *file, file_notifier_t *notifier); 139 long file_notify_wait_file(file_t *file, file_notifier_t *notifier); 140 long file_notify_wait_files(file_t **file, file_notifier_t *notifier); 141 142 143 144 /* Pipe operations. */ 145 146 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server); 147 148 /* Pipe region operations. */ 149 150 long pipe_current(file_t *pipe); 151 long pipe_next(file_t *pipe); 152 long pipe_written(file_t *pipe, offset_t size); 153 154 155 156 /* Directory operations. */ 157 158 long directory_opendir(file_t *file, file_t *reader); 159 160 161 162 EXTERN_C_END 163 164 // vim: tabstop=2 expandtab shiftwidth=2