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