The main loop of GNU cat, in the simplest case is (function
simple_cat
from cat.c
):
while (true)
{
/* Read a block of input. */
n_read = safe_read (input_desc, buf, bufsize);
/* ... */
}
Then the question becomes "how is bufsize
set?" The answer is it's
using io_blksize
(insize = io_blksize (stat_buf)
), which is
defined as follows:
io_blksize (struct stat sb)
{
return MAX (IO_BUFSIZE, ST_BLKSIZE (sb));
}
where ST_BLKSIZE gives the operating system's idea of the file
system's preferred I/O block size (as accessed using stat
), and
IO_BUFSIZE is defined as 128*1024 (128KB). Here is an excerpt of the
Linux stat
syscall documentation:
blksize_t st_blksize; /* blocksize for file system I/O */ (...)
The st_blksize field gives the "preferred" blocksize for efficient
file system I/O. (Writing to a file in smaller chunks may cause
an inefficient read-modify-rewrite.)
So it seems that GNU cat will read in blocks of 128KB or the file
system's recommended I/O block size, whichever is larger.
stat(2)
. – Andreas Wiese Nov 25 '15 at 21:05stat -f -c %s file
to find the block size of the file system where the input file is? Doesn't the block size of the file system where the output file is going matter? And what about copying e.g. /dev/zero to a raw block device? – EmmaV Nov 25 '15 at 23:37stat -f -c %s file
allows you to access that value from the command line. The output file system block size is taken into account, I had elided that part but will expand my answer (the largest of the output size, input size, and hard-coded value is used). I don't see that pseudo-files such as/dev/zero
are handled in a special way: GNU cat will also simply usestat
on those. – dhag Nov 26 '15 at 15:23