diff options
Diffstat (limited to 'src/extra/myriadfs.cpp')
-rw-r--r-- | src/extra/myriadfs.cpp | 333 |
1 files changed, 333 insertions, 0 deletions
diff --git a/src/extra/myriadfs.cpp b/src/extra/myriadfs.cpp new file mode 100644 index 0000000..3e4599b --- /dev/null +++ b/src/extra/myriadfs.cpp | |||
@@ -0,0 +1,333 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #define FUSE_USE_VERSION 26 | ||
9 | |||
10 | #include <fuse.h> | ||
11 | |||
12 | #include <string.h> | ||
13 | #include <errno.h> | ||
14 | |||
15 | #include <bu/file.h> | ||
16 | #include <bu/myriadfs.h> | ||
17 | #include <bu/myriadstream.h> | ||
18 | #include <bu/hash.h> | ||
19 | |||
20 | Bu::File *pF = NULL; | ||
21 | Bu::MyriadFs *pFs = NULL; | ||
22 | |||
23 | typedef Bu::Hash<int64_t, Bu::MyriadStream> FileHash; | ||
24 | FileHash hOpenFiles; | ||
25 | int64_t iNextFileId = 0; | ||
26 | |||
27 | #define TRACE | ||
28 | |||
29 | extern "C" { | ||
30 | static int myriadfs_getattr( const char *sPath, struct stat *stbuf ) | ||
31 | { | ||
32 | #ifdef TRACE | ||
33 | printf("myriadfs_getattr(\"%s\", ... );\n", sPath ); | ||
34 | #endif | ||
35 | try | ||
36 | { | ||
37 | Bu::MyriadFs::Stat st; | ||
38 | pFs->stat( sPath, st ); | ||
39 | stbuf->st_ino = st.iNode; | ||
40 | stbuf->st_mode = st.uPerms; | ||
41 | stbuf->st_nlink = st.iLinks; | ||
42 | stbuf->st_uid = st.iUser; | ||
43 | stbuf->st_gid = st.iGroup; | ||
44 | stbuf->st_rdev = Bu::MyriadFs::devToSys( st.uDev ); | ||
45 | stbuf->st_size = st.iSize; | ||
46 | stbuf->st_blocks = 8; | ||
47 | stbuf->st_atime = st.iATime; | ||
48 | stbuf->st_mtime = st.iMTime; | ||
49 | stbuf->st_ctime = st.iCTime; | ||
50 | return 0; | ||
51 | } | ||
52 | catch(...) | ||
53 | { | ||
54 | return -ENOENT; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | static int myriadfs_readdir( const char *sPath, void *buf, | ||
59 | fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi ) | ||
60 | { | ||
61 | #ifdef TRACE | ||
62 | printf("myriadfs_readdir(\"%s\", ... );\n", sPath ); | ||
63 | #endif | ||
64 | Bu::MyriadFs::Dir lDir = pFs->readDir( sPath ); | ||
65 | filler( buf, ".", NULL, 0 ); | ||
66 | filler( buf, "..", NULL, 0 ); | ||
67 | for( Bu::MyriadFs::Dir::iterator i = lDir.begin(); i; i++ ) | ||
68 | { | ||
69 | filler( buf, (*i).sName.getStr(), NULL, 0 ); | ||
70 | } | ||
71 | |||
72 | return 0; | ||
73 | } | ||
74 | |||
75 | static int myriadfs_mkdir( const char *sPath, mode_t uMode ) | ||
76 | { | ||
77 | #ifdef TRACE | ||
78 | printf("myriadfs_mkdir(\"%s\", 0%o );\n", sPath, uMode ); | ||
79 | #endif | ||
80 | pFs->mkDir( sPath, uMode ); | ||
81 | return 0; | ||
82 | } | ||
83 | |||
84 | static int myriadfs_open( const char *sPath, struct fuse_file_info *fi ) | ||
85 | { | ||
86 | #ifdef TRACE | ||
87 | printf("myriadfs_open(\"%s\", ... );\n", sPath ); | ||
88 | #endif | ||
89 | try | ||
90 | { | ||
91 | Bu::MyriadStream ms = pFs->open( sPath, 0 ); | ||
92 | fi->fh = iNextFileId; | ||
93 | hOpenFiles.insert( iNextFileId++, ms ); | ||
94 | // printf("File '%s' opened, %d files open now.\n", | ||
95 | // sPath, hOpenFiles.getSize() ); | ||
96 | return 0; | ||
97 | } | ||
98 | catch(...) | ||
99 | { | ||
100 | return -EACCES; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | static int myriadfs_read( const char *sPath, char *buf, size_t iSize, | ||
105 | off_t iOffset, struct fuse_file_info *fi ) | ||
106 | { | ||
107 | #ifdef TRACE | ||
108 | printf("myriadfs_read(\"%s\", ..., %d, %d, ... );\n", sPath, iSize, | ||
109 | iOffset ); | ||
110 | #endif | ||
111 | Bu::MyriadStream &ms = hOpenFiles.get( fi->fh ); | ||
112 | ms.setPos( iOffset ); | ||
113 | return ms.read( buf, iSize ); | ||
114 | } | ||
115 | |||
116 | static int myriadfs_write( const char *sPath, const char *buf, size_t iSize, | ||
117 | off_t iOffset, struct fuse_file_info *fi ) | ||
118 | { | ||
119 | #ifdef TRACE | ||
120 | printf("myriadfs_write(\"%s\", ..., %d, %d, ... );\n", sPath, iSize, | ||
121 | iOffset ); | ||
122 | #endif | ||
123 | Bu::MyriadStream &ms = hOpenFiles.get( fi->fh ); | ||
124 | ms.setPos( iOffset ); | ||
125 | return ms.write( buf, iSize ); | ||
126 | } | ||
127 | |||
128 | static int myriadfs_create( const char *sPath, mode_t uPerms, | ||
129 | struct fuse_file_info *fi ) | ||
130 | { | ||
131 | #ifdef TRACE | ||
132 | printf("myriadfs_create(\"%s\", 0%o, ... );\n", sPath, uPerms ); | ||
133 | #endif | ||
134 | try | ||
135 | { | ||
136 | Bu::MyriadStream ms = pFs->open( sPath, 0, uPerms ); | ||
137 | fi->fh = iNextFileId; | ||
138 | hOpenFiles.insert( iNextFileId++, ms ); | ||
139 | // printf("File '%s' created, %d files open now.\n", | ||
140 | // sPath, hOpenFiles.getSize() ); | ||
141 | return 0; | ||
142 | } | ||
143 | catch(...) | ||
144 | { | ||
145 | return -EACCES; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | static int myriadfs_mknod( const char *sPath, mode_t uPerms, dev_t Dev ) | ||
150 | { | ||
151 | #ifdef TRACE | ||
152 | printf("myriadfs_mknod(\"%s\", 0%o, %x );\n", sPath, uPerms, Dev ); | ||
153 | #endif | ||
154 | try | ||
155 | { | ||
156 | pFs->create( sPath, uPerms, Bu::MyriadFs::sysToDev( Dev ) ); | ||
157 | return 0; | ||
158 | } | ||
159 | catch(...) | ||
160 | { | ||
161 | return -EACCES; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | static int myriadfs_release( const char *sPath, struct fuse_file_info *fi ) | ||
166 | { | ||
167 | #ifdef TRACE | ||
168 | printf("myriadfs_release(\"%s\", ... );\n", sPath ); | ||
169 | #endif | ||
170 | hOpenFiles.erase( fi->fh ); | ||
171 | // printf("File '%s' released, %d files open now.\n", | ||
172 | // sPath, hOpenFiles.getSize() ); | ||
173 | |||
174 | return 0; | ||
175 | } | ||
176 | |||
177 | static int myriadfs_utimens( const char *sPath, | ||
178 | const struct timespec tv[2] ) | ||
179 | { | ||
180 | #ifdef TRACE | ||
181 | printf("myriadfs_utimens(\"%s\", ... );\n", sPath ); | ||
182 | #endif | ||
183 | try | ||
184 | { | ||
185 | pFs->setTimes( sPath, tv[0].tv_sec, tv[1].tv_sec ); | ||
186 | } | ||
187 | catch(...) | ||
188 | { | ||
189 | return -EACCES; | ||
190 | } | ||
191 | return 0; | ||
192 | } | ||
193 | |||
194 | static int myriadfs_unlink( const char *sPath ) | ||
195 | { | ||
196 | #ifdef TRACE | ||
197 | printf("myriadfs_unlink(\"%s\");\n", sPath ); | ||
198 | #endif | ||
199 | try | ||
200 | { | ||
201 | pFs->unlink( sPath ); | ||
202 | } | ||
203 | catch( Bu::MyriadFsException &e ) | ||
204 | { | ||
205 | printf("MyriadFsException: %s\n", e.what() ); | ||
206 | return -EACCES; | ||
207 | } | ||
208 | return 0; | ||
209 | } | ||
210 | |||
211 | static int myriadfs_symlink( const char *sTarget, const char *sPath ) | ||
212 | { | ||
213 | #ifdef TRACE | ||
214 | printf("myriadfs_symlink(\"%s\", \"%s\");\n", sTarget, sPath ); | ||
215 | #endif | ||
216 | try | ||
217 | { | ||
218 | pFs->mkSymLink( sTarget, sPath ); | ||
219 | } | ||
220 | catch( Bu::MyriadFsException &e ) | ||
221 | { | ||
222 | printf("MyriadFsException: %s\n", e.what() ); | ||
223 | return -EACCES; | ||
224 | } | ||
225 | return 0; | ||
226 | } | ||
227 | |||
228 | static int myriadfs_readlink( const char *sPath, char *sOut, size_t s ) | ||
229 | { | ||
230 | #ifdef TRACE | ||
231 | printf("myriadfs_readlink(\"%s\", ... );\n", sPath ); | ||
232 | #endif | ||
233 | try | ||
234 | { | ||
235 | Bu::String sTrg = pFs->readSymLink( sPath ); | ||
236 | size_t iLen = (s-1>sTrg.getSize())?(sTrg.getSize()):(s-1); | ||
237 | memcpy( sOut, sTrg.getStr(), iLen ); | ||
238 | sOut[iLen] = '\0'; | ||
239 | } | ||
240 | catch( Bu::MyriadFsException &e ) | ||
241 | { | ||
242 | printf("MyriadFsException: %s\n", e.what() ); | ||
243 | return -EACCES; | ||
244 | } | ||
245 | return 0; | ||
246 | } | ||
247 | |||
248 | static int myriadfs_truncate( const char *sPath, off_t iSize ) | ||
249 | { | ||
250 | #ifdef TRACE | ||
251 | printf("myriadfs_truncate(\"%s\", %d );\n", sPath, iSize ); | ||
252 | #endif | ||
253 | |||
254 | try | ||
255 | { | ||
256 | pFs->setFileSize( sPath, iSize ); | ||
257 | } | ||
258 | catch( Bu::MyriadFsException &e ) | ||
259 | { | ||
260 | printf("MyriadFsException: %s\n", e.what() ); | ||
261 | return -ENOENT; | ||
262 | } | ||
263 | return 0; | ||
264 | } | ||
265 | |||
266 | static int myriadfs_link( const char *sTarget, const char *sPath ) | ||
267 | { | ||
268 | #ifdef TRACE | ||
269 | printf("myriadfs_link(\"%s\", \"%s\");\n", sTarget, sPath ); | ||
270 | #endif | ||
271 | |||
272 | try | ||
273 | { | ||
274 | pFs->mkHardLink( sTarget, sPath ); | ||
275 | } | ||
276 | catch( Bu::MyriadFsException &e ) | ||
277 | { | ||
278 | printf("MyriadFsException: %s\n", e.what() ); | ||
279 | return -EACCES; | ||
280 | } | ||
281 | return 0; | ||
282 | } | ||
283 | |||
284 | static int myriadfs_rename( const char *sFrom, const char *sTo ) | ||
285 | { | ||
286 | #ifdef TRACE | ||
287 | printf("myriadfs_rename(\"%s\", \"%s\");\n", sFrom, sTo ); | ||
288 | #endif | ||
289 | |||
290 | try | ||
291 | { | ||
292 | pFs->rename( sFrom, sTo ); | ||
293 | } | ||
294 | catch( Bu::MyriadFsException &e ) | ||
295 | { | ||
296 | printf("MyriadFsException: %s\n", e.what() ); | ||
297 | return -EACCES; | ||
298 | } | ||
299 | return 0; | ||
300 | } | ||
301 | |||
302 | static struct fuse_operations myriadfs_oper; | ||
303 | |||
304 | int main( int argc, char *argv[] ) | ||
305 | { | ||
306 | pF = new Bu::File("store.myr", Bu::File::Read|Bu::File::Write|Bu::File::Create ); | ||
307 | pFs = new Bu::MyriadFs( *pF, 512 ); | ||
308 | memset( &myriadfs_oper, sizeof(myriadfs_oper), 0 ); | ||
309 | myriadfs_oper.getattr = myriadfs_getattr; | ||
310 | myriadfs_oper.readdir = myriadfs_readdir; | ||
311 | myriadfs_oper.mkdir = myriadfs_mkdir; | ||
312 | myriadfs_oper.open = myriadfs_open; | ||
313 | myriadfs_oper.read = myriadfs_read; | ||
314 | myriadfs_oper.write = myriadfs_write; | ||
315 | myriadfs_oper.create = myriadfs_create; | ||
316 | myriadfs_oper.mknod = myriadfs_mknod; | ||
317 | myriadfs_oper.release = myriadfs_release; | ||
318 | myriadfs_oper.utimens = myriadfs_utimens; | ||
319 | myriadfs_oper.unlink = myriadfs_unlink; | ||
320 | myriadfs_oper.rmdir = myriadfs_unlink; | ||
321 | myriadfs_oper.symlink = myriadfs_symlink; | ||
322 | myriadfs_oper.readlink = myriadfs_readlink; | ||
323 | myriadfs_oper.truncate = myriadfs_truncate; | ||
324 | myriadfs_oper.link = myriadfs_link; | ||
325 | myriadfs_oper.rename = myriadfs_rename; | ||
326 | printf("Starting fuse_main.\n"); | ||
327 | int iRet = fuse_main( argc, argv, &myriadfs_oper, NULL ); | ||
328 | printf("Done with fuse_main.\n"); | ||
329 | delete pFs; | ||
330 | delete pF; | ||
331 | return iRet; | ||
332 | } | ||
333 | } | ||