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