git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] C version of git-count-objects

From
Linus Torvalds <torvalds@osdl.org>
Date
Apr 27, 2006, 20:07 UTC
Message-ID
<Pine.LNX.4.64.0604271257010.3701@g5.osdl.org>
In-Reply-To
<20060427194559.GA26386@brainysmurf.cs.umu.se>
On Thu, 27 Apr 2006, Peter Hagervall wrote:
Show 5 quoted lines
> > 
> > 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
Previous: Peter HagervallNext: Peter Hagervall
Message 8 of 12 in “C version of git-count-objects”
  1. C version of git-count-objectsPeter Hagervall, Apr 27, 2006
  2. Morten WelinderApr 27, 2006
  3. Nicolas PitreApr 27, 2006
  4. C version of git-count-objects, second tryPeter Hagervall, Apr 27, 2006
  5. Junio C HamanoApr 27, 2006
  6. Nicolas PitreApr 27, 2006
  7. Peter HagervallApr 27, 2006
  8. Linus TorvaldsApr 27, 2006
  9. Peter HagervallApr 27, 2006
  10. Junio C HamanoApr 27, 2006
  11. Peter HagervallApr 28, 2006
  12. Junio C HamanoApr 28, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.