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>
30 // BOOST_POSIX or BOOST_WINDOWS specify which API to use.
31 # if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
32 # if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
33 # define BOOST_WINDOWS
39 #if defined (BOOST_WINDOWS)
40 # define WIN32_LEAN_AND_MEAN
44 namespace fs = boost::filesystem;
47 namespace filesystem {
49 bool is_readable(path const & ph)
52 return ::access(ph.string().c_str(), R_OK) == 0;
55 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
56 return attr != INVALID_FILE_ATTRIBUTES &&
57 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
62 bool is_writable(path const & ph)
65 return ::access(ph.string().c_str(), W_OK) == 0;
68 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
69 if (attr != INVALID_FILE_ATTRIBUTES &&
70 (attr & FILE_ATTRIBUTE_READONLY) != 0) {
71 // Read-only - no write access
74 return attr != INVALID_FILE_ATTRIBUTES &&
75 (attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY;
80 bool is_readonly(path const & ph)
83 return is_readable(ph) && !is_writable(ph);
86 DWORD const attr = ::GetFileAttributes(ph.string().c_str());
87 return (attr != INVALID_FILE_ATTRIBUTES
88 && (attr & FILE_ATTRIBUTE_READONLY));
93 void copy_file(path const & source, path const & target, bool noclobber)
97 int const infile = ::open(source.string().c_str(), O_RDONLY);
99 boost::throw_exception(
100 filesystem_path_error(
101 "boost::filesystem::copy_file",
103 fs::lookup_errno(errno)));
106 struct stat source_stat;
107 int const ret = ::fstat(infile, &source_stat);
111 boost::throw_exception(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)));