FileCopy
파일 복사에 대한 내용을 정리한다.
PageSize vs BlockSize vs MemoryMap
Boost filesystem version 3 copy_file()
아래의 코드는, libs/filesystem/src/operations.cpp
에 위치한 BOOST_POSIX_API
가 정의된 경우, copy_file()
에서 사용되는 함수 구현부 이다.
bool // true if ok
copy_file_api(const std::string& from_p,
const std::string& to_p, bool fail_if_exists)
{
const std::size_t buf_sz = 32768;
boost::scoped_array<char> buf(new char [buf_sz]);
int infile=-1, outfile=-1; // -1 means not open
// bug fixed: code previously did a stat()on the from_file first, but that
// introduced a gratuitous race condition; the stat()is now done after the open()
if ((infile = ::open(from_p.c_str(), O_RDONLY))< 0)
{ return false; }
struct stat from_stat;
if (::stat(from_p.c_str(), &from_stat)!= 0)
{
::close(infile);
return false;
}
int oflag = O_CREAT | O_WRONLY | O_TRUNC;
if (fail_if_exists)
oflag |= O_EXCL;
if ((outfile = ::open(to_p.c_str(), oflag, from_stat.st_mode))< 0)
{
int open_errno = errno;
BOOST_ASSERT(infile >= 0);
::close(infile);
errno = open_errno;
return false;
}
ssize_t sz, sz_read=1, sz_write;
while (sz_read > 0
&& (sz_read = ::read(infile, buf.get(), buf_sz)) > 0)
{
// Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
// Marc Rochkind, Addison-Wesley, 2004, page 94
sz_write = 0;
do
{
BOOST_ASSERT(sz_read - sz_write > 0); // #1
// ticket 4438 claimed possible infinite loop if write returns 0. My analysis
// is that POSIX specifies 0 return only if 3rd arg is 0, and that will never
// happen due to loop entry and coninuation conditions. BOOST_ASSERT #1 above
// and #2 below added to verify that analysis.
if ((sz = ::write(outfile, buf.get() + sz_write,
sz_read - sz_write)) < 0)
{
sz_read = sz; // cause read loop termination
break; // and error reported after closes
}
BOOST_ASSERT(sz > 0); // #2
sz_write += sz;
} while (sz_write < sz_read);
}
if (::close(infile)< 0)
sz_read = -1;
if (::close(outfile)< 0)
sz_read = -1;
return sz_read >= 0;
}
See also
Favorite site
- [추천] Stackoverflow: Copy a file in a sane, safe and efficient way 1
- Stackoverflow: Most efficient way to copy a file in Linux
- Stackoverflow: Fastest way to copy data from one file to another in C/C++?
- Stackoverflow: Tried and true simple file copying code in C?
References
-
Copy_a_file_in_a_sane,safe_and_efficient_way-_Stack_Overflow.pdf ↩