]> git.lyx.org Git - lyx.git/blob - src/support/copy.C
Detect mode_t for safe use of chmod, and for scons/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
25 namespace lyx {
26
27
28 using std::ifstream;
29 using std::ofstream;
30 using std::ios;
31 using std::string;
32
33
34 bool lyx::support::chmod(string const & file, unsigned long int mode)
35 {
36 #if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
37         if (::chmod(file.c_str(), mode_t(mode)) != 0)
38                 return false;
39 #else
40 # ifdef WITH_WARNINGS
41 #  warning "File permissions are ignored on this system."
42 # endif
43 #endif
44         return true;
45 }
46
47
48 bool lyx::support::copy(string const & from, string const & to, unsigned long int mode)
49 {
50         ifstream ifs(from.c_str(), ios::binary | ios::in);
51         if (!ifs)
52                 return false;
53
54         if (mode != (unsigned long int)-1) {
55                 ofstream ofs(to.c_str(), ios::binary | ios::out | ios::trunc);
56                 if (!ofs)
57                         return false;
58                 ofs.close();
59                 if (!support::chmod(to, mode))
60                         return false;
61         }
62
63         ofstream ofs(to.c_str(), ios::binary | ios::out | ios::trunc);
64         if (!ofs)
65                 return false;
66
67         ofs << ifs.rdbuf();
68         return ofs.good();
69 }
70
71
72 } // namespace lyx