]> git.lyx.org Git - lyx.git/blob - src/support/copy.cpp
Patch from "hzluo" <memcache@gmail.com>:
[lyx.git] / src / support / copy.cpp
1 /**
2  * \file copy.cpp
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/FileName.h"
16 #include "support/lyxlib.h"
17
18 #ifdef HAVE_SYS_STAT_H
19 # include <sys/stat.h>
20 #endif
21 #ifdef HAVE_SYS_TYPES_H
22 # include <sys/types.h>
23 #endif
24
25
26 namespace lyx {
27
28
29 using std::ifstream;
30 using std::ofstream;
31 using std::ios;
32 using std::string;
33
34
35 bool lyx::support::chmod(FileName const & file, unsigned long int mode)
36 {
37 #if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
38         if (::chmod(file.toFilesystemEncoding().c_str(), mode_t(mode)) != 0)
39                 return false;
40 #else
41 # ifdef WITH_WARNINGS
42 #  warning "File permissions are ignored on this system."
43 # endif
44 #endif
45         return true;
46 }
47
48
49 bool lyx::support::copy(FileName const & from, FileName const & to, unsigned long int mode)
50 {
51         ifstream ifs(from.toFilesystemEncoding().c_str(), ios::binary | ios::in);
52         if (!ifs)
53                 return false;
54
55         if (mode != (unsigned long int)-1) {
56                 ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
57                 if (!ofs)
58                         return false;
59                 ofs.close();
60                 if (!support::chmod(to, mode))
61                         return false;
62         }
63
64         ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
65         if (!ofs)
66                 return false;
67
68         ofs << ifs.rdbuf();
69         return ofs.good();
70 }
71
72
73 } // namespace lyx