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@102 | 29 | /* Return a file identifier for the given 'path'. */ |
paul@102 | 30 | |
paul@146 | 31 | long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@102 | 32 | { |
paul@106 | 33 | ext2_ino_t ino; |
paul@148 | 34 | const char *remaining; |
paul@148 | 35 | long err = _ops->find_file(path, &ino, &remaining); |
paul@147 | 36 | |
paul@148 | 37 | /* Return an existing file. */ |
paul@147 | 38 | |
paul@148 | 39 | if (!err) |
paul@148 | 40 | { |
paul@148 | 41 | *fileid = (fileid_t) ino; |
paul@148 | 42 | return L4_EOK; |
paul@147 | 43 | } |
paul@106 | 44 | |
paul@148 | 45 | *fileid = FILEID_INVALID; |
paul@148 | 46 | |
paul@148 | 47 | /* Create a missing file if possible. */ |
paul@148 | 48 | |
paul@148 | 49 | if (flags & O_CREAT) |
paul@148 | 50 | { |
paul@148 | 51 | /* Determine whether only the leafname is left of the path, with |
paul@148 | 52 | the inode number referring to the parent directory. */ |
paul@148 | 53 | |
paul@148 | 54 | if (path_is_leafname(remaining)) |
paul@148 | 55 | { |
paul@148 | 56 | err = _ops->create_file(ino, remaining, _user, &ino); |
paul@148 | 57 | if (err) |
paul@148 | 58 | return err; |
paul@148 | 59 | |
paul@148 | 60 | *fileid = (fileid_t) ino; |
paul@148 | 61 | return L4_EOK; |
paul@148 | 62 | } |
paul@148 | 63 | } |
paul@148 | 64 | |
paul@148 | 65 | return -L4_ENOENT; |
paul@102 | 66 | } |
paul@102 | 67 | |
paul@102 | 68 | /* Return a new accessor for 'fileid'. */ |
paul@102 | 69 | |
paul@143 | 70 | long Ext2FileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) |
paul@102 | 71 | { |
paul@143 | 72 | (void) path; (void) flags; |
paul@102 | 73 | |
paul@106 | 74 | ext2_file_t file; |
paul@148 | 75 | long err = _ops->open_file((ext2_ino_t) fileid, &file); |
paul@143 | 76 | |
paul@148 | 77 | if (err) |
paul@148 | 78 | return err; |
paul@106 | 79 | |
paul@148 | 80 | *accessor = new Ext2FileAccessor(_ops, file, fileid); |
paul@143 | 81 | return L4_EOK; |
paul@102 | 82 | } |
paul@102 | 83 | |
paul@102 | 84 | // vim: tabstop=4 expandtab shiftwidth=4 |