aboutsummaryrefslogtreecommitdiff
path: root/src/tools/myriadfs.cpp
blob: 8db59d798a96ec781fc4d43bd726a3cda7baf4bf (plain)
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
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#define FUSE_USE_VERSION 26

#include <fuse.h>

#include <string.h>
#include <errno.h>

#include <bu/file.h>
#include <bu/myriadfs.h>
#include <bu/myriadstream.h>
#include <bu/hash.h>

Bu::File *pF = NULL;
Bu::MyriadFs *pFs = NULL;

typedef Bu::Hash<int64_t, Bu::MyriadStream> FileHash;
FileHash hOpenFiles;
int64_t iNextFileId = 0;

extern "C" {
	static int myriadfs_getattr( const char *sPath, struct stat *stbuf )
	{
		try
		{
			Bu::MyriadFs::Stat st;
			pFs->stat( sPath, st );
			stbuf->st_ino = st.iNode;
			stbuf->st_mode = st.uPerms;
			stbuf->st_nlink = st.iLinks;
			stbuf->st_uid = st.iUser;
			stbuf->st_gid = st.iGroup;
			stbuf->st_rdev = Bu::MyriadFs::devToSys( st.uDev );
			stbuf->st_size = st.iSize;
			stbuf->st_blocks = 8;
			stbuf->st_atime = st.iATime;
			stbuf->st_mtime = st.iMTime;
			stbuf->st_ctime = st.iCTime;
			return 0;
		}
		catch(...)
		{
			return -ENOENT;
		}
	}

	static int myriadfs_readdir( const char *sPath, void *buf,
			fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi )
	{
		Bu::MyriadFs::Dir lDir = pFs->readDir( sPath );
		filler( buf, ".", NULL, 0 );
		filler( buf, "..", NULL, 0 );
		for( Bu::MyriadFs::Dir::iterator i = lDir.begin(); i; i++ )
		{
			filler( buf, (*i).sName.getStr(), NULL, 0 );
		}

		return 0;
	}

	static int myriadfs_mkdir( const char *sPath, mode_t uMode )
	{
		pFs->mkDir( sPath, uMode );
		return 0;
	}

	static int myriadfs_open( const char *sPath, struct fuse_file_info *fi )
	{
		try
		{
			Bu::MyriadStream ms = pFs->open( sPath, 0 );
			fi->fh = iNextFileId;
			hOpenFiles.insert( iNextFileId++, ms );
			printf("File '%s' opened, %d files open now.\n",
				sPath, hOpenFiles.getSize() );
			return 0;
		}
		catch(...)
		{
			return -EACCES;
		}
	}

	static int myriadfs_read( const char *sPath, char *buf, size_t iSize,
			off_t iOffset, struct fuse_file_info *fi )
	{
		Bu::MyriadStream &ms = hOpenFiles.get( fi->fh );
		ms.setPos( iOffset );
		return ms.read( buf, iSize );
	}

	static int myriadfs_write( const char *sPath, const char *buf, size_t iSize,
			off_t iOffset, struct fuse_file_info *fi )
	{
		Bu::MyriadStream &ms = hOpenFiles.get( fi->fh );
		ms.setPos( iOffset );
		return ms.write( buf, iSize );
	}

	static int myriadfs_create( const char *sPath, mode_t uPerms,
			struct fuse_file_info *fi )
	{
		try
		{
			Bu::MyriadStream ms = pFs->open( sPath, 0, uPerms );
			fi->fh = iNextFileId;
			hOpenFiles.insert( iNextFileId++, ms );
			printf("File '%s' created, %d files open now.\n",
				sPath, hOpenFiles.getSize() );
			return 0;
		}
		catch(...)
		{
			return -EACCES;
		}
	}
	
	static int myriadfs_mknod( const char *sPath, mode_t uPerms, dev_t Dev )
	{
		try
		{
			pFs->create( sPath, uPerms, Bu::MyriadFs::sysToDev( Dev ) );
			return 0;
		}
		catch(...)
		{
			return -EACCES;
		}
	}

	static int myriadfs_release( const char *sPath, struct fuse_file_info *fi )
	{
		hOpenFiles.erase( fi->fh );
		printf("File '%s' released, %d files open now.\n",
			sPath, hOpenFiles.getSize() );

		return 0;
	}

	static int myriadfs_utimens( const char *sPath,
			const struct timespec tv[2] )
	{
		try
		{
			pFs->setTimes( sPath, tv[0].tv_sec, tv[1].tv_sec );
		}
		catch(...)
		{
			return -EACCES;
		}
		return 0;
	}

	static int myriadfs_unlink( const char *sPath )
	{
		try
		{
			pFs->unlink( sPath );
		}
		catch( Bu::MyriadFsException &e )
		{
			printf("MyriadFsException: %s\n", e.what() );
			return -EACCES;
		}
		return 0;
	}

	static int myriadfs_symlink( const char *sTarget, const char *sPath )
	{
		try
		{
			printf("Path = '%s', Target = '%s'\n", sPath, sTarget );
			pFs->mkSymLink( sPath, sTarget );
		}
		catch( Bu::MyriadFsException &e )
		{
			printf("MyriadFsException: %s\n", e.what() );
			return -EACCES;
		}
		return 0;
	}

	static int myriadfs_readlink( const char *sPath, char *sOut, size_t s )
	{
		try
		{
			Bu::String sTrg = pFs->readSymLink( sPath );
			size_t iLen = (s-1>sTrg.getSize())?(sTrg.getSize()):(s-1);
			memcpy( sOut, sTrg.getStr(), iLen );
			sOut[iLen] = '\0';
		}
		catch( Bu::MyriadFsException &e )
		{
			printf("MyriadFsException: %s\n", e.what() );
			return -EACCES;
		}
		return 0;
	}

	static struct fuse_operations myriadfs_oper;

	int main( int argc, char *argv[] )
	{
		pF = new Bu::File("store.myr", Bu::File::Read|Bu::File::Write|Bu::File::Create );
		pFs = new Bu::MyriadFs( *pF, 512 );
		memset( &myriadfs_oper, sizeof(myriadfs_oper), 0 );
		myriadfs_oper.getattr    = myriadfs_getattr;
		myriadfs_oper.readdir    = myriadfs_readdir;
		myriadfs_oper.mkdir		 = myriadfs_mkdir;
		myriadfs_oper.open       = myriadfs_open;
		myriadfs_oper.read       = myriadfs_read;
		myriadfs_oper.write      = myriadfs_write;
		myriadfs_oper.create	 = myriadfs_create;
		myriadfs_oper.mknod		 = myriadfs_mknod;
		myriadfs_oper.release	 = myriadfs_release;
		myriadfs_oper.utimens	 = myriadfs_utimens;
		myriadfs_oper.unlink	 = myriadfs_unlink;
		myriadfs_oper.rmdir		 = myriadfs_unlink;
		myriadfs_oper.symlink	 = myriadfs_symlink;
		myriadfs_oper.readlink	 = myriadfs_readlink;
		printf("Starting fuse_main.\n");
		int iRet = fuse_main( argc, argv, &myriadfs_oper, NULL );
		printf("Done with fuse_main.\n");
		delete pFs;
		delete pF;
		return iRet;
	}
}