All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonas Fonseca <fonseca@diku.dk>
To: Junio C Hamano <junkio@cox.net>
Cc: Linus Torvalds <torvalds@osdl.org>, git@vger.kernel.org
Subject: [PATCH] Use fstat instead of fseek
Date: Mon, 28 Aug 2006 01:55:46 +0200	[thread overview]
Message-ID: <20060827235546.GA20904@diku.dk> (raw)
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:
> 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

      parent reply	other threads:[~2006-08-27 23:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-26 14:17 [PATCH] dir: do all size checks before seeking back and fix file closing Jonas Fonseca
2006-08-26 18:43 ` Mitchell Blank Jr
2006-08-26 20:44   ` Jonas Fonseca
2006-08-26 22:13 ` Linus Torvalds
2006-08-26 22:35   ` Jakub Narebski
2006-08-27  0:07   ` Junio C Hamano
2006-08-27  2:15     ` Junio C Hamano
2006-08-27  2:37     ` Linus Torvalds
2006-08-27  3:04       ` Junio C Hamano
2006-08-27 23:55     ` Jonas Fonseca [this message]

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=20060827235546.GA20904@diku.dk \
    --to=fonseca@diku.dk \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    --cc=torvalds@osdl.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.