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