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, 95 fileid_t fileid, offset_t *size, 96 l4_cap_idx_t *cap, object_flags_t *object_flags) 97 { 98 /* The path is used to obtain the directory. */ 99 100 (void) flags; (void) fileid; 101 102 file_t *reader, *writer; 103 104 // NOTE: Might be more appropriate to use lower-level file operations to 105 // NOTE: avoid unnecessary mapping of the reader's memory region. 106 107 long err = client_pipe(&reader, &writer); 108 109 if (err) 110 return err; 111 112 *size = reader->size; 113 *cap = reader->ref; 114 *object_flags = 0; /* does not support mmap, has no fixed size */ 115 116 /* Discard the reader structure but preserve the capability. */ 117 118 reader->ref = L4_INVALID_CAP; 119 file_close(reader); 120 121 /* Spawn a independent thread for reading the directory details and writing 122 them to the pipe. */ 123 124 std::thread(read_directory, path, writer).detach(); 125 126 return L4_EOK; 127 } 128 129 /* Return a file identifier for the given 'path'. */ 130 131 long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) 132 { 133 (void) flags; 134 135 /* Test for a valid file. */ 136 137 FILE *fp = fopen(path, "r"); 138 139 if (fp == NULL) 140 return -L4_ENOENT; 141 142 fclose(fp); 143 144 *fileid = _get_fileid(path, true); 145 return L4_EOK; 146 } 147 148 fileid_t HostFileOpener::_get_fileid(const char *path, bool create) 149 { 150 std::lock_guard<std::mutex> guard(_lock); 151 152 /* The inode number would be a good choice, but the L4Re read-only file 153 implementation just provides the value of a counter! 154 155 See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ 156 157 std::string s(path); 158 159 HostFileIdentifiers::iterator it = _fileids.find(s); 160 161 if (it != _fileids.end()) 162 return it->second; 163 164 if (!create) 165 return FILEID_INVALID; 166 167 fileid_t fileid = _fileids.size(); 168 169 _fileids[s] = fileid; 170 171 return fileid; 172 } 173 174 /* Return a new accessor for 'fileid'. */ 175 176 long HostFileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) 177 { 178 // NOTE: Not testing for create or write flags. 179 180 (void) flags; 181 182 FILE *fp = fopen(path, "r"); 183 184 if (fp == NULL) 185 return -L4_ENOENT; 186 187 *accessor = new HostFileAccessor(fp, fileid); 188 return L4_EOK; 189 } 190 191 // vim: tabstop=4 expandtab shiftwidth=4