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