-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpXmlCmdApplication.cpp
290 lines (256 loc) · 7.27 KB
/
cpXmlCmdApplication.cpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* File: mvXmlCmdApplication.cpp
* Author: Jay Sprenkle jsprenkle at gmail.com
*
* Created on April 28, 2015
*/
#include <stdexcept>
#include <string>
#include <iostream>
#include <fcntl.h> // open
#include <unistd.h> // read, write, close
#include <sys/stat.h> // stat
#include <errno.h> // errno, ENOENT, EEXIST
#if defined(_WIN32)
#include <direct.h> // _mkdir
#endif
#include "conversion.h"
//#include "XmlCmd.info.h" // why can't I put these into a static library?
extern ::std::string HostName();
extern ::std::string UserName();
#include "cpXmlCmdApplication.h"
cpXmlCmdApplication::cpXmlCmdApplication( const char* ResultDocumentFileName, int argc, char** argv )
: cpXmlCmdNamespace( "http://www.XmlCommandLine.org/cpXmlCmd/1.0" )
, output( 0 )
, status( true )
, argc( argc )
, argv( argv )
{
if ( ResultDocumentFileName )
{
start = time( 0 );
output = new cpXmlCmdOutput( ResultDocumentFileName );
// run time environment
::std::string separator;
::std::string args;
for ( int i = 0; i < argc; ++i )
{
args.append( separator );
args.append( argv[i] );
separator = " ";
}
// create context area
output->Context( args, UserName(), HostName() );
}
}
cpXmlCmdApplication::~cpXmlCmdApplication()
{
if ( output )
{
finish = time( 0 );
// write results of processing
// notes
::std::string msg( "cpXmlCmd: " );
msg.append( "copied: " );
msg.append( toString<int>( copied ) );
msg.append( ", failed: " );
msg.append( toString<int>( failed ) );
output->Notes( msg.c_str() );
output->Status( status, start, finish );
output->FinishContent();
delete output;
}
}
void cpXmlCmdApplication::initialize()
{
copied = 0;
failed = 0;
// begin content node
output->StartContent();
}
bool cpXmlCmdApplication::process( const char* filename )
{
bool result = true;
try
{
xmlTextReaderPtr readerptr = xmlNewTextReaderFilename( filename );
if ( readerptr != NULL )
{
int ret = xmlTextReaderRead( readerptr );
while ( ret == 1 )
{
processNode( readerptr );
ret = xmlTextReaderRead( readerptr );
}
xmlFreeTextReader( readerptr );
if ( ret != 0 )
{
::std::string msg( filename );
msg.append( " failed to parse " );
msg.append( strerror( errno ) );
throw ::std::runtime_error( msg );
}
}
else
{
::std::string msg( filename );
msg.append( " failed to open: " );
msg.append( strerror( errno ) );
throw ::std::runtime_error( msg );
}
}
catch ( ::std::exception ex )
{
::std::cerr << ex.what() << "\n";
result = false;
status = false;
}
catch ( ... )
{
::std::cerr << "Unknown error\n";
result = false;
status = false;
}
return result;
}
bool isDir( const ::std::string& path )
{
#if defined(_WIN32)
struct _stat info;
if ( _stat( path.c_str(), &info ) != 0 )
return false;
return (info.st_mode & _S_IFDIR ) != 0;
#else
struct stat info;
if ( stat( path.c_str(), &info ) != 0 )
return false;
return (info.st_mode & S_IFDIR ) != 0;
#endif
}
bool makePath( const ::std::string& path )
{
mode_t mode = 0755;
int ret = mkdir( path.c_str(), mode );
if ( ret == 0 )
return true;
switch ( errno )
{
// parent didn't exist, try to create it
case ENOENT:
{
int pos = path.find_last_of( "/\\" );
if ( pos == ::std::string::npos )
return false;
if ( ! makePath( path.substr( 0, pos ) ) )
return false;
}
// now, try to create again
return mkdir( path.c_str(), mode ) == 0;
case EEXIST:
// done!
return isDir( path );
default:
return false;
}
}
void cpXmlCmdApplication::processNode( xmlTextReaderPtr readerptr )
{
if ( compareNode( readerptr, "cp" ) )
{
xmlChar* ns = xmlTextReaderNamespaceUri( readerptr );
if ( ns )
{
if ( strcmp( (const char*) ns, cpXmlCmdNamespace ) == 0 )
{
xmlChar* source = xmlTextReaderGetAttribute( readerptr, (const unsigned char*) "Source" );
if ( source )
{
xmlChar* destination = xmlTextReaderGetAttribute( readerptr, (const unsigned char*) "Destination" );
if ( destination )
{
validate( (const char*) destination );
// rename/move the file
copy( (const char*) source, (const char*) destination );
xmlFree( destination );
}
xmlFree( source );
}
}
xmlFree( ns );
}
}
}
void cpXmlCmdApplication::validate( const char* destination )
{
::std::string PathAndFileName( (const char*) destination );
// if no path
size_t off = PathAndFileName.find_last_of( "/\\" );
if ( off != ::std::string::npos )
if ( ! makePath( PathAndFileName.substr( 0, off ) ) )
{
::std::string msg( "failed to create path to output file: '" );
msg.append( (const char*) destination );
msg.append( "'. " );
msg.append( strerror( errno ) );
throw ::std::runtime_error( msg );
}
}
bool cpXmlCmdApplication::copy( const char* source, const char* destination )
{
time_t start;
if ( output )
start = time( 0 );
bool result;
char buf[BUFSIZ];
size_t size;
int srcfd = open( source, O_RDONLY | O_BINARY, 0 );
if ( srcfd != -1 )
{
int destfd = open( destination, O_WRONLY | O_CREAT | O_BINARY /*| O_TRUNC/**/, 0644 );
if ( destfd != -1 )
{
while ( ( size = read( srcfd, buf, BUFSIZ ) ) > 0 )
write( destfd, buf, size );
close( destfd );
++copied;
result = true;
}
else
{
::std::cerr << "copy failed: "
<< "'" << strerror( errno ) << "'"
<< " copy from:'" << (const char*) source << "'"
<< " to:'" << (const char*) destination << "'"
<< "\n"
;
++failed;
result = false;
}
close( srcfd );
}
else
{
::std::cerr << "copy failed: "
<< "'" << strerror( errno ) << "'"
<< " copy from:'" << (const char*) source << "'"
<< " to:'" << (const char*) destination << "'"
<< "\n"
;
++failed;
result = false;
}
if ( output )
output->cp( source, destination, start );
return result;
}
bool cpXmlCmdApplication::compareNode( xmlTextReaderPtr readerptr, const char* NodeName )
{
bool result = false;
xmlChar* name = xmlTextReaderLocalName( readerptr );
if ( name )
{
result = strcmp( (const char*) name, NodeName ) == 0;
xmlFree( name );
}
return result;
}