]> git.lyx.org Git - lyx.git/blob - src/support/copy.C
chmod fixes for msvc
[lyx.git] / src / support / copy.C
1 /**
2  * \file copy.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include <fstream>
14
15 #include "support/lyxlib.h"
16
17 #ifdef HAVE_SYS_STAT_H
18 # include <sys/stat.h>
19 #endif
20 #ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
22 #endif
23
24 #if defined(HAVE_CHMOD) && defined(_MSC_VER)
25 #include <io.h>
26 #endif
27
28 namespace lyx {
29
30
31 using std::ifstream;
32 using std::ofstream;
33 using std::ios;
34 using std::string;
35
36
37 bool lyx::support::chmod(string const & file, unsigned long int mode)
38 {
39 #ifdef HAVE_CHMOD
40         if (::chmod(file.c_str(), mode_t(mode)) != 0)
41                 return false;
42 #else
43 # ifdef WITH_WARNINGS
44 #  warning "File permissions are ignored on this system."
45 # endif
46 #endif
47         return true;
48 }
49
50
51 bool lyx::support::copy(string const & from, string const & to, unsigned long int mode)
52 {
53         ifstream ifs(from.c_str(), ios::binary | ios::in);
54         if (!ifs)
55                 return false;
56
57         if (mode != (unsigned long int)-1) {
58                 ofstream ofs(to.c_str(), ios::binary | ios::out | ios::trunc);
59                 if (!ofs)
60                         return false;
61                 ofs.close();
62                 if (!chmod(to, mode_t(mode)))
63                         return false;
64         }
65
66         ofstream ofs(to.c_str(), ios::binary | ios::out | ios::trunc);
67         if (!ofs)
68                 return false;
69
70         ofs << ifs.rdbuf();
71         return ofs.good();
72 }
73
74
75 } // namespace lyx