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

[PATCH] Use fstat instead of fseek

From
Jonas Fonseca <fonseca@diku.dk>
Date
Aug 27, 2006, 23:55 UTC
Message-ID
<20060827235546.GA20904@diku.dk>
In-Reply-To
<7v4pvz11o6.fsf@assigned-by-dhcp.cox.net>
Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
---
 dir.c |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)
Junio C Hamano <junkio@cox.net> wrote Sat, Aug 26, 2006:
Show 9 quoted lines
> Linus Torvalds <torvalds@osdl.org> writes:
> 
> > I really think you'd be better off rewriting that to use "fstat()" 
> > instead. I don't know why it uses two lseek's, but it's wrong, and looks 
> > like some bad habit Junio picked up at some point.
> 
> I think the code was written to avoid getting confused by
> unseekable input (pipes) but was done in early morning before
> the first shot of caffeine.

I take it that you want this change, so here's a little addition to the "use X instead of Y" series.

diff --git a/dir.c b/dir.c
index d53d48f..5a40d8f 100644
--- a/dir.c
+++ b/dir.c
@@ -112,17 +112,15 @@ static int add_excludes_from_file_1(cons
 				    int baselen,
 				    struct exclude_list *which)
 {
+	struct stat st;
 	int fd, i;
 	long size;
 	char *buf, *entry;
 
 	fd = open(fname, O_RDONLY);
-	if (fd < 0)
+	if (fd < 0 || fstat(fd, &st) < 0)
 		goto err;
-	size = lseek(fd, 0, SEEK_END);
-	if (size < 0)
-		goto err;
-	lseek(fd, 0, SEEK_SET);
+	size = st.st_size;
 	if (size == 0) {
 		close(fd);
 		return 0;
-- 
1.4.2.g2f76-dirty

-- 
Jonas Fonseca
Previous: Junio C Hamano
Message 10 of 10 in “dir: do all size checks before seeking back and fix file closing”
  1. dir: do all size checks before seeking back and fix file closingJonas Fonseca, Aug 26, 2006
  2. Mitchell Blank JrAug 26, 2006
  3. Jonas FonsecaAug 26, 2006
  4. Linus TorvaldsAug 26, 2006
  5. Jakub NarebskiAug 26, 2006
  6. Junio C HamanoAug 27, 2006
  7. Junio C HamanoAug 27, 2006
  8. Linus TorvaldsAug 27, 2006
  9. Junio C HamanoAug 27, 2006
  10. Use fstat instead of fseekJonas Fonseca, Aug 27, 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.