1 /*
2     This file is part of Android File Transfer For Linux.
3     Copyright (C) 2015-2020  Vladimir Menshakov
4 
5     This library is free software; you can redistribute it and/or modify it
6     under the terms of the GNU Lesser General Public License as published by
7     the Free Software Foundation; either version 2.1 of the License,
8     or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful, but
11     WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public License
16     along with this library; if not, write to the Free Software Foundation,
17     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 #ifndef AFTL_FUSE_FUSEENTRY_H
21 #define AFTL_FUSE_FUSEENTRY_H
22 
23 #include <mtp/ptp/ObjectFormat.h>
24 
25 #include <fuse_lowlevel.h>
26 
27 namespace mtp { namespace fuse
28 {
29 
30 	struct FuseEntry : fuse_entry_param
31 	{
32 		static constexpr const double	Timeout = 60.0;
33 		static constexpr unsigned 		FileMode 		= S_IFREG | 0644;
34 		static constexpr unsigned 		DirectoryMode	= S_IFDIR | 0755;
35 
36 		fuse_req_t Request;
37 
FuseEntryFuseEntry38 		FuseEntry(fuse_req_t req): fuse_entry_param(), Request(req)
39 		{
40 			generation = 1;
41 			attr_timeout = entry_timeout = Timeout;
42 		};
43 
SetIdFuseEntry44 		void SetId(FuseId id)
45 		{
46 			ino = id.Inode;
47 			attr.st_ino = id.Inode;
48 		}
49 
SetFileModeFuseEntry50 		void SetFileMode()
51 		{ attr.st_mode = FileMode; }
52 
SetDirectoryModeFuseEntry53 		void SetDirectoryMode()
54 		{ attr.st_mode = DirectoryMode; }
55 
GetModeFuseEntry56 		static mode_t GetMode(mtp::ObjectFormat format)
57 		{
58 			switch(format)
59 			{
60 			case mtp::ObjectFormat::Association:
61 				return DirectoryMode;
62 			default:
63 				return FileMode;
64 			}
65 		}
66 
ReplyFuseEntry67 		void Reply()
68 		{
69 			if (attr.st_mode == 0)
70 				throw std::runtime_error("uninitialized attr in FuseEntry::Reply");
71 			FUSE_CALL(fuse_reply_entry(Request, this));
72 		}
73 
ReplyCreateFuseEntry74 		void ReplyCreate(fuse_file_info *fi)
75 		{
76 			if (attr.st_mode == 0)
77 				throw std::runtime_error("uninitialized attr in FuseEntry::Reply");
78 			FUSE_CALL(fuse_reply_create(Request, this, fi));
79 		}
80 
ReplyAttrFuseEntry81 		void ReplyAttr()
82 		{
83 			if (attr.st_mode == 0)
84 				throw std::runtime_error("uninitialized attr in FuseEntry::ReplyAttr");
85 			FUSE_CALL(fuse_reply_attr(Request, &attr, Timeout));
86 		}
87 
ReplyErrorFuseEntry88 		void ReplyError(int err)
89 		{
90 			FUSE_CALL(fuse_reply_err(Request, err));
91 		}
92 	};
93 
94 }}
95 
96 #endif
97