Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.unix.programmer > #13630
| From | Rainer Weikusat <rweikusat@talktalk.net> |
|---|---|
| Newsgroups | comp.unix.programmer |
| Subject | Re: How to check for error with write(2) |
| Date | 2021-12-16 19:00 +0000 |
| Message-ID | <87mtl02x3y.fsf@doppelsaurus.mobileactivedefense.com> (permalink) |
| References | <spfuo5$lb0$1@gioia.aioe.org> <spg1nh$1o9$1@dont-email.me> |
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
> On Thu, 16 Dec 2021 18:58:28 +0100, Steve Keller wrote:
>
>> A write() system call can return successfully and even the following
>> close() syscall may not return an error, even if there was an error like
>> EFBIG (File too large).
>>
>> if ((fd = creat(name, 0666)) < 0) {
>> perror(name);
>> exit(1);
>> }
>> while (have_more_data) {
>> if (write(fd, buffer, count) < 0) {
>> perror("write");
>> exit(1);
>> }
>> }
>> if (close(fd) < 0) {
>> perror("close");
>> exit(1);
>> }
>>
>> This may complete successfully, e.g. when EFBIG occurs in the last
>> write() syscall before the loop ends.
>>
>> So what is the best way to check for errors with write()?
>>
> According to the Linux write(2) manpage,
> A successful return from write() does not make any guarantee that data
> has been committed to disk. In fact, on some buggy implementations, it
> does not even guarantee that space has successfully been reserved for
> the data. The only way to be sure is to call fsync(2) after you are
> done writing all your data.
>
> While this may not be true for other Unix and Unix-like systems, an
> appropriately-placed sync(2)/fsync(2) call wouldn't hurt.
Usually, it will hurt. The buffered I/O subsystem in the kernel ("page
cache") is there for a reason and it shouldn't be circumvented just
because it seems somehow suspicious. The default assumption should be
that the system will not suddenly stop and hence, that leaving it to the
kernel to decide when to write what will be ok.
Back to comp.unix.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to check for error with write(2) Steve Keller <keller.steve@gmx.de> - 2021-12-16 18:58 +0100
Re: How to check for error with write(2) scott@slp53.sl.home (Scott Lurndal) - 2021-12-16 18:26 +0000
Re: How to check for error with write(2) Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2021-12-20 13:52 +0000
Re: How to check for error with write(2) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-12-16 18:49 +0000
Re: How to check for error with write(2) Rainer Weikusat <rweikusat@talktalk.net> - 2021-12-16 19:00 +0000
Re: How to check for error with write(2) Rainer Weikusat <rweikusat@talktalk.net> - 2021-12-16 19:06 +0000
Re: How to check for error with write(2) Jorgen Grahn <grahn+nntp@snipabacken.se> - 2021-12-16 22:34 +0000
Re: How to check for error with write(2) Steve Keller <keller.steve@gmx.de> - 2021-12-17 04:38 +0100
Re: How to check for error with write(2) Rainer Weikusat <rweikusat@talktalk.net> - 2021-12-17 15:14 +0000
Re: How to check for error with write(2) Jorgen Grahn <grahn+nntp@snipabacken.se> - 2021-12-18 10:25 +0000
Re: How to check for error with write(2) Philip Guenther <guenther@gmail.com> - 2021-12-16 15:04 -0800
Re: How to check for error with write(2) Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-12-16 20:10 -0700
Re: How to check for error with write(2) Steve Keller <keller.steve@gmx.de> - 2021-12-17 04:35 +0100
Re: How to check for error with write(2) Philip Guenther <guenther@gmail.com> - 2021-12-18 14:58 -0800
Re: How to check for error with write(2) scott@slp53.sl.home (Scott Lurndal) - 2021-12-19 16:07 +0000
csiph-web