-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
200 lines (168 loc) · 5.35 KB
/
main.cpp
File metadata and controls
200 lines (168 loc) · 5.35 KB
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
// #define FUSE_USE_VERSION 31
// #include<fuse3/fuse.h>
// #include<iostream>
// #include "src/include/fuse_functions/getattr.h"
// #include "src/include/fuse_functions/readdir.h"
// #include "src/include/fuse_functions/opendir.h"
// using namespace std;
// // int get_attr(const char* path, struct stat* stbuf, struct fuse_file_info* fi){
// // cout<<"getattr called for path: "<<path<<endl;
// // // Implementation of getattr would go here
// // return 0;
// // }
// // int readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi, enum fuse_readdir_flags flags){
// // cout<<"readdir called for path: "<<path<<endl;
// // // Implementation of readdir would go here
// // return 0;
// // }
// static struct fuse_operations fastdevfs_oper = {
// // .getattr = get_attr,
// // .readdir = readdir,
// .getattr = fastdevfs_getattr,
// .opendir = fastdevfs_opendir,
// .readdir = fastdevfs_readdir,
// };
// int main(int argc, char* argv[]){
// cout<<"Starting FastDevFs Daemon..."<<endl;
// // Initialization code for FastDevFs would go here
// return fuse_main(argc, argv, &fastdevfs_oper, NULL);
// }
#define FUSE_USE_VERSION 31
#include <fuse3/fuse.h>
#include <iostream>
#include "daemon/dir_manager.h"
#include "daemon/hash.h"
#include "daemon/file_io.h"
#include "fuse_functions/getattr.h"
#include "fuse_functions/readdir.h"
#include "fuse_functions/opendir.h"
#include "fuse_functions/mkdir.h"
#include "fuse_functions/rmdir.h"
#include "fuse_functions/access.h"
#include "fuse_functions/statfs.h"
#include "fuse_functions/file_funcs.h"
#include "fuse_functions/utimens.h"
#include "sys/mman.h"
#include "config.h"
#include <unistd.h>
using namespace std;
#define DIR_TREE_FILE "dir_tree.dat"
#define HASH_TABLE_FILE "hash_table.dat"
/*
* Global DirManager instance.
* Used by all FUSE callbacks.
*/
DirManager* g_dir_manager = nullptr;
static struct fuse_operations fdfs_ops;
extern FSConfig g_config;
// static struct fuse_operations fdfs_ops = {
// .getattr = fdfs_getattr,
// .readdir = fdfs_readdir,
// .opendir = fdfs_opendir,
// // .mkdir = fdfs_mkdir,
// // .rmdir = fdfs_rmdir,
// // .access = fdfs_access,
// // .statfs = fdfs_statfs,
// // // safety stubs
// // .open = fdfs_open,
// // .read = fdfs_read,
// // .write = fdfs_write,
// // .create = fdfs_create,
// // .unlink = fdfs_unlink,
// // .truncate = fdfs_truncate,
// // // metadata no-ops
// // .chmod = fdfs_chmod,
// // .chown = fdfs_chown,
// // .utimens = fdfs_utimens,
// };
int main(int argc, char* argv[]) {
cout << "Starting FastDevFS Daemon..." << endl;
// Initialize DirManager with mmap
int fd = open(DIR_TREE_FILE, O_RDWR | O_CREAT, 0644);
if (fd < 0) {
perror("open");
exit(1);
}
size_t dir_manager_size = sizeof(DirManager);
/* ensure file size */
if (ftruncate(fd, dir_manager_size) < 0) {
perror("ftruncate");
exit(1);
}
/* mmap - maps DirManager to shared memory */
g_dir_manager = (DirManager*) mmap(
NULL,
dir_manager_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0
);
if (g_dir_manager == MAP_FAILED) {
perror("mmap");
exit(1);
}
// Initialize directory ADT only if not already initialized
if (!is_dir_manager_initialized(g_dir_manager)) {
dir_manager_init(g_dir_manager);
}
// Initialize data directory for file storage
if (init_data_dir() < 0) {
cerr << "Failed to initialize data directory" << endl;
exit(1);
}
// Initialize HashTable with separate mmap
int hash_fd = open(HASH_TABLE_FILE, O_RDWR | O_CREAT, 0644);
if (hash_fd < 0) {
perror("open hash_table");
exit(1);
}
size_t hash_table_size = sizeof(HashTable);
/* ensure file size */
if (ftruncate(hash_fd, hash_table_size) < 0) {
perror("ftruncate hash_table");
exit(1);
}
/* mmap - maps HashTable to shared memory */
g_hash_table = (HashTable*) mmap(
NULL,
hash_table_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
hash_fd,
0
);
if (g_hash_table == MAP_FAILED) {
perror("mmap hash_table");
exit(1);
}
// Initialize hash table only if not already initialized
if (g_hash_table->magic != HASH_TABLE_MAGIC) {
hash_init(g_hash_table);
}
// basic fuse_functions
fdfs_ops.getattr = fdfs_getattr;
fdfs_ops.opendir = fdfs_opendir;
fdfs_ops.readdir = fdfs_readdir;
fdfs_ops.mkdir = fdfs_mkdir;
fdfs_ops.rmdir = fdfs_rmdir;
fdfs_ops.access = fdfs_access;
fdfs_ops.statfs = fdfs_statfs;
// file ops
fdfs_ops.open = fdfs_open;
fdfs_ops.read = fdfs_read;
fdfs_ops.write = fdfs_write;
fdfs_ops.create = fdfs_create;
fdfs_ops.unlink = fdfs_unlink;
fdfs_ops.truncate = fdfs_truncate;
fdfs_ops.utimens = fdfs_utimens;
// load config
load_config("/home/diya_limbani/FastDevFs/fdfs.conf");
if(g_config.dedup_enabled) {
// to do
}else{
cout<<"WARN: We are not using dedup-thread. This can lead to higher memory usage.";
}
// Start FUSE
return fuse_main(argc, argv, &fdfs_ops, nullptr);
}