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;
36 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
37 return attr != INVALID_FILE_ATTRIBUTES &&
38 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
43 bool is_writable(path const & ph)
46 return ::access(ph.string().c_str(), W_OK) == 0;
49 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
50 if (attr != INVALID_FILE_ATTRIBUTES &&
51 (attr & FILE_ATTRIBUTE_READONLY) != 0) {
52 // Read-only - no write access
55 return attr != INVALID_FILE_ATTRIBUTES &&
56 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
61 bool is_readonly(path const & ph)
64 return is_readable(ph) && !is_writable(ph);
67 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
68 return (attr != INVALID_FILE_ATTRIBUTES
69 && (attr & FILE_ATTRIBUTE_READONLY));
74 void copy_file(path const & source, path const & target, bool noclobber)
78 int const infile = ::open(source.string().c_str(), O_RDONLY);
80 boost::throw_exception(
82 "boost::filesystem::copy_file",
84 fs::detail::system_error_code()));
87 struct stat source_stat;
88 int const ret = ::fstat(infile, &source_stat);
91 boost::throw_exception(
93 "boost::filesystem::copy_file",
95 fs::detail::system_error_code()));
98 int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
100 int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode);
103 boost::throw_exception(
105 "boost::filesystem::copy_file",
107 fs::detail::system_error_code()));
110 std::size_t const buf_sz = 32768;
116 in = ::read(infile, buf, buf_sz);
119 } else if (in == 0) {
122 out = ::write(outfile, buf, in);
132 if (in == -1 || out == -1)
133 boost::throw_exception(
135 "boost::filesystem::copy_file",
137 fs::detail::system_error_code()));
140 if (::CopyFile(source.string().c_str(), target.string().c_str(), !noclobber) == 0) {
141 boost::throw_exception(
143 "boost::filesystem::copy_file",
145 fs::detail::system_error_code()));