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>
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(
99 filesystem_path_error(
100 "boost::filesystem::copy_file",
102 fs::lookup_errno(errno)));
105 struct stat source_stat;
106 int const ret = ::fstat(infile, &source_stat);
110 boost::throw_exception(
111 filesystem_path_error(
112 "boost::filesystem::copy_file",
114 fs::lookup_errno(err)));
117 int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
119 int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode);
123 boost::throw_exception(
124 filesystem_path_error(
125 "boost::filesystem::copy_file",
127 fs::lookup_errno(err)));
130 std::size_t const buf_sz = 32768;
136 in = ::read(infile, buf, buf_sz);
139 } else if (in == 0) {
142 out = ::write(outfile, buf, in);
154 if (in == -1 || out == -1)
155 boost::throw_exception(
156 filesystem_path_error(
157 "boost::filesystem::copy_file",
159 fs::lookup_errno(err)));
162 if (::CopyFile(source.string().c_str(), target.string().c_str(), noclobber) == 0) {
163 // CopyFile is probably not setting errno so this is most
165 boost::throw_exception(
166 filesystem_path_error(
167 "boost::filesystem::copy_file",
169 fs::lookup_error_code(errno)));