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