-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfs.c
482 lines (414 loc) · 11.1 KB
/
sfs.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
/*
Simple File System
This code is derived from function prototypes found /usr/include/fuse/fuse.h
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
His code is licensed under the LGPLv2.
*/
#include "params.h"
#include "block.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse.h>
#include <libgen.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_SYS_XATTR_H
#include <sys/xattr.h>
#endif
#include "log.h"
#include "util.h"
///////////////////////////////////////////////////////////////////////
//
//Helper Function
//
Inode * getFileFD(int fd)
{
if(IS_FILE_TABLE_INIT == 0)
{
//This method should not be called if the File Table is not created
return NULL;
}
Inode * ptr = FT->files[0];
int pos = 1;
while(pos < FT->size && ptr->fd != fd)
{
ptr = FT->files[pos];
pos += 1;
}
//Could not find referenced fd
if(pos >= FT->size)
{
return NULL;
}
else
{
return ptr;
}
}
Inode * getFilePath(char * path)
{
if(path == NULL)
{
log_msg("Nullerino");
return NULL;
}
Inode * ptr = FT->files[0];
int pos = 1;
while(pos < FT->size && strcmp(path,ptr->path) != 0)
{
if(FT->files[pos]->path == NULL)
{
pos+=1;
continue;
}
ptr = FT->files[pos];
pos+=1;
}
//Could not find referenced path
if(pos >= FT->size)
{
return NULL;
}
else
{
return ptr;
}
}
int fileSize(Inode * file)
{
int total = BLOCK_SIZE - file->spaceleft;
file = file->next;
while(file != NULL)
{
total += (BLOCK_SIZE - file->spaceleft);
file = file->next;
}
return total;
}
int fileTotalSize(Inode * file)
{
int total = 0;
file = FT->files[file->fd/BLOCK_SIZE];
while(file != NULL)
{
total += BLOCK_SIZE;
file = file->next;
}
return total;
}
//////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//
// Prototypes for all these functions, and the C-style comments,
// come indirectly from /usr/include/fuse.h
//
/**
* Initialize filesystem
*
* The return value will passed in the private_data field of
* fuse_context to all file operations and as a parameter to the
* destroy() method.
*
* Introduced in version 2.3
* Changed in version 2.6
*/
void *sfs_init(struct fuse_conn_info *conn)
{
fprintf(stderr, "in bb-init\n");
struct sfs_state * state = SFS_DATA;
disk_open(state->diskfile);
log_msg("\nsfs_init()\n");
FT = malloc(sizeof(FileTable *));
FT ->num_free_inodes = totalsize/BLOCK_SIZE;
FT->size = totalsize/BLOCK_SIZE;
FT ->files = malloc(FT->num_free_inodes*sizeof(FileTable *));
int i = 0;
for(i=0;i<FT->num_free_inodes;i++)
{
FT->files[i] = malloc(sizeof(Inode));
FT ->files[i] -> file_position = 0;
FT ->files[i]->fd = i*BLOCK_SIZE;
FT->files[i]->permissions = -1;
FT -> files[i] ->file_type = NONE;
FT -> files[i] -> spaceleft = BLOCK_SIZE;
FT -> files[i] -> next = NULL;
FT ->files[i] ->prev = NULL;
FT ->files[i]->is_init = false;
FT->files[i] -> path = NULL;
FT->files[i]->linkcount = 0;
FT->files[i]->timestamp = 0;
}
FT->files[0]->path = malloc(2);
strcpy(FT->files[0]->path,"/");
FT->files[0]->is_init = true;
FT->files[0] -> file_type = DIR_NODE;
log_conn(conn);
fuse_get_context()->uid = getuid();
fuse_get_context()->gid = getgid();
fuse_get_context()->pid = getpid();
log_fuse_context(fuse_get_context());
return SFS_DATA;
}
/**
* Clean up filesystem
*
* Called on filesystem exit.
*
* Introduced in version 2.3
*/
void sfs_destroy(void *userdata)
{
log_msg("\nsfs_destroy(userdata=0x%08x)\n", userdata);
}
/** Get file attributes.
*
* Similar to stat(). The 'st_dev' and 'st_blksize' fields are
* ignored. The 'st_ino' field is ignored except if the 'use_ino'
* mount option is given.
*/
int sfs_getattr(const char *path, struct stat *statbuf)
{
int retstat = 0;
char fpath[PATH_MAX];
strcpy(fpath,path);
log_msg("\nsfs_getattr(path=\"%s\", statbuf=0x%08x)\n",
path, statbuf);
if(getFilePath(fpath) == NULL)
{
errno = EBADF;
return -1;
}
//lookup the FILE TABLE FOR THE PATH;
statbuf->st_dev = 0;
statbuf->st_ino = 0;
Inode * file = getFilePath(fpath);
statbuf->st_mode = S_IRWXU;
statbuf->st_nlink = file->linkcount;
log_msg("HERE!\n");
//How do we get the userid of the person who ran the program?
statbuf->st_uid = getuid();
statbuf->st_gid = getgid();
log_msg("HERE2\n");
statbuf->st_size = fileSize(file);
statbuf->st_atime = file-> timestamp;
statbuf->st_mtime = 0;
statbuf->st_ctime = 0;
statbuf->st_blksize = 0;
statbuf->st_blocks = fileTotalSize(file)/BLOCK_SIZE;
return retstat;
}
/**
* Create and open a file
*
* If the file does not exist, first create it with the specified
* mode, and then open it.
*
* If this method is not implemented or under Linux kernel
* versions earlier than 2.6.15, the mknod() and open() methods
* will be called instead.
*
* Introduced in version 2.5
*/
int sfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
int retstat = 0;
log_msg("\nsfs_create(path=\"%s\", mode=0%03o, fi=0x%08x)\n",
path, mode, fi);
return retstat;
}
/** Remove a file */
int sfs_unlink(const char *path)
{
int retstat = 0;
log_msg("sfs_unlink(path=\"%s\")\n", path);
return retstat;
}
/** File open operation
*
* No creation, or truncation flags (O_CREAT, O_EXCL, O_TRUNC)
* will be passed to open(). Open should check if the operation
* is permitted for the given flags. Optionally open may also
* return an arbitrary filehandle in the fuse_file_info structure,
* which will be passed to all file operations.
*
* Changed in version 2.2
*/
int sfs_open(const char *path, struct fuse_file_info *fi)
{
int retstat = 0;
log_msg("\nsfs_open(path\"%s\", fi=0x%08x)\n",
path, fi);
return retstat;
}
/** Release an open file
*
* Release is called when there are no more references to an open
* file: all file descriptors are closed and all memory mappings
* are unmapped.
*
* For every open() call there will be exactly one release() call
* with the same flags and file descriptor. It is possible to
* have a file opened more than once, in which case only the last
* release will mean, that no more reads/writes will happen on the
* file. The return value of release is ignored.
*
* Changed in version 2.2
*/
int sfs_release(const char *path, struct fuse_file_info *fi)
{
int retstat = 0;
log_msg("\nsfs_release(path=\"%s\", fi=0x%08x)\n",
path, fi);
return retstat;
}
/** Read data from an open file
*
* Read should return exactly the number of bytes requested except
* on EOF or error, otherwise the rest of the data will be
* substituted with zeroes. An exception to this is when the
* 'direct_io' mount option is specified, in which case the return
* value of the read system call will reflect the return value of
* this operation.
*
* Changed in version 2.2
*/
int sfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int retstat = 0;
log_msg("\nsfs_read(path=\"%s\", buf=0x%08x, size=%d, offset=%lld, fi=0x%08x)\n",
path, buf, size, offset, fi);
return retstat;
}
/** Write data to an open file
*
* Write should return exactly the number of bytes requested
* except on error. An exception to this is when the 'direct_io'
* mount option is specified (see read operation).
*
* Changed in version 2.2
*/
int sfs_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int retstat = 0;
log_msg("\nsfs_write(path=\"%s\", buf=0x%08x, size=%d, offset=%lld, fi=0x%08x)\n",
path, buf, size, offset, fi);
return retstat;
}
/** Create a directory */
int sfs_mkdir(const char *path, mode_t mode)
{
int retstat = 0;
log_msg("\nsfs_mkdir(path=\"%s\", mode=0%3o)\n",
path, mode);
return retstat;
}
/** Remove a directory */
int sfs_rmdir(const char *path)
{
int retstat = 0;
log_msg("sfs_rmdir(path=\"%s\")\n",
path);
return retstat;
}
/** Open directory
*
* This method should check if the open operation is permitted for
* this directory
*
* Introduced in version 2.3
*/
int sfs_opendir(const char *path, struct fuse_file_info *fi)
{
int retstat = 0;
log_msg("\nsfs_opendir(path=\"%s\", fi=0x%08x)\n",
path, fi);
return retstat;
}
/** Read directory
*
* This supersedes the old getdir() interface. New applications
* should use this.
*
* The filesystem may choose between two modes of operation:
*
* 1) The readdir implementation ignores the offset parameter, and
* passes zero to the filler function's offset. The filler
* function will not return '1' (unless an error happens), so the
* whole directory is read in a single readdir operation. This
* works just like the old getdir() method.
*
* 2) The readdir implementation keeps track of the offsets of the
* directory entries. It uses the offset parameter and always
* passes non-zero offset to the filler function. When the buffer
* is full (or an error happens) the filler function will return
* '1'.
*
* Introduced in version 2.3
*/
int sfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
struct fuse_file_info *fi)
{
int retstat = 0;
return retstat;
}
/** Release directory
*
* Introduced in version 2.3
*/
int sfs_releasedir(const char *path, struct fuse_file_info *fi)
{
int retstat = 0;
return retstat;
}
struct fuse_operations sfs_oper = {
.init = sfs_init,
.destroy = sfs_destroy,
.getattr = sfs_getattr,
.create = sfs_create,
.unlink = sfs_unlink,
.open = sfs_open,
.release = sfs_release,
.read = sfs_read,
.write = sfs_write,
.rmdir = sfs_rmdir,
.mkdir = sfs_mkdir,
.opendir = sfs_opendir,
.readdir = sfs_readdir,
.releasedir = sfs_releasedir
};
void sfs_usage()
{
fprintf(stderr, "usage: sfs [FUSE and mount options] diskFile mountPoint\n");
abort();
}
int main(int argc, char *argv[])
{
int fuse_stat;
struct sfs_state *sfs_data;
// sanity checking on the command line
if ((argc < 3) || (argv[argc-2][0] == '-') || (argv[argc-1][0] == '-'))
sfs_usage();
sfs_data = malloc(sizeof(struct sfs_state));
if (sfs_data == NULL) {
perror("main calloc");
abort();
}
// Pull the diskfile and save it in internal data
sfs_data->diskfile = argv[argc-2];
argv[argc-2] = argv[argc-1];
argv[argc-1] = NULL;
argc--;
sfs_data->logfile = log_open();
// turn over control to fuse
fprintf(stderr, "about to call fuse_main, %s \n", sfs_data->diskfile);
fuse_stat = fuse_main(argc, argv, &sfs_oper, sfs_data);
fprintf(stderr, "fuse_main returned %d\n", fuse_stat);
return fuse_stat;
}