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@156 | 22 | #include <sys/stat.h> |
paul@156 | 23 | |
paul@12 | 24 | #include "host_file_accessor.h" |
paul@12 | 25 | #include "host_file_opener.h" |
paul@12 | 26 | |
paul@156 | 27 | HostFileOpener::~HostFileOpener() |
paul@156 | 28 | { |
paul@156 | 29 | } |
paul@156 | 30 | |
paul@156 | 31 | bool HostFileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 32 | { |
paul@156 | 33 | (void) flags; (void) fileid; |
paul@156 | 34 | struct stat st; |
paul@156 | 35 | |
paul@156 | 36 | if (stat(path, &st)) |
paul@156 | 37 | return false; |
paul@156 | 38 | |
paul@156 | 39 | return (st.st_mode & S_IFDIR) ? true : false; |
paul@156 | 40 | } |
paul@156 | 41 | |
paul@156 | 42 | bool HostFileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 43 | { |
paul@156 | 44 | (void) flags; (void) fileid; |
paul@156 | 45 | struct stat st; |
paul@156 | 46 | |
paul@156 | 47 | if (stat(path, &st)) |
paul@156 | 48 | return false; |
paul@156 | 49 | |
paul@156 | 50 | return (st.st_mode & S_IFREG) ? true : false; |
paul@156 | 51 | } |
paul@156 | 52 | |
paul@12 | 53 | /* Return a file identifier for the given 'path'. */ |
paul@12 | 54 | |
paul@146 | 55 | long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@143 | 56 | { |
paul@143 | 57 | (void) flags; |
paul@143 | 58 | |
paul@146 | 59 | /* Test for a valid file. */ |
paul@143 | 60 | |
paul@143 | 61 | FILE *fp = fopen(path, "r"); |
paul@143 | 62 | |
paul@143 | 63 | if (fp == NULL) |
paul@146 | 64 | return -L4_ENOENT; |
paul@143 | 65 | |
paul@143 | 66 | fclose(fp); |
paul@143 | 67 | |
paul@146 | 68 | *fileid = _get_fileid(path, true); |
paul@146 | 69 | return L4_EOK; |
paul@143 | 70 | } |
paul@143 | 71 | |
paul@143 | 72 | fileid_t HostFileOpener::_get_fileid(const char *path, bool create) |
paul@12 | 73 | { |
paul@139 | 74 | std::lock_guard<std::mutex> guard(_lock); |
paul@139 | 75 | |
paul@139 | 76 | /* The inode number would be a good choice, but the L4Re read-only file |
paul@139 | 77 | implementation just provides the value of a counter! |
paul@139 | 78 | |
paul@139 | 79 | See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ |
paul@139 | 80 | |
paul@139 | 81 | std::string s(path); |
paul@12 | 82 | |
paul@139 | 83 | HostFileIdentifiers::iterator it = _fileids.find(s); |
paul@139 | 84 | |
paul@139 | 85 | if (it != _fileids.end()) |
paul@139 | 86 | return it->second; |
paul@12 | 87 | |
paul@143 | 88 | if (!create) |
paul@143 | 89 | return FILEID_INVALID; |
paul@143 | 90 | |
paul@139 | 91 | fileid_t fileid = _fileids.size(); |
paul@12 | 92 | |
paul@139 | 93 | _fileids[s] = fileid; |
paul@139 | 94 | |
paul@139 | 95 | return fileid; |
paul@12 | 96 | } |
paul@12 | 97 | |
paul@12 | 98 | /* Return a new accessor for 'fileid'. */ |
paul@12 | 99 | |
paul@143 | 100 | long HostFileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) |
paul@12 | 101 | { |
paul@143 | 102 | // NOTE: Not testing for create or write flags. |
paul@143 | 103 | |
paul@143 | 104 | (void) flags; |
paul@143 | 105 | |
paul@106 | 106 | FILE *fp = fopen(path, "r"); |
paul@12 | 107 | |
paul@106 | 108 | if (fp == NULL) |
paul@143 | 109 | return -L4_ENOENT; |
paul@106 | 110 | |
paul@143 | 111 | *accessor = new HostFileAccessor(fp, fileid); |
paul@143 | 112 | return L4_EOK; |
paul@12 | 113 | } |
paul@12 | 114 | |
paul@12 | 115 | // vim: tabstop=4 expandtab shiftwidth=4 |