The Grumpy Editor's Python 3 experience
The Grumpy Editor's Python 3 experience
Posted Aug 1, 2018 20:06 UTC (Wed) by cortana (subscriber, #24596)In reply to: The Grumpy Editor's Python 3 experience by marcH
Parent article: The Grumpy Editor's Python 3 experience
Hmm...
>>> 0o400 + 0o200 == 0o600 True
Or am I missing something?
Posted Aug 1, 2018 20:31 UTC (Wed)
by farnz (subscriber, #17727)
[Link] (5 responses)
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.
Posted Aug 1, 2018 20:38 UTC (Wed)
by marcH (subscriber, #57642)
[Link]
Yes (and you're in incredibly large company, never understood why)
Let me give you a more real world example:
chmod -R g+rX friends_can_look/
Good luck octal.
Posted Aug 2, 2018 13:27 UTC (Thu)
by virtex (subscriber, #3019)
[Link] (1 responses)
Posted Aug 6, 2018 12:59 UTC (Mon)
by cortana (subscriber, #24596)
[Link]
The Grumpy Editor's Python 3 experience
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
The Grumpy Editor's Python 3 experience
When setting bits you usually don't want to just add the numbers together like you're doing because if the bit is already set the answer won't be what you want:
The Grumpy Editor's Python 3 experience
>>> oct(0o400 + 0o200)
'0o600' (Looks good)
>>> oct(0o600 + 0o200)
'0o1000' (Likely not what you wanted)
It's better to use the bitwise OR operation which will set the bit if it's unset, or leave it alone if it's already set:
>>> oct(0o400 | 0o200)
'0o600' (Looks good)
>>> oct(0o600 | 0o200)
'0o600' (The bit is already set, so nothing changes)
The Grumpy Editor's Python 3 experience
