scp sink function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm currently looking indepth at scp and am a little confused on how
the sink function works, what its doing, and what atomicio really is
doing.  Could someone shed some light on this for me?


Re: scp sink function

Quoted text here. Click to load it

The sink function reads the stream from the input, decapsulates the
files and directories contained therein and writes them to disk.

Remember that in scp, the same binary runs on both client and server.
The sender (which may be either client or server) calls source(), which
generates the stream which is sent over the connection to another scp,
which calls the sink() function to write the contents of the stream
to disk.

Quoted text here. Click to load it

On Unix (and probably any POSIX platform), the read() and write() syscalls
can partially complete.  atomicio ensures that the entire buffer is
written (or read) in, multiple calls if necessary.

Consider the following code:

    if (write(fd, buf, 100) < 0)
        fatal("foo error");

The write() will return -1 if there is an error, however under some
circumstances the call might write, eg, half the buffer, and return a
value of "50".  If that happens, the remainder of the buffer would not
be written and will be discarded, but the program will continue.  Those
50 bytes may have been important...

atomicio() is basically a while loop that repeatedly calls read() or
write() while advancng a pointer through the buffer, until either the
entire buffer is written or a hard error is encountered.  You can do the
same thing with a while loop inline, but it's much harder to read...

Darren Tucker (dtucker at
GPG key 8FF4FA69 / D9A3 86E9 7EEE AF4B B2D4  37C9 C982 80C7 8FF4 FA69
    Good judgement comes with experience. Unfortunately, the experience
usually comes from bad judgement.

Site Timeline