paul@102 | 1 | /* |
paul@106 | 2 | * An opener for a file provided by an Ext2-compatible filesystem. |
paul@102 | 3 | * |
paul@239 | 4 | * Copyright (C) 2021, 2022 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@168 | 22 | #include <string.h> |
paul@168 | 23 | |
paul@147 | 24 | #include <e2access/path.h> |
paul@168 | 25 | #include <fsclient/client.h> |
paul@147 | 26 | #include <systypes/fcntl.h> |
paul@106 | 27 | |
paul@202 | 28 | #include "ext2_directory_accessor.h" |
paul@106 | 29 | #include "ext2_file_accessor.h" |
paul@106 | 30 | #include "ext2_file_opener.h" |
paul@202 | 31 | #include "resource_server.h" |
paul@168 | 32 | |
paul@168 | 33 | |
paul@168 | 34 | |
paul@156 | 35 | Ext2FileOpener::~Ext2FileOpener() |
paul@156 | 36 | { |
paul@156 | 37 | } |
paul@156 | 38 | |
paul@156 | 39 | /* Test if a directory is being accessed. */ |
paul@156 | 40 | |
paul@240 | 41 | bool Ext2FileOpener::accessing_directory(flags_t flags, fileid_t fileid) |
paul@156 | 42 | { |
paul@240 | 43 | (void) flags; |
paul@156 | 44 | return _ops->is_directory((ext2_ino_t) fileid); |
paul@156 | 45 | } |
paul@156 | 46 | |
paul@156 | 47 | /* Test if a file is being accessed. */ |
paul@156 | 48 | |
paul@240 | 49 | bool Ext2FileOpener::accessing_file(flags_t flags, fileid_t fileid) |
paul@156 | 50 | { |
paul@240 | 51 | (void) flags; |
paul@156 | 52 | return _ops->is_file((ext2_ino_t) fileid); |
paul@156 | 53 | } |
paul@156 | 54 | |
paul@102 | 55 | /* Return a file identifier for the given 'path'. */ |
paul@102 | 56 | |
paul@146 | 57 | long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@102 | 58 | { |
paul@106 | 59 | ext2_ino_t ino; |
paul@148 | 60 | const char *remaining; |
paul@148 | 61 | long err = _ops->find_file(path, &ino, &remaining); |
paul@147 | 62 | |
paul@148 | 63 | /* Return an existing file. */ |
paul@147 | 64 | |
paul@148 | 65 | if (!err) |
paul@148 | 66 | { |
paul@239 | 67 | if (!_ops->can_access(_user, flags, ino)) |
paul@239 | 68 | return -L4_EPERM; |
paul@239 | 69 | |
paul@148 | 70 | *fileid = (fileid_t) ino; |
paul@148 | 71 | return L4_EOK; |
paul@147 | 72 | } |
paul@106 | 73 | |
paul@148 | 74 | *fileid = FILEID_INVALID; |
paul@148 | 75 | |
paul@148 | 76 | /* Create a missing file if possible. */ |
paul@148 | 77 | |
paul@156 | 78 | if ((flags & O_CREAT) && (flags & ~O_DIRECTORY)) |
paul@148 | 79 | { |
paul@148 | 80 | /* Determine whether only the leafname is left of the path, with |
paul@148 | 81 | the inode number referring to the parent directory. */ |
paul@148 | 82 | |
paul@148 | 83 | if (path_is_leafname(remaining)) |
paul@148 | 84 | { |
paul@148 | 85 | err = _ops->create_file(ino, remaining, _user, &ino); |
paul@148 | 86 | if (err) |
paul@148 | 87 | return err; |
paul@148 | 88 | |
paul@148 | 89 | *fileid = (fileid_t) ino; |
paul@148 | 90 | return L4_EOK; |
paul@148 | 91 | } |
paul@148 | 92 | } |
paul@148 | 93 | |
paul@148 | 94 | return -L4_ENOENT; |
paul@102 | 95 | } |
paul@102 | 96 | |
paul@102 | 97 | /* Return a new accessor for 'fileid'. */ |
paul@102 | 98 | |
paul@240 | 99 | long Ext2FileOpener::make_accessor(flags_t flags, fileid_t fileid, |
paul@240 | 100 | Accessor **accessor) |
paul@102 | 101 | { |
paul@240 | 102 | (void) flags; |
paul@102 | 103 | |
paul@106 | 104 | ext2_file_t file; |
paul@148 | 105 | long err = _ops->open_file((ext2_ino_t) fileid, &file); |
paul@143 | 106 | |
paul@148 | 107 | if (err) |
paul@148 | 108 | return err; |
paul@106 | 109 | |
paul@148 | 110 | *accessor = new Ext2FileAccessor(_ops, file, fileid); |
paul@143 | 111 | return L4_EOK; |
paul@102 | 112 | } |
paul@102 | 113 | |
paul@202 | 114 | /* Return a directory object reference for the given file identifier. */ |
paul@202 | 115 | |
paul@240 | 116 | long Ext2FileOpener::make_directory_accessor(flags_t flags, fileid_t fileid, |
paul@202 | 117 | DirectoryAccessor **accessor) |
paul@202 | 118 | { |
paul@240 | 119 | (void) flags; |
paul@202 | 120 | |
paul@202 | 121 | *accessor = new Ext2DirectoryAccessor(_ops, fileid); |
paul@202 | 122 | return L4_EOK; |
paul@202 | 123 | } |
paul@202 | 124 | |
paul@232 | 125 | /* Remove a filesystem object. */ |
paul@232 | 126 | |
paul@240 | 127 | long Ext2FileOpener::remove_object(fileid_t fileid) |
paul@232 | 128 | { |
paul@232 | 129 | return _ops->remove((ext2_ino_t) fileid); |
paul@232 | 130 | } |
paul@232 | 131 | |
paul@236 | 132 | /* Rename a filesystem object, placing source inside the parent of target. */ |
paul@236 | 133 | |
paul@240 | 134 | long Ext2FileOpener::rename_object(const char *source, const char *target) |
paul@236 | 135 | { |
paul@240 | 136 | fileid_t source_fileid, source_parent, target_parent; |
paul@240 | 137 | long err; |
paul@236 | 138 | |
paul@240 | 139 | err = get_fileid(source, 0, &source_fileid); |
paul@240 | 140 | |
paul@240 | 141 | if (err) |
paul@240 | 142 | return err; |
paul@236 | 143 | |
paul@236 | 144 | err = get_parent(source, &source_parent); |
paul@236 | 145 | |
paul@236 | 146 | if (err) |
paul@236 | 147 | return err; |
paul@236 | 148 | |
paul@236 | 149 | err = get_parent(target, &target_parent); |
paul@236 | 150 | |
paul@236 | 151 | if (err) |
paul@236 | 152 | return err; |
paul@236 | 153 | |
paul@236 | 154 | return _ops->rename((ext2_ino_t) source_fileid, |
paul@236 | 155 | (ext2_ino_t) source_parent, path_basename(source), |
paul@236 | 156 | (ext2_ino_t) target_parent, path_basename(target)); |
paul@236 | 157 | } |
paul@236 | 158 | |
paul@232 | 159 | /* Unlink a filesystem object. */ |
paul@232 | 160 | |
paul@240 | 161 | long Ext2FileOpener::unlink_object(fileid_t parent_fileid, fileid_t fileid) |
paul@232 | 162 | { |
paul@240 | 163 | return _ops->unlink((ext2_ino_t) parent_fileid, (ext2_ino_t) fileid); |
paul@232 | 164 | } |
paul@232 | 165 | |
paul@102 | 166 | // vim: tabstop=4 expandtab shiftwidth=4 |