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