aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2024-07-15 12:59:18 -0700
committerMike Buland <mike@xagasoft.com>2024-07-15 12:59:18 -0700
commit499cdaf05204a40d86e0e1b4dd32709b3ab67e20 (patch)
tree5fa31a6e4b4e2c3abde43b4e52ef3a88f6cab864
parent4bf0f0c256693e30017f9bea60cf55aa97f8994a (diff)
downloadlibbu++-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.
-rw-r--r--.gitignore2
-rw-r--r--default.bld8
-rw-r--r--src/extra/mount.myriadfs.cpp (renamed from src/extra/myriadfs.cpp)0
-rw-r--r--src/tests/myriadfs.cpp14
-rw-r--r--src/tools/myriadfs.cpp251
-rw-r--r--src/unstable/myriadfs.cpp9
6 files changed, 278 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index 9009abe..532bcbf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,7 @@
7/myriad.exe 7/myriad.exe
8/jsontool.exe 8/jsontool.exe
9/rununits.exe 9/rununits.exe
10/myriadfs.exe
10/autoconfig 11/autoconfig
11/bin2cpp 12/bin2cpp
12/mkunit 13/mkunit
@@ -14,6 +15,7 @@
14/viewcsv 15/viewcsv
15/jsontool 16/jsontool
16/rununits 17/rununits
18/myriadfs
17.*.swp 19.*.swp
18.*.un~ 20.*.un~
19/src/autoconfig.h 21/src/autoconfig.h
diff --git a/default.bld b/default.bld
index aed9dee..07b562c 100644
--- a/default.bld
+++ b/default.bld
@@ -54,9 +54,9 @@ action "unit"
54 build: targets("unit tests"); 54 build: targets("unit tests");
55} 55}
56 56
57action "myriadfs" 57action "mount.myriadfs"
58{ 58{
59 build: "myriadfs"; 59 build: "mount.myriadfs";
60} 60}
61 61
62target ["src/autoconfig.h", "src/version.h"] 62target ["src/autoconfig.h", "src/version.h"]
@@ -181,12 +181,12 @@ target files("src/extra/*.cpp").replace("src/extra/","").replace(".cpp","")
181// I'm honestly not 100% sure what the policy should be yet, but this seems like 181// I'm honestly not 100% sure what the policy should be yet, but this seems like
182// I shouldn't have to do it this way. 182// I shouldn't have to do it this way.
183// 183//
184target "src/extra/myriadfs.o" 184target "src/extra/mount.myriadfs.o"
185{ 185{
186 CXXFLAGS += "-D_FILE_OFFSET_BITS=64"; 186 CXXFLAGS += "-D_FILE_OFFSET_BITS=64";
187} 187}
188 188
189target "myriadfs" 189target "mount.myriadfs"
190{ 190{
191 LDFLAGS += "-lfuse -lpthread"; 191 LDFLAGS += "-lfuse -lpthread";
192} 192}
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
15enum Mode
16{
17 modeList,
18 modeCat,
19 modeCopyIn,
20 modeCopyOut,
21 modeMkdir,
22 modeInitialize,
23
24 modeNone
25};
26
27Bu::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
33class Options : public Bu::OptParser
34{
35public:
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
86int 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
116Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int /*iMode*/, 116Bu::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 {