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 <thread> 23 24 #include <dirent.h> 25 #include <sys/stat.h> 26 27 #include <fsclient/client.h> 28 29 #include "host_file_accessor.h" 30 #include "host_file_opener.h" 31 32 33 34 /* Thread payload. */ 35 36 static void read_directory(const char *path, file_t *writer) 37 { 38 DIR *dir = opendir(path); 39 struct dirent *dirent; 40 41 /* Subscribe to space and closure notifications on the pipe. */ 42 43 long err = client_set_blocking(writer, NOTIFY_SPACE_AVAILABLE | NOTIFY_PEER_CLOSED); 44 45 if (err) 46 { 47 client_close(writer); 48 return; 49 } 50 51 /* Write directory entries to the pipe, closing the pipe when finished. */ 52 53 while ((dirent = readdir(dir)) != NULL) 54 { 55 offset_t nwritten = client_write(writer, (const void *) dirent, dirent->d_reclen); 56 57 /* Stop writing if the pipe is closed. */ 58 59 if (nwritten < dirent->d_reclen) 60 break; 61 } 62 63 client_close(writer); 64 } 65 66 67 68 HostFileOpener::~HostFileOpener() 69 { 70 } 71 72 bool HostFileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) 73 { 74 (void) flags; (void) fileid; 75 struct stat st; 76 77 if (stat(path, &st)) 78 return false; 79 80 return (st.st_mode & S_IFDIR) ? true : false; 81 } 82 83 bool HostFileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) 84 { 85 (void) flags; (void) fileid; 86 struct stat st; 87 88 if (stat(path, &st)) 89 return false; 90 91 return (st.st_mode & S_IFREG) ? true : false; 92 } 93 94 long HostFileOpener::get_directory(const char *path, flags_t flags, fileid_t fileid, offset_t *size, l4_cap_idx_t *cap) 95 { 96 file_t *reader, *writer; 97 98 // NOTE: Might be more appropriate to use lower-level file operations to 99 // NOTE: avoid unnecessary mapping of the reader's memory region. 100 101 long err = client_pipe(&reader, &writer); 102 103 if (err) 104 return err; 105 106 *size = reader->size; 107 *cap = reader->ref; 108 109 /* Discard the reader structure but preserve the capability. */ 110 111 reader->ref = L4_INVALID_CAP; 112 file_close(reader); 113 114 /* Spawn a independent thread for reading the directory details and writing 115 them to the pipe. */ 116 117 std::thread(read_directory, path, writer).detach(); 118 119 return L4_EOK; 120 } 121 122 /* Return a file identifier for the given 'path'. */ 123 124 long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) 125 { 126 (void) flags; 127 128 /* Test for a valid file. */ 129 130 FILE *fp = fopen(path, "r"); 131 132 if (fp == NULL) 133 return -L4_ENOENT; 134 135 fclose(fp); 136 137 *fileid = _get_fileid(path, true); 138 return L4_EOK; 139 } 140 141 fileid_t HostFileOpener::_get_fileid(const char *path, bool create) 142 { 143 std::lock_guard<std::mutex> guard(_lock); 144 145 /* The inode number would be a good choice, but the L4Re read-only file 146 implementation just provides the value of a counter! 147 148 See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ 149 150 std::string s(path); 151 152 HostFileIdentifiers::iterator it = _fileids.find(s); 153 154 if (it != _fileids.end()) 155 return it->second; 156 157 if (!create) 158 return FILEID_INVALID; 159 160 fileid_t fileid = _fileids.size(); 161 162 _fileids[s] = fileid; 163 164 return fileid; 165 } 166 167 /* Return a new accessor for 'fileid'. */ 168 169 long HostFileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) 170 { 171 // NOTE: Not testing for create or write flags. 172 173 (void) flags; 174 175 FILE *fp = fopen(path, "r"); 176 177 if (fp == NULL) 178 return -L4_ENOENT; 179 180 *accessor = new HostFileAccessor(fp, fileid); 181 return L4_EOK; 182 } 183 184 // vim: tabstop=4 expandtab shiftwidth=4