git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@osdl.org>
To: Peter Hagervall <hager@cs.umu.se>
Cc: Nicolas Pitre <nico@cam.org>, Junio C Hamano <junkio@cox.net>,
	git@vger.kernel.org
Subject: Re: [PATCH] C version of git-count-objects
Date: Thu, 27 Apr 2006 13:07:27 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0604271257010.3701@g5.osdl.org> (raw)
In-Reply-To: <20060427194559.GA26386@brainysmurf.cs.umu.se>



On Thu, 27 Apr 2006, Peter Hagervall wrote:
> > 
> > To avoid appending the filename to the path before each lstat() I'd 
> > guess.
> 
> Yes, that's pretty much the reason.

It's a bad reason, though.

For one thing, it just doesn't work. You'll have to chdir() back, and you 
can't use ".." in case the user has set up some symlink thing. So you end 
up doing other really strange things.

You can do this much more efficiently with something like this:

	const char *obj = git_object_directory();
	int len = strlen(obj);
	char *dir = malloc(len + 300);

	memcpy(dir, obj, len);
	if (len && obj[len-1] != '/')
		dir[len++] = '/';
	dir[len+2] = 0;
	for (i = 0; i < 16; i++) {
		dir[len] = hexdigit[i];
		for (j = 0; j < 16; j+) {
			dir[len+1] = hexdigit[j];
			dir[len+2] = 0;
			DIR *d = opendir(dir);
			if (!d)
				continue;
			nr += count(d, dir, len+2);
			closedir(d);
		}
	}

where the "count()" function just ends up doing something like

	int count(DIR *d, const char *prefix, int len)
	{
		int nr = 0;
		struct dirent *de;

		prefix[len++] = '/';
		while ((de = readdir(d)) != NULL) {
			int fd;
			if (de->d_name[0] == '.')
				continue;
			strcpy(prefix + len, de->d_name);
			fd = open(prefix, O_RDONLY);
			.. check if it's ok, perhaps.. ?
			if (ok)
				nr++;
			close(fd);
		}
		return nr;
	}

and you're done. Efficient, and it's easy to add the endign to the 
pathname, because you're passing in a buffer that is big enough, and 
you're telling people where they should put their suffixes..

And no, the above has never been compiled or tested, and I wrote it with 
one eye closed, while drinking heavily and experimenting with some funky 
'shrooms. So caveat emptor.

		Linus

  reply	other threads:[~2006-04-27 20:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-27 10:12 [PATCH] C version of git-count-objects Peter Hagervall
2006-04-27 13:16 ` Morten Welinder
2006-04-27 13:23 ` Nicolas Pitre
2006-04-27 14:07   ` [PATCH] C version of git-count-objects, second try Peter Hagervall
2006-04-27 18:56   ` [PATCH] C version of git-count-objects Junio C Hamano
2006-04-27 19:39     ` Nicolas Pitre
2006-04-27 19:46       ` Peter Hagervall
2006-04-27 20:07         ` Linus Torvalds [this message]
2006-04-27 20:51           ` Peter Hagervall
2006-04-27 22:07             ` Junio C Hamano
2006-04-28  0:10               ` Peter Hagervall
2006-04-28  0:25                 ` Junio C Hamano

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=Pine.LNX.4.64.0604271257010.3701@g5.osdl.org \
    --to=torvalds@osdl.org \
    --cc=git@vger.kernel.org \
    --cc=hager@cs.umu.se \
    --cc=junkio@cox.net \
    --cc=nico@cam.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.
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).