Subversion Repositories DevTools

Rev

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

Rev 2311 Rev 2313
Line 21... Line 21...
21
**                      The program will perform the following operations:
21
**                      The program will perform the following operations:
22
**                          (c) CopyFile
22
**                          (c) CopyFile
23
**                          (d) DeleteFile
23
**                          (d) DeleteFile
24
**                          (r) Remove Files (wildcard)
24
**                          (r) Remove Files (wildcard)
25
**                          (D) DeleteDir after deleting specified files (wildcard)
25
**                          (D) DeleteDir after deleting specified files (wildcard)
26
**                          (T) Delete Directory Tree
26
**                          (T) Delete Directory Trees
-
 
27
**                          (R) Delete Empty Directories
27
**
28
**
28
**                      Example Usage
29
**                      Example Usage
29
**
30
**
30
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
31
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
31
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
32
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
32
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
33
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
33
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
34
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
34
**                          JatsFileUtil T9 'DeleteTree'  interface
35
**                          JatsFileUtil T9 'DeleteTree'  interface
-
 
36
**                          JatsFileUtil R9 'RmDir'       build
35
**
37
**
36
**                      First two arguments are common to all
38
**                      First two arguments are common to all
37
**                          argv[1]     - Mode specifier, Debug specifier
39
**                          argv[1]     - Mode specifier, Debug specifier
38
**                          argv[2]     - Display Text
40
**                          argv[2]     - Display Text
39
**
41
**
Line 59... Line 61...
59
char * makePath( char *base, char *path);
61
char * makePath( char *base, char *path);
60
int DeleteOneFile (char * dst );
62
int DeleteOneFile (char * dst );
61
void DeleteOneDirectoryTree( char* baseDir );
63
void DeleteOneDirectoryTree( char* baseDir );
62
void copyOneFile( int argc, char* argv[] );
64
void copyOneFile( int argc, char* argv[] );
63
int CopyFile ( char *src, char *dst, mode_t st_mode );
65
int CopyFile ( char *src, char *dst, mode_t st_mode );
-
 
66
void RmDir( int argc, char* argv[] );
64
int wildcmp(char *string, char *wild );
67
int wildcmp(char *string, char *wild );
65
 
68
 
66
/*
69
/*
67
**  Global
70
**  Global
68
*/
71
*/
Line 82... Line 85...
82
----------------------------------------------------------------------------*/
85
----------------------------------------------------------------------------*/
83
 
86
 
84
int main(int argc, char* argv[])
87
int main(int argc, char* argv[])
85
{
88
{
86
    /*
89
    /*
-
 
90
    **  User must provide some thing
-
 
91
    */
-
 
92
    if ( argc < 2 )
-
 
93
    {
-
 
94
        ErrorExit("Insufficient arguments","");
-
 
95
    }
-
 
96
 
-
 
97
    /*
87
    **  Examine the first argument
98
    **  Examine the first argument
88
    **  Must be a character string of
99
    **  Must be a character string of
89
    **      [0] - Mode : One character
100
    **      [0] - Mode : One character
90
    **      [1] - Verbose : 0 .. 9
101
    **      [1] - Verbose : 0 .. 9
91
    */
102
    */
Line 186... Line 197...
186
        **      argv[3]+  Base directory
197
        **      argv[3]+  Base directory
187
        */
198
        */
188
        case 'T':
199
        case 'T':
189
            DeleteDirTree(argc - 2, argv + 2 );
200
            DeleteDirTree(argc - 2, argv + 2 );
190
            break;
201
            break;
-
 
202
 
-
 
203
        /*
-
 
204
        **  Delete Empty Directories
-
 
205
        **      argv[2] - Text
-
 
206
        **      argv[3]+  Base directory
-
 
207
        */
-
 
208
        case 'R':
-
 
209
            RmDir(argc - 2, argv + 2 );
-
 
210
            break;
-
 
211
            
191
            
212
            
192
        default :
213
        default :
193
            ErrorExit("Unknown mode: ",argv[1]);
214
            ErrorExit("Unknown mode: ",argv[1]);
194
            break;
215
            break;
195
    }
216
    }
Line 525... Line 546...
525
    **  Read next directory entry
546
    **  Read next directory entry
526
    */
547
    */
527
    while ( (dirent = readdir(dir)) != NULL )
548
    while ( (dirent = readdir(dir)) != NULL )
528
    {
549
    {
529
        int ii;
550
        int ii;
530
        if ( verbose > 2 )
-
 
531
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
-
 
532
        
-
 
533
        if ( strcmp( ".", dirent->d_name ) == 0 )
551
        if ( strcmp( ".", dirent->d_name ) == 0 )
534
            continue;
552
            continue;
535
 
553
 
536
        if ( strcmp( "..", dirent->d_name ) == 0 )
554
        if ( strcmp( "..", dirent->d_name ) == 0 )
537
            continue;
555
            continue;
538
 
556
 
-
 
557
        if ( verbose > 2 )
-
 
558
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
-
 
559
        
539
        /*
560
        /*
540
        **  Compare against each item in the user list
561
        **  Compare against each item in the user list
541
        */
562
        */
542
        for ( ii = 0; ii < argc ; ii++)
563
        for ( ii = 0; ii < argc ; ii++)
543
        {
564
        {
Line 614... Line 635...
614
 
635
 
615
/*----------------------------------------------------------------------------
636
/*----------------------------------------------------------------------------
616
** FUNCTION           : DeleteOneDirectoryTree
637
** FUNCTION           : DeleteOneDirectoryTree
617
**
638
**
618
** DESCRIPTION        : Delete an entire directory tree(s)
639
** DESCRIPTION        : Delete an entire directory tree(s)
-
 
640
**                      Force it writable and searchable before deletion
-
 
641
**                      Don't follow symbolic links - just delete them
619
**
642
**
620
** INPUTS             : path    - Dir to delete
643
** INPUTS             : path    - Dir to delete
621
**
644
**
622
** RETURNS            : Will not return on error
645
** RETURNS            : Will not return on error
623
**
646
**
Line 639... Line 662...
639
        fprintf(stderr, "DeleteOneDirectoryTree will not delete '.' or '..'\n");
662
        fprintf(stderr, "DeleteOneDirectoryTree will not delete '.' or '..'\n");
640
        return;
663
        return;
641
    }
664
    }
642
 
665
 
643
    /*
666
    /*
-
 
667
    **  Ensure that the directory exists
-
 
668
    **  Force writable and searchable
-
 
669
    */
-
 
670
    rv = stat( baseDir, &fstat );
-
 
671
    if ( rv == 0 )
-
 
672
    {
-
 
673
        if ( fstat.st_mode & S_IFDIR )
-
 
674
        {
-
 
675
            mode_t newMode =  fstat.st_mode | S_IRUSR | S_IWUSR  | S_IXUSR;
-
 
676
            if ( fstat.st_mode != newMode )
-
 
677
            {
-
 
678
                if ( verbose > 1 )
-
 
679
                    fprintf(stderr, "Force u+rwx: %s\n", baseDir);
-
 
680
 
-
 
681
                rv = chmod( baseDir, newMode );
-
 
682
                if ( rv != 0 )
-
 
683
                {
-
 
684
                    fprintf(stderr, "Warning: Attempt to allow u+rwx access: %s\n", baseDir);
-
 
685
                }
-
 
686
            }
-
 
687
        }
-
 
688
    }
-
 
689
 
-
 
690
 
-
 
691
    /*
644
    **  Process all entries
692
    **  Process all entries
645
    */
693
    */
646
    dir = opendir( baseDir );
694
    dir = opendir( baseDir );
647
    if ( dir == NULL )
695
    if ( dir == NULL )
648
    {
696
    {
Line 658... Line 706...
658
    /*
706
    /*
659
    **  Read next directory entry
707
    **  Read next directory entry
660
    */
708
    */
661
    while ( (dirent = readdir(dir)) != NULL )
709
    while ( (dirent = readdir(dir)) != NULL )
662
    {
710
    {
663
        if ( verbose > 2 )
-
 
664
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
-
 
665
        
-
 
666
        if ( strcmp( ".", dirent->d_name ) == 0 )
711
        if ( strcmp( ".", dirent->d_name ) == 0 )
667
            continue;
712
            continue;
668
 
713
 
669
        if ( strcmp( "..", dirent->d_name ) == 0 )
714
        if ( strcmp( "..", dirent->d_name ) == 0 )
670
            continue;
715
            continue;
671
 
716
 
-
 
717
        if ( verbose > 2 )
-
 
718
            fprintf(stderr, "Directory Entry:%s,%s\n", baseDir, dirent->d_name );
-
 
719
        
672
        target = makePath( baseDir, dirent->d_name);
720
        target = makePath( baseDir, dirent->d_name);
673
        rv = stat( target, &fstat );
721
        rv = lstat( target, &fstat );
674
        if ( rv == 0 )
722
        if ( rv == 0 )
675
        {
723
        {
676
            if ( fstat.st_mode & S_IFDIR )
724
            if ( fstat.st_mode & S_IFDIR )
677
            {
725
            {
678
                DeleteOneDirectoryTree( target );
726
                DeleteOneDirectoryTree( target );
Line 702... Line 750...
702
/*----------------------------------------------------------------------------
750
/*----------------------------------------------------------------------------
703
** FUNCTION           : DeleteOneFile
751
** FUNCTION           : DeleteOneFile
704
**
752
**
705
** DESCRIPTION        : Delete a file
753
** DESCRIPTION        : Delete a file
706
**                      Force it writable before deletion
754
**                      Force it writable before deletion
-
 
755
**                      Don't follow symbolic links - just delete them
707
**
756
**
708
** INPUTS             : path        - path to the file
757
** INPUTS             : path        - path to the file
709
**
758
**
710
** RETURNS            : 0           - OK (file deleted or not present)
759
** RETURNS            : 0           - OK (file deleted or not present)
711
**
760
**
Line 718... Line 767...
718
    int result = 0;
767
    int result = 0;
719
 
768
 
720
    if ( verbose > 1)
769
    if ( verbose > 1)
721
        fprintf(stderr, "Delete File: %s\n", dst);
770
        fprintf(stderr, "Delete File: %s\n", dst);
722
 
771
 
723
    rv = stat( dst, &fstat );
772
    rv = lstat( dst, &fstat );
724
    if ( rv == 0 )
773
    if ( rv == 0 )
725
    {
774
    {
726
        if ( verbose )
775
        if ( verbose )
727
            fprintf(stderr, "Delete file: %s : Attr: 0%o\n", dst, fstat.st_mode);
776
            fprintf(stderr, "Delete file: %s : Attr: 0%o\n", dst, fstat.st_mode);
728
        if ( !(fstat.st_mode & S_IWRITE) )
777
        if ( !(fstat.st_mode & S_IWRITE) )
Line 743... Line 792...
743
    }
792
    }
744
    return result;
793
    return result;
745
}
794
}
746
 
795
 
747
/*----------------------------------------------------------------------------
796
/*----------------------------------------------------------------------------
-
 
797
** FUNCTION           : RmDir
-
 
798
**
-
 
799
** DESCRIPTION        : Remove Empty directories
-
 
800
**
-
 
801
** INPUTS             : argc    - count of args
-
 
802
**                      argv    - list of files to delete
-
 
803
**                                [0]:  Text to display
-
 
804
**                                [1]+  Base directory
-
 
805
**
-
 
806
** RETURNS            : Will not return on error
-
 
807
**
-
 
808
----------------------------------------------------------------------------*/
-
 
809
 
-
 
810
void RmDir( int argc, char* argv[] )
-
 
811
{
-
 
812
    int ii;
-
 
813
 
-
 
814
    if ( argc < 2 )
-
 
815
        ErrorExit("Insufficient arguments for RmDir","");
-
 
816
        
-
 
817
    /*
-
 
818
    **  Display the user message
-
 
819
    **      Suppress display if the message is empty
-
 
820
    */
-
 
821
    if ( argv[0][0] )
-
 
822
    {
-
 
823
        fprintf(stderr, "%s\n", argv[0]);
-
 
824
        fflush(stderr) ;
-
 
825
    }
-
 
826
 
-
 
827
    for ( ii = 1; ii < argc ; ii++)
-
 
828
    {
-
 
829
        if ( verbose > 2)
-
 
830
            fprintf(stderr, "RmDir: %s\n", argv[ii]);
-
 
831
        
-
 
832
        if ( rmdir (argv[ii] ) )
-
 
833
        {
-
 
834
            if ( verbose )
-
 
835
                fprintf(stderr, "Directory not deleted: %s\n", argv[ii]);
-
 
836
        }
-
 
837
    }
-
 
838
}
-
 
839
 
-
 
840
/*----------------------------------------------------------------------------
748
** FUNCTION           : makePath
841
** FUNCTION           : makePath
749
**
842
**
750
** DESCRIPTION        : Create a path from two elements
843
** DESCRIPTION        : Create a path from two elements
751
**                      Allocate memory
844
**                      Allocate memory
752
**
845
**