From: Linus Torvalds <torvalds@osdl.org>
To: moreau francis <francis_moreau2000@yahoo.fr>
Cc: git@vger.kernel.org
Subject: Re: open(2) vs fopen(3)
Date: Thu, 14 Sep 2006 08:46:13 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.64.0609140835080.4388@g5.osdl.org> (raw)
In-Reply-To: <20060914091513.19826.qmail@web25812.mail.ukl.yahoo.com>
On Thu, 14 Sep 2006, moreau francis wrote:
>
> I'm reading git source code and falling on this stupid question:
> Why sometime open(2) is used and other time fopen(3) is
> prefered. I'm sorry for this dump question but I have no clue.
fopen() tends to result in easier usage, especially if the file in
question is a line-based ASCII file, and you can just use "fgets()" to
read it. So fopen is the simple alternative for simple problems.
Using a direct open() means that you have to use the low-level IO
functions (I'm ignoring the use of "fdopen()"), but if done right, it has
a number of advantages:
- with the proper use, it's potentially more efficient (but stdio is a
lot more efficient if you do lots of small writes without buffering)
- you can control the creation flags better (ie if you want to do an
exclusive open, you _have_ to use "open()" - there's no portable way to
say O_EXCL with "fopen()")
- error conditions are a lot more obvious and repeatable with the
low-level things, at least so I find personally. Error handling with
stdio routines is _possible_, but probably because almost nobody ever
does it, it's not something that people are conditioned to do, so it
ends up beign "strange".
(So this third one is more a psychological issue than a really
technical issue - at least for me. I'd not use stdio for things I
might expect to do fsync() on, for example. It's _possible_, but very
non-intuitive, because that's now how people generally use stdio).
So it boils down to the fact that people tend to do higher-level things
with stdio interfaces (fopen and friends), and lower-level things with the
raw system call ("unistd.h") interfaces.
In git, you'd expect to see code that actually works on the object
database or the refs using "open()" (both because it's low-level, and it
generally wants to use O_EXCL and friends), and then things that open the
".gitignore" file to use fopen() (because it's a line-based ASCII
interface, and it's not an "important" file in the sense that we don't
really care about some strange situation where it could give us an IO
error).
There might also be a difference in personality. I probably tend to use
the core unistd interfaces more than some other people would, and some
other people might end up using stdio for pretty much everything.
Linus
next prev parent reply other threads:[~2006-09-14 15:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-09-14 9:15 open(2) vs fopen(3) moreau francis
2006-09-14 10:52 ` Andy Whitcroft
2006-09-14 15:46 ` Linus Torvalds [this message]
2006-09-14 16:37 ` Junio C Hamano
2006-09-14 17:31 ` Linus Torvalds
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
List information: http://vger.kernel.org/majordomo-info.html
* 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.0609140835080.4388@g5.osdl.org \
--to=torvalds@osdl.org \
--cc=francis_moreau2000@yahoo.fr \
--cc=git@vger.kernel.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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).