🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #1315

Re: filters

#1315
From: Dan Bernstein
Date: Sun, 02 Dec 1990 21:21
53 lines
2982 bytes
In article <109685@convex.convex.com> tchrist@convex.com (Tom Christiansen) writes:
  [ checking write errors ]
> Cat and col are two utilities I've just caught doing this.  Cat does
> make a stab at detecting this, but it does it wrong.  It just checks
> ferror(stdout), but it's never done an fflush() or an fclose() first.

That's true. However (old discussion resurfacing again), what happens if
another process has the file open, so things don't get written to disk
until that process closes the file? There is simply no way to recover
the data. fsync()ing all the data just to test for things like EDQUOT,
ENOSPC, etc. would be a huge overhead.

You have two strategies for solving this:

1. Replace NFS with a sane remote filesystem, so that you get quota and
space errors on the write()s that trigger them. Unfortunately, it's a
bit difficult to simply throw away NFS at some sites, so go on to #2.

2. Write a program that does nothing but create a file on disk, read
input, and write output to the file, checking all errors. Give the
program options, so that it can do things like fsync if you want, or
write the file to another disk and put in a symbolic link if you run out
of space, or send you a mail message and buffer output in memory as long
as it can hold out. Or whatever.

After #2, you'll notice that a lot of programs can be drastically
simplified, because they don't have to open files for writing. Meanwhile
errors like the one you're complaining about will disappear. Meanwhile
you'll be able to get programs to work with new filesystems, or
networks, or whatever, just by using a different program. Guess what?
This works for reading files as well. Guess what? It also works for
networks (see my auth and authutil packages). Guess what? There really
is an advantage to modularity.

> Plus it only says "output write error", not what the error is.

stdio's fault. cat should use read() and write() for efficiency and
error checking, but that means a rewrite that nobody's bothered doing. 

> My first question I have is what could possible break if cat were to do
> an fclose(stdout) at the end (and check the status and set the exit 
> accordingly).  

There's nothing wrong with the change, but it doesn't solve the problem
unless you're guaranteed to be the only writer.

> My second question is whether this is just a symptom of sloppy programming
> or whether there's some reason why filters (mis)behave this way.

Although management at Sun might wish otherwise, UNIX was not rewritten
from scratch when NFS was released upon the world.

---Dan
Technical Details
Message-ID: <10763:Dec221:21:1590@kramden.acf.nyu.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cmcl2!kramden.acf.nyu.edu!brnstnd
References: <109685@convex.convex.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cmcl2!kramden.acf.nyu.edu!brnstnd
From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein)
Newsgroups: comp.unix.programmer
Subject: Re: filters
Message-ID: <10763:Dec221:21:1590@kramden.acf.nyu.edu>
Date: 2 Dec 90 21:21:15 GMT
References: <109685@convex.convex.com>
Organization: IR
Lines: 53