paul@93 | 1 | /* |
paul@93 | 2 | * An opener for a "host" file provided via the C library. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@160 | 22 | #include <thread> |
paul@160 | 23 | |
paul@160 | 24 | #include <dirent.h> |
paul@156 | 25 | #include <sys/stat.h> |
paul@156 | 26 | |
paul@160 | 27 | #include <fsclient/client.h> |
paul@160 | 28 | |
paul@12 | 29 | #include "host_file_accessor.h" |
paul@12 | 30 | #include "host_file_opener.h" |
paul@12 | 31 | |
paul@160 | 32 | |
paul@160 | 33 | |
paul@160 | 34 | /* Thread payload. */ |
paul@160 | 35 | |
paul@160 | 36 | static void read_directory(const char *path, file_t *writer) |
paul@160 | 37 | { |
paul@160 | 38 | DIR *dir = opendir(path); |
paul@160 | 39 | struct dirent *dirent; |
paul@160 | 40 | |
paul@160 | 41 | /* Subscribe to space and closure notifications on the pipe. */ |
paul@160 | 42 | |
paul@170 | 43 | long err = client_set_blocking(writer, NOTIFY_SPACE_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@160 | 44 | |
paul@160 | 45 | if (err) |
paul@160 | 46 | { |
paul@160 | 47 | client_close(writer); |
paul@160 | 48 | return; |
paul@160 | 49 | } |
paul@160 | 50 | |
paul@160 | 51 | /* Write directory entries to the pipe, closing the pipe when finished. */ |
paul@160 | 52 | |
paul@160 | 53 | while ((dirent = readdir(dir)) != NULL) |
paul@160 | 54 | { |
paul@170 | 55 | offset_t nwritten = client_write(writer, (const void *) dirent, dirent->d_reclen); |
paul@160 | 56 | |
paul@170 | 57 | /* Stop writing if the pipe is closed. */ |
paul@170 | 58 | |
paul@170 | 59 | if (nwritten < dirent->d_reclen) |
paul@170 | 60 | break; |
paul@160 | 61 | } |
paul@160 | 62 | |
paul@160 | 63 | client_close(writer); |
paul@160 | 64 | } |
paul@160 | 65 | |
paul@160 | 66 | |
paul@160 | 67 | |
paul@156 | 68 | HostFileOpener::~HostFileOpener() |
paul@156 | 69 | { |
paul@156 | 70 | } |
paul@156 | 71 | |
paul@156 | 72 | bool HostFileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 73 | { |
paul@156 | 74 | (void) flags; (void) fileid; |
paul@156 | 75 | struct stat st; |
paul@156 | 76 | |
paul@156 | 77 | if (stat(path, &st)) |
paul@156 | 78 | return false; |
paul@156 | 79 | |
paul@156 | 80 | return (st.st_mode & S_IFDIR) ? true : false; |
paul@156 | 81 | } |
paul@156 | 82 | |
paul@156 | 83 | bool HostFileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 84 | { |
paul@156 | 85 | (void) flags; (void) fileid; |
paul@156 | 86 | struct stat st; |
paul@156 | 87 | |
paul@156 | 88 | if (stat(path, &st)) |
paul@156 | 89 | return false; |
paul@156 | 90 | |
paul@156 | 91 | return (st.st_mode & S_IFREG) ? true : false; |
paul@156 | 92 | } |
paul@156 | 93 | |
paul@160 | 94 | long HostFileOpener::get_directory(const char *path, flags_t flags, fileid_t fileid, offset_t *size, l4_cap_idx_t *cap) |
paul@160 | 95 | { |
paul@160 | 96 | file_t *reader, *writer; |
paul@160 | 97 | |
paul@160 | 98 | // NOTE: Might be more appropriate to use lower-level file operations to |
paul@160 | 99 | // NOTE: avoid unnecessary mapping of the reader's memory region. |
paul@160 | 100 | |
paul@160 | 101 | long err = client_pipe(&reader, &writer); |
paul@160 | 102 | |
paul@160 | 103 | if (err) |
paul@160 | 104 | return err; |
paul@160 | 105 | |
paul@160 | 106 | *size = reader->size; |
paul@160 | 107 | *cap = reader->ref; |
paul@160 | 108 | |
paul@170 | 109 | /* Discard the reader structure but preserve the capability. */ |
paul@160 | 110 | |
paul@170 | 111 | reader->ref = L4_INVALID_CAP; |
paul@170 | 112 | file_close(reader); |
paul@160 | 113 | |
paul@160 | 114 | /* Spawn a independent thread for reading the directory details and writing |
paul@160 | 115 | them to the pipe. */ |
paul@160 | 116 | |
paul@160 | 117 | std::thread(read_directory, path, writer).detach(); |
paul@160 | 118 | |
paul@160 | 119 | return L4_EOK; |
paul@160 | 120 | } |
paul@160 | 121 | |
paul@12 | 122 | /* Return a file identifier for the given 'path'. */ |
paul@12 | 123 | |
paul@146 | 124 | long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@143 | 125 | { |
paul@143 | 126 | (void) flags; |
paul@143 | 127 | |
paul@146 | 128 | /* Test for a valid file. */ |
paul@143 | 129 | |
paul@143 | 130 | FILE *fp = fopen(path, "r"); |
paul@143 | 131 | |
paul@143 | 132 | if (fp == NULL) |
paul@146 | 133 | return -L4_ENOENT; |
paul@143 | 134 | |
paul@143 | 135 | fclose(fp); |
paul@143 | 136 | |
paul@146 | 137 | *fileid = _get_fileid(path, true); |
paul@146 | 138 | return L4_EOK; |
paul@143 | 139 | } |
paul@143 | 140 | |
paul@143 | 141 | fileid_t HostFileOpener::_get_fileid(const char *path, bool create) |
paul@12 | 142 | { |
paul@139 | 143 | std::lock_guard<std::mutex> guard(_lock); |
paul@139 | 144 | |
paul@139 | 145 | /* The inode number would be a good choice, but the L4Re read-only file |
paul@139 | 146 | implementation just provides the value of a counter! |
paul@139 | 147 | |
paul@139 | 148 | See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ |
paul@139 | 149 | |
paul@139 | 150 | std::string s(path); |
paul@12 | 151 | |
paul@139 | 152 | HostFileIdentifiers::iterator it = _fileids.find(s); |
paul@139 | 153 | |
paul@139 | 154 | if (it != _fileids.end()) |
paul@139 | 155 | return it->second; |
paul@12 | 156 | |
paul@143 | 157 | if (!create) |
paul@143 | 158 | return FILEID_INVALID; |
paul@143 | 159 | |
paul@139 | 160 | fileid_t fileid = _fileids.size(); |
paul@12 | 161 | |
paul@139 | 162 | _fileids[s] = fileid; |
paul@139 | 163 | |
paul@139 | 164 | return fileid; |
paul@12 | 165 | } |
paul@12 | 166 | |
paul@12 | 167 | /* Return a new accessor for 'fileid'. */ |
paul@12 | 168 | |
paul@143 | 169 | long HostFileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) |
paul@12 | 170 | { |
paul@143 | 171 | // NOTE: Not testing for create or write flags. |
paul@143 | 172 | |
paul@143 | 173 | (void) flags; |
paul@143 | 174 | |
paul@106 | 175 | FILE *fp = fopen(path, "r"); |
paul@12 | 176 | |
paul@106 | 177 | if (fp == NULL) |
paul@143 | 178 | return -L4_ENOENT; |
paul@106 | 179 | |
paul@143 | 180 | *accessor = new HostFileAccessor(fp, fileid); |
paul@143 | 181 | return L4_EOK; |
paul@12 | 182 | } |
paul@12 | 183 | |
paul@12 | 184 | // vim: tabstop=4 expandtab shiftwidth=4 |