-
Notifications
You must be signed in to change notification settings - Fork 48
/
gd_fuse_operations.c
530 lines (464 loc) · 11.5 KB
/
gd_fuse_operations.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
fuse-google-drive: a fuse filesystem wrapper for Google Drive
Copyright (C) 2012 James Cline
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define FUSE_USE_VERSION 26
#include <dirent.h>
#include <errno.h>
#include <fuse.h>
#include <string.h>
#include <sys/stat.h>
#include "gd_cache.h"
#include "gd_interface.h"
#include "str.h"
/**
* Store any state about this mount in this structure.
*
* root: The path to the mount point of the drive.
* credentials: Struct which stores necessary credentials for this mount.
*/
struct gd_state {
char* root;
struct gdi_state gdi_data;
};
/** Get file attributes.
*
*/
int gd_getattr (const char *path, struct stat *statbuf)
{
struct fuse_context *fc = fuse_get_context();
if( strcmp("/", path) == 0)
{
statbuf->st_mode = S_IFDIR | 0700;
statbuf->st_nlink=2;
}
else
{
memset(statbuf, 0, sizeof(struct stat));
const char *filename = gdi_strip_path(path);
struct gd_fs_entry_t * entry = gd_fs_entry_find(filename);
if(!entry)
return -ENOENT;
if(entry)
statbuf->st_size = entry->size;
statbuf->st_mode = S_IFREG | 0600;
statbuf->st_nlink=1;
}
statbuf->st_uid = fc->uid;
statbuf->st_gid = fc->gid;
return 0;
}
/** Read the target of a symbolic link.
*
*/
int gd_readlink (const char *path, char *link, size_t size)
{
return 0;
}
/** Create a regular file.
*
*/
int gd_mknod (const char *path, mode_t mode, dev_t dev)
{
return 0;
}
/** Create a directory.
*
*/
int gd_mkdir (const char *path, mode_t mode)
{
return 0;
}
/** Remove a file.
*
*/
int gd_unlink (const char *path)
{
return 0;
}
/** Remove a directory.
*
*/
int gd_rmdir (const char *path)
{
return 0;
}
/** Create a symbolic link.
*
* Google Drive likely does not support an equivalent operation.
* We could allow this for a specific session, but it would be lost
* after an unmount.
*/
int gd_symlink (const char *path, const char *link)
{
return 0;
}
/** Rename a file.
*
*/
int gd_rename (const char *path, const char *newpath)
{
return 0;
}
/** Create a hard link to a file.
*
* Google Drive likely does not support an equivalent operation.
* We could allow this for a specific session, but it would be lost
* after an unmount.
*/
int gd_link (const char *path, const char *newpath)
{
return 0;
}
/** Change the permission bits of a file.
*
* Could this be used to share a document with others?
* Perhaps o+r would make visible to all, o+rw would be editable by all?
* I can't think of a way to do any sort of group level permissions sanely atm.
*
* Alternatively, these permission settings could be used for local acces only,
* which would let you make a file locally readable or modifiable by another
* system user. This may violate the principle of least astonishment least.
*/
int gd_chmod (const char *path, mode_t mode)
{
return 0;
}
/** Change the owner and group of a file.
*
* Since this uses gid and uids, I cannot think of a way to easily use this for
* Google Drive sharing purposes with specific users right now.
* Perhaps some additional utility could be used to display a mapping of
* uid/gids and Google Drive users you can share with?
*
* Alternatively, these uid/gid settings could be used for local acces only,
* which would let you make a file locally readable or modifiable by another
* system user. This may violate the principle of least astonishment least.
*/
int gd_chown (const char *path, uid_t uid, gid_t gid)
{
return 0;
}
/** Change the size of a file.
*
*/
int gd_truncate (const char *path, off_t newsize)
{
return 0;
}
/** File open operation.
*
*/
int gd_open (const char *path, struct fuse_file_info * fileinfo)
{
struct gdi_state *state = &((struct gd_state*)fuse_get_context()->private_data)->gdi_data;
int flags = fileinfo->flags;
/*
Is it possible to have a file in drive or docs you cannot read?
I suppose it may be the case that you lost read access since last checked,
but between open() and read() that can happen anyway, so why check here at
all?
Maybe something in the Access Control Lists allows this?
*/
if(flags & O_RDONLY)
{
}
// TODO: everything below here
// NOTE: O_RDWR won't work, just O_RDONLY
// people can share read only files with you
if(flags & O_WRONLY)
{
}
if(flags & O_RDWR)
{
}
// Don't need to check O_CREAT, O_EXCL, O_TRUNC
// Do we need to check all these?
/* Comment these out for now to reduce user headacke
if(flags & O_APPEND);
if(flags & O_ASYNC);
//if(flags & O_DIRECT);
// if(flags & O_DIRECTORY); // opendir() only?
// if(flags & O_LARGEFILE);
//if(flags & O_NOATIME); // does google drive do this anyway?
if(flags & O_NOCTTY); // does this do anything/is it passed to us at all?
if(flags & O_NOFOLLOW);
if(flags & O_NONBLOCK || flags & O_NONBLOCK); // read man 2 fcntl and man 7 fifo
if(flags & O_SYNC);
*/
// If we have access to this file, then load it.
// TODO: Make gdi_load() nonblocking if appropriate
const char* filename = gdi_strip_path(path);
struct gd_fs_entry_t *entry = gd_fs_entry_find(filename);
int load = gdi_load(state, entry);
if(load)
return -1;
return 0;
}
/** Read data from an open file.
*
*/
int gd_read (const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fileinfo)
{
struct gdi_state *state = &((struct gd_state*)fuse_get_context()->private_data)->gdi_data;
const char* filename = gdi_strip_path(path);
struct gd_fs_entry_t *entry = gd_fs_entry_find(filename);
if(!entry)
return 0;
size_t length = size;
const char const* chunk = gdi_read(&length, entry, offset);
memcpy(buf, chunk, length);
return length;
}
/** Write data to an open file.
*
*/
int gd_write (const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Get file system statistics.
*
*/
int gd_statfs (const char *path, struct statvfs *statv)
{
return 0;
}
/** Possibly flush cached data.
*
*/
int gd_flush (const char *path, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Release an open file.
*
*/
int gd_release (const char *path, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Synchronize file contents.
*
*/
int gd_fsync (const char *path, int datasync, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Set extended attributes.
*
* Does this mean anything for Google Drive?
*/
int gd_setxattr (const char *path, const char *name, const char *value, size_t size, int flags)
{
return 0;
}
/** Get extended attributes.
*
* Does this mean anything for Google Drive?
*/
int gd_getxattr (const char *path, const char *name, char *value, size_t size)
{
return 0;
}
/** List extended attributes.
*
* Does this mean anything for Google Drive?
*/
int gd_listxattr (const char *path, char *list, size_t size)
{
return 0;
}
/** Remove extended attributes.
*
* Does this mean anything for Google Drive?
*/
int gd_removexattr (const char *path, const char *name)
{
return 0;
}
/** Open a directory.
*
*/
int gd_opendir (const char *path, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Read directory.
*
*/
int gd_readdir (const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fileinfo)
{
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
struct gdi_state *state = &((struct gd_state*)fuse_get_context()->private_data)->gdi_data;
struct gd_fs_entry_t *iter = state->head;
while(iter != NULL)
{
if(filler(buf, iter->filename.str, NULL, 0))
{
fprintf(stderr, "readdir() filler()\n");
return -ENOMEM;
}
iter = iter->next;
}
return 0;
}
/** Release directory.
*
*/
int gd_releasedir (const char *path, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Synchronize directory contents.
*
*/
int gd_fsyncdir (const char *path, int datasync, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Initialize filesystem
*
*/
void *gd_init (struct fuse_conn_info *conn)
{
return ((struct gd_state *) fuse_get_context()->private_data);
}
/** Clean up filesystem
*
* Automatically called by fuse on filesystem exit (unmount).
*/
void gd_destroy (void *userdata)
{
}
/** Check file access permission.
*
*/
int gd_access (const char *path, int mask)
{
return 0;
}
/** Create and open a file.
*
*/
int gd_create (const char *path, mode_t mode, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Change the size of an open file.
*
*/
int gd_ftruncate (const char *path, off_t offset, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Get attributes from an open file.
*
*/
int gd_fgetattr (const char *path, struct stat *statbuf, struct fuse_file_info *fileinfo)
{
return 0;
}
/** Perform POSIX file locking operation.
*
* Does this make any sense for Google Drive?
*/
int gd_lock (const char *path, struct fuse_file_info *fileinfo, int cmd, struct flock *lock)
{
return 0;
}
/** Change the access and modification times of a file with nanosecond
* resoluton.
*
* Does this make any sense for Google Drive?
*/
int gd_utimens (const char *path, const struct timespec tv[2])
{
return 0;
}
/** Ioctl
*
* Does this make any sense for Google Drive?
*/
int gd_ioctl (const char *path, int cmd, void *arg, struct fuse_file_info *fileinfo, unsigned int flags, void *data)
{
return 0;
}
/** Poll for IO readiness events.
*
*/
int gd_poll (const char *path, struct fuse_file_info *fileinfo, struct fuse_pollhandle *ph, unsigned *reventsp)
{
return 0;
}
// Only uncomment these assignments once an operation's function has been
// fleshed out.
struct fuse_operations gd_oper = {
.getattr = gd_getattr,
//.readlink = gd_readlink,
// getdir() deprecated, use readdir()
.getdir = NULL,
//.mknod = gd_mknod,
//.mkdir = gd_mkdir,
//.unlink = gd_unlink,
//.rmdir = gd_rmdir,
//.symlink = gd_symlink,
//.rename = gd_rename,
//.link = gd_link,
//.chmod = gd_chmod,
//.chown = gd_chown,
//.truncate = gd_truncate,
// utime() deprecated, use utimens
.utime = NULL,
.open = gd_open,
.read = gd_read,
//.write = gd_write,
//.statfs = gd_statfs,
//.flush = gd_flush,
.release = gd_release,
//.fsync = gd_fsync,
//.setxattr = gd_setxattr,
//.getxattr = gd_getxattr,
//.listxattr = gd_listxattr,
//.removexattr = gd_removexattr,
//.opendir = gd_opendir,
.readdir = gd_readdir,
//.releasedir = gd_releasedir,
//.fsyncdir = gd_fsyncdir,
.init = gd_init,
//.destroy = gd_destroy,
//.access = gd_access,
//.create = gd_create,
//.ftruncate = gd_ftruncate,
//.getattr = gd_fgetattr,
//.lock = gd_lock,
//.utimens = gd_utimens,
//.ioctl = gd_ioctl,
//.poll = gd_poll,
};
int main(int argc, char* argv[])
{
int fuse_stat;
struct gd_state gd_data;
int ret = gdi_init(&gd_data.gdi_data);
if(ret != 0)
return ret;
// Start fuse
fuse_stat = fuse_main(argc, argv, &gd_oper, &gd_data);
/* When we get here, fuse has finished.
* Do any necessary cleanups.
*/
gdi_destroy(&gd_data.gdi_data);
return fuse_stat;
}