Subversion Repositories DevTools

Rev

Rev 2075 | Rev 2310 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2075 Rev 2085
Line 12... Line 12...
12
**
12
**
13
**                      Operate in two modes
13
**                      Operate in two modes
14
**                          CopyFile
14
**                          CopyFile
15
**                          DeleteFile
15
**                          DeleteFile
16
**                          Remove Files
16
**                          Remove Files
-
 
17
**                          DeleteDir after deleting specified files
17
**
18
**
18
**                      CopyFile (5 args)
19
**                      CopyFile (5 args)
19
**                          1) Display the ----- Text dest
20
**                          1) Display the ----- Text dest
20
**                          2) Error if the source is not found
21
**                          2) Error if the source is not found
21
**                          3) Create target path if not alraedy existing
22
**                          3) Create target path if not alraedy existing
Line 29... Line 30...
29
**
30
**
30
**                      RemoveFiles ( >1 arg)
31
**                      RemoveFiles ( >1 arg)
31
**                          1) Silenty delete a list of files. The files may
32
**                          1) Silenty delete a list of files. The files may
32
**                             be readonly so the attributes will need to be fixed.
33
**                             be readonly so the attributes will need to be fixed.
33
**
34
**
-
 
35
**                             Assumes the current directory
-
 
36
**
-
 
37
**                       DeleteDir (> 2 args)
-
 
38
**                          1) Silenty delete a list of files. The files may
-
 
39
**                             be readonly so the attributes will need to be fixed.
-
 
40
**                          2) Delete the directory if its empty
-
 
41
**
34
**                      Make paths use '/'
42
**                      Make paths use '/'
35
**
43
**
36
**
44
**
37
**  Information     :
45
**  Information     :
38
**   Compiler       : ANSI C++
46
**   Compiler       : ANSI C++
Line 49... Line 57...
49
 
57
 
50
void fullPath ( _TCHAR * src, _TCHAR *dst );
58
void fullPath ( _TCHAR * src, _TCHAR *dst );
51
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2);
59
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2);
52
void createPaths ( _TCHAR *path );
60
void createPaths ( _TCHAR *path );
53
void deleteFileList( int argc, _TCHAR* argv[] );
61
void deleteFileList( int argc, _TCHAR* argv[] );
-
 
62
void DeleteDir( int argc, _TCHAR* argv[] );
54
 
63
 
55
/*
64
/*
56
**  Global
65
**  Global
57
*/
66
*/
58
_TCHAR currentDir[MAX_FILE + 1];            /* Current working directory */
67
_TCHAR currentDir[MAX_FILE + 1];            /* Current working directory */
Line 84... Line 93...
84
	DWORD rv;
93
	DWORD rv;
85
 
94
 
86
    /*
95
    /*
87
    **  Examine the first argument
96
    **  Examine the first argument
88
    **  Must be a character string of
97
    **  Must be a character string of
89
    **      [0] - Mode : c or d or r
98
    **      [0] - Mode : c or d or r or l
90
    **      [1] - Verbose : 0 .. 9
99
    **      [1] - Verbose : 0 .. 9
91
    */
100
    */
92
    if ( argc > 1 )
101
    if ( argc > 1 )
93
    {
102
    {
94
        if ( argv[1][0] == 'c' ) {
103
        if ( argv[1][0] == 'c' ) {
95
            copyMode = 1;
104
            copyMode = 1;
96
        } else if ( argv[1][0] == 'd' ) {
105
        } else if ( argv[1][0] == 'd' ) {
97
            copyMode = 2;
106
            copyMode = 2;
98
        } else if ( argv[1][0] == 'r' ) {
107
        } else if ( argv[1][0] == 'r' ) {
99
            copyMode = 3;
108
            copyMode = 3;
-
 
109
        } else if ( argv[1][0] == 'D' ) {
-
 
110
            copyMode = 4;
100
        } else {
111
        } else {
101
            ErrorExit("Unknown mode: ",argv[1]);
112
            ErrorExit("Unknown mode: ",argv[1]);
102
        }
113
        }
103
 
114
 
104
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
115
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
Line 136... Line 147...
136
    {
147
    {
137
        deleteFileList(argc - 2, argv + 2 );
148
        deleteFileList(argc - 2, argv + 2 );
138
        return 0;
149
        return 0;
139
    }
150
    }
140
 
151
 
-
 
152
    if ( copyMode == 4 )
-
 
153
    {
-
 
154
        DeleteDir(argc - 2, argv + 2 );
-
 
155
        return 0;
-
 
156
    }
-
 
157
 
141
    /*
158
    /*
142
    **  Determine mode of operation
159
    **  Determine mode of operation
143
    **      Copy or Delete
160
    **      Copy or Delete
144
    */
161
    */
145
    if ( copyMode == 1 && argc == 6 ) {
162
    if ( copyMode == 1 && argc == 6 ) {
Line 316... Line 333...
316
void fullPath ( _TCHAR * src, _TCHAR *dst )
333
void fullPath ( _TCHAR * src, _TCHAR *dst )
317
{
334
{
318
    _TCHAR *udst = dst;
335
    _TCHAR *udst = dst;
319
    _TCHAR *usrc = src;
336
    _TCHAR *usrc = src;
320
	_tcscpy(dst, TEXT("\\\\?\\"));
337
	_tcscpy(dst, TEXT("\\\\?\\"));
321
    if ( src[0] && src[1] != ':' )
338
    if ( ( src[0] && (src[1] != ':') ) || src[0] == 0 )
322
    {
339
    {
323
	    _tcscat(dst, currentDir );
340
	    _tcscat(dst, currentDir );
324
	    _tcscat(dst, TEXT("\\") );
341
	    _tcscat(dst, TEXT("\\") );
325
    }
342
    }
326
    udst += _tcslen(udst);
343
    udst += _tcslen(udst);
Line 339... Line 356...
339
    **  Now remove silly relative path bits
356
    **  Now remove silly relative path bits
340
    */
357
    */
341
    udst = dst;
358
    udst = dst;
342
    while ( *udst )
359
    while ( *udst )
343
    {
360
    {
344
        if ( udst[0] == '\\' && udst[1] == '.' && udst[2] == '\\'  )
361
        if ( udst[0] == '\\' && udst[1] == '.' && (udst[2] == '\\' || udst[2] == 0) )
345
        {
362
        {
346
            TCHAR* ptr1 = udst;
363
            TCHAR* ptr1 = udst;
347
            TCHAR* ptr2 = udst + 2;
364
            TCHAR* ptr2 = udst + 2;
348
            while ( *ptr1++ = *ptr2++ ) {}
365
            while ( *ptr1++ = *ptr2++ ) {}
349
        }
366
        }
Line 425... Line 442...
425
        }
442
        }
426
    }
443
    }
427
}
444
}
428
 
445
 
429
/*----------------------------------------------------------------------------
446
/*----------------------------------------------------------------------------
-
 
447
** FUNCTION           : DeleteDir
-
 
448
**
-
 
449
** DESCRIPTION        : Delete a list of files in a specified directory
-
 
450
**                      Ensure files are writable
-
 
451
**                      Wilcarding is supported
-
 
452
**
-
 
453
**                      Then delete the directory - if its empty
-
 
454
**
-
 
455
**
-
 
456
** INPUTS             : argc    - count of args
-
 
457
**                      argv    - list of files to delete
-
 
458
**                                First entry is a directory
-
 
459
**
-
 
460
** RETURNS            : Wil not return on error
-
 
461
**
-
 
462
----------------------------------------------------------------------------*/
-
 
463
 
-
 
464
void DeleteDir( int argc, _TCHAR* argv[] )
-
 
465
{
-
 
466
	DWORD rv;
-
 
467
    _TCHAR* baseDir;
-
 
468
    WIN32_FIND_DATA FindFileData;
-
 
469
    HANDLE hFind;
-
 
470
 
-
 
471
    if ( argc < 3 )
-
 
472
        ErrorExit("Insuffiecient arguments for DeleteDir",L"");
-
 
473
        
-
 
474
    /*
-
 
475
    **  Display the user message
-
 
476
    **      Supress display if the message is empty
-
 
477
    */
-
 
478
    if ( argv[0][0] )
-
 
479
    {
-
 
480
        fprintf(stderr, "%ls\n", argv[0]);
-
 
481
        fflush(stderr) ;
-
 
482
    }
-
 
483
 
-
 
484
    /*
-
 
485
    **  Extract the base directory from the argument list
-
 
486
    **  This must be a directory
-
 
487
    */
-
 
488
    baseDir = argv[1];
-
 
489
    argc -= 2;
-
 
490
    argv+=2;
-
 
491
 
-
 
492
    /*
-
 
493
    **  Ensure that the base directory exists
-
 
494
    */
-
 
495
    fullPath( baseDir, dst);
-
 
496
    rv = GetFileAttributesW( dst );
-
 
497
    if ( rv == INVALID_FILE_ATTIBUTES )
-
 
498
    {
-
 
499
        /*
-
 
500
        **  Directory does not exists
-
 
501
        **  Assume its aleady deleted
-
 
502
        */
-
 
503
        if ( verbose > 1 )
-
 
504
            fprintf(stderr, "Base dir does not exist: %ls\n", baseDir);
-
 
505
        return;
-
 
506
    }
-
 
507
 
-
 
508
    if ( !(rv & FILE_ATTRIBUTE_DIRECTORY) )
-
 
509
    {
-
 
510
        /*
-
 
511
        **  Target is not a directory
-
 
512
        **  Don't do anything
-
 
513
        */
-
 
514
        if ( verbose > 1 )
-
 
515
            fprintf(stderr, "Base dir is not a directory: %ls\n", baseDir);
-
 
516
        return;
-
 
517
    }
-
 
518
 
-
 
519
    /*
-
 
520
    **  Process all the suffixes
-
 
521
    **  They may contain a wild card
-
 
522
    */
-
 
523
    for ( ; argc ; argc--, argv++ )
-
 
524
    {
-
 
525
    	_tcscpy(src, baseDir);
-
 
526
    	_tcscat(src, TEXT("\\"));
-
 
527
    	_tcscat(src, *argv);
-
 
528
        fullPath( src, dst);
-
 
529
 
-
 
530
        hFind = FindFirstFile(dst, &FindFileData);
-
 
531
        if (hFind == INVALID_HANDLE_VALUE)
-
 
532
        {
-
 
533
            /*
-
 
534
            **  No match
-
 
535
            */
-
 
536
            if ( verbose > 1 )
-
 
537
                fprintf(stderr, "No Match: %ls\n", dst);
-
 
538
            continue;
-
 
539
        }
-
 
540
 
-
 
541
        /*
-
 
542
        **  Iterate over all the files
-
 
543
        */
-
 
544
        do {
-
 
545
            if ( _tcscmp( TEXT("."), FindFileData.cFileName ) == 0 )
-
 
546
                continue;
-
 
547
 
-
 
548
            if ( _tcscmp( TEXT(".."), FindFileData.cFileName ) == 0 )
-
 
549
                continue;
-
 
550
 
-
 
551
            /*
-
 
552
            **  Create a full path to the file
-
 
553
            */
-
 
554
    	    _tcscpy(src, baseDir);
-
 
555
    	    _tcscat(src, TEXT("\\"));
-
 
556
    	    _tcscat(src, FindFileData.cFileName);
-
 
557
 
-
 
558
            if ( verbose )
-
 
559
                fprintf(stderr, "Match: %ls\n", FindFileData.cFileName);
-
 
560
 
-
 
561
            /*
-
 
562
            **  Force file to be writable
-
 
563
            */
-
 
564
            if ( FindFileData.dwFileAttributes  & FILE_ATTRIBUTE_READONLY)
-
 
565
            {
-
 
566
                rv = SetFileAttributesW( src, FindFileData.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY );
-
 
567
                if ( rv == 0 )
-
 
568
                {
-
 
569
                    fprintf(stderr, "Warning: Attempt to allow write access: %ls\n", FindFileData.cFileName);
-
 
570
                }
-
 
571
            }
-
 
572
 
-
 
573
            /*
-
 
574
            **  Delete the file
-
 
575
            */
-
 
576
            if (! DeleteFile( src ) )
-
 
577
            {
-
 
578
                fprintf(stderr, "Warning: Did not remove file: %ls\n", FindFileData.cFileName);
-
 
579
            }
-
 
580
 
-
 
581
        } while  (FindNextFile(hFind, &FindFileData) != 0);
-
 
582
        FindClose(hFind);
-
 
583
    }
-
 
584
 
-
 
585
    /*
-
 
586
    **  Finally delete the diretory
-
 
587
    **      Unless its '.'
-
 
588
    */
-
 
589
    if ( _tcscmp( TEXT("."),baseDir) != 0 )
-
 
590
    {
-
 
591
        fullPath( baseDir, dst);
-
 
592
        if ( verbose > 1 )
-
 
593
            fprintf(stderr, "Delete Directory: %ls\n", baseDir);
-
 
594
        if ( ! RemoveDirectory( dst ) )
-
 
595
        {
-
 
596
            if ( verbose )
-
 
597
                fprintf(stderr, "Directory not deleted: %ls\n", baseDir);
-
 
598
        }
-
 
599
    }
-
 
600
}
-
 
601
 
-
 
602
 
-
 
603
/*----------------------------------------------------------------------------
430
** FUNCTION           : ErrorExit
604
** FUNCTION           : ErrorExit
431
**
605
**
432
** DESCRIPTION        : Error processing
606
** DESCRIPTION        : Error processing
433
**                      Report an error and terminate process
607
**                      Report an error and terminate process
434
**
608
**