paul@202 | 1 | /* |
paul@202 | 2 | * An object for a "host" directory provided via the C library. |
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 <dirent.h> |
paul@216 | 23 | #include <stdlib.h> |
paul@216 | 24 | #include <string.h> |
paul@202 | 25 | |
paul@202 | 26 | #include <fsclient/client.h> |
paul@202 | 27 | |
paul@202 | 28 | #include "host_directory_accessor.h" |
paul@202 | 29 | |
paul@202 | 30 | |
paul@202 | 31 | |
paul@216 | 32 | /* Initialise the accessor, copying the path. */ |
paul@216 | 33 | |
paul@202 | 34 | HostDirectoryAccessor::HostDirectoryAccessor(const char *path) |
paul@202 | 35 | { |
paul@216 | 36 | _path = strdup(path); |
paul@202 | 37 | } |
paul@202 | 38 | |
paul@216 | 39 | /* Release the copied path upon deletion. */ |
paul@216 | 40 | |
paul@202 | 41 | HostDirectoryAccessor::~HostDirectoryAccessor() |
paul@202 | 42 | { |
paul@216 | 43 | if (_path != NULL) |
paul@216 | 44 | free(_path); |
paul@202 | 45 | } |
paul@202 | 46 | |
paul@216 | 47 | /* Write directory entries to the given 'writer'. */ |
paul@216 | 48 | |
paul@202 | 49 | void HostDirectoryAccessor::read_directory(file_t *writer) |
paul@202 | 50 | { |
paul@202 | 51 | DIR *dir = opendir(_path); |
paul@202 | 52 | struct dirent *dirent; |
paul@202 | 53 | |
paul@202 | 54 | /* Write directory entries to the pipe, closing the pipe when finished. */ |
paul@202 | 55 | |
paul@202 | 56 | while ((dirent = readdir(dir)) != NULL) |
paul@202 | 57 | { |
paul@202 | 58 | offset_t nwritten = client_write(writer, (const void *) dirent, dirent->d_reclen); |
paul@202 | 59 | |
paul@202 | 60 | /* Stop writing if the pipe is closed. */ |
paul@202 | 61 | |
paul@202 | 62 | if (nwritten < dirent->d_reclen) |
paul@202 | 63 | break; |
paul@202 | 64 | } |
paul@202 | 65 | } |
paul@202 | 66 | |
paul@202 | 67 | // vim: tabstop=4 expandtab shiftwidth=4 |