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