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_FUSEDIRECTORY_H
21 #define AFTL_FUSE_FUSEDIRECTORY_H
22 
23 #include <vector>
24 
25 #include <fuse_lowlevel.h>
26 
27 namespace mtp { namespace fuse
28 {
29 
30 	using CharArray = std::vector<char>;
31 
32 	struct FuseDirectory
33 	{
34 		fuse_req_t			Request;
35 
FuseDirectoryFuseDirectory36 		FuseDirectory(fuse_req_t request): Request(request) { }
37 
AddFuseDirectory38 		void Add(CharArray & data, const std::string &name, const struct stat & entry)
39 		{
40 			if (data.empty())
41 				data.reserve(4096);
42 			size_t size = fuse_add_direntry(Request, NULL, 0, name.c_str(), NULL, 0);
43 			size_t offset = data.size();
44 			data.resize(data.size() + size);
45 			fuse_add_direntry(Request, data.data() + offset, size, name.c_str(), &entry, data.size()); //request is not used inside fuse here, so we could cache resulting dirent data
46 		}
47 
ReplyFuseDirectory48 		static void Reply(fuse_req_t req, const CharArray &data, off_t off, size_t size)
49 		{
50 			if (off >= (off_t)data.size())
51 				FUSE_CALL(fuse_reply_buf(req, NULL, 0));
52 			else
53 			{
54 				FUSE_CALL(fuse_reply_buf(req, data.data() + off, std::min<size_t>(size, data.size() - off)));
55 			}
56 		}
57 	};
58 
59 }}
60 
61 #endif
62