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