paul@102 | 1 | /* |
paul@106 | 2 | * An opener for a file provided by an Ext2-compatible filesystem. |
paul@102 | 3 | * |
paul@102 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@102 | 5 | * |
paul@102 | 6 | * This program is free software; you can redistribute it and/or |
paul@102 | 7 | * modify it under the terms of the GNU General Public License as |
paul@102 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@102 | 9 | * the License, or (at your option) any later version. |
paul@102 | 10 | * |
paul@102 | 11 | * This program is distributed in the hope that it will be useful, |
paul@102 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@102 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@102 | 14 | * GNU General Public License for more details. |
paul@102 | 15 | * |
paul@102 | 16 | * You should have received a copy of the GNU General Public License |
paul@102 | 17 | * along with this program; if not, write to the Free Software |
paul@102 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@102 | 19 | * Boston, MA 02110-1301, USA |
paul@102 | 20 | */ |
paul@102 | 21 | |
paul@147 | 22 | #include <e2access/path.h> |
paul@147 | 23 | |
paul@147 | 24 | #include <systypes/fcntl.h> |
paul@106 | 25 | |
paul@106 | 26 | #include "ext2_file_accessor.h" |
paul@106 | 27 | #include "ext2_file_opener.h" |
paul@102 | 28 | |
paul@156 | 29 | Ext2FileOpener::~Ext2FileOpener() |
paul@156 | 30 | { |
paul@156 | 31 | } |
paul@156 | 32 | |
paul@156 | 33 | /* Test if a directory is being accessed. */ |
paul@156 | 34 | |
paul@156 | 35 | bool Ext2FileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 36 | { |
paul@156 | 37 | (void) path; (void) flags; |
paul@156 | 38 | return _ops->is_directory((ext2_ino_t) fileid); |
paul@156 | 39 | } |
paul@156 | 40 | |
paul@156 | 41 | /* Test if a file is being accessed. */ |
paul@156 | 42 | |
paul@156 | 43 | bool Ext2FileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 44 | { |
paul@156 | 45 | (void) path; (void) flags; |
paul@156 | 46 | return _ops->is_file((ext2_ino_t) fileid); |
paul@156 | 47 | } |
paul@156 | 48 | |
paul@102 | 49 | /* Return a file identifier for the given 'path'. */ |
paul@102 | 50 | |
paul@146 | 51 | long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@102 | 52 | { |
paul@106 | 53 | ext2_ino_t ino; |
paul@148 | 54 | const char *remaining; |
paul@148 | 55 | long err = _ops->find_file(path, &ino, &remaining); |
paul@147 | 56 | |
paul@148 | 57 | /* Return an existing file. */ |
paul@147 | 58 | |
paul@148 | 59 | if (!err) |
paul@148 | 60 | { |
paul@148 | 61 | *fileid = (fileid_t) ino; |
paul@148 | 62 | return L4_EOK; |
paul@147 | 63 | } |
paul@106 | 64 | |
paul@148 | 65 | *fileid = FILEID_INVALID; |
paul@148 | 66 | |
paul@148 | 67 | /* Create a missing file if possible. */ |
paul@148 | 68 | |
paul@156 | 69 | if ((flags & O_CREAT) && (flags & ~O_DIRECTORY)) |
paul@148 | 70 | { |
paul@148 | 71 | /* Determine whether only the leafname is left of the path, with |
paul@148 | 72 | the inode number referring to the parent directory. */ |
paul@148 | 73 | |
paul@148 | 74 | if (path_is_leafname(remaining)) |
paul@148 | 75 | { |
paul@148 | 76 | err = _ops->create_file(ino, remaining, _user, &ino); |
paul@148 | 77 | if (err) |
paul@148 | 78 | return err; |
paul@148 | 79 | |
paul@148 | 80 | *fileid = (fileid_t) ino; |
paul@148 | 81 | return L4_EOK; |
paul@148 | 82 | } |
paul@148 | 83 | } |
paul@148 | 84 | |
paul@148 | 85 | return -L4_ENOENT; |
paul@102 | 86 | } |
paul@102 | 87 | |
paul@102 | 88 | /* Return a new accessor for 'fileid'. */ |
paul@102 | 89 | |
paul@143 | 90 | long Ext2FileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) |
paul@102 | 91 | { |
paul@143 | 92 | (void) path; (void) flags; |
paul@102 | 93 | |
paul@106 | 94 | ext2_file_t file; |
paul@148 | 95 | long err = _ops->open_file((ext2_ino_t) fileid, &file); |
paul@143 | 96 | |
paul@148 | 97 | if (err) |
paul@148 | 98 | return err; |
paul@106 | 99 | |
paul@148 | 100 | *accessor = new Ext2FileAccessor(_ops, file, fileid); |
paul@143 | 101 | return L4_EOK; |
paul@102 | 102 | } |
paul@102 | 103 | |
paul@102 | 104 | // vim: tabstop=4 expandtab shiftwidth=4 |