Language/C&C++
[c/c++] file copy using 'sendfile' on linux.
yhcting
2013. 12. 17. 08:23
Since Linux 2.6.33, sendfile supports regular file as output file.
That is, sendfile can be used as way for fast file copy.
(Faster than normal read -> write operation because it is done inside kernel! )
Please refer follow example code ...
...
#include <stdio.h> #include <sys/sendfile.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argc, const char *argv[]) { int ifd, ofd, r; struct stat st; r = stat(argv[2], &st); r |= (ofd = open(argv[1], O_WRONLY | O_CREAT)); r |= (ifd = open(argv[2], O_RDONLY)); if (r < 0) return 1; if (-1 == sendfile(ofd, ifd, 0, st.st_size)) return 1; fchmod(ofd, 0664); return 0; }
...