Subversion Repositories DevTools

Rev

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

Rev 2073 Rev 2075
Line 1... Line 1...
1
/*============================================================================
1
/*============================================================================
2
** Copyright (C) 1998-2012 Vix Technology, All rights reserved
2
** Copyright (C) 1998-2012 Vix Technology, All rights reserved
3
**============================================================================
3
**============================================================================
4
**
4
**
5
**  Project/Product : 
5
**  Project/Product : 
6
**  Filename        : CopyFile.c
6
**  Filename        : JatsFileUtil.c
7
**  Author(s)       : DDP
7
**  Author(s)       : DDP
8
**
8
**
9
**  Description     :
9
**  Description     :
10
**                      The program mimics - but a lot faster a Jats Makefile fragment
10
**                      The program mimics - but a lot faster a Jats Makefile fragment
11
**                      The program will:
11
**                      The program will:
12
**
12
**
13
**                      Operate in two modes
13
**                      Operate in two modes
14
**                          CopyFile
14
**                          CopyFile
15
**                          DeleteFile
15
**                          DeleteFile
-
 
16
**                          Remove Files
16
**
17
**
17
**                      CopyFile (5 args)
18
**                      CopyFile (5 args)
18
**                          1) Display the ----- Text dest
19
**                          1) Display the ----- Text dest
19
**                          2) Error if the source is not found
20
**                          2) Error if the source is not found
20
**                          3) Create target path if not alraedy existing
21
**                          3) Create target path if not alraedy existing
Line 24... Line 25...
24
**
25
**
25
**                      DeleteFile (3 args)
26
**                      DeleteFile (3 args)
26
**                          1) Display the ----- Text dest
27
**                          1) Display the ----- Text dest
27
**                          2) Delete the target file
28
**                          2) Delete the target file
28
**
29
**
-
 
30
**                      RemoveFiles ( >1 arg)
-
 
31
**                          1) Silenty delete a list of files. The files may
-
 
32
**                             be readonly so the attributes will need to be fixed.
-
 
33
**
29
**                      Make paths use '/'
34
**                      Make paths use '/'
30
**
35
**
31
**
36
**
32
**  Information     :
37
**  Information     :
33
**   Compiler       : ANSI C++
38
**   Compiler       : ANSI C++
Line 43... Line 48...
43
#define INVALID_FILE_ATTIBUTES ((DWORD) -1)
48
#define INVALID_FILE_ATTIBUTES ((DWORD) -1)
44
 
49
 
45
void fullPath ( _TCHAR * src, _TCHAR *dst );
50
void fullPath ( _TCHAR * src, _TCHAR *dst );
46
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2);
51
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2);
47
void createPaths ( _TCHAR *path );
52
void createPaths ( _TCHAR *path );
-
 
53
void deleteFileList( int argc, _TCHAR* argv[] );
48
 
54
 
49
/*
55
/*
50
**  Global
56
**  Global
51
*/
57
*/
52
_TCHAR currentDir[MAX_FILE + 1];            /* Current working directory */
58
_TCHAR currentDir[MAX_FILE + 1];            /* Current working directory */
53
char  verbose = 1;                          /* Debugging aid */
59
char  verbose = 1;                          /* Debugging aid */
54
char  copyMode = 0;                         /* Mode */
60
char  copyMode = 0;                         /* Mode */
55
 
61
 
-
 
62
_TCHAR src [MAX_FILE + 1];
-
 
63
_TCHAR dst [MAX_FILE + 1];
-
 
64
 
56
/*----------------------------------------------------------------------------
65
/*----------------------------------------------------------------------------
57
** FUNCTION           : main
66
** FUNCTION           : main
58
**
67
**
59
** DESCRIPTION        : Main entry points
68
** DESCRIPTION        : Main entry points
60
**
69
**
Line 70... Line 79...
70
**
79
**
71
----------------------------------------------------------------------------*/
80
----------------------------------------------------------------------------*/
72
 
81
 
73
int _tmain(int argc, _TCHAR* argv[])
82
int _tmain(int argc, _TCHAR* argv[])
74
{
83
{
75
 
-
 
76
	DWORD rv;
84
	DWORD rv;
77
	_TCHAR src [MAX_FILE + 1];
-
 
78
	_TCHAR dst [MAX_FILE + 1];
-
 
79
 
85
 
80
    /*
86
    /*
81
    **  Examine the first argument
87
    **  Examine the first argument
82
    **  Must be a character string of
88
    **  Must be a character string of
83
    **      [0] - Mode : c or d
89
    **      [0] - Mode : c or d or r
84
    **      [1] - Verbose : 0 .. 9
90
    **      [1] - Verbose : 0 .. 9
85
    */
91
    */
86
    if ( argc > 1 )
92
    if ( argc > 1 )
87
    {
93
    {
88
        if ( argv[1][0] == 'c' ) {
94
        if ( argv[1][0] == 'c' ) {
89
            copyMode = 1;
95
            copyMode = 1;
90
        } else if ( argv[1][0] == 'd' ) {
96
        } else if ( argv[1][0] == 'd' ) {
91
            copyMode = 2;
97
            copyMode = 2;
-
 
98
        } else if ( argv[1][0] == 'r' ) {
-
 
99
            copyMode = 3;
92
        } else {
100
        } else {
93
            ErrorExit("CopyFile: Unknown mode",argv[1]);
101
            ErrorExit("Unknown mode: ",argv[1]);
94
        }
102
        }
95
 
103
 
96
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
104
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
97
            verbose = argv[1][1] - '0';
105
            verbose = argv[1][1] - '0';
98
        }
106
        }
99
    }
107
    }
100
 
108
 
101
    /*
109
    /*
102
    **  If Verbose, then display arguments
110
    **  If Verbose, then display arguments
103
    */
111
    */
104
    if ( verbose )
112
    if ( verbose > 1 )
105
    {
113
    {
106
        int ii;
114
        int ii;
107
        for ( ii = 0; ii < argc ; ii++ )
115
        for ( ii = 0; ii < argc ; ii++ )
108
        {
116
        {
109
            fprintf(stderr, "Arg%d: %ls:\n", ii, argv[ii] );
117
            fprintf(stderr, "Arg%d: %ls:\n", ii, argv[ii] );
Line 116... Line 124...
116
    /*
124
    /*
117
    **  Determine cwd
125
    **  Determine cwd
118
    **  Used for ab path conversion
126
    **  Used for ab path conversion
119
    */
127
    */
120
    GetCurrentDirectory( MAX_FILE, currentDir );
128
    GetCurrentDirectory( MAX_FILE, currentDir );
121
    if ( verbose )
129
    if ( verbose > 1 )
122
        fprintf(stderr, "CWD: %ls\n", currentDir );
130
        fprintf(stderr, "CWD: %ls\n", currentDir );
-
 
131
 
-
 
132
    /*
-
 
133
    **  Determine required operation
-
 
134
    */
-
 
135
    if ( copyMode == 3 )
123
    
136
    {
-
 
137
        deleteFileList(argc - 2, argv + 2 );
-
 
138
        return 0;
124
    
139
    }
-
 
140
 
125
    /*
141
    /*
126
    **  Determine mode of operation
142
    **  Determine mode of operation
127
    **      Copy or Delete
143
    **      Copy or Delete
128
    */
144
    */
129
    if ( copyMode == 1 && argc == 6 ) {
145
    if ( copyMode == 1 && argc == 6 ) {
Line 155... Line 171...
155
        rv = GetFileAttributesW( src );
171
        rv = GetFileAttributesW( src );
156
        if ( rv == INVALID_FILE_ATTIBUTES )
172
        if ( rv == INVALID_FILE_ATTIBUTES )
157
        {
173
        {
158
    /* Need to be a better message */
174
    /* Need to be a better message */
159
            fprintf(stderr, "Source: %ls\n", src);
175
            fprintf(stderr, "Source: %ls\n", src);
160
            ErrorExit("Error: Source File not found: ", argv[4]);
176
            ErrorExit("Source File not found: ", argv[4]);
161
        }
177
        }
162
    }
178
    }
163
 
179
 
164
    /*
180
    /*
165
    **  Remove the ReadOnly attribute on the dest file
181
    **  Remove the ReadOnly attribute on the dest file
Line 175... Line 191...
175
        {
191
        {
176
            rv &= ~FILE_ATTRIBUTE_READONLY;
192
            rv &= ~FILE_ATTRIBUTE_READONLY;
177
            rv = SetFileAttributesW( dst, rv );
193
            rv = SetFileAttributesW( dst, rv );
178
            if ( rv == 0 )
194
            if ( rv == 0 )
179
            {
195
            {
180
                ErrorExit("Error: Attempt to allow write access: ", argv[3]);
196
                ErrorExit("Attempt to allow write access: ", argv[3]);
181
            }
197
            }
182
        }
198
        }
183
 
199
 
184
        if (! DeleteFile( dst ) )
200
        if (! DeleteFile( dst ) )
185
        {
201
        {
186
                ErrorExit("Error: Deleting file: ", argv[3]);
202
                ErrorExit("Deleting file: ", argv[3]);
187
        }
203
        }
188
    }
204
    }
189
    
205
    
190
    if ( copyMode == 1 )
206
    if ( copyMode == 1 )
191
    {
207
    {
Line 200... Line 216...
200
        **   Copy the file
216
        **   Copy the file
201
        */
217
        */
202
        if ( ! CopyFile( src, dst, FALSE ) )
218
        if ( ! CopyFile( src, dst, FALSE ) )
203
        {
219
        {
204
            rv = GetLastError();
220
            rv = GetLastError();
205
            fprintf(stderr, "CopyFileLast Error: %ld\n", rv);
221
            fprintf(stderr, "CopyFile Last Error: %ld\n", rv);
206
            ErrorExit("Error: Copy Error: ", argv[4]);
222
            ErrorExit("Copy Error: ", argv[4]);
207
        }
223
        }
208
 
224
 
209
        /*
225
        /*
210
        **  Test for files existence
226
        **  Test for files existence
211
        */
227
        */
Line 213... Line 229...
213
            fprintf(stderr, "Test target was copied: %ls\n", dst);
229
            fprintf(stderr, "Test target was copied: %ls\n", dst);
214
        rv = GetFileAttributesW( dst );
230
        rv = GetFileAttributesW( dst );
215
        if ( rv == INVALID_FILE_ATTIBUTES )
231
        if ( rv == INVALID_FILE_ATTIBUTES )
216
        {
232
        {
217
    /* Need to be a better message */
233
    /* Need to be a better message */
218
            ErrorExit("Error: File not found after copy: ", argv[3]);
234
            ErrorExit("File not found after copy: ", argv[3]);
219
        }
235
        }
220
 
236
 
221
        /*
237
        /*
222
        **  Set the files attributes
238
        **  Set the files attributes
223
        **      Assume read-only
239
        **      Assume read-only
Line 230... Line 246...
230
            rv |= FILE_ATTRIBUTE_READONLY;
246
            rv |= FILE_ATTRIBUTE_READONLY;
231
            rv &= ~FILE_ATTRIBUTE_NORMAL;
247
            rv &= ~FILE_ATTRIBUTE_NORMAL;
232
            rv = SetFileAttributesW( dst, rv );
248
            rv = SetFileAttributesW( dst, rv );
233
            if ( rv == 0 )
249
            if ( rv == 0 )
234
            {
250
            {
235
                ErrorExit("Error: Setting ReadOnly: ", argv[3]);
251
                ErrorExit("Setting ReadOnly: ", argv[3]);
236
            }
252
            }
237
        }
253
        }
238
    }
254
    }
239
 
255
 
240
    return 0;
256
    return 0;
Line 265... Line 281...
265
//fprintf(stderr, "createPaths: %ls\n", path);
281
//fprintf(stderr, "createPaths: %ls\n", path);
266
            if ( ! CreateDirectoryW ( path, NULL ))
282
            if ( ! CreateDirectoryW ( path, NULL ))
267
            {
283
            {
268
                rv = GetLastError();
284
                rv = GetLastError();
269
                if ( rv != ERROR_ALREADY_EXISTS )
285
                if ( rv != ERROR_ALREADY_EXISTS )
270
                    ErrorExit("Error: Cound not create directories:", path);
286
                    ErrorExit("Could not create directories:", path);
271
            }
287
            }
272
            *ptr = '\\';
288
            *ptr = '\\';
273
        }
289
        }
274
        ptr++;
290
        ptr++;
275
    }
291
    }
Line 353... Line 369...
353
        {
369
        {
354
            *udst++;
370
            *udst++;
355
        }
371
        }
356
    }
372
    }
357
 
373
 
358
if ( verbose )
374
if ( verbose > 1)
359
    fprintf(stderr, "AbsPath: %ls\n", dst);
375
    fprintf(stderr, "AbsPath: %ls\n", dst);
360
}
376
}
361
 
377
 
-
 
378
/*----------------------------------------------------------------------------
-
 
379
** FUNCTION           : deleteFileList
-
 
380
**
-
 
381
** DESCRIPTION        : Delete a list of files
-
 
382
**                      Ensure files are writable
-
 
383
**
-
 
384
**
-
 
385
** INPUTS             : argc    - count of args
-
 
386
**                      argv    - list of files to delete
-
 
387
**
-
 
388
** RETURNS            : Wil not return on error
-
 
389
**
-
 
390
----------------------------------------------------------------------------*/
-
 
391
 
-
 
392
void deleteFileList( int argc, _TCHAR* argv[] )
-
 
393
{
-
 
394
	DWORD rv;
-
 
395
 
-
 
396
    for ( ; argc ; argc--, argv++ )
-
 
397
    {
-
 
398
        fullPath( *argv, dst);
-
 
399
 
-
 
400
        /*
-
 
401
        **  Remove the ReadOnly attribute on the dest file
-
 
402
        */
-
 
403
        if ( verbose > 1 )
-
 
404
            fprintf(stderr, "Remove target file: %ls\n", dst);
-
 
405
 
-
 
406
        rv = GetFileAttributesW( dst );
-
 
407
        if ( rv != INVALID_FILE_ATTIBUTES )
-
 
408
        {
-
 
409
            if ( verbose )
-
 
410
                fprintf(stderr, "Delete file: %ls : Attr: 0x%lx\n", *argv, rv);
-
 
411
            if ( rv & FILE_ATTRIBUTE_READONLY )
-
 
412
            {
-
 
413
                rv &= ~FILE_ATTRIBUTE_READONLY;
-
 
414
                rv = SetFileAttributesW( dst, rv );
-
 
415
                if ( rv == 0 )
-
 
416
                {
-
 
417
                    fprintf(stderr, "Warning: Attempt to allow write access: %ls\n", *argv);
-
 
418
                }
-
 
419
            }
-
 
420
 
-
 
421
            if (! DeleteFile( dst ) )
-
 
422
            {
-
 
423
                fprintf(stderr, "Warning: Did not remove file: %ls\n", *argv);
-
 
424
            }
-
 
425
        }
-
 
426
    }
-
 
427
}
362
 
428
 
363
/*----------------------------------------------------------------------------
429
/*----------------------------------------------------------------------------
364
** FUNCTION           : ErrorExit
430
** FUNCTION           : ErrorExit
365
**
431
**
366
** DESCRIPTION        : Error processing
432
** DESCRIPTION        : Error processing
Line 374... Line 440...
374
**
440
**
375
----------------------------------------------------------------------------*/
441
----------------------------------------------------------------------------*/
376
 
442
 
377
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2)
443
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2)
378
{ 
444
{ 
379
   fprintf(stderr, "%s%ls\n", lpszMessage,lpszMessage2);
445
   fprintf(stderr, "JatsFileUtil:Error:%s%ls\n", lpszMessage,lpszMessage2);
380
   fflush(stderr) ;
446
   fflush(stderr) ;
381
   ExitProcess(-1);
447
   ExitProcess(-1);
382
} 
448
} 
383
 
449