]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
Reverted due to Darwin conflict.
[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>
65571936 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__)
30a5be97 32
a3ef5bf5 33#ifndef __SALFORDC__
49d5d881
VZ
34 #define WIN32_LEAN_AND_MEAN
35 #define NOSERVICE
36 #define NOIME
37 #define NOATOM
38 #define NOGDI
39 #define NOGDICAPMASKS
40 #define NOMETAFILE
41 #define NOMINMAX
42 #define NOMSG
43 #define NOOPENFILE
44 #define NORASTEROPS
45 #define NOSCROLL
46 #define NOSOUND
47 #define NOSYSMETRICS
48 #define NOTEXTMETRIC
49 #define NOWH
50 #define NOCOMM
51 #define NOKANJI
52 #define NOCRYPT
53 #define NOMCX
a3ef5bf5
JS
54#endif
55
1c193821 56#elif defined(__WXMSW__) && defined(__WXWINCE__)
d109c584 57 #include "wx/msw/missing.h"
5a3912f2
SN
58#elif (defined(__OS2__))
59 #include <io.h>
c801d85f 60#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b 61 #include <unistd.h>
fd938b11 62 #include <time.h>
732b8386 63 #include <sys/stat.h>
3f4a0c5b 64 #ifdef __GNUWIN32__
9ed0d735 65 #include "wx/msw/wrapwin.h"
3f4a0c5b 66 #endif
d3b4d710
VS
67#elif defined(__DOS__)
68 #if defined(__WATCOMC__)
69 #include <io.h>
70 #elif defined(__DJGPP__)
71 #include <io.h>
72 #include <unistd.h>
73 #include <stdio.h>
74 #else
75 #error "Please specify the header with file functions declarations."
76 #endif
34138703 77#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
78 // Have to ifdef this for different environments
79 #include <io.h>
17dff81c 80#elif (defined(__WXMAC__))
5b781a67 81#if __MSL__ < 0x6000
3f4a0c5b 82 int access( const char *path, int mode ) { return 0 ; }
5b781a67
SC
83#else
84 int _access( const char *path, int mode ) { return 0 ; }
85#endif
3f4a0c5b 86 char* mktemp( char * path ) { return path ;}
5b781a67 87 #include <stat.h>
6294ac2e 88 #include <unistd.h>
c801d85f 89#else
3f4a0c5b 90 #error "Please specify the header with file functions declarations."
c801d85f
KB
91#endif //Win/UNIX
92
93#include <stdio.h> // SEEK_xxx constants
1c193821 94
4ea2c29f
VZ
95// Windows compilers don't have these constants
96#ifndef W_OK
97 enum
98 {
99 F_OK = 0, // test for existence
100 X_OK = 1, // execute permission
101 W_OK = 2, // write
102 R_OK = 4 // read
103 };
104#endif // W_OK
34138703 105
b12915c1
VZ
106// there is no distinction between text and binary files under Unix, so define
107// O_BINARY as 0 if the system headers don't do it already
108#if defined(__UNIX__) && !defined(O_BINARY)
49d5d881 109 #define O_BINARY (0)
246037e2 110#endif //__UNIX__
c801d85f 111
a3ef5bf5 112#ifdef __SALFORDC__
3f4a0c5b 113 #include <unix.h>
a3ef5bf5
JS
114#endif
115
81d66cf3 116#ifndef MAX_PATH
3f4a0c5b 117 #define MAX_PATH 512
81d66cf3 118#endif
c801d85f 119
49d5d881
VZ
120// some broken compilers don't have 3rd argument in open() and creat()
121#ifdef __SALFORDC__
122 #define ACCESS(access)
123 #define stat _stat
124#else // normal compiler
125 #define ACCESS(access) , (access)
126#endif // Salford C
127
77ffb593 128// wxWidgets
ade35f11
VZ
129#ifndef WX_PRECOMP
130 #include "wx/string.h"
131 #include "wx/intl.h"
ade35f11
VZ
132 #include "wx/log.h"
133#endif // !WX_PRECOMP
134
135#include "wx/filename.h"
44d568b6 136#include "wx/file.h"
4ea2c29f 137#include "wx/filefn.h"
f6bcfd97 138
28f5082b
VS
139#ifdef __WXMSW__
140 #include "wx/msw/mslu.h"
141#endif
142
1c193821
JS
143#ifdef __WXWINCE__
144 #include "wx/msw/private.h"
145#endif
146
c801d85f
KB
147// ============================================================================
148// implementation of wxFile
149// ============================================================================
150
151// ----------------------------------------------------------------------------
152// static functions
153// ----------------------------------------------------------------------------
4ea2c29f 154
50920146 155bool wxFile::Exists(const wxChar *name)
246037e2 156{
4ea2c29f 157 return wxFileExists(name);
d1427b70
VZ
158}
159
50920146 160bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 161{
4ea2c29f
VZ
162 int how;
163
164 switch ( mode )
165 {
166 default:
167 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
168 // fall through
d1427b70 169
49d5d881
VZ
170 case read:
171 how = R_OK;
172 break;
d1427b70 173
49d5d881
VZ
174 case write:
175 how = W_OK;
176 break;
d1427b70 177
4ea2c29f
VZ
178 case read_write:
179 how = R_OK | W_OK;
180 break;
49d5d881 181 }
d1427b70 182
4ea2c29f 183 return wxAccess(name, how) == 0;
c801d85f
KB
184}
185
186// ----------------------------------------------------------------------------
187// opening/closing
188// ----------------------------------------------------------------------------
189
190// ctors
50920146 191wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 192{
49d5d881 193 m_fd = fd_invalid;
a62848fd 194 m_error = false;
c801d85f 195
49d5d881 196 Open(szFileName, mode);
c801d85f
KB
197}
198
c801d85f 199// create the file, fail if it already exists and bOverwrite
50920146 200bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 201{
49d5d881
VZ
202 // if bOverwrite we create a new file or truncate the existing one,
203 // otherwise we only create the new file and fail if it already exists
c4e41ce3 204#if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
92980e90 205 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
a2b77260 206 // int fd = open( szFileName , O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
92980e90 207 int fd = creat( szFileName , accessMode);
7c74e7fe 208#else
92980e90
RR
209 int fd = wxOpen( szFileName,
210 O_BINARY | O_WRONLY | O_CREAT |
211 (bOverwrite ? O_TRUNC : O_EXCL)
212 ACCESS(accessMode) );
7c74e7fe 213#endif
92980e90
RR
214 if ( fd == -1 )
215 {
49d5d881 216 wxLogSysError(_("can't create file '%s'"), szFileName);
a62848fd 217 return false;
49d5d881 218 }
92980e90
RR
219 else
220 {
49d5d881 221 Attach(fd);
a62848fd 222 return true;
49d5d881 223 }
c801d85f
KB
224}
225
226// open the file
50920146 227bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 228{
49d5d881 229 int flags = O_BINARY;
c801d85f 230
92980e90
RR
231 switch ( mode )
232 {
49d5d881
VZ
233 case read:
234 flags |= O_RDONLY;
235 break;
c801d85f 236
f6bcfd97
BP
237 case write_append:
238 if ( wxFile::Exists(szFileName) )
239 {
240 flags |= O_WRONLY | O_APPEND;
241 break;
242 }
243 //else: fall through as write_append is the same as write if the
244 // file doesn't exist
245
49d5d881
VZ
246 case write:
247 flags |= O_WRONLY | O_CREAT | O_TRUNC;
248 break;
61b02744 249
68164137
RL
250 case write_excl:
251 flags |= O_WRONLY | O_CREAT | O_EXCL;
252 break;
253
49d5d881
VZ
254 case read_write:
255 flags |= O_RDWR;
256 break;
257 }
c801d85f 258
cc8cc54f
VZ
259#ifdef __WINDOWS__
260 // only read/write bits for "all" are supported by this function under
261 // Windows, and VC++ 8 returns EINVAL if any other bits are used in
262 // accessMode, so clear them as they have at best no effect anyhow
263 accessMode &= wxS_IRUSR | wxS_IWUSR;
264#endif // __WINDOWS__
265
92980e90 266 int fd = wxOpen( szFileName, flags ACCESS(accessMode));
6294ac2e 267
92980e90
RR
268 if ( fd == -1 )
269 {
49d5d881 270 wxLogSysError(_("can't open file '%s'"), szFileName);
a62848fd 271 return false;
49d5d881
VZ
272 }
273 else {
274 Attach(fd);
a62848fd 275 return true;
49d5d881 276 }
c801d85f
KB
277}
278
279// close
61b02744 280bool wxFile::Close()
c801d85f 281{
49d5d881 282 if ( IsOpened() ) {
6294ac2e 283 if (wxClose(m_fd) == -1)
1c193821 284 {
49d5d881
VZ
285 wxLogSysError(_("can't close file descriptor %d"), m_fd);
286 m_fd = fd_invalid;
a62848fd 287 return false;
49d5d881
VZ
288 }
289 else
290 m_fd = fd_invalid;
61b02744 291 }
61b02744 292
a62848fd 293 return true;
c801d85f
KB
294}
295
296// ----------------------------------------------------------------------------
297// read/write
298// ----------------------------------------------------------------------------
299
300// read
6294ac2e 301size_t wxFile::Read(void *pBuf, size_t nCount)
c801d85f 302{
49d5d881 303 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 304
6294ac2e
VZ
305 int iRc = wxRead(m_fd, pBuf, nCount);
306
49d5d881
VZ
307 if ( iRc == -1 ) {
308 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
6294ac2e 309 return (size_t)wxInvalidOffset;
49d5d881
VZ
310 }
311 else
312 return (size_t)iRc;
c801d85f
KB
313}
314
315// write
c86f1403 316size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 317{
49d5d881 318 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 319
6294ac2e
VZ
320 int iRc = wxWrite(m_fd, pBuf, nCount);
321
49d5d881
VZ
322 if ( iRc == -1 ) {
323 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
a62848fd 324 m_error = true;
49d5d881
VZ
325 return 0;
326 }
327 else
328 return iRc;
c801d85f
KB
329}
330
331// flush
332bool wxFile::Flush()
333{
49d5d881 334 if ( IsOpened() ) {
6294ac2e 335#if defined(__VISUALC__) || wxHAVE_FSYNC
f6bcfd97 336 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
337 {
338 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
a62848fd 339 return false;
09914df7 340 }
49d5d881 341#else // no fsync
09914df7 342 // just do nothing
49d5d881
VZ
343#endif // fsync
344 }
c801d85f 345
a62848fd 346 return true;
c801d85f
KB
347}
348
349// ----------------------------------------------------------------------------
350// seek
351// ----------------------------------------------------------------------------
352
353// seek
6294ac2e 354wxFileOffset wxFile::Seek(wxFileOffset ofs, wxSeekMode mode)
c801d85f 355{
49d5d881
VZ
356 wxASSERT( IsOpened() );
357
a1b82138 358 int origin;
49d5d881 359 switch ( mode ) {
a1b82138
VZ
360 default:
361 wxFAIL_MSG(_("unknown seek origin"));
362
49d5d881 363 case wxFromStart:
a1b82138 364 origin = SEEK_SET;
49d5d881
VZ
365 break;
366
367 case wxFromCurrent:
a1b82138 368 origin = SEEK_CUR;
49d5d881
VZ
369 break;
370
371 case wxFromEnd:
a1b82138 372 origin = SEEK_END;
49d5d881 373 break;
49d5d881
VZ
374 }
375
6294ac2e
VZ
376 if (ofs == wxInvalidOffset)
377 {
378 wxLogSysError(_("can't seek on file descriptor %d, large files support is not enabled."), m_fd);
379 return wxInvalidOffset;
380 }
381 wxFileOffset iRc = wxSeek(m_fd, ofs, origin);
49d5d881
VZ
382 if ( iRc == -1 ) {
383 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
384 return wxInvalidOffset;
385 }
386 else
6294ac2e 387 return iRc;
c801d85f
KB
388}
389
6294ac2e
VZ
390// get current wxFileOffset
391wxFileOffset wxFile::Tell() const
c801d85f 392{
49d5d881
VZ
393 wxASSERT( IsOpened() );
394
6294ac2e 395 wxFileOffset iRc = wxTell(m_fd);
49d5d881
VZ
396 if ( iRc == -1 ) {
397 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
398 return wxInvalidOffset;
399 }
400 else
6294ac2e 401 return iRc;
c801d85f
KB
402}
403
404// get current file length
6294ac2e 405wxFileOffset wxFile::Length() const
c801d85f 406{
49d5d881 407 wxASSERT( IsOpened() );
c801d85f 408
6294ac2e 409 wxFileOffset iRc = Tell();
c801d85f 410 if ( iRc != -1 ) {
49d5d881 411 // @ have to use const_cast :-(
6294ac2e 412 wxFileOffset iLen = ((wxFile *)this)->SeekEnd();
49d5d881
VZ
413 if ( iLen != -1 ) {
414 // restore old position
415 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
416 // error
417 iLen = -1;
418 }
c801d85f 419 }
c801d85f 420
49d5d881
VZ
421 iRc = iLen;
422 }
49d5d881
VZ
423
424 if ( iRc == -1 ) {
425 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
426 return wxInvalidOffset;
c801d85f 427 }
49d5d881 428 else
6294ac2e 429 return iRc;
c801d85f
KB
430}
431
432// is end of file reached?
433bool wxFile::Eof() const
434{
49d5d881 435 wxASSERT( IsOpened() );
c801d85f 436
49d5d881 437 int iRc;
61b02744 438
d3b4d710 439#if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744 440 // @@ this doesn't work, of course, on unseekable file descriptors
6294ac2e 441 wxFileOffset ofsCur = Tell(),
49d5d881 442 ofsMax = Length();
1678ad78 443 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 444 iRc = -1;
61b02744 445 else
49d5d881
VZ
446 iRc = ofsCur == ofsMax;
447#else // Windows and "native" compiler
6294ac2e 448 iRc = wxEof(m_fd);
49d5d881 449#endif // Windows/Unix
c801d85f 450
49d5d881
VZ
451 switch ( iRc ) {
452 case 1:
453 break;
c801d85f 454
49d5d881 455 case 0:
a62848fd 456 return false;
c801d85f 457
49d5d881 458 case -1:
8e3f1261 459 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
49d5d881 460 break;
c801d85f 461
49d5d881
VZ
462 default:
463 wxFAIL_MSG(_("invalid eof() return value."));
464 }
c801d85f 465
a62848fd 466 return true;
c801d85f
KB
467}
468
469// ============================================================================
470// implementation of wxTempFile
471// ============================================================================
472
473// ----------------------------------------------------------------------------
474// construction
475// ----------------------------------------------------------------------------
44b62d54 476
c801d85f
KB
477wxTempFile::wxTempFile(const wxString& strName)
478{
49d5d881 479 Open(strName);
c801d85f
KB
480}
481
482bool wxTempFile::Open(const wxString& strName)
483{
44b62d54
VZ
484 // we must have an absolute filename because otherwise CreateTempFileName()
485 // would create the temp file in $TMP (i.e. the system standard location
486 // for the temp files) which might be on another volume/drive/mount and
487 // wxRename()ing it later to m_strName from Commit() would then fail
488 //
489 // with the absolute filename, the temp file is created in the same
490 // directory as this one which ensures that wxRename() may work later
491 wxFileName fn(strName);
492 if ( !fn.IsAbsolute() )
493 {
494 fn.Normalize(wxPATH_NORM_ABSOLUTE);
495 }
496
497 m_strName = fn.GetFullPath();
246037e2 498
44b62d54 499 m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file);
ade35f11
VZ
500
501 if ( m_strTemp.empty() )
502 {
503 // CreateTempFileName() failed
a62848fd 504 return false;
ade35f11 505 }
49d5d881 506
49d5d881 507#ifdef __UNIX__
ade35f11
VZ
508 // the temp file should have the same permissions as the original one
509 mode_t mode;
a62848fd 510
f6bcfd97 511 wxStructStat st;
ca11abde 512 if ( stat( (const char*) m_strName.fn_str(), &st) == 0 )
49d5d881 513 {
ade35f11 514 mode = st.st_mode;
49d5d881
VZ
515 }
516 else
517 {
ade35f11 518 // file probably didn't exist, just give it the default mode _using_
68164137 519 // user's umask (new files creation should respect umask)
ade35f11
VZ
520 mode_t mask = umask(0777);
521 mode = 0666 & ~mask;
522 umask(mask);
49d5d881 523 }
49d5d881 524
ca11abde 525 if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 )
fe99e285 526 {
5a3912f2 527#ifndef __OS2__
ade35f11 528 wxLogSysError(_("Failed to set temporary file permissions"));
5a3912f2 529#endif
fe99e285 530 }
49d5d881 531#endif // Unix
246037e2 532
a62848fd 533 return true;
c801d85f
KB
534}
535
536// ----------------------------------------------------------------------------
537// destruction
538// ----------------------------------------------------------------------------
539
540wxTempFile::~wxTempFile()
541{
49d5d881
VZ
542 if ( IsOpened() )
543 Discard();
c801d85f
KB
544}
545
546bool wxTempFile::Commit()
547{
49d5d881 548 m_file.Close();
c801d85f 549
f6bcfd97 550 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881 551 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
a62848fd 552 return false;
49d5d881 553 }
c801d85f 554
f35746ce 555 if ( !wxRenameFile(m_strTemp, m_strName) ) {
49d5d881 556 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
a62848fd 557 return false;
49d5d881 558 }
c801d85f 559
a62848fd 560 return true;
c801d85f
KB
561}
562
563void wxTempFile::Discard()
564{
49d5d881 565 m_file.Close();
f6bcfd97 566 if ( wxRemove(m_strTemp) != 0 )
49d5d881 567 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 568}
ce4169a4 569
ade35f11 570#endif // wxUSE_FILE
cc985fac 571