]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: file.cpp | |
3 | // Purpose: wxFile - encapsulates low-level "file descriptor" | |
4 | // wxTempFile | |
5 | // Author: Vadim Zeitlin | |
6 | // Modified by: | |
7 | // Created: 29/01/98 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
246037e2 | 10 | // Licence: wxWindows license |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
17 | #ifdef __GNUG__ | |
3f4a0c5b | 18 | #pragma implementation "file.h" |
c801d85f KB |
19 | #endif |
20 | ||
21 | // For compilers that support precompilation, includes "wx.h". | |
22 | #include "wx/wxprec.h" | |
c801d85f KB |
23 | |
24 | #ifdef __BORLANDC__ | |
ce4169a4 | 25 | #pragma hdrstop |
c801d85f KB |
26 | #endif |
27 | ||
ce4169a4 RR |
28 | #if wxUSE_FILE |
29 | ||
c801d85f | 30 | // standard |
5ea105e0 | 31 | #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__) |
c801d85f | 32 | #include <io.h> |
30a5be97 | 33 | |
a3ef5bf5 | 34 | #ifndef __SALFORDC__ |
49d5d881 VZ |
35 | #define WIN32_LEAN_AND_MEAN |
36 | #define NOSERVICE | |
37 | #define NOIME | |
38 | #define NOATOM | |
39 | #define NOGDI | |
40 | #define NOGDICAPMASKS | |
41 | #define NOMETAFILE | |
42 | #define NOMINMAX | |
43 | #define NOMSG | |
44 | #define NOOPENFILE | |
45 | #define NORASTEROPS | |
46 | #define NOSCROLL | |
47 | #define NOSOUND | |
48 | #define NOSYSMETRICS | |
49 | #define NOTEXTMETRIC | |
50 | #define NOWH | |
51 | #define NOCOMM | |
52 | #define NOKANJI | |
53 | #define NOCRYPT | |
54 | #define NOMCX | |
a3ef5bf5 JS |
55 | #endif |
56 | ||
49d5d881 | 57 | #include <windows.h> // for GetTempFileName |
c801d85f | 58 | #elif (defined(__UNIX__) || defined(__GNUWIN32__)) |
3f4a0c5b VZ |
59 | #include <unistd.h> |
60 | #ifdef __GNUWIN32__ | |
61 | #include <windows.h> | |
62 | #endif | |
c2ff79b1 DW |
63 | #elif (defined(__WXPM__)) |
64 | #include <io.h> | |
65 | #include <direct.h> | |
66 | #define W_OK 2 | |
67 | #define R_OK 4 | |
34138703 | 68 | #elif (defined(__WXSTUBS__)) |
3f4a0c5b VZ |
69 | // Have to ifdef this for different environments |
70 | #include <io.h> | |
17dff81c | 71 | #elif (defined(__WXMAC__)) |
3f4a0c5b VZ |
72 | int access( const char *path, int mode ) { return 0 ; } |
73 | char* mktemp( char * path ) { return path ;} | |
74 | #include <unistd.h> | |
75 | #include <unix.h> | |
76 | #define W_OK 2 | |
77 | #define R_OK 4 | |
c801d85f | 78 | #else |
3f4a0c5b | 79 | #error "Please specify the header with file functions declarations." |
c801d85f KB |
80 | #endif //Win/UNIX |
81 | ||
82 | #include <stdio.h> // SEEK_xxx constants | |
83 | #include <fcntl.h> // O_RDONLY &c | |
a3ef5bf5 | 84 | |
469e1e5c | 85 | #ifndef __MWERKS__ |
3f4a0c5b VZ |
86 | #include <sys/types.h> // needed for stat |
87 | #include <sys/stat.h> // stat | |
469e1e5c | 88 | #endif |
c801d85f KB |
89 | |
90 | // Microsoft compiler loves underscores, feed them to it | |
3f4a0c5b | 91 | #ifdef __VISUALC__ |
49d5d881 VZ |
92 | // functions |
93 | #define open _open | |
94 | #define close _close | |
95 | #define read _read | |
96 | #define write _write | |
97 | #define lseek _lseek | |
98 | #define fsync _commit | |
99 | #define access _access | |
100 | #define eof _eof | |
101 | ||
102 | // types | |
103 | #define stat _stat | |
104 | ||
105 | // constants | |
106 | ||
107 | #define O_RDONLY _O_RDONLY | |
108 | #define O_WRONLY _O_WRONLY | |
109 | #define O_RDWR _O_RDWR | |
110 | #define O_EXCL _O_EXCL | |
111 | #define O_CREAT _O_CREAT | |
112 | #define O_BINARY _O_BINARY | |
113 | ||
114 | #define S_IFDIR _S_IFDIR | |
115 | #define S_IFREG _S_IFREG | |
c801d85f | 116 | #else |
49d5d881 | 117 | #define tell(fd) lseek(fd, 0, SEEK_CUR) |
3f4a0c5b | 118 | #endif // VC++ |
c801d85f | 119 | |
3f4a0c5b | 120 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
49d5d881 VZ |
121 | #define W_OK 2 |
122 | #define R_OK 4 | |
34138703 JS |
123 | #endif |
124 | ||
c801d85f | 125 | // there is no distinction between text and binary files under Unix |
246037e2 | 126 | #ifdef __UNIX__ |
49d5d881 | 127 | #define O_BINARY (0) |
246037e2 | 128 | #endif //__UNIX__ |
c801d85f | 129 | |
a3ef5bf5 | 130 | #ifdef __SALFORDC__ |
3f4a0c5b | 131 | #include <unix.h> |
a3ef5bf5 JS |
132 | #endif |
133 | ||
c801d85f KB |
134 | // wxWindows |
135 | #include <wx/string.h> | |
136 | #include <wx/intl.h> | |
137 | #include <wx/file.h> | |
138 | #include <wx/log.h> | |
139 | ||
81d66cf3 | 140 | #ifndef MAX_PATH |
3f4a0c5b | 141 | #define MAX_PATH 512 |
81d66cf3 | 142 | #endif |
c801d85f | 143 | |
169935ad | 144 | #ifdef __WXMAC__ |
3f4a0c5b VZ |
145 | char gwxMacFileName[ MAX_PATH ] ; |
146 | char gwxMacFileName2[ MAX_PATH ] ; | |
147 | char gwxMacFileName3[ MAX_PATH ] ; | |
169935ad SC |
148 | #endif |
149 | ||
49d5d881 VZ |
150 | // some broken compilers don't have 3rd argument in open() and creat() |
151 | #ifdef __SALFORDC__ | |
152 | #define ACCESS(access) | |
153 | #define stat _stat | |
154 | #else // normal compiler | |
155 | #define ACCESS(access) , (access) | |
156 | #endif // Salford C | |
157 | ||
c801d85f KB |
158 | // ============================================================================ |
159 | // implementation of wxFile | |
160 | // ============================================================================ | |
161 | ||
162 | // ---------------------------------------------------------------------------- | |
163 | // static functions | |
164 | // ---------------------------------------------------------------------------- | |
50920146 | 165 | bool wxFile::Exists(const wxChar *name) |
246037e2 | 166 | { |
49d5d881 | 167 | struct stat st; |
50920146 | 168 | #if wxUSE_UNICODE && wxMBFILES |
dcf924a3 | 169 | wxCharBuffer fname = wxConvFile.cWC2MB(name); |
a3ef5bf5 | 170 | |
50920146 OK |
171 | return !access(fname, 0) && |
172 | !stat(MBSTRINGCAST fname, &st) && | |
173 | (st.st_mode & S_IFREG); | |
174 | #else | |
49d5d881 | 175 | return !access(name, 0) && |
50920146 | 176 | !stat((wxChar*) name, &st) && |
49d5d881 | 177 | (st.st_mode & S_IFREG); |
50920146 | 178 | #endif |
d1427b70 VZ |
179 | } |
180 | ||
50920146 | 181 | bool wxFile::Access(const wxChar *name, OpenMode mode) |
d1427b70 | 182 | { |
49d5d881 | 183 | int how = 0; |
d1427b70 | 184 | |
49d5d881 VZ |
185 | switch ( mode ) { |
186 | case read: | |
187 | how = R_OK; | |
188 | break; | |
d1427b70 | 189 | |
49d5d881 VZ |
190 | case write: |
191 | how = W_OK; | |
192 | break; | |
d1427b70 | 193 | |
49d5d881 | 194 | default: |
50920146 | 195 | wxFAIL_MSG(_T("bad wxFile::Access mode parameter.")); |
49d5d881 | 196 | } |
d1427b70 | 197 | |
50920146 | 198 | return access(wxFNCONV(name), how) == 0; |
c801d85f KB |
199 | } |
200 | ||
201 | // ---------------------------------------------------------------------------- | |
202 | // opening/closing | |
203 | // ---------------------------------------------------------------------------- | |
204 | ||
205 | // ctors | |
50920146 | 206 | wxFile::wxFile(const wxChar *szFileName, OpenMode mode) |
c801d85f | 207 | { |
49d5d881 VZ |
208 | m_fd = fd_invalid; |
209 | m_error = FALSE; | |
c801d85f | 210 | |
49d5d881 | 211 | Open(szFileName, mode); |
c801d85f KB |
212 | } |
213 | ||
c801d85f | 214 | // create the file, fail if it already exists and bOverwrite |
50920146 | 215 | bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode) |
c801d85f | 216 | { |
49d5d881 VZ |
217 | // if bOverwrite we create a new file or truncate the existing one, |
218 | // otherwise we only create the new file and fail if it already exists | |
50920146 | 219 | int fd = open(wxFNCONV(szFileName), |
49d5d881 VZ |
220 | O_WRONLY | O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL) |
221 | ACCESS(accessMode)); | |
222 | ||
223 | if ( fd == -1 ) { | |
224 | wxLogSysError(_("can't create file '%s'"), szFileName); | |
225 | return FALSE; | |
226 | } | |
227 | else { | |
228 | Attach(fd); | |
229 | return TRUE; | |
230 | } | |
c801d85f KB |
231 | } |
232 | ||
233 | // open the file | |
50920146 | 234 | bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode) |
c801d85f | 235 | { |
49d5d881 | 236 | int flags = O_BINARY; |
c801d85f | 237 | |
49d5d881 VZ |
238 | switch ( mode ) { |
239 | case read: | |
240 | flags |= O_RDONLY; | |
241 | break; | |
c801d85f | 242 | |
49d5d881 VZ |
243 | case write: |
244 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
245 | break; | |
61b02744 | 246 | |
49d5d881 VZ |
247 | case write_append: |
248 | flags |= O_WRONLY | O_APPEND; | |
249 | break; | |
c801d85f | 250 | |
49d5d881 VZ |
251 | case read_write: |
252 | flags |= O_RDWR; | |
253 | break; | |
254 | } | |
c801d85f | 255 | |
50920146 | 256 | int fd = open(wxFNCONV(szFileName), flags ACCESS(accessMode)); |
c801d85f | 257 | |
49d5d881 VZ |
258 | if ( fd == -1 ) { |
259 | wxLogSysError(_("can't open file '%s'"), szFileName); | |
260 | return FALSE; | |
261 | } | |
262 | else { | |
263 | Attach(fd); | |
264 | return TRUE; | |
265 | } | |
c801d85f KB |
266 | } |
267 | ||
268 | // close | |
61b02744 | 269 | bool wxFile::Close() |
c801d85f | 270 | { |
49d5d881 VZ |
271 | if ( IsOpened() ) { |
272 | if ( close(m_fd) == -1 ) { | |
273 | wxLogSysError(_("can't close file descriptor %d"), m_fd); | |
274 | m_fd = fd_invalid; | |
275 | return FALSE; | |
276 | } | |
277 | else | |
278 | m_fd = fd_invalid; | |
61b02744 | 279 | } |
61b02744 | 280 | |
49d5d881 | 281 | return TRUE; |
c801d85f KB |
282 | } |
283 | ||
284 | // ---------------------------------------------------------------------------- | |
285 | // read/write | |
286 | // ---------------------------------------------------------------------------- | |
287 | ||
288 | // read | |
289 | off_t wxFile::Read(void *pBuf, off_t nCount) | |
290 | { | |
49d5d881 | 291 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 292 | |
469e1e5c | 293 | #ifdef __MWERKS__ |
49d5d881 | 294 | int iRc = ::read(m_fd, (char*) pBuf, nCount); |
469e1e5c | 295 | #else |
49d5d881 | 296 | int iRc = ::read(m_fd, pBuf, nCount); |
469e1e5c | 297 | #endif |
49d5d881 VZ |
298 | if ( iRc == -1 ) { |
299 | wxLogSysError(_("can't read from file descriptor %d"), m_fd); | |
300 | return wxInvalidOffset; | |
301 | } | |
302 | else | |
303 | return (size_t)iRc; | |
c801d85f KB |
304 | } |
305 | ||
306 | // write | |
c86f1403 | 307 | size_t wxFile::Write(const void *pBuf, size_t nCount) |
c801d85f | 308 | { |
49d5d881 | 309 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 310 | |
469e1e5c | 311 | #ifdef __MWERKS__ |
49d5d881 | 312 | int iRc = ::write(m_fd, (const char*) pBuf, nCount); |
469e1e5c | 313 | #else |
49d5d881 | 314 | int iRc = ::write(m_fd, pBuf, nCount); |
469e1e5c | 315 | #endif |
49d5d881 VZ |
316 | if ( iRc == -1 ) { |
317 | wxLogSysError(_("can't write to file descriptor %d"), m_fd); | |
318 | m_error = TRUE; | |
319 | return 0; | |
320 | } | |
321 | else | |
322 | return iRc; | |
c801d85f KB |
323 | } |
324 | ||
325 | // flush | |
326 | bool wxFile::Flush() | |
327 | { | |
49d5d881 VZ |
328 | if ( IsOpened() ) { |
329 | #if defined(__VISUALC__) || wxHAVE_FSYNC | |
09914df7 VZ |
330 | if ( fsync(m_fd) == -1 ) |
331 | { | |
332 | wxLogSysError(_("can't flush file descriptor %d"), m_fd); | |
333 | return FALSE; | |
334 | } | |
49d5d881 | 335 | #else // no fsync |
09914df7 | 336 | // just do nothing |
49d5d881 VZ |
337 | #endif // fsync |
338 | } | |
c801d85f | 339 | |
49d5d881 | 340 | return TRUE; |
c801d85f KB |
341 | } |
342 | ||
343 | // ---------------------------------------------------------------------------- | |
344 | // seek | |
345 | // ---------------------------------------------------------------------------- | |
346 | ||
347 | // seek | |
79c3e0e1 | 348 | off_t wxFile::Seek(off_t ofs, wxSeekMode mode) |
c801d85f | 349 | { |
49d5d881 VZ |
350 | wxASSERT( IsOpened() ); |
351 | ||
a1b82138 | 352 | int origin; |
49d5d881 | 353 | switch ( mode ) { |
a1b82138 VZ |
354 | default: |
355 | wxFAIL_MSG(_("unknown seek origin")); | |
356 | ||
49d5d881 | 357 | case wxFromStart: |
a1b82138 | 358 | origin = SEEK_SET; |
49d5d881 VZ |
359 | break; |
360 | ||
361 | case wxFromCurrent: | |
a1b82138 | 362 | origin = SEEK_CUR; |
49d5d881 VZ |
363 | break; |
364 | ||
365 | case wxFromEnd: | |
a1b82138 | 366 | origin = SEEK_END; |
49d5d881 | 367 | break; |
49d5d881 VZ |
368 | } |
369 | ||
a1b82138 | 370 | int iRc = lseek(m_fd, ofs, origin); |
49d5d881 VZ |
371 | if ( iRc == -1 ) { |
372 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); | |
373 | return wxInvalidOffset; | |
374 | } | |
375 | else | |
376 | return (off_t)iRc; | |
c801d85f KB |
377 | } |
378 | ||
379 | // get current off_t | |
380 | off_t wxFile::Tell() const | |
381 | { | |
49d5d881 VZ |
382 | wxASSERT( IsOpened() ); |
383 | ||
384 | int iRc = tell(m_fd); | |
385 | if ( iRc == -1 ) { | |
386 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); | |
387 | return wxInvalidOffset; | |
388 | } | |
389 | else | |
390 | return (off_t)iRc; | |
c801d85f KB |
391 | } |
392 | ||
393 | // get current file length | |
394 | off_t wxFile::Length() const | |
395 | { | |
49d5d881 | 396 | wxASSERT( IsOpened() ); |
c801d85f | 397 | |
49d5d881 | 398 | #ifdef __VISUALC__ |
c801d85f | 399 | int iRc = _filelength(m_fd); |
49d5d881 | 400 | #else // !VC++ |
c801d85f KB |
401 | int iRc = tell(m_fd); |
402 | if ( iRc != -1 ) { | |
49d5d881 VZ |
403 | // @ have to use const_cast :-( |
404 | int iLen = ((wxFile *)this)->SeekEnd(); | |
405 | if ( iLen != -1 ) { | |
406 | // restore old position | |
407 | if ( ((wxFile *)this)->Seek(iRc) == -1 ) { | |
408 | // error | |
409 | iLen = -1; | |
410 | } | |
c801d85f | 411 | } |
c801d85f | 412 | |
49d5d881 VZ |
413 | iRc = iLen; |
414 | } | |
415 | #endif // VC++ | |
416 | ||
417 | if ( iRc == -1 ) { | |
418 | wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd); | |
419 | return wxInvalidOffset; | |
c801d85f | 420 | } |
49d5d881 VZ |
421 | else |
422 | return (off_t)iRc; | |
c801d85f KB |
423 | } |
424 | ||
425 | // is end of file reached? | |
426 | bool wxFile::Eof() const | |
427 | { | |
49d5d881 | 428 | wxASSERT( IsOpened() ); |
c801d85f | 429 | |
49d5d881 | 430 | int iRc; |
61b02744 | 431 | |
49d5d881 | 432 | #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__) |
61b02744 VZ |
433 | // @@ this doesn't work, of course, on unseekable file descriptors |
434 | off_t ofsCur = Tell(), | |
49d5d881 | 435 | ofsMax = Length(); |
1678ad78 | 436 | if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset ) |
49d5d881 | 437 | iRc = -1; |
61b02744 | 438 | else |
49d5d881 VZ |
439 | iRc = ofsCur == ofsMax; |
440 | #else // Windows and "native" compiler | |
61b02744 | 441 | iRc = eof(m_fd); |
49d5d881 | 442 | #endif // Windows/Unix |
c801d85f | 443 | |
49d5d881 VZ |
444 | switch ( iRc ) { |
445 | case 1: | |
446 | break; | |
c801d85f | 447 | |
49d5d881 VZ |
448 | case 0: |
449 | return FALSE; | |
c801d85f | 450 | |
49d5d881 VZ |
451 | case -1: |
452 | wxLogSysError(_("can't determine if the end of file is reached on \ | |
453 | descriptor %d"), m_fd); | |
454 | break; | |
c801d85f | 455 | |
49d5d881 VZ |
456 | default: |
457 | wxFAIL_MSG(_("invalid eof() return value.")); | |
458 | } | |
c801d85f | 459 | |
49d5d881 | 460 | return TRUE; |
c801d85f KB |
461 | } |
462 | ||
463 | // ============================================================================ | |
464 | // implementation of wxTempFile | |
465 | // ============================================================================ | |
466 | ||
467 | // ---------------------------------------------------------------------------- | |
468 | // construction | |
469 | // ---------------------------------------------------------------------------- | |
470 | wxTempFile::wxTempFile(const wxString& strName) | |
471 | { | |
49d5d881 | 472 | Open(strName); |
c801d85f KB |
473 | } |
474 | ||
475 | bool wxTempFile::Open(const wxString& strName) | |
476 | { | |
49d5d881 | 477 | m_strName = strName; |
246037e2 | 478 | |
49d5d881 VZ |
479 | // we want to create the file in the same directory as strName because |
480 | // otherwise rename() in Commit() might not work (if the files are on | |
481 | // different partitions for example). Unfortunately, the only standard | |
482 | // (POSIX) temp file creation function tmpnam() can't do it. | |
483 | #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ ) | |
50920146 | 484 | static const wxChar *szMktempSuffix = _T("XXXXXX"); |
b59650be | 485 | m_strTemp << strName << szMktempSuffix; |
50920146 | 486 | mktemp(MBSTRINGCAST m_strTemp.mb_str()); // will do because length doesn't change |
c2ff79b1 DW |
487 | #elif defined(__WXPM__) |
488 | // for now just create a file | |
489 | // future enhancements can be to set some extended attributes for file systems | |
490 | // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386) | |
491 | static const wxChar *szMktempSuffix = _T("XXX"); | |
492 | m_strTemp << strName << szMktempSuffix; | |
493 | mkdir(m_strTemp.GetWriteBuf(MAX_PATH)); | |
49d5d881 | 494 | #else // Windows |
30a5be97 VZ |
495 | wxString strPath; |
496 | wxSplitPath(strName, &strPath, NULL, NULL); | |
497 | if ( strPath.IsEmpty() ) | |
50920146 | 498 | strPath = _T('.'); // GetTempFileName will fail if we give it empty string |
81d66cf3 | 499 | #ifdef __WIN32__ |
50920146 | 500 | if ( !GetTempFileName(strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) ) |
81d66cf3 | 501 | #else |
49d5d881 | 502 | // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug? |
50920146 | 503 | if ( !GetTempFileName((BYTE) (const wxChar*) strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) ) |
81d66cf3 | 504 | #endif |
50920146 | 505 | wxLogLastError(_T("GetTempFileName")); |
30a5be97 | 506 | m_strTemp.UngetWriteBuf(); |
49d5d881 VZ |
507 | #endif // Windows/Unix |
508 | ||
509 | int access = wxS_DEFAULT; | |
510 | #ifdef __UNIX__ | |
511 | // create the file with the same mode as the original one under Unix | |
f46f1bc6 | 512 | mode_t umaskOld = 0; // just to suppress compiler warning |
fe99e285 VZ |
513 | bool changedUmask; |
514 | ||
49d5d881 | 515 | struct stat st; |
50920146 | 516 | if ( stat(strName.fn_str(), &st) == 0 ) |
49d5d881 VZ |
517 | { |
518 | // this assumes that only lower bits of st_mode contain the access | |
519 | // rights, but it's true for at least all Unices which have S_IXXXX() | |
520 | // macros, so should not be less portable than using (not POSIX) | |
521 | // S_IFREG &c | |
522 | access = st.st_mode & 0777; | |
fe99e285 VZ |
523 | |
524 | // we want to create the file with exactly the same access rights as | |
525 | // the original one, so disable the user's umask for the moment | |
526 | umaskOld = umask(0); | |
527 | changedUmask = TRUE; | |
49d5d881 VZ |
528 | } |
529 | else | |
530 | { | |
fe99e285 VZ |
531 | // file probably didn't exist, just create with default mode _using_ |
532 | // user's umask (new files creation should respet umask) | |
cedda7e6 | 533 | changedUmask = FALSE; |
49d5d881 | 534 | } |
49d5d881 VZ |
535 | #endif // Unix |
536 | ||
cedda7e6 | 537 | bool ok = m_file.Open(m_strTemp, wxFile::write, access); |
49d5d881 VZ |
538 | |
539 | #ifdef __UNIX__ | |
fe99e285 VZ |
540 | if ( changedUmask ) |
541 | { | |
542 | // restore umask now that the file is created | |
543 | (void)umask(umaskOld); | |
544 | } | |
49d5d881 | 545 | #endif // Unix |
246037e2 | 546 | |
49d5d881 | 547 | return ok; |
c801d85f KB |
548 | } |
549 | ||
550 | // ---------------------------------------------------------------------------- | |
551 | // destruction | |
552 | // ---------------------------------------------------------------------------- | |
553 | ||
554 | wxTempFile::~wxTempFile() | |
555 | { | |
49d5d881 VZ |
556 | if ( IsOpened() ) |
557 | Discard(); | |
c801d85f KB |
558 | } |
559 | ||
560 | bool wxTempFile::Commit() | |
561 | { | |
49d5d881 | 562 | m_file.Close(); |
c801d85f | 563 | |
50920146 | 564 | if ( wxFile::Exists(m_strName) && remove(m_strName.fn_str()) != 0 ) { |
49d5d881 VZ |
565 | wxLogSysError(_("can't remove file '%s'"), m_strName.c_str()); |
566 | return FALSE; | |
567 | } | |
c801d85f | 568 | |
50920146 | 569 | if ( rename(m_strTemp.fn_str(), m_strName.fn_str()) != 0 ) { |
49d5d881 VZ |
570 | wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str()); |
571 | return FALSE; | |
572 | } | |
c801d85f | 573 | |
49d5d881 | 574 | return TRUE; |
c801d85f KB |
575 | } |
576 | ||
577 | void wxTempFile::Discard() | |
578 | { | |
49d5d881 | 579 | m_file.Close(); |
50920146 | 580 | if ( remove(m_strTemp.fn_str()) != 0 ) |
49d5d881 | 581 | wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str()); |
c801d85f | 582 | } |
ce4169a4 | 583 | |
cc985fac PA |
584 | #endif |
585 |