1 /* 2 * File operations performed 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 "ext2_file_operations.h" 27 28 /* Create a file in the directory indicated by the given inode number with the 29 given filename. The file is created with the given user permissions. */ 30 31 long Ext2FileOperations::create_file(ext2_ino_t ino_parent, const char *filename, 32 user_t user, ext2_ino_t *ino) 33 { 34 if (!path_is_leafname(filename)) 35 return -L4_EINVAL; 36 37 std::lock_guard<std::mutex> guard(_lock); 38 39 struct ext2_inode inode_parent; 40 errcode_t retval = ext2fs_read_inode(_fs, ino_parent, &inode_parent); 41 42 if (retval) 43 return -L4_EIO; 44 45 if (!access_can_write(user, &inode_parent)) 46 return -L4_EPERM; 47 48 if (image_create_file(_fs, ino_parent, filename, 0666 & ~user.umask, 49 user.uid, user.gid, ino)) 50 return -L4_EIO; 51 52 return L4_EOK; 53 } 54 55 /* For the given 'path', return an inode number or indicate the 'remaining' 56 part of the path that cannot be resolved. */ 57 58 long Ext2FileOperations::find_file(const char *path, ext2_ino_t *ino, 59 const char **remaining) 60 { 61 std::lock_guard<std::mutex> guard(_lock); 62 63 *remaining = path; 64 errcode_t retval = image_find_path(_fs, remaining, ino); 65 66 // NOTE: Map error conditions. 67 68 if (retval) 69 return -L4_EIO; 70 71 return L4_EOK; 72 } 73 74 /* Perform closing operations on a file. */ 75 76 void Ext2FileOperations::close_file(ext2_file_t file) 77 { 78 std::lock_guard<std::mutex> guard(_lock); 79 80 ext2fs_file_flush(file); 81 ext2fs_file_close(file); 82 } 83 84 /* Open the file associated with the indicated inode. */ 85 86 long Ext2FileOperations::open_file(ext2_ino_t ino, ext2_file_t *file) 87 { 88 std::lock_guard<std::mutex> guard(_lock); 89 90 errcode_t retval = ext2fs_file_open(_fs, ino, EXT2_FILE_WRITE, file); 91 92 // NOTE: Map error conditions. 93 94 if (retval) 95 return -L4_EIO; 96 97 return L4_EOK; 98 } 99 100 /* Obtain the size of a file. */ 101 102 offset_t Ext2FileOperations::get_size(ext2_file_t file) 103 { 104 std::lock_guard<std::mutex> guard(_lock); 105 106 return ext2fs_file_get_size(file); 107 } 108 109 /* Update the size of a file. */ 110 111 void Ext2FileOperations::set_size(ext2_file_t file, offset_t size) 112 { 113 std::lock_guard<std::mutex> guard(_lock); 114 115 ext2fs_file_set_size(file, size); 116 } 117 118 /* Populate the given memory region with file content. */ 119 120 offset_t Ext2FileOperations::read_file(ext2_file_t file, offset_t filepos, void *addr, offset_t size) 121 { 122 std::lock_guard<std::mutex> guard(_lock); 123 124 unsigned int nread; 125 126 ext2fs_file_llseek(file, filepos, SEEK_SET, NULL); 127 ext2fs_file_read(file, (void *) addr, size, &nread); 128 129 return (offset_t) nread; 130 } 131 132 /* Transfer content from the given memory region to a file. */ 133 134 void Ext2FileOperations::write_file(ext2_file_t file, offset_t filepos, const void *addr, offset_t size) 135 { 136 std::lock_guard<std::mutex> guard(_lock); 137 138 ext2fs_file_llseek(file, filepos, SEEK_SET, NULL); 139 ext2fs_file_write(file, addr, size, NULL); 140 } 141 142 // vim: tabstop=4 expandtab shiftwidth=4