Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Checking permissions in multiple directories

by Corion (Patriarch)
on Jun 03, 2000 at 21:41 UTC ( #16205=note: print w/replies, xml ) Need Help??


in reply to Checking permissions in multiple directories

If I understood your question right, you have two machines, A and B, and have a directory on machine A of which you want to clone the permissions onto a directory with the same contents on machine B.

Personally, I would think that a program such as rsync would do what you want, as rsync also synchronizes the files themselves, or, if your setup is even simpler, even tar would be enough.

There is also the question of how the data will be transferred between the two directories and the question of the operating system. If you are using a UNIX-like operating system, you have to make sure that the UIDs on both machines are the same.

If you can somehow mount both directories on one machine, cloning the permissions is relatively simple done by using readdir() or File::Find (note that this is untested):

$sourcedir = "/mnt/dev/"; # adapt this $targetdir = "/mnt/test/"; # adapt this use File::Find; find( \&check, $sourcedir ); sub check { my( $name ); $name = File::Find::dir . "/" . $_; &checkpermissions( $name ); }; sub checkpermissions { my( $directory ) = @_; my ( $targetdir = $directory ) =~ s!^$sourcedir!$targetdir!; my ($perm_s, $perm_t); my @statresults; # This could be more optimized, but ... @statresults = stat( $directory ); $perm_s = @statresults[2]; @statresults = stat( $targetdir ); $perm_t = @statresults[2]; if ($perm_s != $perm_t ) { print "$targetdir has wrong permissions."; chmod $perm_s, $targetdir or die "Couldn't update the permission +s for $targetdir : $!\n"; }; };

Replies are listed 'Best First'.
RE: Re: Checking permissions in multiple directories
by takshaka (Friar) on Jun 03, 2000 at 22:58 UTC
    <pedant>
    $File::Find::name is equivalent to $File::Find::dir . "/" . $_
    </pedant>

    I agree that a tool like rsync is more suited to syncronizing directories on different hosts, if that is the ultimate goal.

RE: Re: Checking permissions in multiple directories
by Anonymous Monk on Jun 04, 2000 at 10:41 UTC
    I need to check the entire tree, not just the directories, but the files inside the directories. Also, if a file doesn't exist i need it to tell me it doesnt exist.

      File::Find already descends down through all subdirectories, sorry if I didn't mention that.

      But it really seems to me as if a tool such as rsync or a simpler solution using tar cfz transfer.tgz would do what you want in a much better way, since rsync can keep two directory branches synchronized in all ways without transferring all files and tar works more or less like cp. Please either restate your problem or think about using a readymade tool.

      The necessary modifications for my above script to tell you if a file is missing on machine B would be in the routine checkpermissions() :

      sub checkpermissions { my( $directory ) = @_; my ( $targetdir = $directory ) =~ s!^$sourcedir!$targetdir!; # now we check first of the item in $targetdir exists : if (-f $targetdir || -d $targetdir) { my ($perm_s, $perm_t); my @statresults; # This could be more optimized, but ... @statresults = stat( $directory ); $perm_s = @statresults[2]; @statresults = stat( $targetdir ); $perm_t = @statresults[2]; if ($perm_s != $perm_t ) { print "$targetdir has wrong permissions."; chmod $perm_s, $targetdir or die "Couldn't update the permissi +ons for $targetdir : $!\n"; }; } else { # I assume you don't have links or other stuff. # If you have symlinks etc., use rsync or tar # I cheat and call cp for copying all stuff recursively system "cp", "-R", "$directory", "$targetdir"; }; };
      Please think again about whether you really want to use Perl for this problem.

      When all you have is a hammer,
      every problem looks like a nail.

        I tired using the stat function, but the when I do a $temp = $stats2; i get a number like 33261 then the chmod craps out. Am I doing something wrong?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://16205]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2023-01-27 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?