#include
#include
#include /* for memcpy */
#include
#include
#include
#include
#include
#include
#define PERMS 0600
int main ( int argc, char * argv[] )
{
int src, dst;
void * sm, * dm;
struct stat statbuf;
if ( argc != 3 )
{
fprintf( stderr, " Usage: %s \n " , argv[ 0 ] );
exit( EXIT_FAILURE );
}
if ( ( src = open( argv[ 1 ], O_RDONLY ) ) < 0 )
{
perror( " open source " );
exit( EXIT_FAILURE );
}
/* 为了完成复制,必须包含读打开,否则mmap()失败 */
if ( ( dst = open( argv[ 2 ], O_RDWR | O_CREAT | O_TRUNC, PERMS ) ) < 0 )
{
perror( " open target " );
exit( EXIT_FAILURE );
}
if ( fstat( src, & statbuf ) < 0 )
{
perror( " fstat source " );
exit( EXIT_FAILURE );
}
/*
* 参看前面man手册中的说明,mmap()不能用于扩展文件长度。所以这里必须事
* 先扩大目标文件长度,准备一个空架子等待复制。
*/
if ( lseek( dst, statbuf.st_size - 1 , SEEK_SET ) < 0 )
{
perror( " lseek target " );
exit( EXIT_FAILURE );
}
if ( write( dst, "a", 1 ) != 1 )
{
perror( " write target " );
exit( EXIT_FAILURE );
}
/* 读的时候指定 MAP_PRIVATE 即可 */
sm = mmap( 0 , ( size_t )statbuf.st_size, PROT_READ,
MAP_PRIVATE | MAP_NORESERVE, src, 0 );
if ( MAP_FAILED == sm )
{
perror( " mmap source " );
exit( EXIT_FAILURE );
}
/* 这里必须指定 MAP_SHARED 才可能真正改变静态文件 */
dm = mmap( 0 , ( size_t )statbuf.st_size, PROT_WRITE,
MAP_SHARED, dst, 0 );
if ( MAP_FAILED == dm )
{
perror( " mmap target " );
exit( EXIT_FAILURE );
}
memcpy( dm, sm, ( size_t )statbuf.st_size );
/*
* 可以不要这行代码
*
* msync( dm, ( size_t )statbuf.st_size, MS_SYNC );
*/
return ( EXIT_SUCCESS );
}
上面是一个复制文件的C程序,请问其中的代码
if ( write( dst, "a", 1 ) != 1 )
{
perror( " write target " );
exit( EXIT_FAILURE );
}
写入的a为什么在复制的的文件里找不到???