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@12 | 22 | #include "host_file_accessor.h" |
paul@12 | 23 | #include "host_file_opener.h" |
paul@12 | 24 | |
paul@12 | 25 | /* Return a file identifier for the given 'path'. */ |
paul@12 | 26 | |
paul@12 | 27 | fileid_t HostFileOpener::get_fileid(const char *path) |
paul@12 | 28 | { |
paul@139 | 29 | std::lock_guard<std::mutex> guard(_lock); |
paul@139 | 30 | |
paul@139 | 31 | /* The inode number would be a good choice, but the L4Re read-only file |
paul@139 | 32 | implementation just provides the value of a counter! |
paul@139 | 33 | |
paul@139 | 34 | See: pkg/l4re-core/l4re_vfs/include/impl/ro_file_impl.h */ |
paul@139 | 35 | |
paul@139 | 36 | std::string s(path); |
paul@12 | 37 | |
paul@139 | 38 | HostFileIdentifiers::iterator it = _fileids.find(s); |
paul@139 | 39 | |
paul@139 | 40 | if (it != _fileids.end()) |
paul@139 | 41 | return it->second; |
paul@12 | 42 | |
paul@139 | 43 | fileid_t fileid = _fileids.size(); |
paul@12 | 44 | |
paul@139 | 45 | _fileids[s] = fileid; |
paul@139 | 46 | |
paul@139 | 47 | return fileid; |
paul@12 | 48 | } |
paul@12 | 49 | |
paul@12 | 50 | /* Return a new accessor for 'fileid'. */ |
paul@12 | 51 | |
paul@106 | 52 | Accessor *HostFileOpener::make_accessor(const char *path, fileid_t fileid) |
paul@12 | 53 | { |
paul@106 | 54 | FILE *fp = fopen(path, "r"); |
paul@12 | 55 | |
paul@106 | 56 | if (fp == NULL) |
paul@12 | 57 | return NULL; |
paul@106 | 58 | |
paul@106 | 59 | return new HostFileAccessor(fp, fileid); |
paul@12 | 60 | } |
paul@12 | 61 | |
paul@12 | 62 | // vim: tabstop=4 expandtab shiftwidth=4 |