1、CP部分源代码:
if (*new_dst)
dest_desc = open (dst_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
dst_mode & ~omitted_permissions);
else
omitted_permissions = 0;
if (dest_desc < 0)
{
error (0, errno, _("cannot create regular file %s"), quote (dst_name));
return_val = false;
goto close_src_desc;
}
if (fstat (dest_desc, &sb) != 0)
{
error (0, errno, _("cannot fstat %s"), quote (dst_name));
return_val = false;
goto close_src_and_dst_desc;
}
if (! (S_ISREG (src_open_sb.st_mode) && src_open_sb.st_size == 0))
{
typedef uintptr_t word;
off_t n_read_total = 0;
/* Choose a suitable buffer size; it may be adjusted later. */
size_t buf_alignment = lcm (getpagesize (), sizeof (word));
size_t buf_alignment_slop = sizeof (word) + buf_alignment - 1;
size_t buf_size = ST_BLKSIZE (sb);
/* Deal with sparse files. */
bool last_write_made_hole = false;
bool make_holes = false;
if (S_ISREG (sb.st_mode))
{
/* Even with --sparse=always, try to create holes only
if the destination is a regular file. */
if (x->sparse_mode == SPARSE_ALWAYS)
make_holes = true;
#if HAVE_STRUCT_STAT_ST_BLOCKS
/* Use a heuristic to determine whether SRC_NAME contains any sparse
blocks. If the file has fewer blocks than would normally be
needed for a file of its size, then at least one of the blocks in
the file is a hole. */
if (x->sparse_mode == SPARSE_AUTO && S_ISREG (src_open_sb.st_mode)
&& ST_NBLOCKS (src_open_sb) < src_open_sb.st_size / ST_NBLOCKSIZE)
make_holes = true;
#endif
}
/* If not making a sparse file, try to use a more-efficient
buffer size. */
if (! make_holes)
{
/* These days there's no point ever messing with buffers smaller
than 8 KiB. It would be nice to configure SMALL_BUF_SIZE
dynamically for this host and pair of files, but there doesn't
seem to be a good way to get readahead info portably. */
enum { SMALL_BUF_SIZE = 8 * 1024 };
/* Compute the least common multiple of the input and output
buffer sizes, adjusting for outlandish values. */
size_t blcm_max = MIN (SIZE_MAX, SSIZE_MAX) - buf_alignment_slop;
size_t blcm = buffer_lcm (ST_BLKSIZE (src_open_sb), buf_size,
blcm_max);
/* Do not use a block size that is too small. */
buf_size = MAX (SMALL_BUF_SIZE, blcm);
/* Do not bother with a buffer larger than the input file, plus one
byte to make sure the file has not grown while reading it. */
if (S_ISREG (src_open_sb.st_mode) && src_open_sb.st_size < buf_size)
buf_size = src_open_sb.st_size + 1;
/* However, stick with a block size that is a positive multiple of
blcm, overriding the above adjustments. Watch out for
overflow. */
buf_size += blcm - 1;
buf_size -= buf_size % blcm;
if (buf_size == 0 || blcm_max < buf_size)
buf_size = blcm;
}
/* Make a buffer with space for a sentinel at the end. */
buf_alloc = xmalloc (buf_size + buf_alignment_slop);
buf = ptr_align (buf_alloc, buf_alignment);
for (;;)
{
word *wp = NULL;
ssize_t n_read = read (source_desc, buf, buf_size);
if (n_read < 0)
{
#ifdef EINTR
2、copy的实质:
4.2 实质就是调用open,read,write的系统库函数访问文件,每次IO请求的块大小为4K.当copy的文件很大的时候,dd和cp的性能是一样的。