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
#include <stdlib.h>
48
#include <stdlib.h>
44
#include <string.h>
49
#include <string.h>
45
 
50
 
46
void ErrorExit (char * lpszMessage, char * lpszMessage2);
51
void ErrorExit (char * lpszMessage, char * lpszMessage2);
47
void createPaths ( char *path );
52
void createPaths ( char *path );
48
int CopyFile ( char *src, char *dst, mode_t mode );
53
int CopyFile ( char *src, char *dst, mode_t st_mode );
-
 
54
void deleteFileList( int argc, char* argv[] );
49
 
55
 
50
/*
56
/*
51
**  Global
57
**  Global
52
*/
58
*/
53
char  verbose = 1;                          /* Debugging aid */
59
char  verbose = 1;                          /* Debugging aid */
Line 80... Line 86...
80
    mode_t src_mode;
86
    mode_t src_mode;
81
 
87
 
82
    /*
88
    /*
83
    **  Examine the first argument
89
    **  Examine the first argument
84
    **  Must be a character string of
90
    **  Must be a character string of
85
    **      [0] - Mode : c or d
91
    **      [0] - Mode : c or d or r
86
    **      [1] - Verbose : 0 .. 9
92
    **      [1] - Verbose : 0 .. 9
87
    */
93
    */
88
    if ( argc > 1 )
94
    if ( argc > 1 )
89
    {
95
    {
90
        if ( argv[1][0] == 'c' ) {
96
        if ( argv[1][0] == 'c' ) {
91
            copyMode = 1;
97
            copyMode = 1;
92
        } else if ( argv[1][0] == 'd' ) {
98
        } else if ( argv[1][0] == 'd' ) {
93
            copyMode = 2;
99
            copyMode = 2;
-
 
100
        } else if ( argv[1][0] == 'r' ) {
-
 
101
            copyMode = 3;
94
        } else {
102
        } else {
95
            ErrorExit("CopyFile: Unknown mode",argv[1]);
103
            ErrorExit("Unknown mode: ",argv[1]);
96
        }
104
        }
97
 
105
 
98
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
106
        if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
99
            verbose = argv[1][1] - '0';
107
            verbose = argv[1][1] - '0';
100
        }
108
        }
101
    }
109
    }
102
 
110
 
103
    /*
111
    /*
104
    **  If Verbose, then display arguments
112
    **  If Verbose, then display arguments
105
    */
113
    */
106
    if ( verbose )
114
    if ( verbose > 1 )
107
    {
115
    {
108
        int ii;
116
        int ii;
109
        for ( ii = 0; ii < argc ; ii++ )
117
        for ( ii = 0; ii < argc ; ii++ )
110
        {
118
        {
111
            fprintf(stderr, "Arg%d: %s:\n", ii, argv[ii] );
119
            fprintf(stderr, "Arg%d: %s:\n", ii, argv[ii] );
Line 114... Line 122...
114
        fprintf(stderr, "Verbose: %d:\n", verbose );
122
        fprintf(stderr, "Verbose: %d:\n", verbose );
115
        fflush(stderr) ;
123
        fflush(stderr) ;
116
    }
124
    }
117
 
125
 
118
    /*
126
    /*
-
 
127
    **  Determine required operation
-
 
128
    */
-
 
129
    if ( copyMode == 3 )
-
 
130
    {
-
 
131
        deleteFileList(argc - 2, argv + 2 );
-
 
132
        return 0;
-
 
133
    }
-
 
134
 
-
 
135
    /*
119
    **  Determine mode of operation
136
    **  Determine mode of operation
120
    **      Copy or Delete
137
    **      Copy or Delete
121
    */
138
    */
122
    if ( copyMode == 1 && argc == 6 ) {
139
    if ( copyMode == 1 && argc == 6 ) {
123
        src = argv[4];
140
        src = argv[4];
Line 148... Line 165...
148
        rv = stat( src, &fstat );
165
        rv = stat( src, &fstat );
149
        if ( rv != 0 )
166
        if ( rv != 0 )
150
        {
167
        {
151
    /* Need to be a better message */
168
    /* Need to be a better message */
152
            fprintf(stderr, "Source: %s\n", src);
169
            fprintf(stderr, "Source: %s\n", src);
153
            ErrorExit("Error: Source File not found: ", argv[4]);
170
            ErrorExit("Source File not found: ", argv[4]);
154
        }
171
        }
155
        src_mode = fstat.st_mode;
172
        src_mode = fstat.st_mode;
156
    }
173
    }
157
 
174
 
158
    /*
175
    /*
Line 169... Line 186...
169
        {
186
        {
170
            fstat.st_mode |= S_IWRITE;
187
            fstat.st_mode |= S_IWRITE;
171
            rv = chmod( dst, fstat.st_mode );
188
            rv = chmod( dst, fstat.st_mode );
172
            if ( rv != 0 )
189
            if ( rv != 0 )
173
            {
190
            {
174
                ErrorExit("Error: Attempt to allow write access: ", argv[3]);
191
                ErrorExit("Attempt to allow write access: ", argv[3]);
175
            }
192
            }
176
        }
193
        }
177
 
194
 
178
        if ( unlink( dst ) )
195
        if ( unlink( dst ) )
179
        {
196
        {
180
                ErrorExit("Error: Deleting file: ", argv[3]);
197
                ErrorExit("Deleting file: ", argv[3]);
181
        }
198
        }
182
    }
199
    }
183
    
200
    
184
    if ( copyMode == 1 )
201
    if ( copyMode == 1 )
185
    {
202
    {
Line 193... Line 210...
193
        /*
210
        /*
194
        **   Copy the file
211
        **   Copy the file
195
        */
212
        */
196
        if ( ! CopyFile( src, dst, src_mode ) )
213
        if ( ! CopyFile( src, dst, src_mode ) )
197
        {
214
        {
198
            ErrorExit("Error: Copy Error: ", argv[4]);
215
            ErrorExit("Copy Error: ", argv[4]);
199
        }
216
        }
200
 
217
 
201
        /*
218
        /*
202
        **  Test for files existence
219
        **  Test for files existence
203
        */
220
        */
Line 205... Line 222...
205
            fprintf(stderr, "Test target was copied: %s\n", dst);
222
            fprintf(stderr, "Test target was copied: %s\n", dst);
206
        rv = stat( dst, &fstat );
223
        rv = stat( dst, &fstat );
207
        if ( rv != 0 )
224
        if ( rv != 0 )
208
        {
225
        {
209
    /* Need to be a better message */
226
    /* Need to be a better message */
210
            ErrorExit("Error: File not found after copy: ", argv[3]);
227
            ErrorExit("File not found after copy: ", argv[3]);
211
        }
228
        }
212
 
229
 
213
        /*
230
        /*
214
        **  Set the files attributes
231
        **  Set the files attributes
215
        **      Assume read-only
232
        **      Assume read-only
Line 245... Line 262...
245
        if ( verbose )
262
        if ( verbose )
246
            fprintf(stderr, "Set target perms: %s, 0%o\n", dst, fstat.st_mode);
263
            fprintf(stderr, "Set target perms: %s, 0%o\n", dst, fstat.st_mode);
247
        rv = chmod( dst, fstat.st_mode );
264
        rv = chmod( dst, fstat.st_mode );
248
        if ( rv != 0 )
265
        if ( rv != 0 )
249
        {
266
        {
250
            ErrorExit("Error: Setting ReadOnly: ", argv[3]);
267
            ErrorExit("Setting ReadOnly: ", argv[3]);
251
            }
268
            }
252
        }
269
        }
253
 
270
 
254
    return 0;
271
    return 0;
255
}
272
}
Line 287... Line 304...
287
                    fflush(stderr) ;
304
                    fflush(stderr) ;
288
                }
305
                }
289
                rv = mkdir( path, 0777 );
306
                rv = mkdir( path, 0777 );
290
                if ( rv )
307
                if ( rv )
291
                {
308
                {
292
                    ErrorExit("Error: Cound not create directories:", path);
309
                    ErrorExit("Could not create directories:", path);
293
                }
310
                }
294
            }
311
            }
295
            *ptr = '/';
312
            *ptr = '/';
296
        }
313
        }
297
        ptr++;
314
        ptr++;
Line 329... Line 346...
329
        fflush(stderr) ;
346
        fflush(stderr) ;
330
    }
347
    }
331
 
348
 
332
    in = open( src, O_RDONLY ) ;
349
    in = open( src, O_RDONLY ) ;
333
    if ( in < 0 )
350
    if ( in < 0 )
334
        ErrorExit("Error: Cound not open source:", src);
351
        ErrorExit("Could not open source:", src);
335
 
352
 
336
    out = open( dst, O_WRONLY | O_CREAT, st_mode | S_IWRITE );
353
    out = open( dst, O_WRONLY | O_CREAT, st_mode | S_IWRITE );
337
    if ( out < 0 )
354
    if ( out < 0 )
338
        ErrorExit("Error: Cound not open dst:", dst);
355
        ErrorExit("Could not open dst:", dst);
339
 
356
 
340
    while( (rlen = read( in, buffer, COPYSIZE )) > 0 )
357
    while( (rlen = read( in, buffer, COPYSIZE )) > 0 )
341
    {
358
    {
342
        wlen = write( out, buffer, rlen ) ;
359
        wlen = write( out, buffer, rlen ) ;
343
        if ( wlen != rlen )
360
        if ( wlen != rlen )
Line 360... Line 377...
360
        return 0;
377
        return 0;
361
    }
378
    }
362
    return 1;
379
    return 1;
363
}
380
}
364
 
381
 
-
 
382
/*----------------------------------------------------------------------------
-
 
383
** FUNCTION           : deleteFileList
-
 
384
**
-
 
385
** DESCRIPTION        : Delete a list of files
-
 
386
**                      Ensure files are writable
-
 
387
**
-
 
388
**
-
 
389
** INPUTS             : argc    - count of args
-
 
390
**                      argv    - list of files to delete
-
 
391
**
-
 
392
** RETURNS            : Wil not return on error
-
 
393
**
-
 
394
----------------------------------------------------------------------------*/
-
 
395
 
-
 
396
void deleteFileList( int argc, char* argv[] )
-
 
397
{
-
 
398
	int rv;
-
 
399
    struct stat fstat;
-
 
400
 
-
 
401
    for ( ; argc ; argc--, argv++ )
-
 
402
    {
-
 
403
        /*
-
 
404
        **  Remove the ReadOnly attribute on the dest file
-
 
405
        */
-
 
406
        if ( verbose > 1 )
-
 
407
            fprintf(stderr, "Remove target file: %s\n", *argv);
-
 
408
 
-
 
409
 
-
 
410
        rv = stat( *argv, &fstat );
-
 
411
        if ( rv == 0 )
-
 
412
        {
-
 
413
            if ( verbose )
-
 
414
                fprintf(stderr, "Delete file: %s : Attr: 0%o\n", *argv, fstat.st_mode);
-
 
415
            if ( !(fstat.st_mode & S_IWRITE) )
-
 
416
            {
-
 
417
                fstat.st_mode |= S_IWRITE;
-
 
418
                rv = chmod( *argv, fstat.st_mode );
-
 
419
                if ( rv != 0 )
-
 
420
                {
-
 
421
                    fprintf(stderr, "Warning: Attempt to allow write access: %s\n", *argv);
-
 
422
                }
-
 
423
            }
-
 
424
 
-
 
425
            if ( unlink( *argv ) )
-
 
426
            {
-
 
427
                fprintf(stderr, "Warning: Did not remove file: %s\n", *argv);
-
 
428
            }
-
 
429
        }
-
 
430
    }
-
 
431
}
365
 
432
 
366
 
433
 
367
/*----------------------------------------------------------------------------
434
/*----------------------------------------------------------------------------
368
** FUNCTION           : ErrorExit
435
** FUNCTION           : ErrorExit
369
**
436
**
Line 378... Line 445...
378
**
445
**
379
----------------------------------------------------------------------------*/
446
----------------------------------------------------------------------------*/
380
 
447
 
381
void ErrorExit (char * lpszMessage, char * lpszMessage2)
448
void ErrorExit (char * lpszMessage, char * lpszMessage2)
382
{ 
449
{ 
383
   fprintf(stderr, "%s%s\n", lpszMessage,lpszMessage2);
450
   fprintf(stderr, "JatsFileUtil:Error: %s%s\n", lpszMessage,lpszMessage2);
384
   fflush(stderr) ;
451
   fflush(stderr) ;
385
   exit(-1);
452
   exit(-1);
386
} 
453
} 
387
 
454