]> git.lyx.org Git - lyx.git/blob - src/support/tempname.cpp
add FileName::renameTo() method.
[lyx.git] / src / support / tempname.cpp
1 /**
2  * \file tempname.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 "support/lyxlib.h"
14
15 #include "support/convert.h"
16 #include "support/debug.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
19
20 #include <boost/scoped_array.hpp>
21
22 #include <cstdlib>
23
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27
28 #if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
29 extern "C" int mkstemp(char *);
30 #endif
31
32 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
33 # include <fcntl.h>
34 # ifdef HAVE_SYS_STAT_H
35 #  include <sys/stat.h>
36 # endif
37 # ifdef HAVE_IO_H
38 #  include <io.h>
39 # endif
40 # ifdef HAVE_PROCESS_H
41 #  include <process.h>
42 # endif
43 #endif
44
45 using namespace std;
46
47 using boost::scoped_array;
48
49 namespace lyx {
50 namespace support {
51
52 namespace {
53
54 inline
55 int make_tempfile(char * templ)
56 {
57 #if defined(HAVE_MKSTEMP)
58         return ::mkstemp(templ);
59 #elif defined(HAVE_MKTEMP)
60         // This probably just barely works...
61         ::mktemp(templ);
62 # if defined (HAVE_OPEN)
63 # if (!defined S_IRUSR)
64 #   define S_IRUSR S_IREAD
65 #   define S_IWUSR S_IWRITE
66 # endif
67         return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
68 # elif defined (HAVE__OPEN)
69         return ::_open(templ,
70                        _O_RDWR | _O_CREAT | _O_EXCL,
71                        _S_IREAD | _S_IWRITE);
72 # else
73 #  error No open() function.
74 # endif
75 #else
76 #error FIX FIX FIX
77 #endif
78 }
79
80 } // namespace anon
81
82
83 FileName const tempName(FileName const & dir, string const & mask)
84 {
85         string const tmpdir = dir.empty()
86                 ? package().temp_dir().absFilename() : dir.absFilename();
87         string tmpfl = to_filesystem8bit(from_utf8(addName(tmpdir, mask)));
88 #if defined (HAVE_GETPID)
89         tmpfl += convert<string>(getpid());
90 #elif defined (HAVE__GETPID)
91         tmpfl += convert<string>(_getpid());
92 #else
93 # error No getpid() function
94 #endif
95         tmpfl += "XXXXXX";
96
97         // The supposedly safe mkstemp version
98         scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
99         tmpfl.copy(tmpl.get(), string::npos);
100         tmpl[tmpfl.length()] = '\0'; // terminator
101
102         int const tmpf = make_tempfile(tmpl.get());
103         if (tmpf != -1) {
104                 string const t(to_utf8(from_filesystem8bit(tmpl.get())));
105 #if defined (HAVE_CLOSE)
106                 ::close(tmpf);
107 #elif defined (HAVE__CLOSE)
108                 ::_close(tmpf);
109 #else
110 # error No x() function.
111 #endif
112                 LYXERR(Debug::FILES, "Temporary file `" << t << "' created.");
113                 return FileName(t);
114         }
115         LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
116         return FileName();
117 }
118
119 } // namespace support
120 } // namespace lyx