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 <sys/stat.h> |
paul@12 | 23 | |
paul@12 | 24 | #include "host_file_accessor.h" |
paul@12 | 25 | #include "host_file_opener.h" |
paul@12 | 26 | |
paul@12 | 27 | /* Return a file identifier for the given 'path'. */ |
paul@12 | 28 | |
paul@12 | 29 | fileid_t HostFileOpener::get_fileid(const char *path) |
paul@12 | 30 | { |
paul@12 | 31 | struct stat statbuf; |
paul@12 | 32 | |
paul@12 | 33 | /* Obtain the inode number. |
paul@12 | 34 | NOTE: This does not handle errors! */ |
paul@12 | 35 | |
paul@12 | 36 | stat(path, &statbuf); |
paul@12 | 37 | |
paul@12 | 38 | _paths.insert(FilePathEntry(statbuf.st_ino, path)); |
paul@12 | 39 | |
paul@12 | 40 | return statbuf.st_ino; |
paul@12 | 41 | } |
paul@12 | 42 | |
paul@12 | 43 | /* Return a new accessor for 'fileid'. */ |
paul@12 | 44 | |
paul@12 | 45 | Accessor *HostFileOpener::make_accessor(fileid_t fileid) |
paul@12 | 46 | { |
paul@12 | 47 | FilePaths::iterator found = _paths.find(fileid); |
paul@12 | 48 | |
paul@12 | 49 | if (found != _paths.end()) |
paul@12 | 50 | return new HostFileAccessor(found->second, fileid); |
paul@12 | 51 | else |
paul@12 | 52 | return NULL; |
paul@12 | 53 | } |
paul@12 | 54 | |
paul@12 | 55 | // vim: tabstop=4 expandtab shiftwidth=4 |