diff options
author | Mike Buland <mike@xagasoft.com> | 2024-07-15 12:59:18 -0700 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2024-07-15 12:59:18 -0700 |
commit | 499cdaf05204a40d86e0e1b4dd32709b3ab67e20 (patch) | |
tree | 5fa31a6e4b4e2c3abde43b4e52ef3a88f6cab864 /src/tools | |
parent | 4bf0f0c256693e30017f9bea60cf55aa97f8994a (diff) | |
download | libbu++-499cdaf05204a40d86e0e1b4dd32709b3ab67e20.tar.gz libbu++-499cdaf05204a40d86e0e1b4dd32709b3ab67e20.tar.bz2 libbu++-499cdaf05204a40d86e0e1b4dd32709b3ab67e20.tar.xz libbu++-499cdaf05204a40d86e0e1b4dd32709b3ab67e20.zip |
MyriadFs improvements and new helper tool.
I think the interface could be a lot better...but it does work and we
can use it examine and work with MyriadFs files.
Diffstat (limited to '')
-rw-r--r-- | src/tools/myriadfs.cpp | 251 |
1 files changed, 251 insertions, 0 deletions
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 | } | ||