The Grumpy Editor's Python 3 experience
The Grumpy Editor's Python 3 experience
Posted Aug 1, 2018 20:31 UTC (Wed) by farnz (subscriber, #17727)In reply to: The Grumpy Editor's Python 3 experience by cortana
Parent article: The Grumpy Editor's Python 3 experience
Given a file with an unknown mode, how do you set user readable, remove other writeable and add execute to all without changing the rest of the permission bits?
chmod can't apply logical operations to the disk mode expressed in octal form, but chmod u+r,o-w,a+x will do that operation.
Posted Aug 2, 2018 12:33 UTC (Thu)
by cortana (subscriber, #24596)
[Link]
Posted Aug 2, 2018 15:07 UTC (Thu)
by rweikusat2 (subscriber, #117920)
[Link] (3 responses)
There are two much more interesting questions here, though.
1) Why specify a translation depending on an existing value in order to change that to one you want instead of just using 'the one you want'?
2) 0206? Seriously? May be better fix the program ...
Posted Aug 2, 2018 20:40 UTC (Thu)
by rweikusat2 (subscriber, #117920)
[Link] (2 responses)
Assuming that's called pchmod, it becomes something like pchmod +511,-2.
Posted Aug 2, 2018 21:57 UTC (Thu)
by marcH (subscriber, #57642)
[Link] (1 responses)
Posted Aug 2, 2018 22:10 UTC (Thu)
by rweikusat2 (subscriber, #117920)
[Link]
The features (or lack of features) of chmod are not relevant to the discussion. Apparently, nobody ever needed relative octal mode specifications so badly that this got implemented. As demonstrated above, this is trivial (I wrote this while waiting for a 'git gc' to finish).
Octal mode specifications are convenient in code, especially, C code, because the replacement macronames are lengthy sequences of unpronouncible gibberish. They're easy enough to remember that they're also convenient for specifying absolute modes for the chmod command. I found it useful to overcome my original "numbers ... "-prejudice and would thus encourage others to try the same.
The Grumpy Editor's Python 3 experience
The Grumpy Editor's Python 3 experience
BTW,
Octal mode translations
#!/usr/bin/perl
#
sub usage
{
print STDERR ("Usage: pchmod <mode arg> <path>+\n");
exit(1);
}
sub add { $_[0] | $_[1] }
sub clear { $_[0] & ~$_[1] }
sub set { $_[1] }
sub valid_mode
{
$_[0] =~ /^([+-])?0?[0-7]{1,3}$/;
}
sub parse_mode
{
my (@ops, $op, $v);
for (split(/,/, $_[0])) {
die("invalid mode $_") unless valid_mode($_);
if (/^([+-])(.*)/) {
$op = $1 eq '+' ? \&add : \&clear;
$v = $2;
} else {
$op = \&set;
$v = $_;
}
push(@ops, [$op, oct($v)]);
}
return @ops;
}
my (@ops, @stat, $m, $rc);
@ARGV > 1 || usage();
@ops = parse_mode(shift);
for (@ARGV) {
@stat = stat;
@stat or warn("stat '$_': $!"), next;
$m = $stat[2];
$m = $_->[0]($m, $_->[1]) for @ops;
$rc = chmod($m, $_);
$rc or warn("chmod '$_': $!");
}
Octal mode translations
Octal mode translations
