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/detail/workaround.hpp>
18 #include <boost/throw_exception.hpp>
20 #ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
23 #ifdef HAVE_SYS_STAT_H
24 # include <sys/stat.h>
28 // BOOST_POSIX or BOOST_WINDOWS specify which API to use.
29 # if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
30 # if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
31 # define BOOST_WINDOWS
37 #if defined (BOOST_WINDOWS)
38 # define WIN32_LEAN_AND_MEAN
42 namespace fs = boost::filesystem;
45 namespace filesystem {
47 bool is_readable(path const & ph)
50 return ::access(ph.string().c_str(), R_OK) == 0;
53 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
54 return attr != INVALID_FILE_ATTRIBUTES &&
55 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
60 bool is_writable(path const & ph)
63 return ::access(ph.string().c_str(), W_OK) == 0;
66 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
67 if (attr != INVALID_FILE_ATTRIBUTES &&
68 (attr & FILE_ATTRIBUTE_READONLY) != 0) {
69 // Read-only - no write access
72 return attr != INVALID_FILE_ATTRIBUTES &&
73 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
78 bool is_readonly(path const & ph)
81 return is_readable(ph) && !is_writable(ph);
84 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
85 return (attr != INVALID_FILE_ATTRIBUTES
86 && (attr & FILE_ATTRIBUTE_READONLY));
91 void copy_file(path const & source, path const & target, bool noclobber)
95 int const infile = ::open(source.string().c_str(), O_RDONLY);
97 boost::throw_exception(
98 filesystem_path_error(
99 "boost::filesystem::copy_file",
101 fs::lookup_errno(errno)));
104 struct stat source_stat;
105 int const ret = ::fstat(infile, &source_stat);
109 boost::throw_exception(
110 filesystem_path_error(
111 "boost::filesystem::copy_file",
113 fs::lookup_errno(err)));
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);
122 boost::throw_exception(
123 filesystem_path_error(
124 "boost::filesystem::copy_file",
126 fs::lookup_errno(err)));
129 std::size_t const buf_sz = 32768;
135 in = ::read(infile, buf, buf_sz);
138 } else if (in == 0) {
141 out = ::write(outfile, buf, in);
153 if (in == -1 || out == -1)
154 boost::throw_exception(
155 filesystem_path_error(
156 "boost::filesystem::copy_file",
158 fs::lookup_errno(err)));
161 if (::CopyFile(source.string().c_str(), target.string().c_str(), noclobber) == 0) {
162 // CopyFile is probably not setting errno so this is most
164 boost::throw_exception(
165 filesystem_path_error(
166 "boost::filesystem::copy_file",
168 fs::lookup_error_code(errno)));