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