3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
14 #include "fs_extras.h"
16 #include <boost/filesystem/config.hpp>
17 #include <boost/filesystem/exception.hpp>
18 #include <boost/detail/workaround.hpp>
19 #include <boost/throw_exception.hpp>
21 #ifdef HAVE_SYS_TYPES_H
22 # include <sys/types.h>
24 #ifdef HAVE_SYS_STAT_H
25 # include <sys/stat.h>
29 // BOOST_POSIX or BOOST_WINDOWS specify which API to use.
30 # if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
31 # if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
32 # define BOOST_WINDOWS
38 #if defined (BOOST_WINDOWS)
39 # define WIN32_LEAN_AND_MEAN
43 namespace fs = boost::filesystem;
46 namespace filesystem {
48 bool is_readable(path const & ph)
51 return ::access(ph.string().c_str(), R_OK) == 0;
54 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
55 return attr != INVALID_FILE_ATTRIBUTES &&
56 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
61 bool is_writable(path const & ph)
64 return ::access(ph.string().c_str(), W_OK) == 0;
67 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
68 if (attr != INVALID_FILE_ATTRIBUTES &&
69 (attr & FILE_ATTRIBUTE_READONLY) != 0) {
70 // Read-only - no write access
73 return attr != INVALID_FILE_ATTRIBUTES &&
74 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
79 bool is_readonly(path const & ph)
82 return is_readable(ph) && !is_writable(ph);
85 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
86 return (attr != INVALID_FILE_ATTRIBUTES
87 && (attr & FILE_ATTRIBUTE_READONLY));
92 void copy_file(path const & source, path const & target, bool noclobber)
96 int const infile = ::open(source.string().c_str(), O_RDONLY);
98 boost::throw_exception(
100 "boost::filesystem::copy_file",
102 fs::detail::system_error_code()));
105 struct stat source_stat;
106 int const ret = ::fstat(infile, &source_stat);
109 boost::throw_exception(
111 "boost::filesystem::copy_file",
113 fs::detail::system_error_code()));
116 int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
118 int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode);
121 boost::throw_exception(
123 "boost::filesystem::copy_file",
125 fs::detail::system_error_code()));
128 std::size_t const buf_sz = 32768;
134 in = ::read(infile, buf, buf_sz);
137 } else if (in == 0) {
140 out = ::write(outfile, buf, in);
150 if (in == -1 || out == -1)
151 boost::throw_exception(
153 "boost::filesystem::copy_file",
155 fs::detail::system_error_code()));
158 if (::CopyFile(source.string().c_str(), target.string().c_str(), noclobber) == 0) {
159 boost::throw_exception(
161 "boost::filesystem::copy_file",
163 fs::detail::system_error_code()));