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 #if defined (BOOST_WINDOWS)
26 # define WIN32_LEAN_AND_MEAN
30 namespace fs = boost::filesystem;
33 namespace filesystem {
35 bool is_readable(path const & ph)
38 return ::access(ph.string().c_str(), R_OK) == 0;
41 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
42 return attr != INVALID_FILE_ATTRIBUTES &&
43 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
48 bool is_writable(path const & ph)
51 return ::access(ph.string().c_str(), W_OK) == 0;
54 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
55 if (attr != INVALID_FILE_ATTRIBUTES &&
56 (attr & FILE_ATTRIBUTE_READONLY) != 0) {
57 // Read-only - no write access
60 return attr != INVALID_FILE_ATTRIBUTES &&
61 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
66 bool is_readonly(path const & ph)
69 return is_readable(ph) && !is_writable(ph);
72 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
73 return (attr != INVALID_FILE_ATTRIBUTES
74 && (attr & FILE_ATTRIBUTE_READONLY));
79 void copy_file(path const & source, path const & target, bool noclobber)
83 int const infile = ::open(source.string().c_str(), O_RDONLY);
85 boost::throw_exception(
87 "boost::filesystem::copy_file",
89 fs::detail::system_error_code()));
92 struct stat source_stat;
93 int const ret = ::fstat(infile, &source_stat);
96 boost::throw_exception(
98 "boost::filesystem::copy_file",
100 fs::detail::system_error_code()));
103 int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
105 int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode);
108 boost::throw_exception(
110 "boost::filesystem::copy_file",
112 fs::detail::system_error_code()));
115 std::size_t const buf_sz = 32768;
121 in = ::read(infile, buf, buf_sz);
124 } else if (in == 0) {
127 out = ::write(outfile, buf, in);
137 if (in == -1 || out == -1)
138 boost::throw_exception(
140 "boost::filesystem::copy_file",
142 fs::detail::system_error_code()));
145 if (::CopyFile(source.string().c_str(), target.string().c_str(), !noclobber) == 0) {
146 boost::throw_exception(
148 "boost::filesystem::copy_file",
150 fs::detail::system_error_code()));