paul@93 | 1 | /* |
paul@93 | 2 | * An opener for a "host" file provided via the C library. |
paul@93 | 3 | * |
paul@236 | 4 | * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@256 | 22 | #include <dirent.h> |
paul@236 | 23 | #include <stdio.h> |
paul@256 | 24 | #include <string.h> |
paul@156 | 25 | #include <sys/stat.h> |
paul@232 | 26 | #include <unistd.h> |
paul@156 | 27 | |
paul@160 | 28 | #include <fsclient/client.h> |
paul@160 | 29 | |
paul@202 | 30 | #include "host_directory_accessor.h" |
paul@12 | 31 | #include "host_file_accessor.h" |
paul@12 | 32 | #include "host_file_opener.h" |
paul@202 | 33 | #include "resource_server.h" |
paul@160 | 34 | |
paul@160 | 35 | |
paul@160 | 36 | |
paul@156 | 37 | HostFileOpener::~HostFileOpener() |
paul@156 | 38 | { |
paul@156 | 39 | } |
paul@156 | 40 | |
paul@240 | 41 | bool HostFileOpener::accessing_directory(flags_t flags, fileid_t fileid) |
paul@156 | 42 | { |
paul@240 | 43 | (void) flags; |
paul@240 | 44 | |
paul@240 | 45 | const char *path = _get_path(fileid); |
paul@240 | 46 | |
paul@240 | 47 | if (path == NULL) |
paul@240 | 48 | return false; |
paul@240 | 49 | |
paul@156 | 50 | struct stat st; |
paul@156 | 51 | |
paul@156 | 52 | if (stat(path, &st)) |
paul@156 | 53 | return false; |
paul@156 | 54 | |
paul@156 | 55 | return (st.st_mode & S_IFDIR) ? true : false; |
paul@156 | 56 | } |
paul@156 | 57 | |
paul@240 | 58 | bool HostFileOpener::accessing_file(flags_t flags, fileid_t fileid) |
paul@156 | 59 | { |
paul@240 | 60 | (void) flags; |
paul@156 | 61 | struct stat st; |
paul@156 | 62 | |
paul@240 | 63 | const char *path = _get_path(fileid); |
paul@240 | 64 | |
paul@240 | 65 | if (path == NULL) |
paul@240 | 66 | return false; |
paul@240 | 67 | |
paul@156 | 68 | if (stat(path, &st)) |
paul@156 | 69 | return false; |
paul@156 | 70 | |
paul@156 | 71 | return (st.st_mode & S_IFREG) ? true : false; |
paul@156 | 72 | } |
paul@156 | 73 | |
paul@256 | 74 | /* Test if a directory is empty. */ |
paul@256 | 75 | |
paul@256 | 76 | bool HostFileOpener::directory_is_empty(fileid_t fileid) |
paul@256 | 77 | { |
paul@256 | 78 | const char *path = _get_path(fileid); |
paul@256 | 79 | |
paul@256 | 80 | if (path == NULL) |
paul@256 | 81 | return false; |
paul@256 | 82 | |
paul@256 | 83 | DIR *dir = opendir(path); |
paul@256 | 84 | struct dirent *entry; |
paul@256 | 85 | |
paul@256 | 86 | if (dir == NULL) |
paul@256 | 87 | return false; |
paul@256 | 88 | |
paul@256 | 89 | while ((entry = readdir(dir)) != NULL) |
paul@256 | 90 | { |
paul@256 | 91 | if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) |
paul@256 | 92 | { |
paul@256 | 93 | closedir(dir); |
paul@256 | 94 | return false; |
paul@256 | 95 | } |
paul@256 | 96 | } |
paul@256 | 97 | |
paul@256 | 98 | closedir(dir); |
paul@256 | 99 | return true; |
paul@256 | 100 | } |
paul@256 | 101 | |
paul@12 | 102 | /* Return a file identifier for the given 'path'. */ |
paul@12 | 103 | |
paul@146 | 104 | long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@143 | 105 | { |
paul@143 | 106 | (void) flags; |
paul@143 | 107 | |
paul@146 | 108 | /* Test for a valid file. */ |
paul@143 | 109 | |
paul@143 | 110 | FILE *fp = fopen(path, "r"); |
paul@143 | 111 | |
paul@143 | 112 | if (fp == NULL) |
paul@146 | 113 | return -L4_ENOENT; |
paul@143 | 114 | |
paul@143 | 115 | fclose(fp); |
paul@143 | 116 | |
paul@146 | 117 | *fileid = _get_fileid(path, true); |
paul@146 | 118 | return L4_EOK; |
paul@143 | 119 | } |
paul@143 | 120 | |
paul@143 | 121 | fileid_t HostFileOpener::_get_fileid(const char *path, bool create) |
paul@12 | 122 | { |
paul@139 | 123 | std::lock_guard<std::mutex> guard(_lock); |
paul@139 | 124 | |
paul@139 | 125 | /* The inode number would be a good choice, but the L4Re read-only file |
paul@139 | 126 | implementation just provides the value of a counter! |
paul@139 | 127 | |
paul@139 | 128 | See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ |
paul@139 | 129 | |
paul@139 | 130 | std::string s(path); |
paul@12 | 131 | |
paul@139 | 132 | HostFileIdentifiers::iterator it = _fileids.find(s); |
paul@139 | 133 | |
paul@139 | 134 | if (it != _fileids.end()) |
paul@139 | 135 | return it->second; |
paul@12 | 136 | |
paul@143 | 137 | if (!create) |
paul@143 | 138 | return FILEID_INVALID; |
paul@143 | 139 | |
paul@139 | 140 | fileid_t fileid = _fileids.size(); |
paul@12 | 141 | |
paul@139 | 142 | _fileids[s] = fileid; |
paul@240 | 143 | _paths[fileid] = s; |
paul@139 | 144 | |
paul@139 | 145 | return fileid; |
paul@12 | 146 | } |
paul@12 | 147 | |
paul@240 | 148 | const char *HostFileOpener::_get_path(fileid_t fileid) |
paul@240 | 149 | { |
paul@240 | 150 | std::lock_guard<std::mutex> guard(_lock); |
paul@240 | 151 | |
paul@240 | 152 | HostFilePaths::iterator it = _paths.find(fileid); |
paul@240 | 153 | |
paul@240 | 154 | if (it != _paths.end()) |
paul@240 | 155 | return it->second.c_str(); |
paul@240 | 156 | else |
paul@240 | 157 | return NULL; |
paul@240 | 158 | } |
paul@240 | 159 | |
paul@12 | 160 | /* Return a new accessor for 'fileid'. */ |
paul@12 | 161 | |
paul@240 | 162 | long HostFileOpener::make_accessor(flags_t flags, fileid_t fileid, |
paul@240 | 163 | Accessor **accessor) |
paul@12 | 164 | { |
paul@143 | 165 | // NOTE: Not testing for create or write flags. |
paul@143 | 166 | |
paul@143 | 167 | (void) flags; |
paul@143 | 168 | |
paul@240 | 169 | const char *path = _get_path(fileid); |
paul@240 | 170 | |
paul@240 | 171 | if (path == NULL) |
paul@240 | 172 | return -L4_ENOENT; |
paul@240 | 173 | |
paul@106 | 174 | FILE *fp = fopen(path, "r"); |
paul@12 | 175 | |
paul@106 | 176 | if (fp == NULL) |
paul@143 | 177 | return -L4_ENOENT; |
paul@106 | 178 | |
paul@143 | 179 | *accessor = new HostFileAccessor(fp, fileid); |
paul@143 | 180 | return L4_EOK; |
paul@12 | 181 | } |
paul@12 | 182 | |
paul@202 | 183 | /* Return a directory accessor for 'fileid'. */ |
paul@202 | 184 | |
paul@240 | 185 | long HostFileOpener::make_directory_accessor(flags_t flags, fileid_t fileid, |
paul@202 | 186 | DirectoryAccessor **accessor) |
paul@202 | 187 | { |
paul@240 | 188 | (void) flags; |
paul@240 | 189 | |
paul@240 | 190 | const char *path = _get_path(fileid); |
paul@240 | 191 | |
paul@240 | 192 | if (path == NULL) |
paul@240 | 193 | return -L4_ENOENT; |
paul@202 | 194 | |
paul@202 | 195 | *accessor = new HostDirectoryAccessor(path); |
paul@202 | 196 | return L4_EOK; |
paul@202 | 197 | } |
paul@202 | 198 | |
paul@232 | 199 | /* Remove a filesystem object. */ |
paul@232 | 200 | |
paul@240 | 201 | long HostFileOpener::remove_object(fileid_t fileid) |
paul@232 | 202 | { |
paul@240 | 203 | (void) fileid; |
paul@232 | 204 | return L4_EOK; |
paul@232 | 205 | } |
paul@232 | 206 | |
paul@236 | 207 | /* Rename a filesystem object, placing source inside the parent of target. */ |
paul@236 | 208 | |
paul@240 | 209 | long HostFileOpener::rename_object(const char *source, const char *target) |
paul@236 | 210 | { |
paul@236 | 211 | /* NOTE: Should propagate a more meaningful error. */ |
paul@236 | 212 | |
paul@236 | 213 | if (rename(source, target)) |
paul@236 | 214 | return -L4_EIO; |
paul@236 | 215 | |
paul@236 | 216 | return L4_EOK; |
paul@236 | 217 | } |
paul@236 | 218 | |
paul@232 | 219 | /* Unlink a filesystem object. */ |
paul@232 | 220 | |
paul@240 | 221 | long HostFileOpener::unlink_object(fileid_t parent_fileid, fileid_t fileid) |
paul@232 | 222 | { |
paul@240 | 223 | /* Ignore the parent and always use the path. */ |
paul@240 | 224 | |
paul@240 | 225 | (void) parent_fileid; |
paul@232 | 226 | |
paul@240 | 227 | const char *path = _get_path(fileid); |
paul@240 | 228 | |
paul@240 | 229 | if (path == NULL) |
paul@240 | 230 | return -L4_ENOENT; |
paul@232 | 231 | |
paul@232 | 232 | // NOTE: Return code may need converting. |
paul@232 | 233 | |
paul@232 | 234 | int err = unlink(path); |
paul@232 | 235 | |
paul@232 | 236 | if (err) |
paul@232 | 237 | return err; |
paul@232 | 238 | |
paul@232 | 239 | /* Remove the file identifier mapping. */ |
paul@232 | 240 | |
paul@232 | 241 | std::string s(path); |
paul@232 | 242 | |
paul@232 | 243 | HostFileIdentifiers::iterator it = _fileids.find(s); |
paul@232 | 244 | |
paul@232 | 245 | if (it != _fileids.end()) |
paul@232 | 246 | _fileids.erase(it); |
paul@232 | 247 | |
paul@240 | 248 | HostFilePaths::iterator pit = _paths.find(fileid); |
paul@240 | 249 | |
paul@240 | 250 | if (pit != _paths.end()) |
paul@240 | 251 | _paths.erase(pit); |
paul@240 | 252 | |
paul@232 | 253 | return L4_EOK; |
paul@232 | 254 | } |
paul@232 | 255 | |
paul@12 | 256 | // vim: tabstop=4 expandtab shiftwidth=4 |