paul@148 | 1 | /* |
paul@148 | 2 | * File operations supporting 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 | #pragma once |
paul@148 | 23 | |
paul@149 | 24 | #include <mutex> |
paul@149 | 25 | |
paul@266 | 26 | #include <sys/stat.h> |
paul@266 | 27 | |
paul@148 | 28 | #include <ext2fs/ext2fs.h> |
paul@148 | 29 | |
paul@148 | 30 | #include <systypes/base.h> |
paul@148 | 31 | #include <systypes/user.h> |
paul@148 | 32 | |
paul@148 | 33 | |
paul@148 | 34 | |
paul@191 | 35 | /* Directory iteration helper type. */ |
paul@191 | 36 | |
paul@191 | 37 | class Ext2FileOperations; |
paul@191 | 38 | |
paul@191 | 39 | struct Ext2DirectoryIteration |
paul@191 | 40 | { |
paul@191 | 41 | Ext2FileOperations *ops; |
paul@191 | 42 | int (*func)(struct ext2_dir_entry *, int, int, char *, void *); |
paul@191 | 43 | void *priv_data; |
paul@191 | 44 | }; |
paul@191 | 45 | |
paul@191 | 46 | |
paul@191 | 47 | |
paul@148 | 48 | /* An Ext2 file operations collection. */ |
paul@148 | 49 | |
paul@148 | 50 | class Ext2FileOperations |
paul@148 | 51 | { |
paul@148 | 52 | protected: |
paul@149 | 53 | std::mutex _lock; |
paul@148 | 54 | ext2_filsys _fs; |
paul@148 | 55 | |
paul@148 | 56 | public: |
paul@148 | 57 | explicit Ext2FileOperations(ext2_filsys fs) |
paul@148 | 58 | : _fs(fs) |
paul@148 | 59 | { |
paul@148 | 60 | } |
paul@148 | 61 | |
paul@239 | 62 | /* Access and object type tests. */ |
paul@239 | 63 | |
paul@239 | 64 | bool can_access(user_t user, flags_t flags, ext2_ino_t ino); |
paul@239 | 65 | |
paul@256 | 66 | bool directory_is_empty(ext2_ino_t ino); |
paul@256 | 67 | |
paul@156 | 68 | bool is_directory(ext2_ino_t ino_file); |
paul@156 | 69 | |
paul@156 | 70 | bool is_file(ext2_ino_t ino_file); |
paul@156 | 71 | |
paul@239 | 72 | /* File manipulation. */ |
paul@239 | 73 | |
paul@232 | 74 | void close_file(ext2_file_t file); |
paul@232 | 75 | |
paul@148 | 76 | long create_file(ext2_ino_t ino_parent, const char *filename, user_t user, |
paul@148 | 77 | ext2_ino_t *ino); |
paul@148 | 78 | |
paul@148 | 79 | long find_file(const char *path, ext2_ino_t *ino, const char **remaining); |
paul@148 | 80 | |
paul@271 | 81 | long mkdir(ext2_ino_t ino_parent, const char *basename, sys_mode_t mode, |
paul@271 | 82 | user_t user); |
paul@271 | 83 | |
paul@232 | 84 | long open_file(ext2_ino_t ino, ext2_file_t *file); |
paul@232 | 85 | |
paul@232 | 86 | long remove(ext2_ino_t ino); |
paul@156 | 87 | |
paul@236 | 88 | long rename(ext2_ino_t source, |
paul@236 | 89 | ext2_ino_t source_parent, const char *source_basename, |
paul@236 | 90 | ext2_ino_t target_parent, const char *target_basename); |
paul@236 | 91 | |
paul@232 | 92 | long unlink(ext2_ino_t ino_parent, ext2_ino_t ino); |
paul@232 | 93 | |
paul@232 | 94 | /* File property methods. */ |
paul@148 | 95 | |
paul@148 | 96 | offset_t get_size(ext2_file_t file); |
paul@148 | 97 | |
paul@148 | 98 | void set_size(ext2_file_t file, offset_t size); |
paul@148 | 99 | |
paul@232 | 100 | /* File access methods. */ |
paul@232 | 101 | |
paul@148 | 102 | offset_t read_file(ext2_file_t file, offset_t filepos, void *addr, offset_t size); |
paul@148 | 103 | |
paul@148 | 104 | void write_file(ext2_file_t file, offset_t filepos, const void *addr, offset_t size); |
paul@168 | 105 | |
paul@266 | 106 | /* Directory and metadata access. */ |
paul@232 | 107 | |
paul@168 | 108 | long directory_iterate(ext2_ino_t dir, |
paul@168 | 109 | int func(struct ext2_dir_entry *, int, int, char *, void *), |
paul@168 | 110 | void *priv_data); |
paul@168 | 111 | |
paul@266 | 112 | long read_inode(ext2_ino_t ino, struct ext2_inode *inode); |
paul@266 | 113 | |
paul@266 | 114 | long stat_inode(ext2_ino_t ino, struct stat *st); |
paul@191 | 115 | |
paul@191 | 116 | /* Callback method. */ |
paul@191 | 117 | |
paul@191 | 118 | int directory_iterate_fn(struct ext2_dir_entry *dir_entry, |
paul@191 | 119 | int offset, int blocksize, |
paul@191 | 120 | char *buf, struct Ext2DirectoryIteration *priv_data); |
paul@148 | 121 | }; |
paul@148 | 122 | |
paul@148 | 123 | // vim: tabstop=4 expandtab shiftwidth=4 |