On 26/08/17 11:10, Colin Watson wrote:
> I would like there to be an adverbial version of "cd", which takes a
> path followed by a command and optional arguments and executes the
> command with its working directory set to the given path. Its
> invocation would be similar to chroot(8), that is:
>
> chdir [OPTION] NEWDIR [COMMAND [ARG]...]
>
> This would allow these two commands to be synonyms aside from a
> different arrangement of processes:
>
> (cd /foo && ls -l)
> chdir /foo ls -l
>
>
> Why do I want this when there's a perfectly good shell builtin? Well, I
> often find that I want to write programs that take an argument list and
> run them in various different contexts, for example in a chroot (using
> "chroot") or in a container (using "lxc exec"). Unless the
> context-entering command has an option to set the current directory -
> neither of the two aforementioned examples does - I have to do this by
> writing something along the lines of:
>
> sudo chroot /path/to/chroot sh -c 'cd /foo && ls -l'
>
> This means dealing with shell quoting, which is tedious and error-prone.
> It would be much easier if I had something that composed nicely with
> other programs that use this kind of pattern for running a child process
> in some different context:
>
> sudo chroot /path/to/chroot chdir /foo ls -l
>
>
> If people think this is a reasonable idea then I'd be happy to write the
> code, although I'd have to faff around with getting my employer to sign
> a copyright assignment. I'm aware that (as far as I know) this is an
> innovation rather than something that other operating systems already
> implement; I think my suggested name is the most natural choice by
> analogy with the system call and chroot(8).
If we were to provide this I think chdir is a fine name.
It's a pity we have to have this heavy weight chaining of execs like this
to set each process attribute. This is more general though, than adding
--chdir to other "context" type programs like chroot etc.
Note GNU chroot has a --skip-chdir which nearly does what you want.
I.E. you could os.chdir("/chroot/build") before calling "chroot --skip-dir ...",
however that's currently restricted for greater protection to when
you're not actually changing the root directory. I suppose we could relax
the check to verify that getcwd() was _below_ the specified chroot path.
Of course none of this helps with jexec, lxc etc.
Another option to consider is to add --chdir to env(1) since they
would often be used together and could be done more efficiently with a single
exec?
cheers,
Pádraig.