aboutsummaryrefslogtreecommitdiff
path: root/src/tools/myriadfs.cpp
blob: 90517793976f2d3e905dd2de8fb35019c9dc5d74 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (C) 2007-2023 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.
 */

#include "bu/sio.h"
#include "bu/streamstack.h"
#include "bu/file.h"
#include "bu/myriadfs.h"
#include "bu/myriadstream.h"
#include "bu/optparser.h"

enum Mode
{
    modeList,
    modeCat,
    modeCopyIn,
    modeCopyOut,
    modeMkdir,
    modeInitialize,

    modeNone
};

Bu::Formatter &operator>>( Bu::Formatter &f, Mode & /*e*/ )
{
    Bu::sio << "Uh oh, the formatter was called..." << Bu::sio.nl;
    return f;
}

class Options : public Bu::OptParser
{
public:
    Options( int argc, char *argv[] ) :
        eMode( modeNone ),
        iBlockSize( 64 )
    {
        addHelpBanner("Options:");
        addOption( sFile, 'f', "file", "Myriadfs file");
        addOption( iBlockSize, 'b', "block-size",
            "Specify the block size when initializing a new MyriadFs");

        setNonOption( Bu::slot( this, &Options::nonOption ) );

        addHelpOption();

        parse( argc, argv );
    }

    int nonOption( Bu::Array<Bu::String> aParams )
    {
        if( eMode == modeNone )
        {
            //Bu::println("Checking mode");
            // First param, must be the mode
            if( aParams[0] == "ls" )
                eMode = modeList;
            else if( aParams[0] == "cat" )
                eMode = modeCat;
            else if( aParams[0] == "cp-in" )
                eMode = modeCopyIn;
            else if( aParams[0] == "cp-out" )
                eMode = modeCopyOut;
            else if( aParams[0] == "mkdir" )
                eMode = modeMkdir;
            else if( aParams[0] == "initialize" )
                eMode = modeInitialize;
            else
                Bu::println("Unknown command, try --help");
            return 0;
        } else {
            lParams.append( aParams[0] );
        }
        //Bu::println("Got option: \"%1\"").arg( aParams[0] );
        return 0;
    }

    Mode eMode;
    Bu::String sFile;
    Bu::StringList lParams;
    int iBlockSize;
};

int main( int argc, char *argv[] )
{
    Options opt( argc, argv );

    if( opt.sFile.isEmpty() )
    {
        Bu::println("You must specify a MyriadFs stream (see -f).\n");
        return 1;
    }

    if( opt.eMode == modeNone )
    {
        Bu::println("Specify an operation to perfrom.\n");
        return 1;
    }

    int iFlags = Bu::File::ReadWrite;

    if( opt.eMode == modeInitialize )
    {
        iFlags |= Bu::File::Create|Bu::File::Truncate;
    }

    Bu::File fFs( opt.sFile, iFlags );
    Bu::MyriadFs mFs( fFs, opt.iBlockSize );

    switch( opt.eMode )
    {
		case modeList:
            {
                Bu::String sPath = "/";
                if( opt.lParams.getSize() > 0 )
                {
                    sPath = opt.lParams.first();
                }
                Bu::MyriadFs::Dir lEnt = mFs.readDir( sPath );
                for( Bu::MyriadFs::Dir::iterator i = lEnt.begin(); i; i++ )
                {
                    Bu::String sPerm;
                    sPerm += ((*i).uPerms&Bu::MyriadFs::typeDir)?"d":
                        ((*i).uPerms&Bu::MyriadFs::typeChrDev)?"c":
                        ((*i).uPerms&Bu::MyriadFs::typeBlkDev)?"b":
                        ((*i).uPerms&Bu::MyriadFs::typeSymLink)?"l":
                        ((*i).uPerms&Bu::MyriadFs::typeSocket)?"s":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrR)?"r":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrW)?"w":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrX)?"x":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpR)?"r":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpW)?"w":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpX)?"x":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permOthR)?"r":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permOthW)?"w":"-";
                    sPerm += ((*i).uPerms&Bu::MyriadFs::permOthX)?"x":"-";
                    Bu::println("%1 %2 %3:%4 %5 %6")
                        .arg( sPerm )
                        .arg( (*i).iNode )
                        .arg( (*i).iUser )
                        .arg( (*i).iGroup )
                        .arg( (*i).iSize )
                        .arg( (*i).sName );
                }
            }
			break;

		case modeCat:
            {
                if( opt.lParams.isEmpty() )
                {
                    Bu::println("Specify at least one file.");
                    return 1;
                }
                int iBlockSize = 1024*1024;
                char *pBuf = new char[iBlockSize];
                for( Bu::StringList::iterator i = opt.lParams.begin(); i; i++ )
                {
                    Bu::MyriadStream ms = mFs.open( *i, Bu::MyriadFs::Read );
                    int iRead = 0;
                    do
                    {
                        iRead = ms.read( pBuf, iBlockSize );
                        if( iRead > 0 )
                        {
                            Bu::sioRaw.write( pBuf, iRead );
                        }
                    } while( iRead == iBlockSize );
                }
                delete[] pBuf;
            }
			break;

		case modeCopyIn:
            {
                if( opt.lParams.getSize() != 2 )
                {
                    Bu::println("Specify a source file and destination file.");
                    return 1;
                }
                int iBlockSize = 1024*1024;
                char *pBuf = new char[iBlockSize];
                Bu::File fs = Bu::File(
                    opt.lParams.first(), Bu::File::Read );
                Bu::MyriadStream ms = mFs.open(
                    opt.lParams.last(), Bu::MyriadFs::WriteNew );
                int iRead = 0;
                do
                {
                    iRead = fs.read( pBuf, iBlockSize );
                    if( iRead > 0 )
                    {
                        ms.write( pBuf, iRead );
                    }
                } while( iRead == iBlockSize );
                delete[] pBuf;
            }
			break;

		case modeCopyOut:
            {
                if( opt.lParams.getSize() != 2 )
                {
                    Bu::println("Specify a source file and destination file.");
                    return 1;
                }
                int iBlockSize = 1024*1024;
                char *pBuf = new char[iBlockSize];
                Bu::MyriadStream ms = mFs.open(
                    opt.lParams.first(), Bu::MyriadFs::Read );
                Bu::File fs = Bu::File(
                    opt.lParams.last(), Bu::File::WriteNew );
                int iRead = 0;
                do
                {
                    iRead = ms.read( pBuf, iBlockSize );
                    if( iRead > 0 )
                    {
                        fs.write( pBuf, iRead );
                    }
                } while( iRead == iBlockSize );
                delete[] pBuf;
            }
			break;

		case modeMkdir:
            {
                if( opt.lParams.isEmpty() )
                {
                    Bu::println("Specify at least one directory.");
                    return 1;
                }
                for( Bu::StringList::iterator i = opt.lParams.begin(); i; i++ )
                {
                    mFs.mkDir( *i, 0777 );
                }
            }
			break;

		case modeInitialize:
            Bu::println("MyriadFs initialized.\n");
			break;

		case modeNone:
			break;
    }

    return 0;
}