-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse-fs-special.c
309 lines (225 loc) · 7.7 KB
/
fuse-fs-special.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
2010, 2011, 2012, 2103, 2014, 2015, 2016 Stef Bon <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "global-defines.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <err.h>
#include <sys/time.h>
#include <time.h>
#include <pthread.h>
#include <ctype.h>
#include <inttypes.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/statfs.h>
#ifndef ENOATTR
#define ENOATTR ENODATA /* No such attribute */
#endif
#include "logging.h"
#include "main.h"
#include "pathinfo.h"
#include "utils.h"
#include "fuse-dentry.h"
#include "fuse-directory.h"
#include "fuse-utils.h"
#include "simple-hash.h"
#include "fuse-interface.h"
#include "fuse-fs.h"
#include "workspaces.h"
#include "fuse-fs-virtual.h"
#include "fuse-fs-common.h"
#define UINT32_T_MAX 0xFFFFFFFF
static struct fuse_fs_s fs;
static char *desktopentryname=".directory";
static pthread_mutex_t desktopmutex=PTHREAD_MUTEX_INITIALIZER;
static struct statfs statfs_keep;
struct special_path_s {
uint64_t ino;
unsigned int size;
char path[];
};
static void _fs_special_forget(struct inode_s *inode)
{
struct inode_link_s *link=NULL;
logoutput("_fs_special_forget");
fs_get_inode_link(inode, &link);
if (link->type==INODE_LINK_TYPE_SPECIAL_ENTRY) {
struct special_path_s *s=(struct special_path_s *) link->link.ptr;
if (s) free(s);
}
link->type=0;
link->link.ptr=NULL;
}
static void _fs_special_getattr(struct service_context_s *context, struct fuse_request_s *request, struct inode_s *inode)
{
_fs_common_getattr(context, request, inode);
}
static void _fs_special_setattr(struct service_context_s *context, struct fuse_request_s *request, struct inode_s *inode, struct stat *st, unsigned int set)
{
if (set & (FATTR_SIZE | FATTR_UID | FATTR_GID | FATTR_MODE | FATTR_MTIME | FATTR_CTIME)) {
reply_VFS_error(request, EPERM);
return;
}
if (set & FATTR_ATIME) {
if (set & FATTR_ATIME_NOW) get_current_time(&st->st_atim);
inode->st.st_atim.tv_sec=st->st_atim.tv_sec;
inode->st.st_atim.tv_nsec=st->st_atim.tv_nsec;
}
_fs_common_getattr(context, request, inode);
}
static void _fs_special_open(struct fuse_openfile_s *openfile, struct fuse_request_s *request, unsigned int flags)
{
unsigned int error=EIO;
struct inode_s *inode=openfile->inode;
int fd=0;
struct inode_link_s *link=NULL;
struct special_path_s *s=NULL;
fs_get_inode_link(inode, &link);
s=(struct special_path_s *) link->link.ptr;
fd=open((char *) s->path, flags);
if (fd>0) {
struct fuse_open_out open_out;
openfile->handle.fd=fd;
open_out.fh=(uint64_t) openfile;
open_out.open_flags=0; //FOPEN_KEEP_CACHE;
open_out.padding=0;
reply_VFS_data(request, (char *) &open_out, sizeof(open_out));
return;
} else {
error=errno;
}
openfile->error=error;
reply_VFS_error(request, error);
}
void _fs_special_read(struct fuse_openfile_s *openfile, struct fuse_request_s *request, size_t size, off_t off, unsigned int flags, uint64_t lock_owner)
{
unsigned int error=EIO;
char buffer[size];
ssize_t read=0;
read=pread(openfile->handle.fd, (void *) buffer, size, off);
if (read==-1) {
error=errno;
reply_VFS_error(request, error);
return;
}
reply_VFS_data(request, buffer, read);
}
void _fs_special_write(struct fuse_openfile_s *openfile, struct fuse_request_s *request, const char *buffer, size_t size, off_t off, unsigned int flags, uint64_t lock_owner)
{
reply_VFS_error(request, EPERM);
}
void _fs_special_flush(struct fuse_openfile_s *openfile, struct fuse_request_s *request, uint64_t lock_owner)
{
reply_VFS_error(request, 0);
}
void _fs_special_fsync(struct fuse_openfile_s *openfile, struct fuse_request_s *request, unsigned char sync)
{
reply_VFS_error(request, 0);
}
void _fs_special_release(struct fuse_openfile_s *openfile, struct fuse_request_s *request, unsigned int flags, uint64_t lock_owner)
{
close(openfile->handle.fd);
openfile->handle.fd=0;
reply_VFS_error(request, 0);
}
void _fs_special_fsetattr(struct fuse_openfile_s *openfile, struct fuse_request_s *request, struct stat *st, int fuse_set)
{
_fs_special_setattr(openfile->context, request, openfile->inode, st, fuse_set);
}
void _fs_special_fgetattr(struct fuse_openfile_s *openfile, struct fuse_request_s *request)
{
_fs_special_getattr(openfile->context, request, openfile->inode);
}
static void _fs_special_statfs(struct service_context_s *context, struct fuse_request_s *request, struct inode_s *inode)
{
struct fuse_statfs_out statfs_out;
memset(&statfs_out, 0, sizeof(struct fuse_statfs_out));
statfs_out.st.blocks=statfs_keep.f_blocks;
statfs_out.st.bfree=statfs_keep.f_bfree;
statfs_out.st.bavail=statfs_keep.f_bavail;
statfs_out.st.bsize=statfs_keep.f_bsize;
statfs_out.st.files=(uint64_t) context->workspace->nrinodes;
statfs_out.st.ffree=(uint64_t) (UINT32_T_MAX - statfs_out.st.files);
statfs_out.st.namelen=255;
statfs_out.st.frsize=statfs_out.st.bsize;
statfs_out.st.padding=0;
reply_VFS_data(request, (char *) &statfs_out, sizeof(struct fuse_statfs_out));
}
void init_special_fs()
{
unsigned int error=0;
statfs("/", &statfs_keep);
memset(&fs, 0, sizeof(struct fuse_fs_s));
set_virtual_fs(&fs);
fs.forget=_fs_special_forget;
fs.getattr=_fs_special_getattr;
fs.setattr=_fs_special_setattr;
fs.type.nondir.open=_fs_special_open;
fs.type.nondir.read=_fs_special_read;
fs.type.nondir.write=_fs_special_write;
fs.type.nondir.flush=_fs_special_flush;
fs.type.nondir.fsync=_fs_special_fsync;
fs.type.nondir.release=_fs_special_release;
fs.type.nondir.fsetattr=_fs_special_fsetattr;
fs.type.nondir.fgetattr=_fs_special_fgetattr;
fs.statfs=_fs_special_statfs;
}
void free_special_fs()
{
}
void set_fs_special(struct inode_s *inode)
{
if (! S_ISDIR(inode->st.st_mode)) inode->fs=&fs;
}
void create_desktopentry_file(char *path, struct entry_s *parent, struct workspace_mount_s *workspace)
{
struct stat st;
if (lstat(path, &st)==0 && S_ISREG(st.st_mode)) {
struct name_s xname;
unsigned int error=0;
struct directory_s *directory=get_directory(parent->inode, &error);
xname.name=desktopentryname;
xname.len=strlen(xname.name);
calculate_nameindex(&xname);
struct entry_s *entry=_fs_common_create_entry_unlocked(workspace, directory, &xname, &st, 0, 0, &error);
if (entry) {
struct special_path_s *s=malloc(sizeof(struct special_path_s) + strlen(path) + 1); /* inlcuding terminating zero */
if (s) {
struct inode_s *inode=entry->inode;
inode->fs=&fs;
s->ino=inode->st.st_ino;
strcpy(s->path, path);
s->size=strlen(path);
inode->link.link.ptr=(void *)s;
inode->link.type=INODE_LINK_TYPE_SPECIAL_ENTRY;
}
}
}
}
int check_entry_special(struct inode_s *inode)
{
int result=-1;
if (! S_ISDIR(inode->st.st_mode)) result=(inode->fs==&fs) ? 0 : -1;
return result;
}