Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkdir: compatible permissions #854

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions bin/mkdir
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use strict;
use File::Basename qw(basename dirname);
use Getopt::Std qw(getopts);

our $VERSION = '1.3';
our $VERSION = '1.4';
my $Program = basename($0);

$SIG{__WARN__} = sub { warn "$Program: @_\n" };
Expand Down Expand Up @@ -126,7 +126,7 @@ foreach my $directory (@ARGV) {

my $realmode = $options{'m'};
if ($symbolic) {
$realmode = mod($options{'m'}, $dir) or
$realmode = mod($options{'m'}) or
die "invalid mode: $options{m}\n";
}
chmod oct($realmode) => $dir or warn "$!\n";
Expand All @@ -145,26 +145,26 @@ exit($err ? EX_ERROR : EX_SUCCESS);
#
#

sub mod ($$) {
sub mod {
my $symbolic = shift;
my $file = shift;

# Initialization.
# The 'user', 'group' and 'other' groups.
my @ugo = qw /u g o/;
# Bit masks for '[sg]uid', 'sticky', 'read', 'write' and 'execute'.
# Can't use qw // cause silly Perl doesn't know '2' is a number
# when dealing with &= ~$bit.
my %bits = (s => 8, t => 8, r => 4, w => 2, x => 1);

my @ugo = qw/u g o/;
my %bits = ('s' => 8, 't' => 8, 'r' => 4, 'w' => 2, 'x' => 1);

# For parsing.
my $who_re = '[augo]*';
my $action_re = '[-+=][rstwxXugo]*';


# Find the current permissions. This is what we start with.
my $mode = sprintf "%04o" => (stat $file) [2] || 0;
my $mode = '777';
if ($symbolic =~ m/[\-\+]/) {
my @st = stat $file;
if (@st) {
$mode = sprintf '%04o', $st[2];
}
}
my $current = substr $mode => -3; # rwx permissions for ugo.

my %perms;
Expand Down Expand Up @@ -248,6 +248,9 @@ sub mod ($$) {
else {%perms = %umask;}
next;
}
if ($operator eq '=') {
$perms{$who} = 0;
}

# If we arrive here, $perms is a string.
# We can iterate over the characters.
Expand Down Expand Up @@ -308,9 +311,8 @@ sub mod ($$) {

# Apply.
foreach my $s (@set) {
do {$perms {$s} |= $bit; next} if $operator eq '+';
do {$perms {$s} |= $bit; next} if ($operator eq '+' || $operator eq '=');
do {$perms {$s} &= ~$bit; next} if $operator eq '-';
do {$perms {$s} = $bit; next} if $operator eq '=';
die "Weird operator `$operator' found\n";
# Should not happen.
}
Expand Down
Loading