|
@@ -2,9 +2,13 @@
|
2
|
2
|
#include "cmGlobal.h"
|
3
|
3
|
#include "cmRpt.h"
|
4
|
4
|
#include "cmErr.h"
|
|
5
|
+#include "cmCtx.h"
|
5
|
6
|
#include "cmFile.h"
|
|
7
|
+#include "cmFileSys.h"
|
6
|
8
|
#include "cmMem.h"
|
7
|
9
|
#include "cmMallocDebug.h"
|
|
10
|
+#include "cmLinkedHeap.h"
|
|
11
|
+#include "cmText.h"
|
8
|
12
|
#include <sys/stat.h>
|
9
|
13
|
cmFileH_t cmFileNullHandle = { NULL };
|
10
|
14
|
|
|
@@ -34,9 +38,9 @@ cmFileRC_t _cmFileError( cmFile_t* p, cmFileRC_t rc, int errNumb, const cmChar_t
|
34
|
38
|
|
35
|
39
|
cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlags_t flags, cmRpt_t* rpt )
|
36
|
40
|
{
|
37
|
|
- char mode[] = "/0/0/0";
|
38
|
|
- cmFile_t* p = NULL;
|
39
|
|
- cmErr_t err;
|
|
41
|
+ char mode[] = "/0/0/0";
|
|
42
|
+ cmFile_t* p = NULL;
|
|
43
|
+ cmErr_t err;
|
40
|
44
|
cmFileRC_t rc;
|
41
|
45
|
|
42
|
46
|
if((rc = cmFileClose(hp)) != kOkFileRC )
|
|
@@ -47,21 +51,21 @@ cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag
|
47
|
51
|
hp->h = NULL;
|
48
|
52
|
|
49
|
53
|
if( cmIsFlag(flags,kReadFileFl) )
|
50
|
|
- mode[0]='r';
|
|
54
|
+ mode[0] = 'r';
|
51
|
55
|
else
|
52
|
56
|
if( cmIsFlag(flags,kWriteFileFl) )
|
53
|
|
- mode[0]='w';
|
|
57
|
+ mode[0] = 'w';
|
54
|
58
|
else
|
55
|
59
|
if( cmIsFlag(flags,kAppendFileFl) )
|
56
|
|
- mode[0]='a';
|
|
60
|
+ mode[0] = 'a';
|
57
|
61
|
else
|
58
|
62
|
cmErrMsg(&err,kInvalidFlagFileRC,"File open flags must contain 'kReadFileFl','kWriteFileFl', or 'kAppendFileFl'.");
|
59
|
63
|
|
60
|
64
|
if( cmIsFlag(flags,kUpdateFileFl) )
|
61
|
|
- mode[1]='+';
|
|
65
|
+ mode[1] = '+';
|
62
|
66
|
|
63
|
67
|
// handle requests to use stdin,stdout,stderr
|
64
|
|
- FILE* sfp = NULL;
|
|
68
|
+ FILE* sfp = NULL;
|
65
|
69
|
if( cmIsFlag(flags,kStdoutFileFl) )
|
66
|
70
|
{
|
67
|
71
|
sfp = stdout;
|
|
@@ -107,7 +111,7 @@ cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag
|
107
|
111
|
}
|
108
|
112
|
}
|
109
|
113
|
|
110
|
|
- hp->h = p;
|
|
114
|
+ hp->h = p;
|
111
|
115
|
|
112
|
116
|
return kOkFileRC;
|
113
|
117
|
}
|
|
@@ -158,7 +162,7 @@ cmFileRC_t cmFileWrite( cmFileH_t h, const void* buf, unsigned bufByteCnt )
|
158
|
162
|
cmFileRC_t cmFileSeek( cmFileH_t h, enum cmFileSeekFlags_t flags, int offsByteCnt )
|
159
|
163
|
{
|
160
|
164
|
cmFile_t* p = _cmFileHandleToPtr(h);
|
161
|
|
- unsigned fileflags = 0;
|
|
165
|
+ unsigned fileflags = 0;
|
162
|
166
|
|
163
|
167
|
if( cmIsFlag(flags,kBeginFileFl) )
|
164
|
168
|
fileflags = SEEK_SET;
|
|
@@ -171,7 +175,7 @@ cmFileRC_t cmFileSeek( cmFileH_t h, enum cmFileSeekFlags_t flags, int offsByt
|
171
|
175
|
else
|
172
|
176
|
return cmErrMsg(&p->err,kInvalidFlagFileRC,"Invalid file seek flag on '%s'.",p->fnStr);
|
173
|
177
|
|
174
|
|
- errno = 0;
|
|
178
|
+ errno = 0;
|
175
|
179
|
if( fseek(p->fp,offsByteCnt,fileflags) != 0 )
|
176
|
180
|
return _cmFileError(p,kSeekFailFileRC,errno,"File seek failed");
|
177
|
181
|
|
|
@@ -181,9 +185,10 @@ cmFileRC_t cmFileSeek( cmFileH_t h, enum cmFileSeekFlags_t flags, int offsByt
|
181
|
185
|
cmFileRC_t cmFileTell( cmFileH_t h, long* offsPtr )
|
182
|
186
|
{
|
183
|
187
|
assert( offsPtr != NULL );
|
184
|
|
- *offsPtr = -1;
|
185
|
|
- cmFile_t* p = _cmFileHandleToPtr(h);
|
186
|
|
- errno = 0;
|
|
188
|
+ *offsPtr = -1;
|
|
189
|
+ cmFile_t* p = _cmFileHandleToPtr(h);
|
|
190
|
+ errno = 0;
|
|
191
|
+
|
187
|
192
|
if((*offsPtr = ftell(p->fp)) == -1)
|
188
|
193
|
return _cmFileError(p,kTellFailFileRC,errno,"File tell failed");
|
189
|
194
|
return kOkFileRC;
|
|
@@ -222,7 +227,8 @@ cmFileRC_t cmFileByteCountFn( const cmChar_t* fn, cmRpt_t* rpt, unsigned* file
|
222
|
227
|
{
|
223
|
228
|
assert( fileByteCntPtr != NULL );
|
224
|
229
|
cmFileRC_t rc;
|
225
|
|
- cmFileH_t h = cmFileNullHandle;
|
|
230
|
+ cmFileH_t h = cmFileNullHandle;
|
|
231
|
+
|
226
|
232
|
if((rc = cmFileOpen(&h,fn,kReadFileFl,rpt)) != kOkFileRC )
|
227
|
233
|
return rc;
|
228
|
234
|
|
|
@@ -241,8 +247,8 @@ cmFileRC_t cmFileCompare( const cmChar_t* fn0, const cmChar_t* fn1, cmRpt_t* rpt
|
241
|
247
|
cmFileH_t h0 = cmFileNullHandle;
|
242
|
248
|
cmFileH_t h1 = cmFileNullHandle;
|
243
|
249
|
|
244
|
|
- char b0[ bufByteCnt ];
|
245
|
|
- char b1[ bufByteCnt ];
|
|
250
|
+ char b0[ bufByteCnt ];
|
|
251
|
+ char b1[ bufByteCnt ];
|
246
|
252
|
|
247
|
253
|
assert(isEqualPtr != NULL );
|
248
|
254
|
*isEqualPtr = true;
|
|
@@ -253,14 +259,14 @@ cmFileRC_t cmFileCompare( const cmChar_t* fn0, const cmChar_t* fn1, cmRpt_t* rpt
|
253
|
259
|
if((rc = cmFileOpen(&h1,fn1,kReadFileFl,rpt)) != kOkFileRC )
|
254
|
260
|
goto errLabel;
|
255
|
261
|
|
256
|
|
- cmFile_t* p0 = _cmFileHandleToPtr(h0);
|
257
|
|
- cmFile_t* p1 = _cmFileHandleToPtr(h1);
|
|
262
|
+ cmFile_t* p0 = _cmFileHandleToPtr(h0);
|
|
263
|
+ cmFile_t* p1 = _cmFileHandleToPtr(h1);
|
258
|
264
|
|
259
|
265
|
while(1)
|
260
|
266
|
{
|
261
|
267
|
size_t n0 = fread(b0,1,bufByteCnt,p0->fp);
|
262
|
268
|
size_t n1 = fread(b1,1,bufByteCnt,p1->fp);
|
263
|
|
- if( n0 != n1 || memcmp(b0,b1,n0)!=0 )
|
|
269
|
+ if( n0 != n1 || memcmp(b0,b1,n0) != 0 )
|
264
|
270
|
{
|
265
|
271
|
*isEqualPtr = false;
|
266
|
272
|
break;
|
|
@@ -285,7 +291,7 @@ const cmChar_t* cmFileName( cmFileH_t h )
|
285
|
291
|
|
286
|
292
|
cmFileRC_t cmFileFnWrite( const cmChar_t* fn, cmRpt_t* rpt, const void* buf, unsigned bufByteCnt )
|
287
|
293
|
{
|
288
|
|
- cmFileH_t h = cmFileNullHandle;
|
|
294
|
+ cmFileH_t h = cmFileNullHandle;
|
289
|
295
|
cmFileRC_t rc;
|
290
|
296
|
|
291
|
297
|
if((rc = cmFileOpen(&h,fn,kWriteFileFl,rpt)) != kOkFileRC )
|
|
@@ -305,7 +311,7 @@ cmChar_t* _cmFileToBuf( cmFileH_t h, unsigned nn, unsigned* bufByteCntPtr )
|
305
|
311
|
|
306
|
312
|
unsigned n = cmFileByteCount(h);
|
307
|
313
|
cmChar_t* buf = NULL;
|
308
|
|
- cmFile_t* p = _cmFileHandleToPtr(h);
|
|
314
|
+ cmFile_t* p = _cmFileHandleToPtr(h);
|
309
|
315
|
|
310
|
316
|
|
311
|
317
|
// if the file size calculation is ok
|
|
@@ -346,7 +352,7 @@ cmChar_t* _cmFileToBuf( cmFileH_t h, unsigned nn, unsigned* bufByteCntPtr )
|
346
|
352
|
|
347
|
353
|
cmChar_t* _cmFileFnToBuf( const cmChar_t* fn, cmRpt_t* rpt, unsigned nn, unsigned* bufByteCntPtr )
|
348
|
354
|
{
|
349
|
|
- cmFileH_t h = cmFileNullHandle;
|
|
355
|
+ cmFileH_t h = cmFileNullHandle;
|
350
|
356
|
cmChar_t* buf = NULL;
|
351
|
357
|
|
352
|
358
|
if( cmFileOpen(&h,fn,kReadFileFl | kBinaryFileFl,rpt) != kOkFileRC )
|
|
@@ -360,6 +366,128 @@ cmChar_t* _cmFileFnToBuf( const cmChar_t* fn, cmRpt_t* rpt, unsigned nn, unsigne
|
360
|
366
|
return buf;
|
361
|
367
|
}
|
362
|
368
|
|
|
369
|
+cmFileRC_t cmFileCopy(
|
|
370
|
+ const cmChar_t* srcDir,
|
|
371
|
+ const cmChar_t* srcFn,
|
|
372
|
+ const cmChar_t* srcExt,
|
|
373
|
+ const cmChar_t* dstDir,
|
|
374
|
+ const cmChar_t* dstFn,
|
|
375
|
+ const cmChar_t* dstExt,
|
|
376
|
+ cmErr_t* err)
|
|
377
|
+{
|
|
378
|
+ cmFileRC_t rc = kOkFileRC;
|
|
379
|
+ unsigned byteCnt = 0;
|
|
380
|
+ cmChar_t* buf = NULL;
|
|
381
|
+ const cmChar_t* srcPathFn = NULL;
|
|
382
|
+ const cmChar_t* dstPathFn = NULL;
|
|
383
|
+
|
|
384
|
+ // form the source path fn
|
|
385
|
+ if((srcPathFn = cmFsMakeFn(srcDir,srcFn,srcExt,NULL)) == NULL )
|
|
386
|
+ {
|
|
387
|
+ rc = cmErrMsg(err,kFileSysFailFileRC,"The soure file name for dir:%s name:%s ext:%s could not be formed.",cmStringNullGuard(srcDir),cmStringNullGuard(srcFn),cmStringNullGuard(srcExt));
|
|
388
|
+ goto errLabel;
|
|
389
|
+ }
|
|
390
|
+
|
|
391
|
+ // form the dest path fn
|
|
392
|
+ if((dstPathFn = cmFsMakeFn(dstDir,dstFn,dstExt,NULL)) == NULL )
|
|
393
|
+ {
|
|
394
|
+ rc = cmErrMsg(err,kFileSysFailFileRC,"The destination file name for dir:%s name:%s ext:%s could not be formed.",cmStringNullGuard(dstDir),cmStringNullGuard(dstFn),cmStringNullGuard(dstExt));
|
|
395
|
+ goto errLabel;
|
|
396
|
+ }
|
|
397
|
+
|
|
398
|
+ // verify that the source exists
|
|
399
|
+ if( cmFsIsFile(srcPathFn) == false )
|
|
400
|
+ {
|
|
401
|
+ rc = cmErrMsg(err,kOpenFailFileRC,"The source file '%s' does not exist.",cmStringNullGuard(srcPathFn));
|
|
402
|
+ goto errLabel;
|
|
403
|
+ }
|
|
404
|
+
|
|
405
|
+ // read the source file into a buffer
|
|
406
|
+ if((buf = cmFileFnToBuf(srcPathFn,err->rpt,&byteCnt)) == NULL )
|
|
407
|
+ rc = cmErrMsg(err,kReadFailFileRC,"Attempt to fill a buffer from '%s' failed.",cmStringNullGuard(srcPathFn));
|
|
408
|
+ else
|
|
409
|
+ {
|
|
410
|
+ // write the file to the output file
|
|
411
|
+ if( cmFileFnWrite(dstPathFn,err->rpt,buf,byteCnt) != kOkFileRC )
|
|
412
|
+ rc = cmErrMsg(err,kWriteFailFileRC,"An attempt to write a buffer to '%s' failed.",cmStringNullGuard(dstPathFn));
|
|
413
|
+ }
|
|
414
|
+
|
|
415
|
+ errLabel:
|
|
416
|
+ // free the buffer
|
|
417
|
+ cmMemFree(buf);
|
|
418
|
+ cmFsFreeFn(srcPathFn);
|
|
419
|
+ cmFsFreeFn(dstPathFn);
|
|
420
|
+ return rc;
|
|
421
|
+
|
|
422
|
+}
|
|
423
|
+
|
|
424
|
+cmFileRC_t cmFileBackup( const cmChar_t* dir, const cmChar_t* name, const cmChar_t* ext, cmErr_t* err )
|
|
425
|
+{
|
|
426
|
+ cmFileRC_t rc = kOkFileRC;
|
|
427
|
+ cmChar_t* newName = NULL;
|
|
428
|
+ const cmChar_t* newFn = NULL;
|
|
429
|
+ unsigned n = 0;
|
|
430
|
+ const cmChar_t* srcFn = NULL;
|
|
431
|
+
|
|
432
|
+ // form the name of the backup file
|
|
433
|
+ if((srcFn = cmFsMakeFn(dir,name,ext,NULL)) == NULL )
|
|
434
|
+ {
|
|
435
|
+ rc = cmErrMsg(err,kFileSysFailFileRC,"Backup source file name formation failed.");
|
|
436
|
+ goto errLabel;
|
|
437
|
+ }
|
|
438
|
+
|
|
439
|
+ // if the src file does not exist then there is nothing to do
|
|
440
|
+ if( cmFsIsFile(srcFn) == false )
|
|
441
|
+ return rc;
|
|
442
|
+
|
|
443
|
+ // break the source file name up into dir/fn/ext.
|
|
444
|
+ cmFileSysPathPart_t* pp = NULL;
|
|
445
|
+ if((pp = cmFsPathParts(srcFn)) == NULL || pp->fnStr==NULL)
|
|
446
|
+ {
|
|
447
|
+ rc = cmErrMsg(err,kFileSysFailFileRC,"The file name '%s' could not be parsed into its parts.",cmStringNullGuard(srcFn));
|
|
448
|
+ goto errLabel;
|
|
449
|
+ }
|
|
450
|
+
|
|
451
|
+ // iterate until a unique file name is found
|
|
452
|
+ for(n=0; 1; ++n)
|
|
453
|
+ {
|
|
454
|
+ cmFsFreeFn(newFn);
|
|
455
|
+
|
|
456
|
+ // generate a new file name
|
|
457
|
+ newName = cmTsPrintfP(newName,"%s_%i",pp->fnStr,n);
|
|
458
|
+
|
|
459
|
+ // form the new file name into a complete path
|
|
460
|
+ if((newFn = cmFsMakeFn(pp->dirStr,newName,pp->extStr,NULL)) == NULL )
|
|
461
|
+ {
|
|
462
|
+ rc = cmErrMsg(err,kFileSysFailFileRC,"A backup file name could not be formed for the file '%s'.",cmStringNullGuard(newName));
|
|
463
|
+ goto errLabel;
|
|
464
|
+ }
|
|
465
|
+
|
|
466
|
+ // if the new file name is not already in use ...
|
|
467
|
+ if( cmFsIsFile(newFn) == false )
|
|
468
|
+ {
|
|
469
|
+ // .. then duplicate the file
|
|
470
|
+ if((rc = cmFileCopy(srcFn,NULL,NULL,newFn,NULL,NULL,err)) != kOkFileRC )
|
|
471
|
+ rc = cmErrMsg(err,rc,"The file '%s' could not be duplicated as '%s'.",cmStringNullGuard(srcFn),cmStringNullGuard(newFn));
|
|
472
|
+
|
|
473
|
+ break;
|
|
474
|
+ }
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+ }
|
|
478
|
+
|
|
479
|
+ errLabel:
|
|
480
|
+
|
|
481
|
+ cmFsFreeFn(srcFn);
|
|
482
|
+ cmFsFreeFn(newFn);
|
|
483
|
+ cmMemFree(newName);
|
|
484
|
+ cmFsFreePathParts(pp);
|
|
485
|
+
|
|
486
|
+ return rc;
|
|
487
|
+
|
|
488
|
+}
|
|
489
|
+
|
|
490
|
+
|
363
|
491
|
cmChar_t* cmFileToBuf( cmFileH_t h, unsigned* bufByteCntPtr )
|
364
|
492
|
{ return _cmFileToBuf(h,0,bufByteCntPtr); }
|
365
|
493
|
|