woc
cat woc.c
#include <linux/fs.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
int main () {
int sfd, dfd;
sfd = open("./a", O_RDONLY);
if (sfd < 0) printf("open sfile error, errno=%d\n", errno);
dfd = open("./b", O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (dfd < 0) printf("open dfile error, errno=%d\n", errno);
//
//// int r = ioctl(dfd, FICLONE, sfd);
// if (r < 0) {
// printf("ioctl error, errno=%d\n desc:%s", errno, strerror(errno));
// }
struct stat st;
if( fstat(sfd, &st) == -1 ) {
perror("fstat");
return -1;
}
int len = st.st_size;
printf("a len=%d\n", len);
int ret = 0;
do{
ret = copy_file_range(sfd, NULL, dfd, NULL, len, 0);
if (ret < 0) {
printf("coyp_file_range return less 0, ret= %d\n", ret);
printf("copy_file_range error, errno=%d\n desc:%s\n", errno, strerror(errno));
return -1;
}
len -=ret;
} while(len>0 && ret >0);
close(sfd);
close(dfd);
sfd = 0;
sfd = open("./a", O_WRONLY|O_SYNC);
if (ret = ftruncate(sfd, 0) <0) {
printf("truncate error.ret=%d\n", ret);
printf("truncate error, errno=%d\n desc:%s\n", errno, strerror(errno));
}
write(sfd, "aaaaaaaaaaa\n", 12);
close(sfd);
return 0;
}
======================before=======================
~ ll
total 66G
-rw-r--r-- 1 tong tong 550M Oct 22 10:51 a
-rwxrwxr-x 1 tong tong 17K Oct 22 10:51 a.out
drwxrwxr-x 2 tong tong 4.0K Jul 20 21:13 AppImages
==================================================
gcc ./woc.c
./a.out
=======================after=======================
~ ll
total 66G
-rw-r--r-- 1 tong tong 12 Oct 22 10:51 a
-rwxrwxr-x 1 tong tong 17K Oct 22 10:51 a.out
drwxrwxr-x 2 tong tong 4.0K Jul 20 21:13 AppImages
-rw-r--r-- 1 tong tong 550M Oct 22 10:51 b
==================================================
浙公网安备 33010602011771号