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