|
|
Subscribe / Log in / New account

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.


to post comments

The Grumpy Editor's Python 3 experience

Posted Aug 2, 2018 12:33 UTC (Thu) by cortana (subscriber, #24596) [Link]

Oh I see, we're talking about the chmod command. I was thinking more generally, in terms of the arithmetic that you'd perform if you were implementing the chmod command. :)

The Grumpy Editor's Python 3 experience

Posted Aug 2, 2018 15:07 UTC (Thu) by rweikusat2 (subscriber, #117920) [Link] (3 responses)

perl -e '$f=$ARGV[0]; $m=(stat $f)[2]; chmod((($m | 0511) & ~02), $f)'

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 ...

Octal mode translations

Posted Aug 2, 2018 20:40 UTC (Thu) by rweikusat2 (subscriber, #117920) [Link] (2 responses)

BTW,
#!/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 '$_': $!");
}

Assuming that's called pchmod, it becomes something like pchmod +511,-2.

Octal mode translations

Posted Aug 2, 2018 21:57 UTC (Thu) by marcH (subscriber, #57642) [Link] (1 responses)

Wow, I didn't think I would ever see that much (coding!!) effort put into trolling...

Octal mode translations

Posted Aug 2, 2018 22:10 UTC (Thu) by rweikusat2 (subscriber, #117920) [Link]

You shouldn't post peiorative assumptions about other people just because they happen to disagree with your opinion and argue about that.

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.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds