[Bio] / FigKernelPackages / Tracer.pm Repository:
ViewVC logotype

Diff of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.45, Mon May 8 20:37:02 2006 UTC revision 1.49, Wed Jun 14 00:54:59 2006 UTC
# Line 24  Line 24 
24      use strict;      use strict;
25      use Carp qw(longmess croak);      use Carp qw(longmess croak);
26      use CGI;      use CGI;
27        use Cwd;
28      use FIG_Config;      use FIG_Config;
29      use PageBuilder;      use PageBuilder;
30      use Digest::MD5;      use Digest::MD5;
31      use File::Basename;      use File::Basename;
32      use File::Path;      use File::Path;
33        use File::stat;
34    
35  =head1 Tracing and Debugging Helpers  =head1 Tracing and Debugging Helpers
36    
# Line 2008  Line 2010 
2010      }      }
2011  }  }
2012    
2013    =head3 SetPermissions
2014    
2015    C<< Tracer::SetPermissions($dirName, $group, $mask, %otherMasks); >>
2016    
2017    Set the permissions for a directory and all the files and folders inside it.
2018    In addition, the group ownership will be changed to the specified value.
2019    
2020    This method is more vulnerable than most to permission and compatability
2021    problems, so it does internal error recovery.
2022    
2023    =over 4
2024    
2025    =item dirName
2026    
2027    Name of the directory to process.
2028    
2029    =item group
2030    
2031    Name of the group to be assigned.
2032    
2033    =item mask
2034    
2035    Permission mask. Bits that are C<1> in this mask will be ORed into the
2036    permission bits of any file or directory that does not already have them
2037    set to 1.
2038    
2039    =item otherMasks
2040    
2041    Map of search patterns to permission masks. If a directory name matches
2042    one of the patterns, that directory and all its members and subdirectories
2043    will be assigned the new pattern. For example, the following would
2044    assign 01664 to most files, but would use 01777 for directories named C<tmp>.
2045    
2046        Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp$' => 01777);
2047    
2048    The list is ordered, so the following would use 0777 for C<tmp1> and
2049    0666 for C<tmp>, C<tmp2>, or C<tmp3>.
2050    
2051        Tracer::SetPermissions($dirName, 'fig', 01664, '^tmp1' => 0777,
2052                                                       '^tmp' => 0666);
2053    
2054    Note that the pattern matches are all case-insensitive, and only directory
2055    names are matched, not file names.
2056    
2057    =back
2058    
2059    =cut
2060    
2061    sub SetPermissions {
2062        # Get the parameters.
2063        my ($dirName, $group, $mask, @otherMasks) = @_;
2064        # Set up for error recovery.
2065        eval {
2066            # Switch to the specified directory.
2067            ChDir($dirName);
2068            # Get the group ID.
2069            my $gid = getgrnam($group);
2070            Trace("Fixing permissions for directory $dirName using group $group($gid).") if T(2);
2071            my $fixCount = 0;
2072            my $lookCount = 0;
2073            # @dirs will be a stack of directories to be processed.
2074            my @dirs = (getcwd());
2075            while (scalar(@dirs) > 0) {
2076                # Get the current directory.
2077                my $dir = pop @dirs;
2078                # Check for a match to one of the specified directory names. To do
2079                # that, we need to pull the individual part of the name off of the
2080                # whole path.
2081                my $simpleName = $dir;
2082                if ($dir =~ m!/(.+)$!) {
2083                    $simpleName = $1;
2084                }
2085                # Search for a match.
2086                my $match = 0;
2087                my $i;
2088                for ($i = 0; $i < $#otherMasks && ! $match; $i += 2) {
2089                    my $pattern = $otherMasks[$i];
2090                    if ($simpleName =~ /$pattern/i) {
2091                        $match = 1;
2092                    }
2093                }
2094                # Check for a match.
2095                if ($match && $otherMasks[$i+1] != $mask) {
2096                    # This directory matches one of the incoming patterns, and it's
2097                    # a different mask, so we process it recursively with that mask.
2098                    SetPermissions($dir, $group, $otherMasks[$i+1], @otherMasks);
2099                } else {
2100                    # Here we can process normally. Get all of the non-hidden members.
2101                    my @submems = OpenDir($dir, 1);
2102                    for my $submem (@submems) {
2103                        # Get the full name.
2104                        my $thisMem = "$dir/$submem";
2105                        Trace("Checking member $thisMem.") if T(4);
2106                        $lookCount++;
2107                        if ($lookCount % 1000 == 0) {
2108                            Trace("$lookCount members examined. Current is $thisMem.") if T(3);
2109                        }
2110                        # Fix the group.
2111                        chown -1, $gid, $thisMem;
2112                        # Insure this member is not a symlink.
2113                        if (! -l $thisMem) {
2114                            # Get its info.
2115                            my $fileInfo = stat $thisMem;
2116                            # Only proceed if we got the info. Otherwise, it's a hard link
2117                            # and we want to skip it anyway.
2118                            if ($fileInfo) {
2119                                my $fileMode = $fileInfo->mode;
2120                                if (($fileMode & $mask) == 0) {
2121                                    # Fix this member.
2122                                    $fileMode |= $mask;
2123                                    chmod $fileMode, $thisMem;
2124                                    $fixCount++;
2125                                }
2126                                # If it's a subdirectory, stack it.
2127                                if (-d $thisMem) {
2128                                    push @dirs, $thisMem;
2129                                }
2130                            }
2131                        }
2132                    }
2133                }
2134            }
2135            Trace("$lookCount files and directories processed, $fixCount fixed.") if T(2);
2136        };
2137        # Check for an error.
2138        if ($@) {
2139            Confess("SetPermissions error: $@");
2140        }
2141    }
2142    
2143  1;  1;

Legend:
Removed from v.1.45  
changed lines
  Added in v.1.49

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3