paul@202 | 1 | /* |
paul@202 | 2 | * A resource offering support for accessing directories. |
paul@202 | 3 | * |
paul@202 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@202 | 5 | * |
paul@202 | 6 | * This program is free software; you can redistribute it and/or |
paul@202 | 7 | * modify it under the terms of the GNU General Public License as |
paul@202 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@202 | 9 | * the License, or (at your option) any later version. |
paul@202 | 10 | * |
paul@202 | 11 | * This program is distributed in the hope that it will be useful, |
paul@202 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@202 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@202 | 14 | * GNU General Public License for more details. |
paul@202 | 15 | * |
paul@202 | 16 | * You should have received a copy of the GNU General Public License |
paul@202 | 17 | * along with this program; if not, write to the Free Software |
paul@202 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@202 | 19 | * Boston, MA 02110-1301, USA |
paul@202 | 20 | */ |
paul@202 | 21 | |
paul@202 | 22 | #include <thread> |
paul@202 | 23 | |
paul@202 | 24 | #include <fsclient/client.h> |
paul@202 | 25 | #include <ipc/cap_alloc.h> |
paul@202 | 26 | |
paul@202 | 27 | #include "directory_resource.h" |
paul@202 | 28 | #include "directory_object_server.h" |
paul@202 | 29 | |
paul@202 | 30 | |
paul@202 | 31 | |
paul@202 | 32 | DirectoryResource::DirectoryResource(fileid_t fileid, |
paul@202 | 33 | DirectoryProvider *provider, |
paul@202 | 34 | FileRegistry *registry) |
paul@202 | 35 | : _registry(registry), _provider(provider), fileid(fileid) |
paul@202 | 36 | { |
paul@202 | 37 | } |
paul@202 | 38 | |
paul@202 | 39 | DirectoryResource::~DirectoryResource() |
paul@202 | 40 | { |
paul@202 | 41 | } |
paul@202 | 42 | |
paul@202 | 43 | |
paul@202 | 44 | |
paul@202 | 45 | int DirectoryResource::expected_items() |
paul@202 | 46 | { |
paul@202 | 47 | return DirectoryObject_expected_items; |
paul@202 | 48 | } |
paul@202 | 49 | |
paul@202 | 50 | ipc_server_handler_type DirectoryResource::handler() |
paul@202 | 51 | { |
paul@202 | 52 | return (ipc_server_handler_type) handle_DirectoryObject; |
paul@202 | 53 | } |
paul@202 | 54 | |
paul@202 | 55 | |
paul@202 | 56 | |
paul@202 | 57 | /* Close the resource, removing the provider from the registry if |
paul@202 | 58 | appropriate. */ |
paul@202 | 59 | |
paul@202 | 60 | void DirectoryResource::close() |
paul@202 | 61 | { |
paul@202 | 62 | /* Detach the resource, potentially removing the file provider. */ |
paul@202 | 63 | |
paul@202 | 64 | _registry->detach(fileid, _provider); |
paul@202 | 65 | } |
paul@202 | 66 | |
paul@202 | 67 | |
paul@202 | 68 | |
paul@202 | 69 | /* Thread payload for directory listing production. */ |
paul@202 | 70 | |
paul@202 | 71 | static void read_directory(FileRegistry *registry, DirectoryProvider *provider, |
paul@202 | 72 | fileid_t fileid, file_t *writer) |
paul@202 | 73 | { |
paul@202 | 74 | DirectoryAccessor *accessor = provider->accessor(); |
paul@202 | 75 | |
paul@202 | 76 | /* Attach to the provider to keep it available, even if the directory |
paul@202 | 77 | resource is released. */ |
paul@202 | 78 | |
paul@202 | 79 | provider->attach(); |
paul@202 | 80 | |
paul@202 | 81 | /* Invoke the directory reading mechanism. */ |
paul@202 | 82 | |
paul@202 | 83 | accessor->read_directory(writer); |
paul@202 | 84 | |
paul@202 | 85 | /* Close and detach from the registry. */ |
paul@202 | 86 | |
paul@202 | 87 | client_close(writer); |
paul@202 | 88 | registry->detach(fileid, provider); |
paul@202 | 89 | } |
paul@202 | 90 | |
paul@202 | 91 | |
paul@202 | 92 | |
paul@202 | 93 | /* Open a directory to read the directory entries via a file-like object. */ |
paul@202 | 94 | |
paul@202 | 95 | long DirectoryResource::opendir(offset_t *size, l4_cap_idx_t *file, |
paul@202 | 96 | object_flags_t *object_flags) |
paul@202 | 97 | { |
paul@202 | 98 | file_t *reader, *writer; |
paul@202 | 99 | |
paul@202 | 100 | /* Mapping of the reader's memory region should be avoided because no use |
paul@202 | 101 | of the reader will be made here. */ |
paul@202 | 102 | |
paul@202 | 103 | long err = client_pipe(&reader, &writer, 0); |
paul@202 | 104 | |
paul@202 | 105 | if (err) |
paul@202 | 106 | return err; |
paul@202 | 107 | |
paul@202 | 108 | *size = reader->size; |
paul@202 | 109 | *file = reader->ref; |
paul@202 | 110 | *object_flags = 0; /* does not support mmap, has no fixed size */ |
paul@202 | 111 | |
paul@202 | 112 | /* Discard the reader structure but preserve the capability. */ |
paul@202 | 113 | |
paul@202 | 114 | reader->ref = L4_INVALID_CAP; |
paul@202 | 115 | file_close(reader); |
paul@202 | 116 | |
paul@202 | 117 | /* Initiate the reading process. */ |
paul@202 | 118 | |
paul@202 | 119 | std::thread(read_directory, _registry, _provider, fileid, writer).detach(); |
paul@202 | 120 | |
paul@202 | 121 | /* Send the capability immediately, discarding it afterwards. */ |
paul@202 | 122 | |
paul@202 | 123 | complete_Directory_opendir(*size, *file, *object_flags); |
paul@202 | 124 | ipc_cap_free_um(*file); |
paul@202 | 125 | |
paul@202 | 126 | return IPC_MESSAGE_SENT; |
paul@202 | 127 | } |
paul@202 | 128 | |
paul@202 | 129 | // vim: tabstop=4 expandtab shiftwidth=4 |