7 #include <boost/filesystem/config.hpp>
8 #include <boost/filesystem/exception.hpp>
9 #include <boost/detail/workaround.hpp>
10 #include <boost/throw_exception.hpp>
12 #include <sys/types.h>
16 // BOOST_POSIX or BOOST_WINDOWS specify which API to use.
17 # if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
18 # if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
19 # define BOOST_WINDOWS
25 namespace fs = boost::filesystem;
28 namespace filesystem {
30 bool is_readable(path const & ph)
33 return ::access(ph.string().c_str(), R_OK) == 0;
38 bool is_writable(path const & ph)
41 return ::access(ph.string().c_str(), W_OK) == 0;
46 bool is_readonly(path const & ph)
49 return is_readable(ph) && !is_writable(ph);
54 void copy_file(path const & source, path const & target, bool noclobber)
58 int const infile = ::open(source.string().c_str(), O_RDONLY);
60 boost::throw_exception(
62 "boost::filesystem::copy_file",
64 fs::detail::system_error_code()));
67 struct stat source_stat;
68 int const ret = ::fstat(infile, &source_stat);
71 boost::throw_exception(
73 "boost::filesystem::copy_file",
75 fs::detail::system_error_code()));
78 int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
80 int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode);
83 boost::throw_exception(
85 "boost::filesystem::copy_file",
87 fs::detail::system_error_code()));
90 std::size_t const buf_sz = 32768;
96 in = ::read(infile, buf, buf_sz);
102 out = ::write(outfile, buf, in);
112 if (in == -1 || out == -1)
113 boost::throw_exception(
115 "boost::filesystem::copy_file",
117 fs::detail::system_error_code()));