Subversion Repositories DevTools

Rev

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

Rev 2764 Rev 5882
Line 27... Line 27...
27
**                          (R) Remove Files and Empty Directories
27
**                          (R) Remove Files and Empty Directories
28
**                          (P) Print Arguments
28
**                          (P) Print Arguments
29
**
29
**
30
**                      Example Usage
30
**                      Example Usage
31
**
31
**
32
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
32
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w+x+l
33
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
33
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
34
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
34
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
35
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
35
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
36
**                          JatsFileUtil T9 'DeleteTree'  interface
36
**                          JatsFileUtil T9 'DeleteTree'  interface
37
**                          JatsFileUtil R9 'RmItem'       build
37
**                          JatsFileUtil R9 'RmItem'       build
Line 60... Line 60...
60
void DeleteDir( int argc, char* argv[] );
60
void DeleteDir( int argc, char* argv[] );
61
char * makePath( char *base, char *path);
61
char * makePath( char *base, char *path);
62
void DeleteOneDirectoryTree( char* baseDir );
62
void DeleteOneDirectoryTree( char* baseDir );
63
void copyOneFile( int argc, char* argv[] );
63
void copyOneFile( int argc, char* argv[] );
64
int CopyFile ( char *src, char *dst, mode_t st_mode );
64
int CopyFile ( char *src, char *dst, mode_t st_mode );
-
 
65
int CopyLink ( char *src, char *dst, int lsize );
65
void RmItem( int argc, char* argv[] );
66
void RmItem( int argc, char* argv[] );
66
int wildcmp(char *string, char *wild );
67
int wildcmp(char *string, char *wild );
67
int DeleteOneFile (char * dst );
68
int DeleteOneFile (char * dst );
68
void DeleteOneDirectory (char *path );
69
void DeleteOneDirectory (char *path );
69
void stdCheck( char *name, int argBad, char *txt );
70
void stdCheck( char *name, int argBad, char *txt );
Line 80... Line 81...
80
         "      1 - Operation Specifier\n"
81
         "      1 - Operation Specifier\n"
81
         "      2 - Debug Mode. 0..9\n"
82
         "      2 - Debug Mode. 0..9\n"
82
         "  Where 'Text' is a, possibly empty, display string\n"
83
         "  Where 'Text' is a, possibly empty, display string\n"
83
         "\n"
84
         "\n"
84
         "  By Example:\n"
85
         "  By Example:\n"
85
         "      c9 copyFile     dstPath srcPath chmod\n"
86
         "      c9 copyFile     dstPath srcPath modes(wxl)\n"
86
         "      d9 unCopyFile   dstPath\n"
87
         "      d9 unCopyFile   dstPath\n"
87
         "      r9 RmFile       file+\n"
88
         "      r9 RmFile       file+\n"
88
         "      D9 DeleteFiles  dstDir file+ - supports (?*)\n"
89
         "      D9 DeleteFiles  dstDir file+ - supports (?*)\n"
89
         "      T9 DeleteTree   dstDir+\n"
90
         "      T9 DeleteTree   dstDir+\n"
90
         "      R9 RmItem       (dir|file)+\n"
91
         "      R9 RmItem       (dir|file)+\n"
Line 335... Line 336...
335
** INPUTS             : argc            - Argc count
336
** INPUTS             : argc            - Argc count
336
**                      argv            - Argument list
337
**                      argv            - Argument list
337
**                          argv[2]     - Display text Prefix
338
**                          argv[2]     - Display text Prefix
338
**                          argv[3]     - Target path
339
**                          argv[3]     - Target path
339
**                          argv[4]     - Source Path
340
**                          argv[4]     - Source Path
340
**                          argv[5]     - File attributes
341
**                          argv[5]     - File attributes / Copy mode
341
**
342
**
342
** RETURNS            :
343
** RETURNS            :
343
**
344
**
344
----------------------------------------------------------------------------*/
345
----------------------------------------------------------------------------*/
345
 
346
 
346
void copyOneFile( int argc, char* argv[] )
347
void copyOneFile( int argc, char* argv[] )
347
{
348
{
348
	int    rv;
349
    int    rv;
349
    char * src;
350
    char * src;
350
    char * dst;
351
    char * dst;
351
    struct stat fstat;
352
    struct stat fstat;
-
 
353
    struct stat lfstat;
-
 
354
    int    symlink = 0;
352
    
355
    
353
    dst = argv[3];
356
    dst = argv[3];
354
    src = argv[4];
357
    src = argv[4];
355
 
358
 
356
    /*
359
    /*
Line 379... Line 382...
379
    **  as the createPaths function will not create the last element
382
    **  as the createPaths function will not create the last element
380
    */
383
    */
381
    createPaths( dst );
384
    createPaths( dst );
382
 
385
 
383
    /*
386
    /*
-
 
387
    ** Sometimes symlinks are handled differently
-
 
388
    */
-
 
389
    if (strstr( argv[5], "+l" ))
-
 
390
    {
-
 
391
        rv = lstat(src, &lfstat); 
-
 
392
        if ( rv == 0 && S_ISLNK(lfstat.st_mode))
-
 
393
        {
-
 
394
            if ( verbose > 2)
-
 
395
                fprintf(stderr, "Copy Symlink: %s\n", src);
-
 
396
            symlink = 1;
-
 
397
            if ( ! CopyLink( src, dst, lfstat.st_size ) )
-
 
398
            {
-
 
399
                ErrorExit("Copy Symlink Error: ", argv[4]);
-
 
400
            }
-
 
401
        }
-
 
402
    }
-
 
403
 
-
 
404
    /*
384
    **   Copy the file
405
    **   Copy the file
385
    */
406
    */
386
    if ( ! CopyFile( src, dst, fstat.st_mode ) )
407
    if ( ! CopyFile( src, dst, fstat.st_mode ) )
387
    {
408
    {
388
        ErrorExit("Copy Error: ", argv[4]);
409
        ErrorExit("Copy Error: ", argv[4]);
Line 400... Line 421...
400
/* Need to be a better message */
421
/* Need to be a better message */
401
        ErrorExit("File not found after copy: ", argv[3]);
422
        ErrorExit("File not found after copy: ", argv[3]);
402
    }
423
    }
403
 
424
 
404
    /*
425
    /*
405
    **  Set the files attributes
426
    **  Set the files attributes, unless copying a symlink
406
    **      Assume read-only
427
    **      Assume read-only
407
    */
428
    */
408
    if ( strstr( argv[5], "-w" ) )
429
    if (!symlink)
409
    {
430
    {
-
 
431
        if (strstr(argv[5], "-w")) 
-
 
432
        {
410
        if ( verbose > 1 )
433
            if ( verbose > 1 )
411
            fprintf(stderr, "Set target read-only: %s\n", dst);
434
                fprintf(stderr, "Set target read-only: %s\n", dst);
412
        fstat.st_mode &= ~(S_IWRITE | S_IWOTH | S_IWGRP );
435
            fstat.st_mode &= ~(S_IWRITE | S_IWOTH | S_IWGRP );
413
    }
436
        }
414
 
437
 
415
    if ( strstr( argv[5], "+w" ) )
438
        if ( strstr( argv[5], "+w" ) )
416
    {
439
        {
417
        if ( verbose > 1 )
440
            if ( verbose > 1 )
418
            fprintf(stderr, "Set target writable: %s\n", dst);
441
                fprintf(stderr, "Set target writable: %s\n", dst);
419
        fstat.st_mode |= (S_IWRITE | S_IWOTH | S_IWGRP );
442
            fstat.st_mode |= (S_IWRITE | S_IWOTH | S_IWGRP );
420
    }
443
        }
421
 
444
 
422
    if ( strstr( argv[5], "+x" ) )
445
        if ( strstr( argv[5], "+x" ) )
423
    {
446
        {
424
        if ( verbose > 1 )
447
            if ( verbose > 1 )
425
            fprintf(stderr, "Set target executable: %s\n", dst);
448
                fprintf(stderr, "Set target executable: %s\n", dst);
426
        fstat.st_mode |= ( S_IXUSR | S_IXOTH | S_IXGRP );
449
            fstat.st_mode |= ( S_IXUSR | S_IXOTH | S_IXGRP );
427
    }
450
        }
428
 
451
 
429
    if ( strstr( argv[5], "-x" ) )
452
        if ( strstr( argv[5], "-x" ) )
430
    {
453
        {
431
        if ( verbose > 1)
454
            if ( verbose > 1)
432
            fprintf(stderr, "Set target executable: %s\n", dst);
455
                fprintf(stderr, "Set target not executable: %s\n", dst);
433
        fstat.st_mode &= ~( S_IXUSR | S_IXOTH | S_IXGRP );
456
            fstat.st_mode &= ~( S_IXUSR | S_IXOTH | S_IXGRP );
434
    }
457
        }
435
        
458
            
436
    if ( verbose > 1 )
459
        if ( verbose > 1 )
437
        fprintf(stderr, "Set target perms: %s, 0%o\n", dst, fstat.st_mode);
460
            fprintf(stderr, "Set target perms: %s, 0%o\n", dst, fstat.st_mode);
438
    rv = chmod( dst, fstat.st_mode );
461
        rv = chmod( dst, fstat.st_mode );
439
    if ( rv != 0 )
462
        if ( rv != 0 )
440
    {
463
        {
441
        ErrorExit("Setting ReadOnly: ", argv[3]);
464
            ErrorExit("Setting ReadOnly: ", argv[3]);
-
 
465
        }
442
    }
466
    }
443
}
467
}
444
 
468
 
445
/*----------------------------------------------------------------------------
469
/*----------------------------------------------------------------------------
446
** FUNCTION           : CopyFile
470
** FUNCTION           : CopyFile
Line 505... Line 529...
505
    }
529
    }
506
    return 1;
530
    return 1;
507
}
531
}
508
 
532
 
509
/*----------------------------------------------------------------------------
533
/*----------------------------------------------------------------------------
-
 
534
** FUNCTION           : CopyLink
-
 
535
**
-
 
536
** DESCRIPTION        : Just copy a symlink
-
 
537
**
-
 
538
**
-
 
539
** INPUTS             : src - source path to a symlink - already exists
-
 
540
**                      dst - path. Dirs already exist
-
 
541
**                      lsize - size of the link text
-
 
542
**
-
 
543
** RETURNS            : false - Error
-
 
544
**
-
 
545
----------------------------------------------------------------------------*/
-
 
546
 
-
 
547
int CopyLink ( char *src, char *dst, int lsize )
-
 
548
{
-
 
549
    char *linkname = malloc(lsize + 1);
-
 
550
    ssize_t rlen = 0 ;
-
 
551
    int rv;
-
 
552
 
-
 
553
    if (linkname == NULL)
-
 
554
    {
-
 
555
        ErrorExit("Malloc error: %s", src);
-
 
556
    }
-
 
557
 
-
 
558
    rlen = readlink(src, linkname, lsize + 1);
-
 
559
    if (rlen < 0)
-
 
560
    {
-
 
561
        ErrorExit("Reading Link: %s", src);
-
 
562
    }
-
 
563
 
-
 
564
    rv = symlink( linkname, dst);
-
 
565
    if (rv)
-
 
566
    {
-
 
567
        unlink(dst) ;
-
 
568
        ErrorExit("Reading Link: %s", src);
-
 
569
    }
-
 
570
 
-
 
571
    return 1;
-
 
572
}
-
 
573
 
-
 
574
/*----------------------------------------------------------------------------
510
** FUNCTION           : DeleteDir
575
** FUNCTION           : DeleteDir
511
**
576
**
512
** DESCRIPTION        : Delete a list of files in a specified directory
577
** DESCRIPTION        : Delete a list of files in a specified directory
513
**                      Ensure files are writable
578
**                      Ensure files are writable
514
**                      Wilcarding is supported
579
**                      Wilcarding is supported
Line 525... Line 590...
525
**
590
**
526
----------------------------------------------------------------------------*/
591
----------------------------------------------------------------------------*/
527
 
592
 
528
void DeleteDir( int argc, char* argv[] )
593
void DeleteDir( int argc, char* argv[] )
529
{
594
{
530
	int rv;
595
    int rv;
531
    struct stat fstat;
596
    struct stat fstat;
532
    char* baseDir;
597
    char* baseDir;
533
    DIR *dir;
598
    DIR *dir;
534
    struct dirent *dirent;
599
    struct dirent *dirent;
535
    char *target;
600
    char *target;
Line 793... Line 858...
793
**
858
**
794
----------------------------------------------------------------------------*/
859
----------------------------------------------------------------------------*/
795
 
860
 
796
int DeleteOneFile (char * dst )
861
int DeleteOneFile (char * dst )
797
{
862
{
798
	int rv;
863
    int rv;
799
    struct stat fstat;
864
    struct stat fstat;
800
    int result = 0;
865
    int result = 0;
801
 
866
 
802
    if ( verbose > 1)
867
    if ( verbose > 1)
803
        fprintf(stderr, "Delete File: %s\n", dst);
868
        fprintf(stderr, "Delete File: %s\n", dst);