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); 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 = 0; 56 57 while (nwritten < dirent->d_reclen) 58 nwritten += client_write(writer, (const void *) (dirent + nwritten), dirent->d_reclen - nwritten); 59 } 60 61 client_close(writer); 62 } 63 64 65 66 HostFileOpener::~HostFileOpener() 67 { 68 } 69 70 bool HostFileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) 71 { 72 (void) flags; (void) fileid; 73 struct stat st; 74 75 if (stat(path, &st)) 76 return false; 77 78 return (st.st_mode & S_IFDIR) ? true : false; 79 } 80 81 bool HostFileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) 82 { 83 (void) flags; (void) fileid; 84 struct stat st; 85 86 if (stat(path, &st)) 87 return false; 88 89 return (st.st_mode & S_IFREG) ? true : false; 90 } 91 92 long HostFileOpener::get_directory(const char *path, flags_t flags, fileid_t fileid, offset_t *size, l4_cap_idx_t *cap) 93 { 94 file_t *reader, *writer; 95 96 // NOTE: Might be more appropriate to use lower-level file operations to 97 // NOTE: avoid unnecessary mapping of the reader's memory region. 98 99 long err = client_pipe(&reader, &writer); 100 101 if (err) 102 return err; 103 104 *size = reader->size; 105 *cap = reader->ref; 106 107 /* Discard the reader structure but do not close the reader itself. */ 108 109 delete reader; 110 111 /* Spawn a independent thread for reading the directory details and writing 112 them to the pipe. */ 113 114 std::thread(read_directory, path, writer).detach(); 115 116 return L4_EOK; 117 } 118 119 /* Return a file identifier for the given 'path'. */ 120 121 long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) 122 { 123 (void) flags; 124 125 /* Test for a valid file. */ 126 127 FILE *fp = fopen(path, "r"); 128 129 if (fp == NULL) 130 return -L4_ENOENT; 131 132 fclose(fp); 133 134 *fileid = _get_fileid(path, true); 135 return L4_EOK; 136 } 137 138 fileid_t HostFileOpener::_get_fileid(const char *path, bool create) 139 { 140 std::lock_guard<std::mutex> guard(_lock); 141 142 /* The inode number would be a good choice, but the L4Re read-only file 143 implementation just provides the value of a counter! 144 145 See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ 146 147 std::string s(path); 148 149 HostFileIdentifiers::iterator it = _fileids.find(s); 150 151 if (it != _fileids.end()) 152 return it->second; 153 154 if (!create) 155 return FILEID_INVALID; 156 157 fileid_t fileid = _fileids.size(); 158 159 _fileids[s] = fileid; 160 161 return fileid; 162 } 163 164 /* Return a new accessor for 'fileid'. */ 165 166 long HostFileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) 167 { 168 // NOTE: Not testing for create or write flags. 169 170 (void) flags; 171 172 FILE *fp = fopen(path, "r"); 173 174 if (fp == NULL) 175 return -L4_ENOENT; 176 177 *accessor = new HostFileAccessor(fp, fileid); 178 return L4_EOK; 179 } 180 181 // vim: tabstop=4 expandtab shiftwidth=4