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

Reply via email to