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/access.h> |
paul@106 | 23 | #include <e2access/image.h> |
paul@147 | 24 | #include <e2access/path.h> |
paul@147 | 25 | |
paul@147 | 26 | #include <systypes/fcntl.h> |
paul@106 | 27 | |
paul@106 | 28 | #include "ext2_file_accessor.h" |
paul@106 | 29 | #include "ext2_file_opener.h" |
paul@102 | 30 | |
paul@102 | 31 | /* Return a file identifier for the given 'path'. */ |
paul@102 | 32 | |
paul@146 | 33 | long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) |
paul@102 | 34 | { |
paul@147 | 35 | const char *filename = path; |
paul@147 | 36 | |
paul@106 | 37 | /* Obtain the inode number. */ |
paul@106 | 38 | |
paul@106 | 39 | ext2_ino_t ino; |
paul@147 | 40 | errcode_t retval = image_find_path(_fs, &filename, &ino); |
paul@102 | 41 | |
paul@147 | 42 | /* Handle a missing file. */ |
paul@143 | 43 | |
paul@106 | 44 | if (retval) |
paul@147 | 45 | { |
paul@147 | 46 | /* Create a missing file if possible. */ |
paul@147 | 47 | |
paul@147 | 48 | if (flags & O_CREAT) |
paul@147 | 49 | { |
paul@147 | 50 | /* Determine whether only the leafname is left of the path, with |
paul@147 | 51 | the inode number referring to the parent directory. */ |
paul@147 | 52 | |
paul@147 | 53 | if (path_is_leafname(filename)) |
paul@147 | 54 | { |
paul@147 | 55 | struct ext2_inode inode_parent; |
paul@147 | 56 | |
paul@147 | 57 | /* Determine write access in the directory. */ |
paul@147 | 58 | |
paul@147 | 59 | retval = ext2fs_read_inode(_fs, ino, &inode_parent); |
paul@147 | 60 | |
paul@147 | 61 | if (retval) |
paul@147 | 62 | return -L4_EIO; |
paul@147 | 63 | |
paul@147 | 64 | if (!access_can_write(_user, &inode_parent)) |
paul@147 | 65 | return -L4_EPERM; |
paul@147 | 66 | |
paul@147 | 67 | /* If so, use the parent directory inode to create a new file. */ |
paul@147 | 68 | |
paul@147 | 69 | if (image_create_file(_fs, ino, filename, 0666 & ~_user.umask, |
paul@147 | 70 | _user.uid, _user.gid, &ino)) |
paul@147 | 71 | return -L4_EIO; |
paul@147 | 72 | |
paul@147 | 73 | *fileid = (fileid_t) ino; |
paul@147 | 74 | return L4_EOK; |
paul@147 | 75 | } |
paul@147 | 76 | } |
paul@147 | 77 | |
paul@147 | 78 | *fileid = FILEID_INVALID; |
paul@146 | 79 | return -L4_ENOENT; |
paul@147 | 80 | } |
paul@106 | 81 | |
paul@146 | 82 | *fileid = (fileid_t) ino; |
paul@146 | 83 | return L4_EOK; |
paul@102 | 84 | } |
paul@102 | 85 | |
paul@102 | 86 | /* Return a new accessor for 'fileid'. */ |
paul@102 | 87 | |
paul@143 | 88 | long Ext2FileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) |
paul@102 | 89 | { |
paul@143 | 90 | (void) path; (void) flags; |
paul@102 | 91 | |
paul@106 | 92 | ext2_file_t file; |
paul@106 | 93 | errcode_t retval = ext2fs_file_open(_fs, (ext2_ino_t) fileid, EXT2_FILE_WRITE, &file); |
paul@106 | 94 | |
paul@143 | 95 | // NOTE: Map error conditions. |
paul@143 | 96 | |
paul@106 | 97 | if (retval) |
paul@143 | 98 | return -L4_EIO; |
paul@106 | 99 | |
paul@143 | 100 | *accessor = new Ext2FileAccessor(file, fileid); |
paul@143 | 101 | return L4_EOK; |
paul@102 | 102 | } |
paul@102 | 103 | |
paul@102 | 104 | // vim: tabstop=4 expandtab shiftwidth=4 |