-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiskObject.cpp
1645 lines (1340 loc) · 43 KB
/
DiskObject.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ==========================================================================
Class : CDiskObject
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-03-12
Purpose : "CDiskObject" encapsulates several high-level file-
and directory operations.
Description : All error handling is managed internally, and the
different API-functions return a "BOOL" to signal success
or failure. In case of failure, "FALSE" returned, the
member function "GetErrorMessage" can be called to get a
"CString" with the error message.
Usage : The class is used by creating a "CDiskObject", and
calling the methods of the class. No other setup is
necessary. If a "CWnd"-pointer is submitted to the "ctor",
"CDiskObject" will give feedback by calling "SetWindowText"
with, for example, filenames during processing.This
means that a "CStatic" (or other appropriate control) can
be set up to display the file currently copied, for
example.
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-04-14
Purpose : 1. Added const-correctness for inparams.
2. Replacing "/" with "\" when qualifying file names/
directories
3. Skipping directory qualify when the path starts
with \\. Can't add a drive letter then... The path
is assumed to be fully qualified.
4. Added pragma to get rid of C4706 assignment warning.
5. Added "RemoveFile" for reasons of symmetry.
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-05-14
Purpose : 1. Added default ctor
2. Changed name of "EnumDirectories" to "EnumAllDirectories"
and added a non-recursive "EnumDirectories".
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-05-19
Purpose 1. Checking FILE_ATTRIBUTE_DIRECTORY as a flag instead
of a value.
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-08-04
Purpose 1. Changed drive length from _MAX_DRIVE to _MAX_PATH
in UNC parsing. (jkaspzyk)
2. Added UNICODE macro to SetSystemErrorMessage
declaration. (nuhi)
========================================================================
Author : Allen Rossouw
Date : 2005-04-16
Purpose 1. Sorting the output in EnumFilesInDirectoryWithFilter
2. Added a DirectoryExists function, returning TRUE if
the inparam directory exists.
3. Added a CopyFile with an extra parameter for a new
filename
4. Added a FileInformation function, returning a
BY_HANDLE_FILE_INFORMATION for the inparam file.
========================================================================
Author : Johan Rosengren
Date : 2005-05-15
Purpose 1. Corrected bug in CopyFile
2. Added RenameFile
========================================================================*/
#include "stdafx.h"
#include <tchar.h>
#include "DiskObject.h"
#pragma warning( disable : 4706 )
#define Trigger( a) if( m_feedbackWindow ) m_feedbackWindow->SetWindowText( a );
////////////////////////////////////////////////////////////////////
// CDiskObject construction/destruction/initialization
CDiskObject::CDiskObject()
/* ============================================================
Function : CDiskObject::CDiskObject
Description : Constructor
Access : Public
Return : void
Parameters : none
Usage : Should normally be created on the stack.
============================================================*/
{
m_feedbackWindow = NULL;
}
CDiskObject::CDiskObject( CWnd* feedbackWindow )
/* ============================================================
Function : CDiskObject::CDiskObject
Description : Constructor
Access : Public
Return : void
Parameters : CWnd* hwndFeedback - "CWnd" to feedback
window
Usage : Should normally be created on the stack.
============================================================*/
{
m_feedbackWindow = feedbackWindow;
}
CDiskObject::~CDiskObject( )
/* ============================================================
Function : CDiskObject::~CDiskObject
Description : Destructor
Access : Public
Return : void
Parameters : none
Usage : Should normally be created on the stack.
============================================================*/
{
}
////////////////////////////////////////////////////////////////////
// CDiskObject operations
//
// File operations
BOOL CDiskObject::CopyFiles( const CString& sourceDirectory,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyFiles
Description : The member copies all files from
"sourceDirectory" to "destDirectory".
Subdirectories will not be copied.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will return
errors.
Parameters : CString sourceDirectory - Copy from. If
empty, current.
CString destDirectory - Copy to. If
empty, current.
Usage : Call to copy the files from one directory to
another.
============================================================*/
{
ClearError( );
CString source( sourceDirectory );
CString dest( destDirectory );
QualifyPath( source );
QualifyPath( dest );
// First, we enumerate all files
CStringArray files;
BOOL result = EnumFilesInDirectory( source, files );
if( result )
{
// Create the destination directory, if necessary
if( ( result = CreateDirectory( dest ) ) )
{
int max = files.GetSize( );
for( int t = 0 ; t < max ; t++ )
{
// Copy the files
CString file;
file = files[ t ];
Trigger( file );
if( !( result = ::CopyFile( source + file, dest + file, FALSE ) ) )
{
// Set error message
SetSystemErrorMessage( ::GetLastError( ),
source +
file +
_T( " -> " ) +
dest +
file );
t = max;
}
}
}
}
return result;
}
BOOL CDiskObject::CopyFiles( CStringArray& files,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyFiles
Description : The function copies the files in the
"CStringArray" "files" to the directory
"destDirectory". Existing files will be
overwritten. The destination will be
created if it doesn't already exist.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will return
errors.
Parameters : CStringArray& files - a string array
with the files
to copy
CString destDirectory - destination
Usage : Copy a list of files to a directory.
============================================================*/
{
ClearError( );
CString dest( destDirectory );
BOOL result = TRUE;
if( files.GetSize( ) )
{
QualifyPath( dest );
// Create destination, if necessary
if( ( result = CreateDirectory( dest ) ) )
{
int max = files.GetSize( );
for( int t = 0 ; t < max ; t++ )
{
// Loop and copy the files
CString file;
file = files[ t ];
if( file.GetLength( ) )
{
Trigger( file );
QualifyFile( file );
// Create destination filename
CString to = dest + GetFileName( file );
if( !( result = ::CopyFile( file, to, FALSE ) ) )
{
// Set error message
SetSystemErrorMessage( ::GetLastError( ),
file +
_T( " -> " ) +
dest +
file );
t = max;
}
}
}
}
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::CopyFile( const CString& sourceFile,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyFile
Description : Will copy "sourceFile" to "destDirectory".
An existing file will be overwritten. The
directory will be created if it doesn't exist.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will contain
errors
Parameters : CString sourceFile - file to copy
CString destDirectory - destination
Usage : Call to copy a file to a directory.
============================================================*/
{
ClearError( );
CString source( sourceFile );
CString dest( destDirectory );
BOOL result = TRUE;
if( sourceFile.GetLength( ) )
{
QualifyFile( source );
QualifyPath( dest );
// Creating destDirectory if necessary.
if( ( result = CreateDirectory( dest ) ) )
{
CString filePart = GetFileName( source );
// Copy the file
Trigger( filePart );
if( !( result = ::CopyFile( source, dest + filePart, FALSE ) ) )
SetSystemErrorMessage( ::GetLastError( ), source );
}
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::CopyFile( const CString& sourceFile,const CString& destDirectory, const CString& destFile )
/* ============================================================
Function : CDiskObject::CopyFile
Description : Will copy "sourceFile" to "destDirectory"
with the new name "destFile".
An existing file will be overwritten. The
directory will be created if it doesn't exist.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will contain
errors
Parameters : CString sourceFile - file to copy
CString destDirectory - destination
CString destFile - destination file name
(in "destDirectory")
Usage : Call to copy a file to a directory.
============================================================*/
{
ClearError( );
CString source( sourceFile );
CString destDir( destDirectory );
CString dest( destFile );
BOOL result = TRUE;
if( sourceFile.GetLength( ) )
{
QualifyFile( source );
QualifyPath( destDir );
dest = destDir + dest;
// Creating destDirectory if necessary.
if( ( result = CreateDirectory( destDir ) ) )
{
// Copy the file
Trigger( dest );
if( !( result = ::CopyFile( source, dest, FALSE ) ) )
SetSystemErrorMessage( ::GetLastError( ), source );
}
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::RemoveFile( const CString& sourceFile )
/* ============================================================
Function : CDiskObject::RemoveFile
Description : Will remove "sourceFile".
Access : Public
Return : BOOL - "TRUE" if
removed ok
Parameters : const CString& sourceFile - File to
remove
Usage : Call to delete a file. Added for reasons
of symmetry.
============================================================*/
{
ClearError( );
CString source( sourceFile );
BOOL result = TRUE;
if( sourceFile.GetLength( ) )
{
QualifyFile( source );
if( !( result = ::DeleteFile( source ) ) )
SetSystemErrorMessage( ::GetLastError( ), source );
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
////////////////////////////////////////
// Directory operations
//
BOOL CDiskObject::DirectoryExists( const CString& directory )
/* ============================================================
Function : CDiskObject::DirectoryExists
Description : Returns "TRUE" if the directory "directory"
exists
Access : Public
Return : BOOL - "TRUE" if found
Parameters : CString directory - directory to check
Usage : Call to check for directory existence.
============================================================*/
{
ClearError( );
BOOL result = FALSE;
if( directory.GetLength( ) )
{
CString indir( directory );
QualifyPath( indir );
TCHAR buff[ _MAX_PATH ];
::GetCurrentDirectory(_MAX_PATH, buff );
if( ::SetCurrentDirectory( indir ) )
result = TRUE;
::SetCurrentDirectory( buff );
}
else
{
SetInternalErrorMessage( );
}
return result;
}
BOOL CDiskObject::CreateDirectory( const CString& directory )
/* ============================================================
Function : CDiskObject::CreateDirectory
Description : Will recursively create the directory
"directory".
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will get an
error string if
"FALSE"
Parameters : CString directory - directory to
create
Usage : Call to create a directory chain.
============================================================*/
{
ClearError( );
BOOL result = TRUE;
CString indir( directory );
if( indir.GetLength( ) )
{
QualifyPath( indir );
_TCHAR drive[ _MAX_PATH ];
_TCHAR dir[ _MAX_DIR ];
_TCHAR fname[ _MAX_FNAME ];
_TCHAR ext[ _MAX_EXT ];
// Split directory into parts
_tsplitpath( indir, drive, dir, fname, ext );
TCHAR currentDirectory[ _MAX_PATH ];
::GetCurrentDirectory( _MAX_PATH, currentDirectory );
CStringArray directories;
CString parts = dir;
if( parts.GetLength( ) > 2 )
{
if( parts.Left( 2 ) == _T( "\\\\" ) )
{
// We have an UNC name
CString strComputer;
parts = parts.Right( parts.GetLength( ) - 2 );
int findDir = parts.Find( _TCHAR( '\\' ) );
if( findDir!=-1)
{
strComputer = _T( "\\\\" ) + parts.Left( findDir );
parts = parts.Right( parts.GetLength( ) - ( findDir + 1 ) );
}
_tcscpy( drive, strComputer );
}
}
CString strRoot( drive );
// Strip leading \'s
while( parts.GetLength( ) && parts[0] == _TCHAR( '\\' ) )
parts = parts.Right( parts.GetLength( ) - 1 );
// Cut into separate directories
int find = parts.Find( _TCHAR( '\\' ) );
while( find != -1 )
{
directories.Add( parts.Left( find ) );
parts = parts.Right( parts.GetLength( ) - ( find + 1 ) );
find = parts.Find( _TCHAR( '\\' ) );
}
if( parts.GetLength( ) )
directories.Add( parts );
if( fname )
directories.Add( fname );
// Loop directories one-by-one, creating as necessary
int max = directories.GetSize( );
CString strCurrentDirectory( strRoot );
for( int t = 0 ; t < max ; t++ )
{
strCurrentDirectory += _TCHAR( '\\' ) + directories[ t ];
Trigger( strCurrentDirectory );
if( !( result = ::SetCurrentDirectory( strCurrentDirectory ) ) )
{
if( !( result = ::CreateDirectory( strCurrentDirectory, NULL ) ) )
{
SetSystemErrorMessage( ::GetLastError( ), strCurrentDirectory );
t = max;
}
}
}
::SetCurrentDirectory( currentDirectory );
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::EmptyDirectory( const CString& directory )
/* ============================================================
Function : CDiskObject::EmptyDirectory
Description : Will delete all files in directory.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage" will
get an error
string if "FALSE"
Parameters : CString directory - the directory to
empty.
Usage : Call to empty a directory.
============================================================*/
{
ClearError( );
CString indir( directory );
QualifyPath( indir );
// Enumerate all files
CStringArray files;
BOOL result = EnumFilesInDirectory( indir, files );
if( result )
{
int max = files.GetSize( );
for( int t = 0 ; t < max ; t++ )
{
// Loop and delete
CString file = files[ t ];
Trigger( file );
if( !( result = ::DeleteFile( indir + file ) ) )
{
SetSystemErrorMessage( ::GetLastError( ), indir + file );
t = max;
}
}
}
return result;
}
BOOL CDiskObject::RemoveDirectory( const CString& directory )
/* ============================================================
Function : CDiskObject::RemoveDirectory
Description : Will remove the directory "directory", even
if not empty. Will not remove
subdirectories.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will get an error
string if "FALSE"
Parameters : CString directory - directory to
remove.
Usage : Call to remove a directory.
============================================================*/
{
ClearError( );
BOOL result = TRUE;
CString indir( directory );
if( indir.GetLength( ) )
{
QualifyPath( indir );
// Wipe and remove directory
if( ( result = EmptyDirectory( indir ) ) )
{
Trigger( indir );
if( !( result = ::RemoveDirectory( indir ) ) )
SetSystemErrorMessage( ::GetLastError( ), indir );
}
}
else
{
// Small sanity check, we can't
// delete the current directory.
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::RemoveDirectories( const CString& directory )
/* ============================================================
Function : CDiskObject::RemoveDirectories
Description : Will remove the directory "directory", even
if not empty. Will remove subdirectories.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will get an error
string if "FALSE"
Parameters : CString directory - root directory to
remove.
Usage : Call to remove a directory tree.
============================================================*/
{
ClearError( );
BOOL result = TRUE;
CString indir( directory );
if( indir.GetLength( ) )
{
QualifyPath( indir );
CStringArray directories;
// Get all directories
;
if( ( result = EnumAllDirectories( indir, directories ) ) )
{
// Loop and remove
int max = directories.GetSize( );
for( int t = max - 1; t >= 0 ; t-- )
if( !( result = RemoveDirectory( directories[ t ] ) ) )
t = -1;
if( result )
result = RemoveDirectory( indir );
}
}
else
{
// Small sanity check, we can't
// delete the current directory.
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::EmptyDirectories( const CString& directory )
/* ============================================================
Function : CDiskObject::EmptyDirectories
Description : Will delete all files in "directory". Will
also empty subdirectories.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will get an error
string if "FALSE"
Parameters : CString directory - the root directory
to empty.
Usage : Call to empty a directory tree.
============================================================*/
{
ClearError( );
CString indir( directory );
QualifyPath( indir );
CStringArray directories;
// Get all directories
BOOL result = EnumAllDirectories( indir, directories );
if( result )
{
int max = directories.GetSize( );
// Loop and empty
for( int t = max - 1 ; t >= 0 ; t-- )
if( !( result = EmptyDirectory( directories[ t ] ) ) )
t = -1;
if( result )
result = EmptyDirectory( indir );
}
return result;
}
BOOL CDiskObject::CopyDirectory( const CString& sourceDirectory,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyDirectory
Description : Copies all the files from "sourceDirectory"
to "destDirectory". Existing files will be
overwritten. "destDirectory" will be created
if necessary. Subdirectories will not be
copied.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will get an error
string if "FALSE"
Parameters : CString sourceDirectory - copy from.
CString destDirectory - copy to.
Usage : Call to copy a directory to another
directory.
============================================================*/
{
ClearError( );
CString source( sourceDirectory );
CString dest( destDirectory );
QualifyPath( source );
QualifyPath( dest );
Trigger( dest );
// We don't care if this fails - CopyFiles will
// return an error in that case.
::CreateDirectory( dest, NULL );
return CopyFiles( source, dest );
}
BOOL CDiskObject::CopyDirectories( const CString& sourceDirectory,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyDirectories
Description : Copies all the files and subdirectories
from "sourceDirectory" to "destDirectory",
keeping the directory structure. Existing
files will be overwritten. "destDirectory"
and subdirectories will be created if
necessary.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will get an
error string
if "FALSE"
Parameters : CString sourceDirectory - copy from.
CString destDirectory - copy to.
Usage : Call to copy a directory tree to a new
directory tree.
============================================================*/
{
ClearError( );
CString source( sourceDirectory );
CString dest( destDirectory );
QualifyPath( source );
QualifyPath( dest );
// Enumerate all directories and files below sourceDirectory
CStringArray directories;
directories.Add( source );
BOOL result = EnumAllDirectories( source, directories );
if( result )
{
// Create and copy directories
int max = directories.GetSize( );
for( int t = 0 ; t < max ; t++ )
{
// Create names and copy
CString from = directories[ t ];
CString part = from.Right( from.GetLength( ) - source.GetLength( ) );
CString to = dest + part;
if( !( result = CopyFiles( from, to ) ) )
t = max;
}
}
return result;
}
////////////////////////////////////////
// File-oriented operations
//
BOOL CDiskObject::FileExists( const CString& file )
/* ============================================================
Function : CDiskObject::FileExists
Description : Returns "TRUE" if the file file exists
Access : Public
Return : BOOL - "TRUE" if found
Parameters : CString file - file to check
Usage : Call to check for file existence.
============================================================*/
{
ClearError( );
BOOL result = TRUE;
if( file.GetLength( ) )
{
CString infile( file );
QualifyFile( infile );
HANDLE filehandle = ::CreateFile(
infile,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if( filehandle == INVALID_HANDLE_VALUE )
result = FALSE;
else
CloseHandle( filehandle );
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::FileInformation( const CString& file, BY_HANDLE_FILE_INFORMATION &file_info )
/* ============================================================
Function : CDiskObject::FileInformation
Description : Returns "TRUE" if the file file exists
At this time file_info is filled out
Access : Public
Return : BOOL - "TRUE" if found
Parameters : CString file - file to check
BY_HANDLE_FILE_INFORMATION &file_info - info filled out
Usage : Call to check for file existence and if there
return data about that file.
============================================================*/
{
ClearError( );
BOOL result = TRUE;
if( file.GetLength( ) )
{
CString infile( file );
QualifyFile( infile );
HANDLE filehandle = ::CreateFile(infile,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if( filehandle == INVALID_HANDLE_VALUE )
result = FALSE;
else
{
BY_HANDLE_FILE_INFORMATION file_info_local;
if (GetFileInformationByHandle( filehandle, &file_info_local ))
{
memcpy(&file_info, &file_info_local, sizeof(file_info_local));
}
else
{
result = FALSE;
SetInternalErrorMessage( );
}
CloseHandle( filehandle );
}
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::CreateFile( const CString& file )
/* ============================================================
Function : CDiskObject::CreateFile
Description : Creates the file "file", as well as the
directories necessary
Access : Public
Return : BOOL - "TRUE" if OK
Parameters : CString file - file to create
Usage : Call to create a file.
============================================================*/
{
ClearError( );
BOOL result = TRUE;
if( file.GetLength( ) )
{
CString infile( file );
QualifyFile( infile );
// Split into directory and file
CString directory = GetDirectoryName( infile );
CString filename = GetFileName( infile );
if( ( result = CreateDirectory( directory ) ) )
{
Trigger( file );
HANDLE filehandle = ::CreateFile(
infile,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if( filehandle == INVALID_HANDLE_VALUE )
result = FALSE;
else
CloseHandle( filehandle );
}
}