]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
unicode compilation fix
[wxWidgets.git] / src / common / file.cpp
CommitLineData
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>
55d99c7a 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
14f355c2 17#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
1c193821 31#if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
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
1c193821
JS
57#elif defined(__WXMSW__) && defined(__WXWINCE__)
58 // TODO: what to include?
c801d85f 59#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b 60 #include <unistd.h>
fd938b11 61 #include <time.h>
732b8386 62 #include <sys/stat.h>
3f4a0c5b 63 #ifdef __GNUWIN32__
9ed0d735 64 #include "wx/msw/wrapwin.h"
3f4a0c5b 65 #endif
d3b4d710
VS
66#elif defined(__DOS__)
67 #if defined(__WATCOMC__)
68 #include <io.h>
69 #elif defined(__DJGPP__)
70 #include <io.h>
71 #include <unistd.h>
72 #include <stdio.h>
73 #else
74 #error "Please specify the header with file functions declarations."
75 #endif
c2ff79b1
DW
76#elif (defined(__WXPM__))
77 #include <io.h>
34138703 78#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
79 // Have to ifdef this for different environments
80 #include <io.h>
17dff81c 81#elif (defined(__WXMAC__))
5b781a67 82#if __MSL__ < 0x6000
3f4a0c5b 83 int access( const char *path, int mode ) { return 0 ; }
5b781a67
SC
84#else
85 int _access( const char *path, int mode ) { return 0 ; }
86#endif
3f4a0c5b 87 char* mktemp( char * path ) { return path ;}
5b781a67 88 #include <stat.h>
5b781a67 89 #include <unistd.h>
c801d85f 90#else
3f4a0c5b 91 #error "Please specify the header with file functions declarations."
c801d85f
KB
92#endif //Win/UNIX
93
94#include <stdio.h> // SEEK_xxx constants
1c193821
JS
95
96#ifndef __WXWINCE__
c801d85f 97#include <fcntl.h> // O_RDONLY &c
1c193821 98#endif
a3ef5bf5 99
1c193821
JS
100#ifdef __WXWINCE__
101// Nothing
102#elif !defined(__MWERKS__)
31907d03
SC
103 #include <sys/types.h> // needed for stat
104 #include <sys/stat.h> // stat
105#elif defined(__MWERKS__) && ( defined(__WXMSW__) || defined(__MACH__) )
de85a884
VZ
106 #include <sys/types.h> // needed for stat
107 #include <sys/stat.h> // stat
469e1e5c 108#endif
c801d85f 109
4ea2c29f
VZ
110// Windows compilers don't have these constants
111#ifndef W_OK
112 enum
113 {
114 F_OK = 0, // test for existence
115 X_OK = 1, // execute permission
116 W_OK = 2, // write
117 R_OK = 4 // read
118 };
119#endif // W_OK
34138703 120
b12915c1
VZ
121// there is no distinction between text and binary files under Unix, so define
122// O_BINARY as 0 if the system headers don't do it already
123#if defined(__UNIX__) && !defined(O_BINARY)
49d5d881 124 #define O_BINARY (0)
246037e2 125#endif //__UNIX__
c801d85f 126
a3ef5bf5 127#ifdef __SALFORDC__
3f4a0c5b 128 #include <unix.h>
a3ef5bf5
JS
129#endif
130
81d66cf3 131#ifndef MAX_PATH
3f4a0c5b 132 #define MAX_PATH 512
81d66cf3 133#endif
c801d85f 134
49d5d881
VZ
135// some broken compilers don't have 3rd argument in open() and creat()
136#ifdef __SALFORDC__
137 #define ACCESS(access)
138 #define stat _stat
139#else // normal compiler
140 #define ACCESS(access) , (access)
141#endif // Salford C
142
f6bcfd97 143// wxWindows
ade35f11
VZ
144#ifndef WX_PRECOMP
145 #include "wx/string.h"
146 #include "wx/intl.h"
ade35f11
VZ
147 #include "wx/log.h"
148#endif // !WX_PRECOMP
149
150#include "wx/filename.h"
44d568b6 151#include "wx/file.h"
4ea2c29f 152#include "wx/filefn.h"
f6bcfd97 153
28f5082b
VS
154#ifdef __WXMSW__
155 #include "wx/msw/mslu.h"
156#endif
157
1c193821
JS
158#ifdef __WXWINCE__
159 #include "wx/msw/private.h"
160#endif
161
c801d85f
KB
162// ============================================================================
163// implementation of wxFile
164// ============================================================================
165
166// ----------------------------------------------------------------------------
167// static functions
168// ----------------------------------------------------------------------------
4ea2c29f 169
50920146 170bool wxFile::Exists(const wxChar *name)
246037e2 171{
4ea2c29f 172 return wxFileExists(name);
d1427b70
VZ
173}
174
50920146 175bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 176{
4ea2c29f
VZ
177 int how;
178
179 switch ( mode )
180 {
181 default:
182 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
183 // fall through
d1427b70 184
49d5d881
VZ
185 case read:
186 how = R_OK;
187 break;
d1427b70 188
49d5d881
VZ
189 case write:
190 how = W_OK;
191 break;
d1427b70 192
4ea2c29f
VZ
193 case read_write:
194 how = R_OK | W_OK;
195 break;
49d5d881 196 }
d1427b70 197
1c193821
JS
198#ifdef __WXWINCE__
199 // FIXME: use CreateFile with 0 access to query the file
200 return TRUE;
201#else
4ea2c29f 202 return wxAccess(name, how) == 0;
1c193821 203#endif
c801d85f
KB
204}
205
206// ----------------------------------------------------------------------------
207// opening/closing
208// ----------------------------------------------------------------------------
209
210// ctors
50920146 211wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 212{
49d5d881
VZ
213 m_fd = fd_invalid;
214 m_error = FALSE;
c801d85f 215
49d5d881 216 Open(szFileName, mode);
c801d85f
KB
217}
218
c801d85f 219// create the file, fail if it already exists and bOverwrite
50920146 220bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 221{
49d5d881
VZ
222 // if bOverwrite we create a new file or truncate the existing one,
223 // otherwise we only create the new file and fail if it already exists
c4e41ce3 224#if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
92980e90
RR
225 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
226 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
227 int fd = creat( szFileName , accessMode);
1c193821
JS
228#else
229#ifdef __WXWINCE__
230 HANDLE fileHandle = ::CreateFile(szFileName, GENERIC_WRITE, 0, NULL,
231 bOverwrite ? CREATE_ALWAYS : CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
232 0);
233 int fd = 0;
234 if (fileHandle == INVALID_HANDLE_VALUE)
235 fd = (int) fileHandle;
236 else
237 fd = -1;
7c74e7fe 238#else
92980e90
RR
239 int fd = wxOpen( szFileName,
240 O_BINARY | O_WRONLY | O_CREAT |
241 (bOverwrite ? O_TRUNC : O_EXCL)
242 ACCESS(accessMode) );
1c193821 243#endif
7c74e7fe 244#endif
92980e90
RR
245 if ( fd == -1 )
246 {
49d5d881
VZ
247 wxLogSysError(_("can't create file '%s'"), szFileName);
248 return FALSE;
249 }
92980e90
RR
250 else
251 {
49d5d881
VZ
252 Attach(fd);
253 return TRUE;
254 }
c801d85f
KB
255}
256
257// open the file
50920146 258bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 259{
1c193821
JS
260#ifdef __WXWINCE__
261 DWORD access = 0;
262 DWORD shareMode = 0;
263 DWORD disposition = 0;
264
265 int flags = O_BINARY;
266
267 switch ( mode )
268 {
269 case read:
270 access = GENERIC_READ;
271 shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
272 disposition = OPEN_EXISTING;
273 break;
274
275 case write_append:
276 if ( wxFile::Exists(szFileName) )
277 {
278 access = GENERIC_READ|GENERIC_WRITE;
279 shareMode = FILE_SHARE_READ;
280 disposition = 0;
281 break;
282 }
283 //else: fall through as write_append is the same as write if the
284 // file doesn't exist
285
286 case write:
287 access = GENERIC_WRITE;
288 shareMode = 0;
289 disposition = TRUNCATE_EXISTING;
290 break;
291
292 case write_excl:
293 access = GENERIC_WRITE;
294 shareMode = 0;
295 disposition = TRUNCATE_EXISTING;
296 break;
297
298 case read_write:
299 access = GENERIC_READ|GENERIC_WRITE;
300 shareMode = 0;
301 disposition = 0;
302 break;
303 }
304
305 int fd = 0;
306 HANDLE fileHandle = ::CreateFile(szFileName, access, shareMode, NULL,
307 disposition, FILE_ATTRIBUTE_NORMAL, 0);
308 if (fileHandle == INVALID_HANDLE_VALUE)
309 fd = -1;
310 else
311 fd = (int) fileHandle;
312#else
49d5d881 313 int flags = O_BINARY;
c801d85f 314
92980e90
RR
315 switch ( mode )
316 {
49d5d881
VZ
317 case read:
318 flags |= O_RDONLY;
319 break;
c801d85f 320
f6bcfd97
BP
321 case write_append:
322 if ( wxFile::Exists(szFileName) )
323 {
324 flags |= O_WRONLY | O_APPEND;
325 break;
326 }
327 //else: fall through as write_append is the same as write if the
328 // file doesn't exist
329
49d5d881
VZ
330 case write:
331 flags |= O_WRONLY | O_CREAT | O_TRUNC;
332 break;
61b02744 333
68164137
RL
334 case write_excl:
335 flags |= O_WRONLY | O_CREAT | O_EXCL;
336 break;
337
49d5d881
VZ
338 case read_write:
339 flags |= O_RDWR;
340 break;
341 }
c801d85f 342
92980e90 343 int fd = wxOpen( szFileName, flags ACCESS(accessMode));
1c193821 344#endif
92980e90
RR
345 if ( fd == -1 )
346 {
49d5d881
VZ
347 wxLogSysError(_("can't open file '%s'"), szFileName);
348 return FALSE;
349 }
350 else {
351 Attach(fd);
352 return TRUE;
353 }
c801d85f
KB
354}
355
356// close
61b02744 357bool wxFile::Close()
c801d85f 358{
49d5d881 359 if ( IsOpened() ) {
1c193821
JS
360#ifdef __WXWINCE__
361 if (!CloseHandle((HANDLE) m_fd))
362#else
363 if ( close(m_fd) == -1 )
364#endif
365 {
49d5d881
VZ
366 wxLogSysError(_("can't close file descriptor %d"), m_fd);
367 m_fd = fd_invalid;
368 return FALSE;
369 }
370 else
371 m_fd = fd_invalid;
61b02744 372 }
61b02744 373
49d5d881 374 return TRUE;
c801d85f
KB
375}
376
377// ----------------------------------------------------------------------------
378// read/write
379// ----------------------------------------------------------------------------
380
381// read
382off_t wxFile::Read(void *pBuf, off_t nCount)
383{
49d5d881 384 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 385
1c193821
JS
386#ifdef __WXWINCE__
387 DWORD bytesRead = 0;
388 int iRc = 0;
389 if (ReadFile((HANDLE) m_fd, pBuf, (DWORD) nCount, & bytesRead, NULL))
390 iRc = bytesRead;
391 else
392 iRc = -1;
393#elif defined(__MWERKS__)
49d5d881 394 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 395#else
49d5d881 396 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 397#endif
49d5d881
VZ
398 if ( iRc == -1 ) {
399 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
400 return wxInvalidOffset;
401 }
402 else
403 return (size_t)iRc;
c801d85f
KB
404}
405
406// write
c86f1403 407size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 408{
49d5d881 409 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 410
1c193821
JS
411#ifdef __WXWINCE__
412 DWORD bytesRead = 0;
413 int iRc = 0;
414 if (WriteFile((HANDLE) m_fd, pBuf, (DWORD) nCount, & bytesRead, NULL))
415 iRc = bytesRead;
416 else
417 iRc = -1;
418#elif defined(__MWERKS__)
5b781a67
SC
419#if __MSL__ >= 0x6000
420 int iRc = ::write(m_fd, (void*) pBuf, nCount);
421#else
49d5d881 422 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
5b781a67 423#endif
469e1e5c 424#else
49d5d881 425 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 426#endif
49d5d881
VZ
427 if ( iRc == -1 ) {
428 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
429 m_error = TRUE;
430 return 0;
431 }
432 else
433 return iRc;
c801d85f
KB
434}
435
436// flush
437bool wxFile::Flush()
438{
49d5d881 439 if ( IsOpened() ) {
1c193821
JS
440#ifdef __WXWINCE__
441 // Do nothing
442#elif defined(__VISUALC__) || wxHAVE_FSYNC
f6bcfd97 443 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
444 {
445 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
446 return FALSE;
447 }
49d5d881 448#else // no fsync
09914df7 449 // just do nothing
49d5d881
VZ
450#endif // fsync
451 }
c801d85f 452
49d5d881 453 return TRUE;
c801d85f
KB
454}
455
456// ----------------------------------------------------------------------------
457// seek
458// ----------------------------------------------------------------------------
459
460// seek
79c3e0e1 461off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f 462{
49d5d881
VZ
463 wxASSERT( IsOpened() );
464
1c193821
JS
465#ifdef __WXWINCE__
466 int origin;
467 switch ( mode ) {
468 default:
469 wxFAIL_MSG(_("unknown seek origin"));
470
471 case wxFromStart:
472 origin = FILE_BEGIN;
473 break;
474
475 case wxFromCurrent:
476 origin = FILE_CURRENT;
477 break;
478
479 case wxFromEnd:
480 origin = FILE_END;
481 break;
482 }
483
484 DWORD res = SetFilePointer((HANDLE) m_fd, ofs, 0, origin) ;
485 if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR)
486 {
487 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
488 return wxInvalidOffset;
489 }
490 else
491 return (off_t)res;
492#else
a1b82138 493 int origin;
49d5d881 494 switch ( mode ) {
a1b82138
VZ
495 default:
496 wxFAIL_MSG(_("unknown seek origin"));
497
49d5d881 498 case wxFromStart:
a1b82138 499 origin = SEEK_SET;
49d5d881
VZ
500 break;
501
502 case wxFromCurrent:
a1b82138 503 origin = SEEK_CUR;
49d5d881
VZ
504 break;
505
506 case wxFromEnd:
a1b82138 507 origin = SEEK_END;
49d5d881 508 break;
49d5d881
VZ
509 }
510
a1b82138 511 int iRc = lseek(m_fd, ofs, origin);
49d5d881
VZ
512 if ( iRc == -1 ) {
513 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
514 return wxInvalidOffset;
515 }
516 else
517 return (off_t)iRc;
1c193821 518#endif
c801d85f
KB
519}
520
521// get current off_t
522off_t wxFile::Tell() const
523{
49d5d881
VZ
524 wxASSERT( IsOpened() );
525
1c193821
JS
526#ifdef __WXWINCE__
527 DWORD res = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_CURRENT) ;
528 if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR)
529 {
530 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
531 return wxInvalidOffset;
532 }
533 else
534 return (off_t)res;
535#else
f6bcfd97 536 int iRc = wxTell(m_fd);
49d5d881
VZ
537 if ( iRc == -1 ) {
538 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
539 return wxInvalidOffset;
540 }
541 else
542 return (off_t)iRc;
1c193821 543#endif
c801d85f
KB
544}
545
546// get current file length
547off_t wxFile::Length() const
548{
49d5d881 549 wxASSERT( IsOpened() );
c801d85f 550
1c193821
JS
551#ifdef __WXWINCE__
552 DWORD off0 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_CURRENT);
553 DWORD off1 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_END);
554 off_t len = off1;
555
556 // Restore position
557 SetFilePointer((HANDLE) m_fd, off0, 0, FILE_BEGIN);
558 return len;
559#else
49d5d881 560#ifdef __VISUALC__
c801d85f 561 int iRc = _filelength(m_fd);
49d5d881 562#else // !VC++
f6bcfd97 563 int iRc = wxTell(m_fd);
c801d85f 564 if ( iRc != -1 ) {
49d5d881
VZ
565 // @ have to use const_cast :-(
566 int iLen = ((wxFile *)this)->SeekEnd();
567 if ( iLen != -1 ) {
568 // restore old position
569 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
570 // error
571 iLen = -1;
572 }
c801d85f 573 }
c801d85f 574
49d5d881
VZ
575 iRc = iLen;
576 }
577#endif // VC++
578
579 if ( iRc == -1 ) {
580 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
581 return wxInvalidOffset;
c801d85f 582 }
49d5d881
VZ
583 else
584 return (off_t)iRc;
1c193821 585#endif
c801d85f
KB
586}
587
588// is end of file reached?
589bool wxFile::Eof() const
590{
49d5d881 591 wxASSERT( IsOpened() );
c801d85f 592
1c193821
JS
593#ifdef __WXWINCE__
594 DWORD off0 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_CURRENT);
595 DWORD off1 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_END);
596 if (off0 == off1)
597 return TRUE;
598 else
599 {
600 SetFilePointer((HANDLE) m_fd, off0, 0, FILE_BEGIN);
601 return FALSE;
602 }
603#else
49d5d881 604 int iRc;
61b02744 605
d3b4d710 606#if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744
VZ
607 // @@ this doesn't work, of course, on unseekable file descriptors
608 off_t ofsCur = Tell(),
49d5d881 609 ofsMax = Length();
1678ad78 610 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 611 iRc = -1;
61b02744 612 else
49d5d881
VZ
613 iRc = ofsCur == ofsMax;
614#else // Windows and "native" compiler
61b02744 615 iRc = eof(m_fd);
49d5d881 616#endif // Windows/Unix
c801d85f 617
49d5d881
VZ
618 switch ( iRc ) {
619 case 1:
620 break;
c801d85f 621
49d5d881
VZ
622 case 0:
623 return FALSE;
c801d85f 624
49d5d881 625 case -1:
8e3f1261 626 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
49d5d881 627 break;
c801d85f 628
49d5d881
VZ
629 default:
630 wxFAIL_MSG(_("invalid eof() return value."));
631 }
c801d85f 632
49d5d881 633 return TRUE;
1c193821 634#endif
c801d85f
KB
635}
636
637// ============================================================================
638// implementation of wxTempFile
639// ============================================================================
640
641// ----------------------------------------------------------------------------
642// construction
643// ----------------------------------------------------------------------------
44b62d54 644
c801d85f
KB
645wxTempFile::wxTempFile(const wxString& strName)
646{
49d5d881 647 Open(strName);
c801d85f
KB
648}
649
650bool wxTempFile::Open(const wxString& strName)
651{
44b62d54
VZ
652 // we must have an absolute filename because otherwise CreateTempFileName()
653 // would create the temp file in $TMP (i.e. the system standard location
654 // for the temp files) which might be on another volume/drive/mount and
655 // wxRename()ing it later to m_strName from Commit() would then fail
656 //
657 // with the absolute filename, the temp file is created in the same
658 // directory as this one which ensures that wxRename() may work later
659 wxFileName fn(strName);
660 if ( !fn.IsAbsolute() )
661 {
662 fn.Normalize(wxPATH_NORM_ABSOLUTE);
663 }
664
665 m_strName = fn.GetFullPath();
246037e2 666
44b62d54 667 m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file);
ade35f11
VZ
668
669 if ( m_strTemp.empty() )
670 {
671 // CreateTempFileName() failed
672 return FALSE;
673 }
49d5d881 674
49d5d881 675#ifdef __UNIX__
ade35f11
VZ
676 // the temp file should have the same permissions as the original one
677 mode_t mode;
ca11abde 678
f6bcfd97 679 wxStructStat st;
ca11abde 680 if ( stat( (const char*) m_strName.fn_str(), &st) == 0 )
49d5d881 681 {
ade35f11 682 mode = st.st_mode;
49d5d881
VZ
683 }
684 else
685 {
ade35f11 686 // file probably didn't exist, just give it the default mode _using_
68164137 687 // user's umask (new files creation should respect umask)
ade35f11
VZ
688 mode_t mask = umask(0777);
689 mode = 0666 & ~mask;
690 umask(mask);
49d5d881 691 }
49d5d881 692
ca11abde 693 if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 )
fe99e285 694 {
ade35f11 695 wxLogSysError(_("Failed to set temporary file permissions"));
fe99e285 696 }
49d5d881 697#endif // Unix
246037e2 698
ade35f11 699 return TRUE;
c801d85f
KB
700}
701
702// ----------------------------------------------------------------------------
703// destruction
704// ----------------------------------------------------------------------------
705
706wxTempFile::~wxTempFile()
707{
49d5d881
VZ
708 if ( IsOpened() )
709 Discard();
c801d85f
KB
710}
711
712bool wxTempFile::Commit()
713{
49d5d881 714 m_file.Close();
c801d85f 715
f6bcfd97 716 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881
VZ
717 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
718 return FALSE;
719 }
c801d85f 720
f35746ce 721 if ( !wxRenameFile(m_strTemp, m_strName) ) {
49d5d881
VZ
722 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
723 return FALSE;
724 }
c801d85f 725
49d5d881 726 return TRUE;
c801d85f
KB
727}
728
729void wxTempFile::Discard()
730{
49d5d881 731 m_file.Close();
f6bcfd97 732 if ( wxRemove(m_strTemp) != 0 )
49d5d881 733 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 734}
ce4169a4 735
ade35f11 736#endif // wxUSE_FILE
cc985fac 737