1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libfsserver/lib/files/ext2_file_operations.cc Fri Jul 30 00:20:35 2021 +0200
1.3 @@ -0,0 +1,126 @@
1.4 +/*
1.5 + * File operations performed by an Ext2-compatible filesystem.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <e2access/access.h>
1.26 +#include <e2access/image.h>
1.27 +#include <e2access/path.h>
1.28 +
1.29 +#include "ext2_file_operations.h"
1.30 +
1.31 +/* Create a file in the directory indicated by the given inode number with the
1.32 + given filename. The file is created with the given user permissions. */
1.33 +
1.34 +long Ext2FileOperations::create_file(ext2_ino_t ino_parent, const char *filename,
1.35 + user_t user, ext2_ino_t *ino)
1.36 +{
1.37 + if (!path_is_leafname(filename))
1.38 + return -L4_EINVAL;
1.39 +
1.40 + struct ext2_inode inode_parent;
1.41 + errcode_t retval = ext2fs_read_inode(_fs, ino_parent, &inode_parent);
1.42 +
1.43 + if (retval)
1.44 + return -L4_EIO;
1.45 +
1.46 + if (!access_can_write(user, &inode_parent))
1.47 + return -L4_EPERM;
1.48 +
1.49 + if (image_create_file(_fs, ino_parent, filename, 0666 & ~user.umask,
1.50 + user.uid, user.gid, ino))
1.51 + return -L4_EIO;
1.52 +
1.53 + return L4_EOK;
1.54 +}
1.55 +
1.56 +/* For the given 'path', return an inode number or indicate the 'remaining'
1.57 + part of the path that cannot be resolved. */
1.58 +
1.59 +long Ext2FileOperations::find_file(const char *path, ext2_ino_t *ino,
1.60 + const char **remaining)
1.61 +{
1.62 + *remaining = path;
1.63 + errcode_t retval = image_find_path(_fs, remaining, ino);
1.64 +
1.65 + // NOTE: Map error conditions.
1.66 +
1.67 + if (retval)
1.68 + return -L4_EIO;
1.69 +
1.70 + return L4_EOK;
1.71 +}
1.72 +
1.73 +/* Perform closing operations on a file. */
1.74 +
1.75 +void Ext2FileOperations::close_file(ext2_file_t file)
1.76 +{
1.77 + ext2fs_file_flush(file);
1.78 + ext2fs_file_close(file);
1.79 +}
1.80 +
1.81 +/* Open the file associated with the indicated inode. */
1.82 +
1.83 +long Ext2FileOperations::open_file(ext2_ino_t ino, ext2_file_t *file)
1.84 +{
1.85 + errcode_t retval = ext2fs_file_open(_fs, ino, EXT2_FILE_WRITE, file);
1.86 +
1.87 + // NOTE: Map error conditions.
1.88 +
1.89 + if (retval)
1.90 + return -L4_EIO;
1.91 +
1.92 + return L4_EOK;
1.93 +}
1.94 +
1.95 +/* Obtain the size of a file. */
1.96 +
1.97 +offset_t Ext2FileOperations::get_size(ext2_file_t file)
1.98 +{
1.99 + return ext2fs_file_get_size(file);
1.100 +}
1.101 +
1.102 +/* Update the size of a file. */
1.103 +
1.104 +void Ext2FileOperations::set_size(ext2_file_t file, offset_t size)
1.105 +{
1.106 + ext2fs_file_set_size(file, size);
1.107 +}
1.108 +
1.109 +/* Populate the given memory region with file content. */
1.110 +
1.111 +offset_t Ext2FileOperations::read_file(ext2_file_t file, offset_t filepos, void *addr, offset_t size)
1.112 +{
1.113 + unsigned int nread;
1.114 +
1.115 + ext2fs_file_llseek(file, filepos, SEEK_SET, NULL);
1.116 + ext2fs_file_read(file, (void *) addr, size, &nread);
1.117 +
1.118 + return (offset_t) nread;
1.119 +}
1.120 +
1.121 +/* Transfer content from the given memory region to a file. */
1.122 +
1.123 +void Ext2FileOperations::write_file(ext2_file_t file, offset_t filepos, const void *addr, offset_t size)
1.124 +{
1.125 + ext2fs_file_llseek(file, filepos, SEEK_SET, NULL);
1.126 + ext2fs_file_write(file, addr, size, NULL);
1.127 +}
1.128 +
1.129 +// vim: tabstop=4 expandtab shiftwidth=4