]> git.lyx.org Git - lyx.git/blob - src/support/fs_extras.C
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[lyx.git] / src / support / fs_extras.C
1 // -*- C++ -*-
2
3 #include <config.h>
4
5 #include "fs_extras.h"
6
7 #include <boost/filesystem/config.hpp>
8 #include <boost/filesystem/exception.hpp>
9 #include <boost/detail/workaround.hpp>
10 #include <boost/throw_exception.hpp>
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15
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
20 #   else
21 #     define BOOST_POSIX
22 #   endif
23 # endif
24
25 namespace fs = boost::filesystem;
26
27 namespace boost {
28 namespace filesystem {
29
30 bool is_readable(path const & ph)
31 {
32 #ifdef BOOST_POSIX
33         return ::access(ph.string().c_str(), R_OK) == 0;
34 #endif
35 }
36
37
38 bool is_writable(path const & ph)
39 {
40 #ifdef BOOST_POSIX
41         return ::access(ph.string().c_str(), W_OK) == 0;
42 #endif
43 }
44
45
46 bool is_readonly(path const & ph)
47 {
48 #ifdef BOOST_POSIX
49         return is_readable(ph) && !is_writable(ph);
50 #endif
51 }
52
53
54 void copy_file(path const & source, path const & target, bool noclobber)
55 {
56
57 #ifdef BOOST_POSIX
58         int const infile = ::open(source.string().c_str(), O_RDONLY);
59         if (infile == -1) {
60                 boost::throw_exception(
61                         filesystem_error(
62                                 "boost::filesystem::copy_file",
63                                 source, target,
64                                 fs::detail::system_error_code()));
65         }
66
67         struct stat source_stat;
68         int const ret = ::fstat(infile, &source_stat);
69         if (ret == -1) {
70                 ::close(infile);
71                 boost::throw_exception(
72                         filesystem_error(
73                                 "boost::filesystem::copy_file",
74                                 source, target,
75                                 fs::detail::system_error_code()));
76         }
77
78         int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
79
80         int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode);
81         if (outfile == -1) {
82                 ::close(infile);
83                 boost::throw_exception(
84                         filesystem_error(
85                                 "boost::filesystem::copy_file",
86                                 source, target,
87                                 fs::detail::system_error_code()));
88         }
89
90         std::size_t const buf_sz = 32768;
91         char buf[buf_sz];
92         ssize_t in = -1;
93         ssize_t out = -1;
94
95         while (true) {
96                 in = ::read(infile, buf, buf_sz);
97                 if (in == -1) {
98                         break;
99                 } else if (in == 0) {
100                         break;
101                 } else {
102                         out = ::write(outfile, buf, in);
103                         if (out == -1) {
104                                 break;
105                         }
106                 }
107         }
108
109         ::close(infile);
110         ::close(outfile);
111
112         if (in == -1 || out == -1)
113                 boost::throw_exception(
114                         filesystem_error(
115                                 "boost::filesystem::copy_file",
116                                 source, target,
117                                 fs::detail::system_error_code()));
118 #endif
119 }
120
121
122 }
123 }