1 /* 2 * An opener for a "host" file provided via the C library. 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 <stdio.h> 23 #include <sys/stat.h> 24 #include <unistd.h> 25 26 #include <fsclient/client.h> 27 28 #include "host_directory_accessor.h" 29 #include "host_file_accessor.h" 30 #include "host_file_opener.h" 31 #include "resource_server.h" 32 33 34 35 HostFileOpener::~HostFileOpener() 36 { 37 } 38 39 bool HostFileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) 40 { 41 (void) flags; (void) fileid; 42 struct stat st; 43 44 if (stat(path, &st)) 45 return false; 46 47 return (st.st_mode & S_IFDIR) ? true : false; 48 } 49 50 bool HostFileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) 51 { 52 (void) flags; (void) fileid; 53 struct stat st; 54 55 if (stat(path, &st)) 56 return false; 57 58 return (st.st_mode & S_IFREG) ? true : false; 59 } 60 61 /* Return a file identifier for the given 'path'. */ 62 63 long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) 64 { 65 (void) flags; 66 67 /* Test for a valid file. */ 68 69 FILE *fp = fopen(path, "r"); 70 71 if (fp == NULL) 72 return -L4_ENOENT; 73 74 fclose(fp); 75 76 *fileid = _get_fileid(path, true); 77 return L4_EOK; 78 } 79 80 fileid_t HostFileOpener::_get_fileid(const char *path, bool create) 81 { 82 std::lock_guard<std::mutex> guard(_lock); 83 84 /* The inode number would be a good choice, but the L4Re read-only file 85 implementation just provides the value of a counter! 86 87 See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ 88 89 std::string s(path); 90 91 HostFileIdentifiers::iterator it = _fileids.find(s); 92 93 if (it != _fileids.end()) 94 return it->second; 95 96 if (!create) 97 return FILEID_INVALID; 98 99 fileid_t fileid = _fileids.size(); 100 101 _fileids[s] = fileid; 102 103 return fileid; 104 } 105 106 /* Return a new accessor for 'fileid'. */ 107 108 long HostFileOpener::make_accessor(const char *path, flags_t flags, 109 fileid_t fileid, Accessor **accessor) 110 { 111 // NOTE: Not testing for create or write flags. 112 113 (void) flags; 114 115 FILE *fp = fopen(path, "r"); 116 117 if (fp == NULL) 118 return -L4_ENOENT; 119 120 *accessor = new HostFileAccessor(fp, fileid); 121 return L4_EOK; 122 } 123 124 /* Return a directory accessor for 'fileid'. */ 125 126 long HostFileOpener::make_directory_accessor(const char *path, flags_t flags, 127 fileid_t fileid, 128 DirectoryAccessor **accessor) 129 { 130 (void) flags; (void) fileid; 131 132 *accessor = new HostDirectoryAccessor(path); 133 return L4_EOK; 134 } 135 136 /* Remove a filesystem object. */ 137 138 long HostFileOpener::remove_object(const char *path, fileid_t fileid) 139 { 140 (void) path; (void) fileid; 141 return L4_EOK; 142 } 143 144 /* Rename a filesystem object, placing source inside the parent of target. */ 145 146 long HostFileOpener::rename_object(const char *source, fileid_t source_fileid, 147 const char *target) 148 { 149 (void) source_fileid; 150 151 /* NOTE: Should propagate a more meaningful error. */ 152 153 if (rename(source, target)) 154 return -L4_EIO; 155 156 return L4_EOK; 157 } 158 159 /* Unlink a filesystem object. */ 160 161 long HostFileOpener::unlink_object(const char *path, fileid_t fileid) 162 { 163 /* Ignore the fileid and always use the path. */ 164 165 (void) fileid; 166 167 // NOTE: Return code may need converting. 168 169 int err = unlink(path); 170 171 if (err) 172 return err; 173 174 /* Remove the file identifier mapping. */ 175 176 std::string s(path); 177 178 HostFileIdentifiers::iterator it = _fileids.find(s); 179 180 if (it != _fileids.end()) 181 _fileids.erase(it); 182 183 return L4_EOK; 184 } 185 186 // vim: tabstop=4 expandtab shiftwidth=4