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@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@156 | 41 | bool Ext2FileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 42 | { |
paul@156 | 43 | (void) path; (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@156 | 49 | bool Ext2FileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) |
paul@156 | 50 | { |
paul@156 | 51 | (void) path; (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@148 | 67 | *fileid = (fileid_t) ino; |
paul@148 | 68 | return L4_EOK; |
paul@147 | 69 | } |
paul@106 | 70 | |
paul@148 | 71 | *fileid = FILEID_INVALID; |
paul@148 | 72 | |
paul@148 | 73 | /* Create a missing file if possible. */ |
paul@148 | 74 | |
paul@156 | 75 | if ((flags & O_CREAT) && (flags & ~O_DIRECTORY)) |
paul@148 | 76 | { |
paul@148 | 77 | /* Determine whether only the leafname is left of the path, with |
paul@148 | 78 | the inode number referring to the parent directory. */ |
paul@148 | 79 | |
paul@148 | 80 | if (path_is_leafname(remaining)) |
paul@148 | 81 | { |
paul@148 | 82 | err = _ops->create_file(ino, remaining, _user, &ino); |
paul@148 | 83 | if (err) |
paul@148 | 84 | return err; |
paul@148 | 85 | |
paul@148 | 86 | *fileid = (fileid_t) ino; |
paul@148 | 87 | return L4_EOK; |
paul@148 | 88 | } |
paul@148 | 89 | } |
paul@148 | 90 | |
paul@148 | 91 | return -L4_ENOENT; |
paul@102 | 92 | } |
paul@102 | 93 | |
paul@102 | 94 | /* Return a new accessor for 'fileid'. */ |
paul@102 | 95 | |
paul@202 | 96 | long Ext2FileOpener::make_accessor(const char *path, flags_t flags, |
paul@202 | 97 | fileid_t fileid, Accessor **accessor) |
paul@102 | 98 | { |
paul@143 | 99 | (void) path; (void) flags; |
paul@102 | 100 | |
paul@106 | 101 | ext2_file_t file; |
paul@148 | 102 | long err = _ops->open_file((ext2_ino_t) fileid, &file); |
paul@143 | 103 | |
paul@148 | 104 | if (err) |
paul@148 | 105 | return err; |
paul@106 | 106 | |
paul@148 | 107 | *accessor = new Ext2FileAccessor(_ops, file, fileid); |
paul@143 | 108 | return L4_EOK; |
paul@102 | 109 | } |
paul@102 | 110 | |
paul@202 | 111 | /* Return a directory object reference for the given file identifier. */ |
paul@202 | 112 | |
paul@202 | 113 | long Ext2FileOpener::make_directory_accessor(const char *path, flags_t flags, |
paul@202 | 114 | fileid_t fileid, |
paul@202 | 115 | DirectoryAccessor **accessor) |
paul@202 | 116 | { |
paul@202 | 117 | (void) path; (void) flags; |
paul@202 | 118 | |
paul@202 | 119 | *accessor = new Ext2DirectoryAccessor(_ops, fileid); |
paul@202 | 120 | return L4_EOK; |
paul@202 | 121 | } |
paul@202 | 122 | |
paul@232 | 123 | /* Remove a filesystem object. */ |
paul@232 | 124 | |
paul@232 | 125 | long Ext2FileOpener::remove_object(const char *path, fileid_t fileid) |
paul@232 | 126 | { |
paul@232 | 127 | (void) path; |
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@236 | 134 | long Ext2FileOpener::rename_object(const char *source, fileid_t source_fileid, |
paul@236 | 135 | const char *target) |
paul@236 | 136 | { |
paul@236 | 137 | (void) source; |
paul@236 | 138 | |
paul@236 | 139 | fileid_t source_parent, target_parent; |
paul@236 | 140 | long err; |
paul@236 | 141 | |
paul@236 | 142 | err = get_parent(source, &source_parent); |
paul@236 | 143 | |
paul@236 | 144 | if (err) |
paul@236 | 145 | return err; |
paul@236 | 146 | |
paul@236 | 147 | err = get_parent(target, &target_parent); |
paul@236 | 148 | |
paul@236 | 149 | if (err) |
paul@236 | 150 | return err; |
paul@236 | 151 | |
paul@236 | 152 | return _ops->rename((ext2_ino_t) source_fileid, |
paul@236 | 153 | (ext2_ino_t) source_parent, path_basename(source), |
paul@236 | 154 | (ext2_ino_t) target_parent, path_basename(target)); |
paul@236 | 155 | } |
paul@236 | 156 | |
paul@232 | 157 | /* Unlink a filesystem object. */ |
paul@232 | 158 | |
paul@232 | 159 | long Ext2FileOpener::unlink_object(const char *path, fileid_t fileid) |
paul@232 | 160 | { |
paul@232 | 161 | fileid_t parent; |
paul@232 | 162 | long err = get_parent(path, &parent); |
paul@232 | 163 | |
paul@232 | 164 | if (err) |
paul@232 | 165 | return err; |
paul@232 | 166 | |
paul@232 | 167 | return _ops->unlink((ext2_ino_t) parent, (ext2_ino_t) fileid); |
paul@232 | 168 | } |
paul@232 | 169 | |
paul@102 | 170 | // vim: tabstop=4 expandtab shiftwidth=4 |