Subversion Repositories DevTools

Rev

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

Rev 2310 Rev 2311
Line 4... Line 4...
4
**
4
**
5
**  Project/Product : 
5
**  Project/Product : 
6
**  Filename        : JatsFileUtil.c
6
**  Filename        : JatsFileUtil.c
7
**  Author(s)       : DDP
7
**  Author(s)       : DDP
8
**
8
**
9
**  Description     :
9
**  Description     :   Jats Build System File utility
10
**                      The program mimics - but a lot faster a Jats Makefile fragment
10
**                      Used by the generated makefiles to perform specific operations
-
 
11
**
11
**                      The program will:
12
**                      The program exists to solve problems:
-
 
13
**                          Windows: Shell is very very slow to start up
-
 
14
**                          Windows: Some commands have ~260 character path length issue
-
 
15
**                          Windows/Solaris/Linux: Compatibility issues with the 'rm' command
-
 
16
**                          All: Consistent use of '/' as a path separator
12
**
17
**
-
 
18
**                      Note: There are two flavors of this program that MUST be
13
**                      Operate in 5 modes
19
**                            kept in sync.
-
 
20
**
-
 
21
**                      The program will perform the following operations:
14
**                          CopyFile
22
**                          (c) CopyFile
15
**                          DeleteFile
23
**                          (d) DeleteFile
16
**                          Remove Files (wildcard)
24
**                          (r) Remove Files (wildcard)
17
**                          DeleteDir after deleting specified files (wildcard)
25
**                          (D) DeleteDir after deleting specified files (wildcard)
18
**                          Delete Directory Tree
26
**                          (T) Delete Directory Tree
-
 
27
**
-
 
28
**                      Example Usage
19
**
29
**
20
**                          JatsFileUtil.exe c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
30
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
21
**                          JatsFileUtil.exe d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
31
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
22
**                          JatsFileUtil.exe r9 'deleteFile'  a1 b2 c3
32
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
23
**                          JatsFileUtil.exe D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
33
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
24
**                          JatsFileUtil.exe T9 'DeleteTree'  interface
34
**                          JatsFileUtil T9 'DeleteTree'  interface
25
**
35
**
26
**                      First two arguments are common to all
36
**                      First two arguments are common to all
27
**                          argv[1]     - Mode specifier, Debug specifier
37
**                          argv[1]     - Mode specifier, Debug specifier
28
**                          argv[2]     - Display Text
38
**                          argv[2]     - Display Text
29
**
39
**
30
**                      CopyFile (5 args)
-
 
31
**                          1) Display the ----- Text dest
-
 
32
**                          2) Error if the source is not found
-
 
33
**                          3) Create target path if not alraedy existing
-
 
34
**                          4) Copy the source to the target - overwritting the target
-
 
35
**                          5) Set File mode
-
 
36
**                          6) Test for file existence
-
 
37
**
-
 
38
**                      DeleteFile (3 args)
-
 
39
**                          1) Display the ----- Text dest
-
 
40
**                          2) Delete the target file
-
 
41
**
-
 
42
**                      RemoveFiles ( >1 arg)
-
 
43
**                          1) Silenty delete a list of files. The files may
-
 
44
**                             be readonly so the attributes will need to be fixed.
-
 
45
**
-
 
46
**                             Assumes the current directory
-
 
47
**
-
 
48
**                       DeleteDir (> 2 args)
-
 
49
**                          1) Silenty delete a list of files. The files may
-
 
50
**                             be readonly so the attributes will need to be fixed.
-
 
51
**                          2) Delete the directory if its empty
-
 
52
**
-
 
53
**                        Delete directory tree
-
 
54
**                          1) Silently delete an entire directory tree
-
 
55
**                             including files and subdiretories
-
 
56
**
-
 
57
**                      Make paths use '/'
-
 
58
**
-
 
59
**
-
 
60
**  Information     :
40
**  Information     :
61
**   Compiler       : ANSI C++
41
**   Compiler       : ANSI C
62
**   Target         : 
42
**   Target         : Windows 2000+, Linux, Solaris8+
63
**
43
**
64
***==========================================================================*/
44
***==========================================================================*/
65
 
45
 
66
#include <tchar.h>
46
#include <tchar.h>
67
#include <stdio.h>
47
#include <stdio.h>
Line 87... Line 67...
87
** FUNCTION           : main
67
** FUNCTION           : main
88
**
68
**
89
** DESCRIPTION        : Main entry points
69
** DESCRIPTION        : Main entry points
90
**
70
**
91
**
71
**
92
** INPUTS             : Arguments are fixed
-
 
93
**                          Mode    - ModeDebug
-
 
94
**                          Text    - Text to output
72
** INPUTS             : argc            - Argument Count
95
**                          dest    - Target Path
73
**                      argv            - Argument Vector
96
**                          file    - Source path   [Copy Only]
-
 
97
**                          fmode   - File mode     [Copy Only]
-
 
98
**
74
**
99
** RETURNS            : 0 - All is good
75
** RETURNS            : 0 - All is good
100
**
76
**
101
----------------------------------------------------------------------------*/
77
----------------------------------------------------------------------------*/
102
 
78
 
103
int _tmain(int argc, _TCHAR* argv[])
79
int _tmain(int argc, _TCHAR* argv[])
104
{
80
{
105
    /*
81
    /*
106
    **  Examine the first argument
82
    **  Examine the first argument
107
    **  Must be a character string of
83
    **  Must be a character string of
108
    **      [0] - Mode : c or d or r or l
84
    **      [0] - Mode : One character
109
    **      [1] - Verbose : 0 .. 9
85
    **      [1] - Verbose : 0 .. 9
110
    */
86
    */
111
    if ( argc > 1 && ( argv[1][1] >= '0' && argv[1][1] <= '9' ) )
87
    if ( argc > 1 && ( argv[1][1] >= '0' && argv[1][1] <= '9' ) )
112
    {
88
    {
113
            verbose = argv[1][1] - '0';
89
        verbose = argv[1][1] - '0';
114
    }
-
 
115
 
90
 
116
    /*
91
        /*
117
    **  If Verbose, then display arguments
92
        **  If Verbose, then display arguments
118
    */
93
        */
119
    if ( verbose > 2 )
94
        if ( verbose > 2 )
120
    {
-
 
121
        int ii;
-
 
122
        for ( ii = 0; ii < argc ; ii++ )
-
 
123
        {
95
        {
-
 
96
            int ii;
-
 
97
            for ( ii = 0; ii < argc ; ii++ )
-
 
98
            {
124
            fprintf(stderr, "Arg%d: %ls:\n", ii, argv[ii] );
99
                fprintf(stderr, "Arg%d: %ls:\n", ii, argv[ii] );
-
 
100
            }
-
 
101
            fflush(stderr) ;
125
        }
102
        }
126
        fflush(stderr) ;
-
 
127
    }
103
    }
128
 
104
    
129
    /*
105
    /*
130
    **  Switch to required operation
106
    **  Switch to required operation
131
    */
107
    */
132
    switch ( argv[1][0]  )
108
    switch( argv[1][0] )
133
    {
109
    {
-
 
110
        /*
-
 
111
        **  CopyFile
-
 
112
        **      argv[2] - Text
-
 
113
        **      argv[3] - target path
-
 
114
        **      argv[4] - Source path
-
 
115
        **      argv[5] - File attributes
-
 
116
        */
134
        case 'c':
117
        case 'c':
135
            copyOneFile(argc, argv);
118
            copyOneFile(argc, argv);
136
            break;
119
            break;
137
 
120
 
-
 
121
        /*
-
 
122
        **  UnCopy a file
-
 
123
        **      argv[2] - Text
-
 
124
        **      argv[3] - target path
-
 
125
        */
138
        case 'd':
126
        case 'd':
139
            {
127
            {
140
                _TCHAR * dst;
128
                _TCHAR * dst;
141
        
129
        
142
                if ( argc != 4 )
130
                if ( argc != 4 )
Line 152... Line 140...
152
                DeleteOneFile(dst);
140
                DeleteOneFile(dst);
153
                free (dst);
141
                free (dst);
154
            }
142
            }
155
            break;
143
            break;
156
 
144
 
-
 
145
        /*
-
 
146
        **  Remove named files
-
 
147
        **      argv[2]     - Text
-
 
148
        **      argv[3]+    - target path
-
 
149
        */
157
        case 'r':
150
        case 'r':
158
                {
151
                {
159
                    int ii;
152
                    int ii;
160
 
153
 
161
                    if ( argc < 4 )
154
                    if ( argc < 4 )
Line 177... Line 170...
177
                        free (dst);
170
                        free (dst);
178
                    }
171
                    }
179
                }
172
                }
180
            break;
173
            break;
181
 
174
 
-
 
175
        /*
-
 
176
        **  Delete files in directory - with wildcards
-
 
177
        **      argv[2]     - Text
-
 
178
        **      argv[3]     - Base directory
-
 
179
        **      argv[4]+    - Files in dir to delete.
-
 
180
        */
182
        case 'D':
181
        case 'D':
183
            DeleteDir(argc - 2, argv + 2 );
182
            DeleteDir(argc - 2, argv + 2 );
184
            break;
183
            break;
185
 
184
 
-
 
185
        /*
-
 
186
        **  Delete files recursively
-
 
187
        **      argv[2] - Text
-
 
188
        **      argv[3]+  Base directory
-
 
189
        */
186
        case 'T':
190
        case 'T':
187
            DeleteDirTree(argc - 2, argv + 2 );
191
            DeleteDirTree(argc - 2, argv + 2 );
188
            break;
192
            break;
189
            
193
            
190
        default :
194
        default :
Line 361... Line 365...
361
    HANDLE hFind;
365
    HANDLE hFind;
362
    _TCHAR *target;
366
    _TCHAR *target;
363
    _TCHAR *dirPath;
367
    _TCHAR *dirPath;
364
 
368
 
365
    if ( argc < 3 )
369
    if ( argc < 3 )
366
        ErrorExit("Insuffiecient arguments for DeleteDir",L"");
370
        ErrorExit("Insufficient arguments for DeleteDir",L"");
367
        
371
        
368
    /*
372
    /*
369
    **  Display the user message
373
    **  Display the user message
370
    **      Supress display if the message is empty
374
    **      Suppress display if the message is empty
371
    */
375
    */
372
    if ( argv[0][0] )
376
    if ( argv[0][0] )
373
    {
377
    {
374
        fprintf(stderr, "%ls\n", argv[0]);
378
        fprintf(stderr, "%ls\n", argv[0]);
375
        fflush(stderr) ;
379
        fflush(stderr) ;
Line 390... Line 394...
390
    rv = GetFileAttributesW( dirPath );
394
    rv = GetFileAttributesW( dirPath );
391
    if ( rv == INVALID_FILE_ATTIBUTES )
395
    if ( rv == INVALID_FILE_ATTIBUTES )
392
    {
396
    {
393
        /*
397
        /*
394
        **  Directory does not exists
398
        **  Directory does not exists
395
        **  Assume its aleady deleted
399
        **  Assume its already deleted
396
        */
400
        */
397
        if ( verbose > 1 )
401
        if ( verbose > 1 )
398
            fprintf(stderr, "Base dir does not exist: %ls\n", baseDir);
402
            fprintf(stderr, "Base dir does not exist: %ls\n", baseDir);
399
        free(dirPath);
403
        free(dirPath);
400
        return;
404
        return;
Line 455... Line 459...
455
        } while  (FindNextFile(hFind, &FindFileData) != 0);
459
        } while  (FindNextFile(hFind, &FindFileData) != 0);
456
        FindClose(hFind);
460
        FindClose(hFind);
457
    }
461
    }
458
 
462
 
459
    /*
463
    /*
460
    **  Finally delete the diretory
464
    **  Finally delete the directory
461
    **      Unless its '.'
465
    **      Unless its '.'
462
    */
466
    */
463
    if ( _tcscmp( TEXT("."),baseDir) != 0 )
467
    if ( _tcscmp( TEXT("."),baseDir) != 0 )
464
    {
468
    {
465
        if ( verbose > 1 )
469
        if ( verbose > 1 )
Line 491... Line 495...
491
void DeleteDirTree( int argc, _TCHAR* argv[] )
495
void DeleteDirTree( int argc, _TCHAR* argv[] )
492
{
496
{
493
    int ii;
497
    int ii;
494
 
498
 
495
    if ( argc < 2 )
499
    if ( argc < 2 )
496
        ErrorExit("Insuffiecient arguments for DeleteDir",L"");
500
        ErrorExit("Insufficient arguments for DeleteDirTree",L"");
497
        
501
        
498
    /*
502
    /*
499
    **  Display the user message
503
    **  Display the user message
500
    **      Supress display if the message is empty
504
    **      Suppress display if the message is empty
501
    */
505
    */
502
    if ( argv[0][0] )
506
    if ( argv[0][0] )
503
    {
507
    {
504
        fprintf(stderr, "%ls\n", argv[0]);
508
        fprintf(stderr, "%ls\n", argv[0]);
505
        fflush(stderr) ;
509
        fflush(stderr) ;
Line 547... Line 551...
547
    rv = GetFileAttributesW( dirPath );
551
    rv = GetFileAttributesW( dirPath );
548
    if ( rv == INVALID_FILE_ATTIBUTES )
552
    if ( rv == INVALID_FILE_ATTIBUTES )
549
    {
553
    {
550
        /*
554
        /*
551
        **  Directory does not exists
555
        **  Directory does not exists
552
        **  Assume its aleady deleted
556
        **  Assume its already deleted
553
        */
557
        */
554
        if ( verbose > 1 )
558
        if ( verbose > 1 )
555
            fprintf(stderr, "Base dir does not exist: %ls\n", baseDir);
559
            fprintf(stderr, "Base dir does not exist: %ls\n", baseDir);
556
        free(dirPath);
560
        free(dirPath);
557
        return;
561
        return;
Line 609... Line 613...
609
        } while  (FindNextFile(hFind, &FindFileData) != 0);
613
        } while  (FindNextFile(hFind, &FindFileData) != 0);
610
        FindClose(hFind);
614
        FindClose(hFind);
611
    }
615
    }
612
 
616
 
613
    /*
617
    /*
614
    **  Finally delete the diretory
618
    **  Finally delete the directory
615
    */
619
    */
616
    if ( verbose > 1 )
620
    if ( verbose > 1 )
617
        fprintf(stderr, "Delete Directory: %ls\n", baseDir);
621
        fprintf(stderr, "Delete Directory: %ls\n", baseDir);
618
 
622
 
619
    if ( ! RemoveDirectory( dirPath ) )
623
    if ( ! RemoveDirectory( dirPath ) )