Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Chmod a+x a file in Perl

by atcroft (Abbot)
on Feb 18, 2022 at 05:39 UTC ( [id://11141451]=note: print w/replies, xml ) Need Help??


in reply to Re: Chmod a+x a file in Perl
in thread Chmod a+x a file in Perl

If you want to see all 4096 possible file permissions, the following code will generate each of them in the directory of your choice (named '0nnnn_xxxxxxxxx.txt', where '0nnnn' is the octal permission, and 'xxxxxxxxx' is the string form of the permissions). (Each file contains only the text of its file name, including the provided path.)

Code:

#!/usr/bin/env perl use strict; use warnings; use utf8; use Data::Dumper; use File::Path; use File::Spec; use Getopt::Long; $Data::Dumper::Deepcopy = 1; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Useqq = 1; $| = 1; srand(); my ( $_DEBUG, $_TEST, $_VERBOSE, ) = ( 0, 0, 0, ); my $create_dir = 0; my $target_dir = q{.}; GetOptions( q{debug!} => \$_DEBUG, q{test!} => \$_TEST, q{verbose!} => \$_VERBOSE, q{create_dir!} => \$create_dir, q{target_dir:s} => \$target_dir, q{help} => sub { &help; }, ); sub help { print <<HELP; Usage: $0 [--help] $0 [--option1 [--option2 param] ...] Options: --help - Display help text and exit. --create_dir - Create directory if it does not exist. --target_dir path/to/target - Directory to create files. --debug - Enable some debug output. --test - Enable some test code. --verbose - Increase verbosity. HELP exit; } if ( !-e $target_dir ) { if ( !$create_dir ) { die qq{Target directory $target_dir does not exist, but missing '--create_ +dir' option.\n}; } my @created = File::Path::make_path( $target_dir, verbose => $_VERBOSE, mode => oct(q{0711}), ) or die $!; print qq{Created:\n}, map { qq{\t} . $_ . qq{\n}; } @created if ($_VERBOSE); } my %known = ( oct_00000 => oct(q{00000}), oct_00001 => oct(q{00001}), oct_00002 => oct(q{00002}), oct_00004 => oct(q{00004}), oct_00007 => oct(q{00007}), oct_00010 => oct(q{00010}), oct_00020 => oct(q{00020}), oct_00040 => oct(q{00040}), oct_00070 => oct(q{00070}), oct_00100 => oct(q{00100}), oct_00200 => oct(q{00200}), oct_00400 => oct(q{00400}), oct_00700 => oct(q{00700}), oct_01000 => oct(q{01000}), oct_02000 => oct(q{02000}), oct_04000 => oct(q{04000}), oct_07000 => oct(q{07000}), ); my %shift_right = ( q{0_special} => 9, q{1_user} => 6, q{2_group} => 3, q{3_other} => 0, ); foreach my $i ( $known{oct_00000} .. ( $known{oct_07000} | $known{oct_00700} | $known{oct_00070} | $known{oct_00007} ) ) { my %perm = ( raw_octal => $i, octal => { q{0_special} => 0, q{1_user} => 0, q{2_group} => 0, q{3_other} => 0, }, str => { q{1_user} => q{---}, q{2_group} => q{---}, q{3_other} => q{---}, }, ); $perm{octal}{q{0_special}} = ( $i & $known{oct_07000} ); $perm{octal}{q{1_user}} = ( $i & $known{oct_00700} ); $perm{octal}{q{2_group}} = ( $i & $known{oct_00070} ); $perm{octal}{q{3_other}} = ( $i & $known{oct_00007} ); foreach my $k (qw/ 0_special 1_user 2_group 3_other /) { $perm{octal_value}{$k} = ( $perm{octal}{$k} >> $shift_right{$k} ); } foreach my $k (qw/ 1_user 2_group 3_other /) { substr( $perm{str}{$k}, 0, 1 ) = q{r} if ( ( $perm{octal_value}{$k} & $known{oct_00004} ) == $known{oct_00004} ); substr( $perm{str}{$k}, 1, 1 ) = q{w} if ( ( $perm{octal_value}{$k} & $known{oct_00002} ) == $known{oct_00002} ); substr( $perm{str}{$k}, 2, 1 ) = q{x} if ( ( $perm{octal_value}{$k} & $known{oct_00001} ) == $known{oct_00001} ); } if ( ( $perm{octal_value}{q{0_special}} & $known{oct_00004} ) == $known{oct_00004} ) { substr( $perm{str}{q{1_user}}, 2, 1 ) = q{S}; substr( $perm{str}{q{1_user}}, 2, 1 ) = q{s} if ( ( $perm{octal_value}{q{1_user}} & $known{oct_00001} ) == $known{oct_00001} ); } if ( ( $perm{octal_value}{q{0_special}} & $known{oct_00002} ) == $known{oct_00002} ) { substr( $perm{str}{q{2_group}}, 2, 1 ) = q{S}; substr( $perm{str}{q{2_group}}, 2, 1 ) = q{s} if ( ( $perm{octal_value}{q{2_group}} & $known{oct_00001} ) == $known{oct_00001} ); } if ( ( $perm{octal_value}{q{0_special}} & $known{oct_00001} ) == $known{oct_00001} ) { substr( $perm{str}{q{3_other}}, 2, 1 ) = q{T}; substr( $perm{str}{q{3_other}}, 2, 1 ) = q{t} if ( ( $perm{octal_value}{q{3_other}} & $known{oct_00001} ) == $known{oct_00001} ); } my $str = q{}; $str .= sprintf qq{%05o }, $perm{raw_octal}; $str .= q{= }; $str .= sprintf q{%05o | %05o | %05o | %05o }, $perm{octal}{q{0_special}}, $perm{octal}{q{1_user}}, $perm{octal}{q{2_group}}, $perm{octal}{q{3_other}}; $str .= q{= }; $str .= sprintf q{%3s%3s%3s}, $perm{str}{q{1_user}}, $perm{str}{q{2_group}}, $perm{str}{q{3_other}}; print Data::Dumper->Dump( [ \%perm, ], [ qw( *perm ), ], ), qq{\n} if ($_DEBUG); print $str, qq{\n} if ($_VERBOSE); my $fn = sprintf qq{%05o_%s.txt}, $perm{raw_octal}, join( q{}, $perm{str}{q{1_user}}, $perm{str}{q{2_group}}, $perm{str}{q{3_other}}, ); $fn = File::Spec->catfile( $target_dir, $fn ) or die $!; if ( !$_TEST ) { if ( !-f $fn ) { open my $fh, q{>}, $fn or die $!; print $fh $fn, qq{\n}; close $fh; } chmod $perm{raw_octal}, $fn or die $!; } }

Example of created files:

$ ./working --create_dir --target_dir my_foo $ cd my_foo && ls -labhrt | tail -n 1000 | head -n 10 ---S-ws--x 1 User Group 27 Feb 17 23:29 06031_--S-ws--x.txt ---S-ws-w- 1 User Group 27 Feb 17 23:29 06032_--S-ws-w-.txt ---S-ws-wx 1 User Group 27 Feb 17 23:29 06033_--S-ws-wx.txt ---S-wsr-- 1 User Group 27 Feb 17 23:29 06034_--S-wsr--.txt ---S-wsr-x 1 User Group 27 Feb 17 23:29 06035_--S-wsr-x.txt ---S-wsrw- 1 User Group 27 Feb 17 23:29 06036_--S-wsrw-.txt ---S-wsrwx 1 User Group 27 Feb 17 23:29 06037_--S-wsrwx.txt ---Sr-S--- 1 User Group 27 Feb 17 23:29 06040_--Sr-S---.txt ---Sr-S--x 1 User Group 27 Feb 17 23:29 06041_--Sr-S--x.txt ---Sr-S-w- 1 User Group 27 Feb 17 23:29 06042_--Sr-S-w-.txt

Example of --debug output (d3106 = 06042):

$ ./working --create_dir --target_dir my_foo --debug # ---skip--- %perm = ( "octal" => { "0_special" => 3072, "1_user" => 0, "2_group" => 32, "3_other" => 2 }, "octal_value" => { "0_special" => 6, "1_user" => 0, "2_group" => 4, "3_other" => 2 }, "raw_octal" => 3106, "str" => { "1_user" => "--S", "2_group" => "r-S", "3_other" => "-w-" } ); # ---skip---

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-04-18 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found