1 /**
2  * Copyright (c) 2008-2015 Alper Akcan <alper.akcan@gmail.com>
3  * Copyright (c) 2009 Renzo Davoli <renzo@cs.unibo.it>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program (in the main directory of the fuse-ext2
17  * distribution in the file COPYING); if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "fuse-ext2.h"
22 
do_open(ext2_filsys e2fs,const char * path,int flags)23 ext2_file_t do_open (ext2_filsys e2fs, const char *path, int flags)
24 {
25 	int rt;
26 	errcode_t rc;
27 	ext2_ino_t ino;
28 	ext2_file_t efile;
29 	struct ext2_inode inode;
30 	struct fuse_context *cntx = fuse_get_context();
31 	struct extfs_data *e2data = cntx->private_data;
32 
33 	debugf("enter");
34 	debugf("path = %s", path);
35 
36 	rt = do_check(path);
37 	if (rt != 0) {
38 		debugf("do_check(%s); failed", path);
39 		return NULL;
40 	}
41 
42 	rt = do_readinode(e2fs, path, &ino, &inode);
43 	if (rt) {
44 		debugf("do_readinode(%s, &ino, &inode); failed", path);
45 		return NULL;
46 	}
47 
48 	rc = ext2fs_file_open2(
49 			e2fs,
50 			ino,
51 			&inode,
52 			(((flags & O_ACCMODE) != 0) ? EXT2_FILE_WRITE : 0) | EXT2_FILE_SHARED_INODE,
53 			&efile);
54 	if (rc) {
55 		return NULL;
56 	}
57 
58 	if (e2data->readonly == 0) {
59 		inode.i_atime = e2fs->now ? e2fs->now : time(NULL);
60 		rt = do_writeinode(e2fs, ino, &inode);
61 		if (rt) {
62 			debugf("do_writeinode(%s, &ino, &inode); failed", path);
63 			return NULL;
64 		}
65 	}
66 
67 	debugf("leave");
68 	return efile;
69 }
70 
op_open(const char * path,struct fuse_file_info * fi)71 int op_open (const char *path, struct fuse_file_info *fi)
72 {
73 	ext2_file_t efile;
74 	ext2_filsys e2fs = current_ext2fs();
75 
76 	debugf("enter");
77 	debugf("path = %s", path);
78 
79 	efile = do_open(e2fs, path, fi->flags);
80 	if (efile == NULL) {
81 		debugf("do_open(%s); failed", path);
82 		return -ENOENT;
83 	}
84 	fi->fh = (uint64_t) efile;
85 
86 	debugf("leave");
87 	return 0;
88 }
89