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/path.h> 23 24 #include <systypes/fcntl.h> 25 26 #include "ext2_file_accessor.h" 27 #include "ext2_file_opener.h" 28 29 Ext2FileOpener::~Ext2FileOpener() 30 { 31 } 32 33 /* Test if a directory is being accessed. */ 34 35 bool Ext2FileOpener::accessing_directory(const char *path, flags_t flags, fileid_t fileid) 36 { 37 (void) path; (void) flags; 38 return _ops->is_directory((ext2_ino_t) fileid); 39 } 40 41 /* Test if a file is being accessed. */ 42 43 bool Ext2FileOpener::accessing_file(const char *path, flags_t flags, fileid_t fileid) 44 { 45 (void) path; (void) flags; 46 return _ops->is_file((ext2_ino_t) fileid); 47 } 48 49 /* Return a file identifier for the given 'path'. */ 50 51 long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) 52 { 53 ext2_ino_t ino; 54 const char *remaining; 55 long err = _ops->find_file(path, &ino, &remaining); 56 57 /* Return an existing file. */ 58 59 if (!err) 60 { 61 *fileid = (fileid_t) ino; 62 return L4_EOK; 63 } 64 65 *fileid = FILEID_INVALID; 66 67 /* Create a missing file if possible. */ 68 69 if ((flags & O_CREAT) && (flags & ~O_DIRECTORY)) 70 { 71 /* Determine whether only the leafname is left of the path, with 72 the inode number referring to the parent directory. */ 73 74 if (path_is_leafname(remaining)) 75 { 76 err = _ops->create_file(ino, remaining, _user, &ino); 77 if (err) 78 return err; 79 80 *fileid = (fileid_t) ino; 81 return L4_EOK; 82 } 83 } 84 85 return -L4_ENOENT; 86 } 87 88 /* Return a new accessor for 'fileid'. */ 89 90 long Ext2FileOpener::make_accessor(const char *path, flags_t flags, fileid_t fileid, Accessor **accessor) 91 { 92 (void) path; (void) flags; 93 94 ext2_file_t file; 95 long err = _ops->open_file((ext2_ino_t) fileid, &file); 96 97 if (err) 98 return err; 99 100 *accessor = new Ext2FileAccessor(_ops, file, fileid); 101 return L4_EOK; 102 } 103 104 // vim: tabstop=4 expandtab shiftwidth=4