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 /* 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(flags_t flags, fileid_t fileid, 100 Accessor **accessor) 101 { 102 (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(flags_t flags, fileid_t fileid, 117 DirectoryAccessor **accessor) 118 { 119 (void) flags; 120 121 *accessor = new Ext2DirectoryAccessor(_ops, fileid); 122 return L4_EOK; 123 } 124 125 /* Remove a filesystem object. */ 126 127 long Ext2FileOpener::remove_object(fileid_t fileid) 128 { 129 return _ops->remove((ext2_ino_t) fileid); 130 } 131 132 /* Rename a filesystem object, placing source inside the parent of target. */ 133 134 long Ext2FileOpener::rename_object(const char *source, const char *target) 135 { 136 fileid_t source_fileid, source_parent, target_parent; 137 long err; 138 139 err = get_fileid(source, 0, &source_fileid); 140 141 if (err) 142 return err; 143 144 err = get_parent(source, &source_parent); 145 146 if (err) 147 return err; 148 149 err = get_parent(target, &target_parent); 150 151 if (err) 152 return err; 153 154 return _ops->rename((ext2_ino_t) source_fileid, 155 (ext2_ino_t) source_parent, path_basename(source), 156 (ext2_ino_t) target_parent, path_basename(target)); 157 } 158 159 /* Unlink a filesystem object. */ 160 161 long Ext2FileOpener::unlink_object(fileid_t parent_fileid, fileid_t fileid) 162 { 163 return _ops->unlink((ext2_ino_t) parent_fileid, (ext2_ino_t) fileid); 164 } 165 166 // vim: tabstop=4 expandtab shiftwidth=4