paul@148 | 1 | /* |
paul@148 | 2 | * File operations performed by an Ext2-compatible filesystem. |
paul@148 | 3 | * |
paul@236 | 4 | * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@148 | 5 | * |
paul@148 | 6 | * This program is free software; you can redistribute it and/or |
paul@148 | 7 | * modify it under the terms of the GNU General Public License as |
paul@148 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@148 | 9 | * the License, or (at your option) any later version. |
paul@148 | 10 | * |
paul@148 | 11 | * This program is distributed in the hope that it will be useful, |
paul@148 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@148 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@148 | 14 | * GNU General Public License for more details. |
paul@148 | 15 | * |
paul@148 | 16 | * You should have received a copy of the GNU General Public License |
paul@148 | 17 | * along with this program; if not, write to the Free Software |
paul@148 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@148 | 19 | * Boston, MA 02110-1301, USA |
paul@148 | 20 | */ |
paul@148 | 21 | |
paul@148 | 22 | #include <e2access/access.h> |
paul@148 | 23 | #include <e2access/image.h> |
paul@148 | 24 | #include <e2access/path.h> |
paul@148 | 25 | |
paul@148 | 26 | #include "ext2_file_operations.h" |
paul@148 | 27 | |
paul@156 | 28 | /* Object type tests. */ |
paul@156 | 29 | |
paul@156 | 30 | bool Ext2FileOperations::is_directory(ext2_ino_t ino_file) |
paul@156 | 31 | { |
paul@156 | 32 | std::lock_guard<std::mutex> guard(_lock); |
paul@156 | 33 | |
paul@156 | 34 | return _image_isdir(_fs, ino_file); |
paul@156 | 35 | } |
paul@156 | 36 | |
paul@156 | 37 | bool Ext2FileOperations::is_file(ext2_ino_t ino_file) |
paul@156 | 38 | { |
paul@156 | 39 | std::lock_guard<std::mutex> guard(_lock); |
paul@156 | 40 | |
paul@156 | 41 | return _image_isfile(_fs, ino_file); |
paul@156 | 42 | } |
paul@156 | 43 | |
paul@228 | 44 | /* Perform closing operations on a file. */ |
paul@228 | 45 | |
paul@228 | 46 | void Ext2FileOperations::close_file(ext2_file_t file) |
paul@228 | 47 | { |
paul@228 | 48 | std::lock_guard<std::mutex> guard(_lock); |
paul@228 | 49 | |
paul@228 | 50 | ext2fs_file_flush(file); |
paul@228 | 51 | ext2fs_file_close(file); |
paul@228 | 52 | } |
paul@228 | 53 | |
paul@148 | 54 | /* Create a file in the directory indicated by the given inode number with the |
paul@148 | 55 | given filename. The file is created with the given user permissions. */ |
paul@148 | 56 | |
paul@148 | 57 | long Ext2FileOperations::create_file(ext2_ino_t ino_parent, const char *filename, |
paul@148 | 58 | user_t user, ext2_ino_t *ino) |
paul@148 | 59 | { |
paul@148 | 60 | if (!path_is_leafname(filename)) |
paul@148 | 61 | return -L4_EINVAL; |
paul@148 | 62 | |
paul@149 | 63 | std::lock_guard<std::mutex> guard(_lock); |
paul@149 | 64 | |
paul@148 | 65 | struct ext2_inode inode_parent; |
paul@148 | 66 | errcode_t retval = ext2fs_read_inode(_fs, ino_parent, &inode_parent); |
paul@148 | 67 | |
paul@148 | 68 | if (retval) |
paul@148 | 69 | return -L4_EIO; |
paul@148 | 70 | |
paul@148 | 71 | if (!access_can_write(user, &inode_parent)) |
paul@148 | 72 | return -L4_EPERM; |
paul@148 | 73 | |
paul@148 | 74 | if (image_create_file(_fs, ino_parent, filename, 0666 & ~user.umask, |
paul@148 | 75 | user.uid, user.gid, ino)) |
paul@148 | 76 | return -L4_EIO; |
paul@148 | 77 | |
paul@148 | 78 | return L4_EOK; |
paul@148 | 79 | } |
paul@148 | 80 | |
paul@148 | 81 | /* For the given 'path', return an inode number or indicate the 'remaining' |
paul@148 | 82 | part of the path that cannot be resolved. */ |
paul@148 | 83 | |
paul@148 | 84 | long Ext2FileOperations::find_file(const char *path, ext2_ino_t *ino, |
paul@148 | 85 | const char **remaining) |
paul@148 | 86 | { |
paul@149 | 87 | std::lock_guard<std::mutex> guard(_lock); |
paul@149 | 88 | |
paul@148 | 89 | *remaining = path; |
paul@148 | 90 | errcode_t retval = image_find_path(_fs, remaining, ino); |
paul@148 | 91 | |
paul@148 | 92 | // NOTE: Map error conditions. |
paul@148 | 93 | |
paul@148 | 94 | if (retval) |
paul@148 | 95 | return -L4_EIO; |
paul@148 | 96 | |
paul@148 | 97 | return L4_EOK; |
paul@148 | 98 | } |
paul@148 | 99 | |
paul@148 | 100 | /* Open the file associated with the indicated inode. */ |
paul@148 | 101 | |
paul@148 | 102 | long Ext2FileOperations::open_file(ext2_ino_t ino, ext2_file_t *file) |
paul@148 | 103 | { |
paul@149 | 104 | std::lock_guard<std::mutex> guard(_lock); |
paul@149 | 105 | |
paul@148 | 106 | errcode_t retval = ext2fs_file_open(_fs, ino, EXT2_FILE_WRITE, file); |
paul@148 | 107 | |
paul@148 | 108 | // NOTE: Map error conditions. |
paul@148 | 109 | |
paul@148 | 110 | if (retval) |
paul@148 | 111 | return -L4_EIO; |
paul@148 | 112 | |
paul@148 | 113 | return L4_EOK; |
paul@148 | 114 | } |
paul@148 | 115 | |
paul@232 | 116 | /* Remove an object from a directory. */ |
paul@232 | 117 | |
paul@232 | 118 | long Ext2FileOperations::remove(ext2_ino_t ino) |
paul@232 | 119 | { |
paul@232 | 120 | std::lock_guard<std::mutex> guard(_lock); |
paul@232 | 121 | |
paul@232 | 122 | errcode_t retval = image_remove_by_inode(_fs, ino); |
paul@232 | 123 | |
paul@232 | 124 | // NOTE: Map error conditions. |
paul@232 | 125 | |
paul@232 | 126 | if (retval) |
paul@232 | 127 | return -L4_EIO; |
paul@232 | 128 | |
paul@232 | 129 | return L4_EOK; |
paul@232 | 130 | } |
paul@232 | 131 | |
paul@236 | 132 | /* Rename an object. */ |
paul@236 | 133 | |
paul@236 | 134 | long Ext2FileOperations::rename(ext2_ino_t source, |
paul@236 | 135 | ext2_ino_t source_parent, const char *source_basename, |
paul@236 | 136 | ext2_ino_t target_parent, const char *target_basename) |
paul@236 | 137 | { |
paul@236 | 138 | std::lock_guard<std::mutex> guard(_lock); |
paul@236 | 139 | |
paul@236 | 140 | errcode_t retval = image_rename(_fs, source, source_parent, source_basename, |
paul@236 | 141 | target_parent, target_basename); |
paul@236 | 142 | |
paul@236 | 143 | // NOTE: Map error conditions. |
paul@236 | 144 | |
paul@236 | 145 | if (retval) |
paul@236 | 146 | return -L4_EIO; |
paul@236 | 147 | |
paul@236 | 148 | return L4_EOK; |
paul@236 | 149 | } |
paul@236 | 150 | |
paul@232 | 151 | /* Unlink an object from a directory. */ |
paul@232 | 152 | |
paul@232 | 153 | long Ext2FileOperations::unlink(ext2_ino_t ino_parent, ext2_ino_t ino) |
paul@232 | 154 | { |
paul@232 | 155 | std::lock_guard<std::mutex> guard(_lock); |
paul@232 | 156 | |
paul@232 | 157 | errcode_t retval = image_unlink_by_inode(_fs, ino_parent, ino); |
paul@232 | 158 | |
paul@232 | 159 | // NOTE: Map error conditions. |
paul@232 | 160 | |
paul@232 | 161 | if (retval) |
paul@232 | 162 | return -L4_EIO; |
paul@232 | 163 | |
paul@232 | 164 | return L4_EOK; |
paul@232 | 165 | } |
paul@232 | 166 | |
paul@236 | 167 | |
paul@236 | 168 | |
paul@148 | 169 | /* Obtain the size of a file. */ |
paul@148 | 170 | |
paul@148 | 171 | offset_t Ext2FileOperations::get_size(ext2_file_t file) |
paul@148 | 172 | { |
paul@149 | 173 | std::lock_guard<std::mutex> guard(_lock); |
paul@149 | 174 | |
paul@148 | 175 | return ext2fs_file_get_size(file); |
paul@148 | 176 | } |
paul@148 | 177 | |
paul@148 | 178 | /* Update the size of a file. */ |
paul@148 | 179 | |
paul@148 | 180 | void Ext2FileOperations::set_size(ext2_file_t file, offset_t size) |
paul@148 | 181 | { |
paul@149 | 182 | std::lock_guard<std::mutex> guard(_lock); |
paul@149 | 183 | |
paul@148 | 184 | ext2fs_file_set_size(file, size); |
paul@148 | 185 | } |
paul@148 | 186 | |
paul@148 | 187 | /* Populate the given memory region with file content. */ |
paul@148 | 188 | |
paul@148 | 189 | offset_t Ext2FileOperations::read_file(ext2_file_t file, offset_t filepos, void *addr, offset_t size) |
paul@148 | 190 | { |
paul@149 | 191 | std::lock_guard<std::mutex> guard(_lock); |
paul@149 | 192 | |
paul@148 | 193 | unsigned int nread; |
paul@148 | 194 | |
paul@148 | 195 | ext2fs_file_llseek(file, filepos, SEEK_SET, NULL); |
paul@148 | 196 | ext2fs_file_read(file, (void *) addr, size, &nread); |
paul@148 | 197 | |
paul@148 | 198 | return (offset_t) nread; |
paul@148 | 199 | } |
paul@148 | 200 | |
paul@148 | 201 | /* Transfer content from the given memory region to a file. */ |
paul@148 | 202 | |
paul@148 | 203 | void Ext2FileOperations::write_file(ext2_file_t file, offset_t filepos, const void *addr, offset_t size) |
paul@148 | 204 | { |
paul@149 | 205 | std::lock_guard<std::mutex> guard(_lock); |
paul@149 | 206 | |
paul@148 | 207 | ext2fs_file_llseek(file, filepos, SEEK_SET, NULL); |
paul@148 | 208 | ext2fs_file_write(file, addr, size, NULL); |
paul@148 | 209 | } |
paul@148 | 210 | |
paul@191 | 211 | /* Obtain information for the given inode. */ |
paul@191 | 212 | |
paul@191 | 213 | long Ext2FileOperations::read_inode(ext2_ino_t ino, struct ext2_inode *inode) |
paul@191 | 214 | { |
paul@191 | 215 | std::lock_guard<std::mutex> guard(_lock); |
paul@191 | 216 | |
paul@191 | 217 | if (ext2fs_read_inode(_fs, ino, inode)) |
paul@191 | 218 | return -L4_EIO; |
paul@191 | 219 | |
paul@191 | 220 | return L4_EOK; |
paul@191 | 221 | } |
paul@191 | 222 | |
paul@191 | 223 | |
paul@191 | 224 | |
paul@191 | 225 | /* Directory iteration support. */ |
paul@191 | 226 | |
paul@191 | 227 | /* Callback function invoking the callback method. */ |
paul@191 | 228 | |
paul@191 | 229 | static int _directory_iterate_fn(struct ext2_dir_entry *dir_entry, |
paul@191 | 230 | int offset, int blocksize, char *buf, |
paul@191 | 231 | void *priv_data) |
paul@191 | 232 | { |
paul@191 | 233 | struct Ext2DirectoryIteration *data = reinterpret_cast<struct Ext2DirectoryIteration *>(priv_data); |
paul@191 | 234 | |
paul@191 | 235 | return data->ops->directory_iterate_fn(dir_entry, offset, blocksize, buf, data); |
paul@191 | 236 | } |
paul@191 | 237 | |
paul@191 | 238 | /* Callback method invoking the actual callback, with control over locking. */ |
paul@191 | 239 | |
paul@191 | 240 | int Ext2FileOperations::directory_iterate_fn(struct ext2_dir_entry *dir_entry, |
paul@191 | 241 | int offset, int blocksize, |
paul@191 | 242 | char *buf, struct Ext2DirectoryIteration *priv_data) |
paul@191 | 243 | { |
paul@191 | 244 | /* Release the lock to allow other operations to proceed. */ |
paul@191 | 245 | |
paul@191 | 246 | _lock.unlock(); |
paul@191 | 247 | |
paul@191 | 248 | /* Invoke the actual callback function. */ |
paul@191 | 249 | |
paul@191 | 250 | struct Ext2DirectoryIteration *data = reinterpret_cast<struct Ext2DirectoryIteration *>(priv_data); |
paul@191 | 251 | int result = data->func(dir_entry, offset, blocksize, buf, data->priv_data); |
paul@191 | 252 | |
paul@191 | 253 | /* Acquire the lock and continue iteration. */ |
paul@191 | 254 | |
paul@191 | 255 | _lock.lock(); |
paul@191 | 256 | return result; |
paul@191 | 257 | } |
paul@191 | 258 | |
paul@168 | 259 | /* Initiate iteration over a directory, with the given 'func' being called |
paul@168 | 260 | with directory entry details for each entry. */ |
paul@168 | 261 | |
paul@168 | 262 | long Ext2FileOperations::directory_iterate(ext2_ino_t dir, |
paul@168 | 263 | int func(struct ext2_dir_entry *, int, int, |
paul@168 | 264 | char *, void *), |
paul@168 | 265 | void *priv_data) |
paul@168 | 266 | { |
paul@191 | 267 | /* NOTE: Iteration should probably be interrupted if it appears that the |
paul@191 | 268 | directory has been modified and the structure of the data has |
paul@191 | 269 | been changed. This condition might then cause iteration to begin |
paul@191 | 270 | again, skipping ahead to the entry after the last visited entry. */ |
paul@168 | 271 | |
paul@191 | 272 | /* Acquire the lock for iteration to proceed safely. */ |
paul@191 | 273 | |
paul@191 | 274 | _lock.lock(); |
paul@168 | 275 | |
paul@191 | 276 | /* Begin iteration using the given callback function to invoke a callback |
paul@191 | 277 | method in this object that will wrap the actual callback with locking |
paul@191 | 278 | operations. */ |
paul@191 | 279 | |
paul@191 | 280 | struct Ext2DirectoryIteration data = {this, func, priv_data}; |
paul@191 | 281 | int result = ext2fs_dir_iterate(_fs, dir, 0, 0, _directory_iterate_fn, &data); |
paul@168 | 282 | |
paul@191 | 283 | /* Release the lock upon finishing iteration. */ |
paul@191 | 284 | |
paul@191 | 285 | _lock.unlock(); |
paul@191 | 286 | |
paul@191 | 287 | if (result) |
paul@168 | 288 | return -L4_EIO; |
paul@191 | 289 | else |
paul@191 | 290 | return L4_EOK; |
paul@168 | 291 | } |
paul@168 | 292 | |
paul@148 | 293 | // vim: tabstop=4 expandtab shiftwidth=4 |