zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: can we have an option for cd to do a plain chdir()
Date: Thu, 10 Feb 2022 20:27:15 +0000	[thread overview]
Message-ID: <20220210202715.56vzsgvuqrnyazmg@chazelas.org> (raw)
In-Reply-To: <CAH+w=7ZrjbpxT6sfu_DiAq24hytK6km=ViWRDA0o48V-mx6rHw@mail.gmail.com>

2022-02-09 09:16:46 -0800, Bart Schaefer:
> I started looking at this myself a while back but couldn't find a way
> to untangle the function dependencies.  They're oddly factored and
> other parts of the shell depend on some of the bits in the middle.
[...]

Thanks, that's the impression I had from glancing at the code.

But then what about the idea of having an option like -r that
bypasses all that and does the simple chdir().

BTW, I've just thought of another way to do the equivalent of a
chdir() reliably on Linux provided you have read access to a
directory:

chdir() { cd /dev/fd/3 3< "$1"; }

Beside adding a "cd -r whatever" that just does a
chdir(whatever), we could add:

- "cd -u 3" which does a fchdir(3)
- sysopen -o path -u fd some/path && cd -u "$fd"
  (or sysopen -pu fd some/path as it's neither a read nor write
  open)
  (with -o nofollow, that can help do some safe directory
  traversal)
- sysopen -@ "$fd" -u otherfd some/path
  for openat()

Being able to do fchdir would allow improvements over the
less reliable:

for dir in $dirs; do
  cd $dir
  do-something
  cd ..
done

Or

for dir in $dirs; do
  pushd $dir
  do-something
  popd
done

Which we could replace with:

savedir() sysopen -po cloexec -u "$1" .
chdir_no_follow() {
  local fd ret
  sysopen -po nofollow -u fd -- "$1" || return
  cd -u "$fd"; ret=$?
  exec {fd}<&-
  return "$ret"
}
savedir || exit
for dir in $dirs; do
  chdir_no_follow $dir && do-something
  cd -u "$fd"
done

What do you think?

-- 
Stephane


      reply	other threads:[~2022-02-10 20:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-06 17:44 Stephane Chazelas
2022-02-09 17:16 ` Bart Schaefer
2022-02-10 20:27   ` Stephane Chazelas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220210202715.56vzsgvuqrnyazmg@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).