paul@148 | 1 | /* |
paul@148 | 2 | * File operations performed by an Ext2-compatible filesystem. |
paul@148 | 3 | * |
paul@148 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@148 | 5 | * |
paul@148 | 6 | * This program is free software; you can redistribute it and/or |
paul@148 | 7 | * modify it under the terms of the GNU General Public License as |
paul@148 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@148 | 9 | * the License, or (at your option) any later version. |
paul@148 | 10 | * |
paul@148 | 11 | * This program is distributed in the hope that it will be useful, |
paul@148 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@148 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@148 | 14 | * GNU General Public License for more details. |
paul@148 | 15 | * |
paul@148 | 16 | * You should have received a copy of the GNU General Public License |
paul@148 | 17 | * along with this program; if not, write to the Free Software |
paul@148 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@148 | 19 | * Boston, MA 02110-1301, USA |
paul@148 | 20 | */ |
paul@148 | 21 | |
paul@148 | 22 | #include <e2access/access.h> |
paul@148 | 23 | #include <e2access/image.h> |
paul@148 | 24 | #include <e2access/path.h> |
paul@148 | 25 | |
paul@148 | 26 | #include "ext2_file_operations.h" |
paul@148 | 27 | |
paul@148 | 28 | /* Create a file in the directory indicated by the given inode number with the |
paul@148 | 29 | given filename. The file is created with the given user permissions. */ |
paul@148 | 30 | |
paul@148 | 31 | long Ext2FileOperations::create_file(ext2_ino_t ino_parent, const char *filename, |
paul@148 | 32 | user_t user, ext2_ino_t *ino) |
paul@148 | 33 | { |
paul@148 | 34 | if (!path_is_leafname(filename)) |
paul@148 | 35 | return -L4_EINVAL; |
paul@148 | 36 | |
paul@148 | 37 | struct ext2_inode inode_parent; |
paul@148 | 38 | errcode_t retval = ext2fs_read_inode(_fs, ino_parent, &inode_parent); |
paul@148 | 39 | |
paul@148 | 40 | if (retval) |
paul@148 | 41 | return -L4_EIO; |
paul@148 | 42 | |
paul@148 | 43 | if (!access_can_write(user, &inode_parent)) |
paul@148 | 44 | return -L4_EPERM; |
paul@148 | 45 | |
paul@148 | 46 | if (image_create_file(_fs, ino_parent, filename, 0666 & ~user.umask, |
paul@148 | 47 | user.uid, user.gid, ino)) |
paul@148 | 48 | return -L4_EIO; |
paul@148 | 49 | |
paul@148 | 50 | return L4_EOK; |
paul@148 | 51 | } |
paul@148 | 52 | |
paul@148 | 53 | /* For the given 'path', return an inode number or indicate the 'remaining' |
paul@148 | 54 | part of the path that cannot be resolved. */ |
paul@148 | 55 | |
paul@148 | 56 | long Ext2FileOperations::find_file(const char *path, ext2_ino_t *ino, |
paul@148 | 57 | const char **remaining) |
paul@148 | 58 | { |
paul@148 | 59 | *remaining = path; |
paul@148 | 60 | errcode_t retval = image_find_path(_fs, remaining, ino); |
paul@148 | 61 | |
paul@148 | 62 | // NOTE: Map error conditions. |
paul@148 | 63 | |
paul@148 | 64 | if (retval) |
paul@148 | 65 | return -L4_EIO; |
paul@148 | 66 | |
paul@148 | 67 | return L4_EOK; |
paul@148 | 68 | } |
paul@148 | 69 | |
paul@148 | 70 | /* Perform closing operations on a file. */ |
paul@148 | 71 | |
paul@148 | 72 | void Ext2FileOperations::close_file(ext2_file_t file) |
paul@148 | 73 | { |
paul@148 | 74 | ext2fs_file_flush(file); |
paul@148 | 75 | ext2fs_file_close(file); |
paul@148 | 76 | } |
paul@148 | 77 | |
paul@148 | 78 | /* Open the file associated with the indicated inode. */ |
paul@148 | 79 | |
paul@148 | 80 | long Ext2FileOperations::open_file(ext2_ino_t ino, ext2_file_t *file) |
paul@148 | 81 | { |
paul@148 | 82 | errcode_t retval = ext2fs_file_open(_fs, ino, EXT2_FILE_WRITE, file); |
paul@148 | 83 | |
paul@148 | 84 | // NOTE: Map error conditions. |
paul@148 | 85 | |
paul@148 | 86 | if (retval) |
paul@148 | 87 | return -L4_EIO; |
paul@148 | 88 | |
paul@148 | 89 | return L4_EOK; |
paul@148 | 90 | } |
paul@148 | 91 | |
paul@148 | 92 | /* Obtain the size of a file. */ |
paul@148 | 93 | |
paul@148 | 94 | offset_t Ext2FileOperations::get_size(ext2_file_t file) |
paul@148 | 95 | { |
paul@148 | 96 | return ext2fs_file_get_size(file); |
paul@148 | 97 | } |
paul@148 | 98 | |
paul@148 | 99 | /* Update the size of a file. */ |
paul@148 | 100 | |
paul@148 | 101 | void Ext2FileOperations::set_size(ext2_file_t file, offset_t size) |
paul@148 | 102 | { |
paul@148 | 103 | ext2fs_file_set_size(file, size); |
paul@148 | 104 | } |
paul@148 | 105 | |
paul@148 | 106 | /* Populate the given memory region with file content. */ |
paul@148 | 107 | |
paul@148 | 108 | offset_t Ext2FileOperations::read_file(ext2_file_t file, offset_t filepos, void *addr, offset_t size) |
paul@148 | 109 | { |
paul@148 | 110 | unsigned int nread; |
paul@148 | 111 | |
paul@148 | 112 | ext2fs_file_llseek(file, filepos, SEEK_SET, NULL); |
paul@148 | 113 | ext2fs_file_read(file, (void *) addr, size, &nread); |
paul@148 | 114 | |
paul@148 | 115 | return (offset_t) nread; |
paul@148 | 116 | } |
paul@148 | 117 | |
paul@148 | 118 | /* Transfer content from the given memory region to a file. */ |
paul@148 | 119 | |
paul@148 | 120 | void Ext2FileOperations::write_file(ext2_file_t file, offset_t filepos, const void *addr, offset_t size) |
paul@148 | 121 | { |
paul@148 | 122 | ext2fs_file_llseek(file, filepos, SEEK_SET, NULL); |
paul@148 | 123 | ext2fs_file_write(file, addr, size, NULL); |
paul@148 | 124 | } |
paul@148 | 125 | |
paul@148 | 126 | // vim: tabstop=4 expandtab shiftwidth=4 |