1.1 --- a/libfsserver/lib/files/ext2_file_opener.cc Sun Jul 25 17:14:53 2021 +0200
1.2 +++ b/libfsserver/lib/files/ext2_file_opener.cc Sun Jul 25 23:18:39 2021 +0200
1.3 @@ -19,7 +19,11 @@
1.4 * Boston, MA 02110-1301, USA
1.5 */
1.6
1.7 +#include <e2access/access.h>
1.8 #include <e2access/image.h>
1.9 +#include <e2access/path.h>
1.10 +
1.11 +#include <systypes/fcntl.h>
1.12
1.13 #include "ext2_file_accessor.h"
1.14 #include "ext2_file_opener.h"
1.15 @@ -28,15 +32,52 @@
1.16
1.17 long Ext2FileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid)
1.18 {
1.19 + const char *filename = path;
1.20 +
1.21 /* Obtain the inode number. */
1.22
1.23 ext2_ino_t ino;
1.24 - errcode_t retval = image_find_path(_fs, &path, &ino);
1.25 + errcode_t retval = image_find_path(_fs, &filename, &ino);
1.26
1.27 - // NOTE: Support file creation.
1.28 + /* Handle a missing file. */
1.29
1.30 if (retval)
1.31 + {
1.32 + /* Create a missing file if possible. */
1.33 +
1.34 + if (flags & O_CREAT)
1.35 + {
1.36 + /* Determine whether only the leafname is left of the path, with
1.37 + the inode number referring to the parent directory. */
1.38 +
1.39 + if (path_is_leafname(filename))
1.40 + {
1.41 + struct ext2_inode inode_parent;
1.42 +
1.43 + /* Determine write access in the directory. */
1.44 +
1.45 + retval = ext2fs_read_inode(_fs, ino, &inode_parent);
1.46 +
1.47 + if (retval)
1.48 + return -L4_EIO;
1.49 +
1.50 + if (!access_can_write(_user, &inode_parent))
1.51 + return -L4_EPERM;
1.52 +
1.53 + /* If so, use the parent directory inode to create a new file. */
1.54 +
1.55 + if (image_create_file(_fs, ino, filename, 0666 & ~_user.umask,
1.56 + _user.uid, _user.gid, &ino))
1.57 + return -L4_EIO;
1.58 +
1.59 + *fileid = (fileid_t) ino;
1.60 + return L4_EOK;
1.61 + }
1.62 + }
1.63 +
1.64 + *fileid = FILEID_INVALID;
1.65 return -L4_ENOENT;
1.66 + }
1.67
1.68 *fileid = (fileid_t) ino;
1.69 return L4_EOK;