1 /* 2 * An opener for a file provided by an Ext2-compatible filesystem. 3 * 4 * Copyright (C) 2021 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 *fileid = (fileid_t) ino; 68 return L4_EOK; 69 } 70 71 *fileid = FILEID_INVALID; 72 73 /* Create a missing file if possible. */ 74 75 if ((flags & O_CREAT) && (flags & ~O_DIRECTORY)) 76 { 77 /* Determine whether only the leafname is left of the path, with 78 the inode number referring to the parent directory. */ 79 80 if (path_is_leafname(remaining)) 81 { 82 err = _ops->create_file(ino, remaining, _user, &ino); 83 if (err) 84 return err; 85 86 *fileid = (fileid_t) ino; 87 return L4_EOK; 88 } 89 } 90 91 return -L4_ENOENT; 92 } 93 94 /* Return a new accessor for 'fileid'. */ 95 96 long Ext2FileOpener::make_accessor(const char *path, flags_t flags, 97 fileid_t fileid, Accessor **accessor) 98 { 99 (void) path; (void) flags; 100 101 ext2_file_t file; 102 long err = _ops->open_file((ext2_ino_t) fileid, &file); 103 104 if (err) 105 return err; 106 107 *accessor = new Ext2FileAccessor(_ops, file, fileid); 108 return L4_EOK; 109 } 110 111 /* Return a directory object reference for the given file identifier. */ 112 113 long Ext2FileOpener::make_directory_accessor(const char *path, flags_t flags, 114 fileid_t fileid, 115 DirectoryAccessor **accessor) 116 { 117 (void) path; (void) flags; 118 119 *accessor = new Ext2DirectoryAccessor(_ops, fileid); 120 return L4_EOK; 121 } 122 123 /* Remove a filesystem object. */ 124 125 long Ext2FileOpener::remove_object(const char *path, fileid_t fileid) 126 { 127 (void) path; 128 129 return _ops->remove((ext2_ino_t) fileid); 130 } 131 132 /* Unlink a filesystem object. */ 133 134 long Ext2FileOpener::unlink_object(const char *path, fileid_t fileid) 135 { 136 fileid_t parent; 137 long err = get_parent(path, &parent); 138 139 if (err) 140 return err; 141 142 return _ops->unlink((ext2_ino_t) parent, (ext2_ino_t) fileid); 143 } 144 145 // vim: tabstop=4 expandtab shiftwidth=4