Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2072 dpurdie 1
/*============================================================================ 
2
** Copyright (C) 1998-2012 Vix Technology, All rights reserved
3
**============================================================================
4
**
5
**  Project/Product : 
6
**  Filename        : CopyFile.c
7
**  Author(s)       : DDP
8
**
9
**  Description     :
10
**                      The program mimics - but a lot faster a Jats Makefile fragment
11
**                      The program will:
12
**
13
**                      Operate in two modes
14
**                          CopyFile
15
**                          DeleteFile
16
**
17
**                      CopyFile (5 args)
18
**                          1) Display the ----- Text dest
19
**                          2) Error if the source is not found
20
**                          3) Create target path if not alraedy existing
21
**                          4) Copy the source to the target - overwritting the target
22
**                          5) Set File mode
23
**                          6) Test for file existence
24
**
25
**                      DeleteFile (2 args)
26
**                          1) Display the ----- Text dest
27
**                          2) Delete the target file
28
**
29
**                      Make paths use '/'
30
**
31
**
32
**  Information     :
33
**   Compiler       : ANSI C++
34
**   Target         : 
35
**
36
***==========================================================================*/
37
 
38
#include <stdio.h>
39
#include <windows.h>
40
 
41
#define INVALID_FILE_ATTIBUTES ((DWORD) -1)
42
 
43
VOID ErrorExit (LPTSTR lpszMessage, LPTSTR lpszMessage2);
44
void createPaths ( char *path );
45
 
46
/*----------------------------------------------------------------------------
47
** FUNCTION           : main
48
**
49
** DESCRIPTION        : Main entry points
50
**
51
**
52
** INPUTS             : Arguments are fixed
53
**                          Text    - Text to output
54
**                          dest    - Target Path
55
**                          file    - Source path
56
**                          path    - Target dir [Not used]
57
**                          fmode   - File mode
58
**
59
** RETURNS            : 0 - All is good
60
**
61
----------------------------------------------------------------------------*/
62
main( int argc, char *argv[] )
63
{
64
    DWORD rv;
65
    char  verbose = 0;
66
    char  copyMode = 0;
67
 
68
    /*
69
    **  Allow first argument to be a '-v'
70
    */
71
    if ( argc > 1 && argv[1][0] == '-' && argv[1][1] == 'v')
72
    {
73
        int ii;
74
        for ( ii = 0; ii < argc ; ii++ )
75
        {
76
            if ( ii != 1 )
77
                fprintf(stderr, " -Arg%d:%s:\n", ii, argv[ii] );
78
        }
79
        fflush(stderr) ;
80
 
81
        verbose = 1;
82
        argc--;
83
        argv++;
84
    }
85
 
86
    /*
87
    **  Determine mode of operation
88
    **      Copy or Delete
89
    */
90
    if ( argc == 6 ) {
91
        copyMode = 1;
92
    } else if ( argc == 3) {
93
        copyMode = 0;
94
    } else {
95
        ErrorExit("Incorrect argument count","");
96
    }
97
 
98
    /*
99
    **  Display user text
100
    */
101
    fprintf(stderr, "---- %s %s\n", argv[1], argv[2]);
102
    fflush(stderr) ;
103
 
104
    /*
105
    **   Check that the source is a file
106
    */
107
    if ( copyMode )
108
    {
109
        rv = GetFileAttributes( argv[3] );
110
        if ( rv == INVALID_FILE_ATTIBUTES )
111
        {
112
    // Need to be a better message
113
            ErrorExit("Error: Source File not found: ", argv[3]);
114
        }
115
    }
116
 
117
    /*
118
    **  Remove the ReadOnly attribute on the dest file
119
    */
120
    rv = GetFileAttributes( argv[2] );
121
    if ( rv != INVALID_FILE_ATTIBUTES )
122
    {
123
        if ( verbose )
124
            fprintf(stderr, "FileExists with attr: %ld\n", rv);
125
        if ( rv & FILE_ATTRIBUTE_READONLY )
126
        {
127
            rv &= ~FILE_ATTRIBUTE_READONLY;
128
            rv = SetFileAttributes( argv[2], rv );
129
            if ( rv == 0 )
130
            {
131
                ErrorExit("Error: Attempt to allow write access: ", argv[2]);
132
            }
133
        }
134
 
135
        if (! DeleteFile( argv[2] ) )
136
        {
137
                ErrorExit("Error: Deleting file: ", argv[2]);
138
        }
139
    }
140
 
141
    if ( copyMode )
142
    {
143
        /*
144
        **  Create directories
145
        **  Use the path to the target - not the provided directory
146
        **  as the createPaths function will not create the last element
147
        */
148
        createPaths( argv[2] );
149
 
150
        /*
151
        **   Copy the file
152
        */
153
        if ( ! CopyFile( argv[3], argv[2], FALSE ) )
154
        {
155
            rv = GetLastError();
156
            fprintf(stderr, "CopyFileLast Error: %ld\n", rv);
157
            ErrorExit("Error: Copy Error: ", argv[3]);
158
        }
159
 
160
        /*
161
        **  Test for files existence
162
        */
163
        rv = GetFileAttributes( argv[2] );
164
        if ( rv == INVALID_FILE_ATTIBUTES )
165
        {
166
    // Need to be a better message
167
            ErrorExit("Error: File not found after copy: ", argv[2]);
168
        }
169
 
170
        /*
171
        **  Set the files attributes
172
        **      Assume read-only
173
        */
174
        rv |= FILE_ATTRIBUTE_READONLY;
175
        rv &= ~FILE_ATTRIBUTE_NORMAL;
176
        rv = SetFileAttributes( argv[2], rv );
177
        if ( rv == 0 )
178
        {
179
            ErrorExit("Error: Setting ReadOnly: ", argv[2]);
180
        }
181
    }
182
 
183
    return 0;
184
}
185
 
186
/*----------------------------------------------------------------------------
187
** FUNCTION           : createPaths
188
**
189
** DESCRIPTION        : Create the path to the target
190
**
191
**
192
** INPUTS             : path
193
**
194
** RETURNS            : Will not return in error
195
**
196
----------------------------------------------------------------------------*/
197
 
198
void createPaths ( char *path )
199
{
200
    DWORD rv;
201
    char *ptr = path;
202
    while ( *ptr )
203
    {
204
        if ( *ptr == '\\' || *ptr == '/' )
205
        {
206
            *ptr = 0;
207
//printf( "Create: %s\n", path );
208
            if ( ! CreateDirectory ( path, NULL ))
209
            {
210
                rv = GetLastError();
211
                if ( rv != ERROR_ALREADY_EXISTS )
212
                    ErrorExit("Error: Cound not create directories:", path);
213
            }
214
            *ptr = '\\';
215
        }
216
        ptr++;
217
    }
218
}
219
 
220
/*----------------------------------------------------------------------------
221
** FUNCTION           : ErrorExit
222
**
223
** DESCRIPTION        : Error processing
224
**                      Report an error and terminate process
225
**
226
**
227
** INPUTS             : lpszMessage     - Message to display
228
**
229
** RETURNS            : Does't return
230
**                      Will exit with bad code
231
**
232
----------------------------------------------------------------------------*/
233
 
234
VOID ErrorExit (LPTSTR lpszMessage, LPTSTR lpszMessage2)
235
{ 
236
   fprintf(stderr, "%s%s\n", lpszMessage,lpszMessage2);
237
   fflush(stderr) ;
238
   ExitProcess(-1);
239
} 
240