diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/extra/mount.myriadfs.cpp (renamed from src/extra/myriadfs.cpp) | 0 | ||||
-rw-r--r-- | src/tests/myriadfs.cpp | 14 | ||||
-rw-r--r-- | src/tools/myriadfs.cpp | 251 | ||||
-rw-r--r-- | src/unstable/myriadfs.cpp | 9 |
4 files changed, 272 insertions, 2 deletions
diff --git a/src/extra/myriadfs.cpp b/src/extra/mount.myriadfs.cpp index ff2df3d..ff2df3d 100644 --- a/src/extra/myriadfs.cpp +++ b/src/extra/mount.myriadfs.cpp | |||
diff --git a/src/tests/myriadfs.cpp b/src/tests/myriadfs.cpp index cb6c6c3..1266e4b 100644 --- a/src/tests/myriadfs.cpp +++ b/src/tests/myriadfs.cpp | |||
@@ -30,6 +30,14 @@ int main() | |||
30 | Bu::MyriadStream ms = mfs.open("/etc/hello", Bu::MyriadFs::Read ); | 30 | Bu::MyriadStream ms = mfs.open("/etc/hello", Bu::MyriadFs::Read ); |
31 | ms.write("world, again!"); | 31 | ms.write("world, again!"); |
32 | } | 32 | } |
33 | { | ||
34 | Bu::MyriadStream ms = mfs.open("/etc/trunc", Bu::MyriadFs::Write ); | ||
35 | ms.write("[longer text shouldn't be seen]"); | ||
36 | } | ||
37 | { | ||
38 | Bu::MyriadStream ms = mfs.open("/etc/trunc", Bu::MyriadFs::Write|Bu::MyriadFs::Create|Bu::MyriadFs::Truncate ); | ||
39 | ms.write("[short text]"); | ||
40 | } | ||
33 | 41 | ||
34 | sio << "Reading files..." << sio.nl; | 42 | sio << "Reading files..." << sio.nl; |
35 | { | 43 | { |
@@ -44,5 +52,11 @@ int main() | |||
44 | buf[ms.read( buf, 512 )] = '\0'; | 52 | buf[ms.read( buf, 512 )] = '\0'; |
45 | sio << "read: '" << buf << "'" << sio.nl; | 53 | sio << "read: '" << buf << "'" << sio.nl; |
46 | } | 54 | } |
55 | { | ||
56 | Bu::MyriadStream ms = mfs.open("/etc/trunc", Bu::MyriadFs::Read ); | ||
57 | char buf[512]; | ||
58 | buf[ms.read( buf, 512 )] = '\0'; | ||
59 | sio << "read: '" << buf << "'" << sio.nl; | ||
60 | } | ||
47 | } | 61 | } |
48 | 62 | ||
diff --git a/src/tools/myriadfs.cpp b/src/tools/myriadfs.cpp new file mode 100644 index 0000000..9051779 --- /dev/null +++ b/src/tools/myriadfs.cpp | |||
@@ -0,0 +1,251 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2023 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 | #include "bu/sio.h" | ||
9 | #include "bu/streamstack.h" | ||
10 | #include "bu/file.h" | ||
11 | #include "bu/myriadfs.h" | ||
12 | #include "bu/myriadstream.h" | ||
13 | #include "bu/optparser.h" | ||
14 | |||
15 | enum Mode | ||
16 | { | ||
17 | modeList, | ||
18 | modeCat, | ||
19 | modeCopyIn, | ||
20 | modeCopyOut, | ||
21 | modeMkdir, | ||
22 | modeInitialize, | ||
23 | |||
24 | modeNone | ||
25 | }; | ||
26 | |||
27 | Bu::Formatter &operator>>( Bu::Formatter &f, Mode & /*e*/ ) | ||
28 | { | ||
29 | Bu::sio << "Uh oh, the formatter was called..." << Bu::sio.nl; | ||
30 | return f; | ||
31 | } | ||
32 | |||
33 | class Options : public Bu::OptParser | ||
34 | { | ||
35 | public: | ||
36 | Options( int argc, char *argv[] ) : | ||
37 | eMode( modeNone ), | ||
38 | iBlockSize( 64 ) | ||
39 | { | ||
40 | addHelpBanner("Options:"); | ||
41 | addOption( sFile, 'f', "file", "Myriadfs file"); | ||
42 | addOption( iBlockSize, 'b', "block-size", | ||
43 | "Specify the block size when initializing a new MyriadFs"); | ||
44 | |||
45 | setNonOption( Bu::slot( this, &Options::nonOption ) ); | ||
46 | |||
47 | addHelpOption(); | ||
48 | |||
49 | parse( argc, argv ); | ||
50 | } | ||
51 | |||
52 | int nonOption( Bu::Array<Bu::String> aParams ) | ||
53 | { | ||
54 | if( eMode == modeNone ) | ||
55 | { | ||
56 | //Bu::println("Checking mode"); | ||
57 | // First param, must be the mode | ||
58 | if( aParams[0] == "ls" ) | ||
59 | eMode = modeList; | ||
60 | else if( aParams[0] == "cat" ) | ||
61 | eMode = modeCat; | ||
62 | else if( aParams[0] == "cp-in" ) | ||
63 | eMode = modeCopyIn; | ||
64 | else if( aParams[0] == "cp-out" ) | ||
65 | eMode = modeCopyOut; | ||
66 | else if( aParams[0] == "mkdir" ) | ||
67 | eMode = modeMkdir; | ||
68 | else if( aParams[0] == "initialize" ) | ||
69 | eMode = modeInitialize; | ||
70 | else | ||
71 | Bu::println("Unknown command, try --help"); | ||
72 | return 0; | ||
73 | } else { | ||
74 | lParams.append( aParams[0] ); | ||
75 | } | ||
76 | //Bu::println("Got option: \"%1\"").arg( aParams[0] ); | ||
77 | return 0; | ||
78 | } | ||
79 | |||
80 | Mode eMode; | ||
81 | Bu::String sFile; | ||
82 | Bu::StringList lParams; | ||
83 | int iBlockSize; | ||
84 | }; | ||
85 | |||
86 | int main( int argc, char *argv[] ) | ||
87 | { | ||
88 | Options opt( argc, argv ); | ||
89 | |||
90 | if( opt.sFile.isEmpty() ) | ||
91 | { | ||
92 | Bu::println("You must specify a MyriadFs stream (see -f).\n"); | ||
93 | return 1; | ||
94 | } | ||
95 | |||
96 | if( opt.eMode == modeNone ) | ||
97 | { | ||
98 | Bu::println("Specify an operation to perfrom.\n"); | ||
99 | return 1; | ||
100 | } | ||
101 | |||
102 | int iFlags = Bu::File::ReadWrite; | ||
103 | |||
104 | if( opt.eMode == modeInitialize ) | ||
105 | { | ||
106 | iFlags |= Bu::File::Create|Bu::File::Truncate; | ||
107 | } | ||
108 | |||
109 | Bu::File fFs( opt.sFile, iFlags ); | ||
110 | Bu::MyriadFs mFs( fFs, opt.iBlockSize ); | ||
111 | |||
112 | switch( opt.eMode ) | ||
113 | { | ||
114 | case modeList: | ||
115 | { | ||
116 | Bu::String sPath = "/"; | ||
117 | if( opt.lParams.getSize() > 0 ) | ||
118 | { | ||
119 | sPath = opt.lParams.first(); | ||
120 | } | ||
121 | Bu::MyriadFs::Dir lEnt = mFs.readDir( sPath ); | ||
122 | for( Bu::MyriadFs::Dir::iterator i = lEnt.begin(); i; i++ ) | ||
123 | { | ||
124 | Bu::String sPerm; | ||
125 | sPerm += ((*i).uPerms&Bu::MyriadFs::typeDir)?"d": | ||
126 | ((*i).uPerms&Bu::MyriadFs::typeChrDev)?"c": | ||
127 | ((*i).uPerms&Bu::MyriadFs::typeBlkDev)?"b": | ||
128 | ((*i).uPerms&Bu::MyriadFs::typeSymLink)?"l": | ||
129 | ((*i).uPerms&Bu::MyriadFs::typeSocket)?"s":"-"; | ||
130 | sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrR)?"r":"-"; | ||
131 | sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrW)?"w":"-"; | ||
132 | sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrX)?"x":"-"; | ||
133 | sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpR)?"r":"-"; | ||
134 | sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpW)?"w":"-"; | ||
135 | sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpX)?"x":"-"; | ||
136 | sPerm += ((*i).uPerms&Bu::MyriadFs::permOthR)?"r":"-"; | ||
137 | sPerm += ((*i).uPerms&Bu::MyriadFs::permOthW)?"w":"-"; | ||
138 | sPerm += ((*i).uPerms&Bu::MyriadFs::permOthX)?"x":"-"; | ||
139 | Bu::println("%1 %2 %3:%4 %5 %6") | ||
140 | .arg( sPerm ) | ||
141 | .arg( (*i).iNode ) | ||
142 | .arg( (*i).iUser ) | ||
143 | .arg( (*i).iGroup ) | ||
144 | .arg( (*i).iSize ) | ||
145 | .arg( (*i).sName ); | ||
146 | } | ||
147 | } | ||
148 | break; | ||
149 | |||
150 | case modeCat: | ||
151 | { | ||
152 | if( opt.lParams.isEmpty() ) | ||
153 | { | ||
154 | Bu::println("Specify at least one file."); | ||
155 | return 1; | ||
156 | } | ||
157 | int iBlockSize = 1024*1024; | ||
158 | char *pBuf = new char[iBlockSize]; | ||
159 | for( Bu::StringList::iterator i = opt.lParams.begin(); i; i++ ) | ||
160 | { | ||
161 | Bu::MyriadStream ms = mFs.open( *i, Bu::MyriadFs::Read ); | ||
162 | int iRead = 0; | ||
163 | do | ||
164 | { | ||
165 | iRead = ms.read( pBuf, iBlockSize ); | ||
166 | if( iRead > 0 ) | ||
167 | { | ||
168 | Bu::sioRaw.write( pBuf, iRead ); | ||
169 | } | ||
170 | } while( iRead == iBlockSize ); | ||
171 | } | ||
172 | delete[] pBuf; | ||
173 | } | ||
174 | break; | ||
175 | |||
176 | case modeCopyIn: | ||
177 | { | ||
178 | if( opt.lParams.getSize() != 2 ) | ||
179 | { | ||
180 | Bu::println("Specify a source file and destination file."); | ||
181 | return 1; | ||
182 | } | ||
183 | int iBlockSize = 1024*1024; | ||
184 | char *pBuf = new char[iBlockSize]; | ||
185 | Bu::File fs = Bu::File( | ||
186 | opt.lParams.first(), Bu::File::Read ); | ||
187 | Bu::MyriadStream ms = mFs.open( | ||
188 | opt.lParams.last(), Bu::MyriadFs::WriteNew ); | ||
189 | int iRead = 0; | ||
190 | do | ||
191 | { | ||
192 | iRead = fs.read( pBuf, iBlockSize ); | ||
193 | if( iRead > 0 ) | ||
194 | { | ||
195 | ms.write( pBuf, iRead ); | ||
196 | } | ||
197 | } while( iRead == iBlockSize ); | ||
198 | delete[] pBuf; | ||
199 | } | ||
200 | break; | ||
201 | |||
202 | case modeCopyOut: | ||
203 | { | ||
204 | if( opt.lParams.getSize() != 2 ) | ||
205 | { | ||
206 | Bu::println("Specify a source file and destination file."); | ||
207 | return 1; | ||
208 | } | ||
209 | int iBlockSize = 1024*1024; | ||
210 | char *pBuf = new char[iBlockSize]; | ||
211 | Bu::MyriadStream ms = mFs.open( | ||
212 | opt.lParams.first(), Bu::MyriadFs::Read ); | ||
213 | Bu::File fs = Bu::File( | ||
214 | opt.lParams.last(), Bu::File::WriteNew ); | ||
215 | int iRead = 0; | ||
216 | do | ||
217 | { | ||
218 | iRead = ms.read( pBuf, iBlockSize ); | ||
219 | if( iRead > 0 ) | ||
220 | { | ||
221 | fs.write( pBuf, iRead ); | ||
222 | } | ||
223 | } while( iRead == iBlockSize ); | ||
224 | delete[] pBuf; | ||
225 | } | ||
226 | break; | ||
227 | |||
228 | case modeMkdir: | ||
229 | { | ||
230 | if( opt.lParams.isEmpty() ) | ||
231 | { | ||
232 | Bu::println("Specify at least one directory."); | ||
233 | return 1; | ||
234 | } | ||
235 | for( Bu::StringList::iterator i = opt.lParams.begin(); i; i++ ) | ||
236 | { | ||
237 | mFs.mkDir( *i, 0777 ); | ||
238 | } | ||
239 | } | ||
240 | break; | ||
241 | |||
242 | case modeInitialize: | ||
243 | Bu::println("MyriadFs initialized.\n"); | ||
244 | break; | ||
245 | |||
246 | case modeNone: | ||
247 | break; | ||
248 | } | ||
249 | |||
250 | return 0; | ||
251 | } | ||
diff --git a/src/unstable/myriadfs.cpp b/src/unstable/myriadfs.cpp index 919f8b9..e02620d 100644 --- a/src/unstable/myriadfs.cpp +++ b/src/unstable/myriadfs.cpp | |||
@@ -113,7 +113,7 @@ void Bu::MyriadFs::stat( const Bu::String &sPath, Bu::MyriadFs::Stat &rBuf ) | |||
113 | stat( iNode, rBuf, is ); | 113 | stat( iNode, rBuf, is ); |
114 | } | 114 | } |
115 | 115 | ||
116 | Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int /*iMode*/, | 116 | Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int iMode, |
117 | uint16_t uPerms ) | 117 | uint16_t uPerms ) |
118 | { | 118 | { |
119 | int32_t iParent = -1; | 119 | int32_t iParent = -1; |
@@ -123,7 +123,12 @@ Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int /*iMode*/, | |||
123 | iNode = lookupInode( sPath, iParent ); | 123 | iNode = lookupInode( sPath, iParent ); |
124 | // sio << "File found." << sio.nl; | 124 | // sio << "File found." << sio.nl; |
125 | // The file was found | 125 | // The file was found |
126 | return openByInode( iNode ); | 126 | Bu::MyriadStream ms = openByInode( iNode ); |
127 | if( (iMode&Truncate) ) | ||
128 | { | ||
129 | ms.setSize( 0 ); | ||
130 | } | ||
131 | return ms; | ||
127 | } | 132 | } |
128 | catch( Bu::MyriadFsException &e ) | 133 | catch( Bu::MyriadFsException &e ) |
129 | { | 134 | { |