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