Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.programmer > #13631

Re: How to check for error with write(2)

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Rainer Weikusat <rweikusat@talktalk.net>
Newsgroups comp.unix.programmer
Subject Re: How to check for error with write(2)
Date Thu, 16 Dec 2021 19:06:26 +0000
Lines 32
Message-ID <87ilvo2wul.fsf@doppelsaurus.mobileactivedefense.com> (permalink)
References <spfuo5$lb0$1@gioia.aioe.org>
Mime-Version 1.0
Content-Type text/plain
X-Trace individual.net S3RZVpCiHDfU8A3zZf2O+QE7NEACqY0KMveZLxswhYcdcyovE=
Cancel-Lock sha1:V7xlUIA7qVjXKHx//jI57wJdB+M= sha1:AMm0+ltuyzcKN1rOyi5bJQ3c+pY=
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)
Xref csiph.com comp.unix.programmer:13631

Show key headers only | View raw


Steve Keller <keller.steve@gmx.de> writes:
> 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()?

Avoid checking the return value of close. The way to ensure that a
"physical write" took place is to use fsync before close (and checking
the return value of that) and this should usually be avoided.

The only thing checking the return value of close is actually good for
is to tell a user that "my program lost your data because I didn't use
fsync" --- not terribly useful.

Back to comp.unix.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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