From: Jeff King <peff@peff.net>
To: Robert Stanca <robert.stanca7@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] [GSOC] remove_temporary_files(): reimplement using iterators
Date: Sat, 1 Apr 2017 01:37:16 -0400 [thread overview]
Message-ID: <20170401053716.yzxqxokuz3awqki4@sigill.intra.peff.net> (raw)
In-Reply-To: <20170401002458.3494-1-robert.stanca7@gmail.com>
On Sat, Apr 01, 2017 at 03:24:58AM +0300, Robert Stanca wrote:
> @@ -49,12 +51,7 @@ static void remove_temporary_files(void)
> {
> struct strbuf buf = STRBUF_INIT;
> size_t dirlen, prefixlen;
> - DIR *dir;
> - struct dirent *e;
> -
> - dir = opendir(packdir);
> - if (!dir)
> - return;
> + struct dir_iterator *diter = dir_iterator_begin(packdir);
>
> /* Point at the slash at the end of ".../objects/pack/" */
> dirlen = strlen(packdir) + 1;
> @@ -62,14 +59,13 @@ static void remove_temporary_files(void)
> /* Hold the length of ".tmp-%d-pack-" */
> prefixlen = buf.len - dirlen;
>
> - while ((e = readdir(dir))) {
> - if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
> + while (dir_iterator_advance(diter) == ITER_OK) {
> + if (strncmp(diter->relative_path, buf.buf + dirlen, prefixlen))
> continue;
> strbuf_setlen(&buf, dirlen);
> - strbuf_addstr(&buf, e->d_name);
> + strbuf_addstr(&buf, diter->relative_path);
> unlink(buf.buf);
> }
> - closedir(dir);
I think you could actually clean this code up more. The dir_iterator
already does this strbuf magic to hold the full path, so you should be
able to just run "unlink(iter->path.buf)", get rid of the extra strbuf
entirely.
We use that strbuf for the prefix-comparison, too, but the way it is
done is rather confusing. AFAICT, we could just be comparing against
"packtmp + strlen(packdir) + 1". Though it would be simpler still to
make "packtmp" just the basename, rather than the full path.
I do agree with the point Junio raised, though, that this loop isn't
recursive, but dir_iterator is. I think there was talk elsewhere of
giving it more options, so perhaps it could be taught a non-recursive
version. Until we have that, though, I'm not sure this is a good spot to
convert.
-Peff
next prev parent reply other threads:[~2017-04-01 5:37 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-01 0:24 [PATCH] [GSOC] remove_temporary_files(): reimplement using iterators Robert Stanca
2017-04-01 5:37 ` Jeff King [this message]
2017-04-01 5:41 ` Jeff King
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: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170401053716.yzxqxokuz3awqki4@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=robert.stanca7@gmail.com \
/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://80x24.org/mirrors/git.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).