1 /* 2 * An opener for a file provided by 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 #include <string.h> 23 24 #include <e2access/path.h> 25 #include <fsclient/client.h> 26 #include <systypes/fcntl.h> 27 28 #include "ext2_directory_accessor.h" 29 #include "ext2_file_accessor.h" 30 #include "ext2_file_opener.h" 31 #include "resource_server.h" 32 33 34 35 Ext2FileOpener::~Ext2FileOpener() 36 { 37 } 38 39 /* Test if a directory is being accessed. */ 40 41 bool Ext2FileOpener::accessing_directory(flags_t flags, fileid_t fileid) 42 { 43 (void) flags; 44 return _ops->is_directory((ext2_ino_t) fileid); 45 } 46 47 /* Test if a file is being accessed. */ 48 49 bool Ext2FileOpener::accessing_file(flags_t flags, fileid_t fileid) 50 { 51 (void) flags; 52 return _ops->is_file((ext2_ino_t) fileid); 53 } 54 55 /* Test if a directory is empty. */ 56 57 bool Ext2FileOpener::directory_is_empty(fileid_t fileid) 58 { 59 return _ops->directory_is_empty(fileid); 60 } 61 62 /* Return a file identifier for the given 'path'. */ 63 64 long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) 65 { 66 ext2_ino_t ino; 67 const char *remaining; 68 long err = _ops->find_file(path, &ino, &remaining); 69 70 /* Return an existing file. */ 71 72 if (!err) 73 { 74 if (!_ops->can_access(_user, flags, ino)) 75 return -L4_EPERM; 76 77 *fileid = (fileid_t) ino; 78 return L4_EOK; 79 } 80 81 *fileid = FILEID_INVALID; 82 83 /* Create a missing file if possible. */ 84 85 if ((flags & O_CREAT) && (flags & ~O_DIRECTORY)) 86 { 87 /* Determine whether only the leafname is left of the path, with 88 the inode number referring to the parent directory. */ 89 90 if (path_is_leafname(remaining)) 91 { 92 err = _ops->create_file(ino, remaining, _user, &ino); 93 if (err) 94 return err; 95 96 *fileid = (fileid_t) ino; 97 return L4_EOK; 98 } 99 } 100 101 return -L4_ENOENT; 102 } 103 104 /* Return a new accessor for 'fileid'. */ 105 106 long Ext2FileOpener::make_accessor(flags_t flags, fileid_t fileid, 107 Accessor **accessor) 108 { 109 (void) flags; 110 111 ext2_file_t file; 112 long err = _ops->open_file((ext2_ino_t) fileid, &file); 113 114 if (err) 115 return err; 116 117 *accessor = new Ext2FileAccessor(_ops, file, fileid); 118 return L4_EOK; 119 } 120 121 /* Return a directory object reference for the given file identifier. */ 122 123 long Ext2FileOpener::make_directory_accessor(flags_t flags, fileid_t fileid, 124 DirectoryAccessor **accessor) 125 { 126 (void) flags; 127 128 *accessor = new Ext2DirectoryAccessor(_ops, fileid); 129 return L4_EOK; 130 } 131 132 /* Remove a filesystem object. */ 133 134 long Ext2FileOpener::remove_object(fileid_t fileid) 135 { 136 return _ops->remove((ext2_ino_t) fileid); 137 } 138 139 /* Rename a filesystem object, placing source inside the parent of target. */ 140 141 long Ext2FileOpener::rename_object(const char *source, const char *target) 142 { 143 fileid_t source_fileid, source_parent, target_parent; 144 long err; 145 146 err = get_fileid(source, 0, &source_fileid); 147 148 if (err) 149 return err; 150 151 err = get_parent(source, &source_parent); 152 153 if (err) 154 return err; 155 156 err = get_parent(target, &target_parent); 157 158 if (err) 159 return err; 160 161 return _ops->rename((ext2_ino_t) source_fileid, 162 (ext2_ino_t) source_parent, path_basename(source), 163 (ext2_ino_t) target_parent, path_basename(target)); 164 } 165 166 /* Unlink a filesystem object. */ 167 168 long Ext2FileOpener::unlink_object(fileid_t parent_fileid, fileid_t fileid) 169 { 170 return _ops->unlink((ext2_ino_t) parent_fileid, (ext2_ino_t) fileid); 171 } 172 173 // vim: tabstop=4 expandtab shiftwidth=4