From: Eli Zaretskii <eliz@gnu.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: locate-dominating-file calls `stat' too eagerly
Date: Mon, 29 Sep 2008 17:36:53 +0300 [thread overview]
Message-ID: <uzllrj9x6.fsf@gnu.org> (raw)
In-Reply-To: <jwvprmnt5b7.fsf-monnier+emacs@gnu.org>
> From: Stefan Monnier <monnier@iro.umontreal.ca>
> Cc: emacs-devel@gnu.org
> Date: Mon, 29 Sep 2008 10:06:54 -0400
>
> Wouldn't it be simpler to wrap the `directroy-files' call inside
> a condition-case and drop the file-directory-p altogether?
That would work as well, but I'm not sure about the ``simpler'' part.
The code is already pretty convoluted, and I'm not sure why. All it
needs to do is (1) to find an existing parent directory of its
argument FILE by chopping directories from the end until it finds an
existing one, and (2) go up the tree of existing directories until it
finds one whose owner is different. This looks like 2 separate loops
to me, but the code for some reason insists on doing it in a single
loop.
How about this:
(defun locate-dominating-file (file regexp)
"Look up the directory hierarchy from FILE for a file matching REGEXP."
;; If FILE does not exist, find its parent directory that does.
(or (file-exists-p file)
(while (and file (not (file-directory-p file)))
(setq file (file-name-directory (directory-file-name file)))))
(catch 'found
(let ((user (nth 2 (file-attributes file)))
;; Abbreviate, so as to stop when we cross ~/.
(dir (abbreviate-file-name (file-name-as-directory file)))
files)
(while (and dir
;; As a heuristic, we stop looking up the hierarchy of
;; directories as soon as we find a directory belonging to
;; another user. This should save us from looking in
;; things like /net and /afs. This assumes that all the
;; files inside a project belong to the same user.
(let ((prev-user user))
(setq user (nth 2 (file-attributes dir)))
(equal user prev-user)))
(if (setq files (directory-files dir 'full regexp))
(throw 'found (car files))
(if (equal dir
(setq dir (file-name-directory
(directory-file-name dir))))
(setq dir nil))))
nil)))
And btw, won't the user test cover the case of crossing ~/ as well?
If so, we don't need to abbreviate-file-name, which then incurs
further overhead inside expand-file-name.
next prev parent reply other threads:[~2008-09-29 14:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-29 11:54 locate-dominating-file calls `stat' too eagerly Eli Zaretskii
2008-09-29 14:06 ` Stefan Monnier
2008-09-29 14:36 ` Eli Zaretskii [this message]
2008-09-29 16:19 ` Stefan Monnier
2008-09-29 19:05 ` Eli Zaretskii
2008-09-29 20:45 ` Stefan Monnier
2008-09-30 7:14 ` Eli Zaretskii
2008-09-30 14:02 ` Stefan Monnier
2008-09-30 16:30 ` Eli Zaretskii
2008-09-30 12:52 ` Eli Zaretskii
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=uzllrj9x6.fsf@gnu.org \
--to=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
/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.savannah.gnu.org/cgit/emacs.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).