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