]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
an attempt to fix glibc2.0 stupidness
[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>
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"
23#include "wx/defs.h"
24
25#ifdef __BORLANDC__
3f4a0c5b 26 #pragma hdrstop
c801d85f
KB
27#endif
28
29// standard
246037e2 30#if defined(__WXMSW__) && !defined(__GNUWIN32__)
c801d85f 31 #include <io.h>
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
49d5d881 56 #include <windows.h> // for GetTempFileName
c801d85f 57#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b
VZ
58 #include <unistd.h>
59 #ifdef __GNUWIN32__
60 #include <windows.h>
61 #endif
34138703 62#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
63 // Have to ifdef this for different environments
64 #include <io.h>
17dff81c 65#elif (defined(__WXMAC__))
3f4a0c5b
VZ
66 int access( const char *path, int mode ) { return 0 ; }
67 char* mktemp( char * path ) { return path ;}
68 #include <unistd.h>
69 #include <unix.h>
70 #define W_OK 2
71 #define R_OK 4
c801d85f 72#else
3f4a0c5b 73 #error "Please specify the header with file functions declarations."
c801d85f
KB
74#endif //Win/UNIX
75
76#include <stdio.h> // SEEK_xxx constants
77#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 78
469e1e5c 79#ifndef __MWERKS__
3f4a0c5b
VZ
80 #include <sys/types.h> // needed for stat
81 #include <sys/stat.h> // stat
469e1e5c 82#endif
c801d85f
KB
83
84// Microsoft compiler loves underscores, feed them to it
3f4a0c5b 85#ifdef __VISUALC__
49d5d881
VZ
86 // functions
87 #define open _open
88 #define close _close
89 #define read _read
90 #define write _write
91 #define lseek _lseek
92 #define fsync _commit
93 #define access _access
94 #define eof _eof
95
96 // types
97 #define stat _stat
98
99 // constants
100
101 #define O_RDONLY _O_RDONLY
102 #define O_WRONLY _O_WRONLY
103 #define O_RDWR _O_RDWR
104 #define O_EXCL _O_EXCL
105 #define O_CREAT _O_CREAT
106 #define O_BINARY _O_BINARY
107
108 #define S_IFDIR _S_IFDIR
109 #define S_IFREG _S_IFREG
c801d85f 110#else
49d5d881 111 #define tell(fd) lseek(fd, 0, SEEK_CUR)
3f4a0c5b 112#endif // VC++
c801d85f 113
3f4a0c5b 114#if defined(__BORLANDC__) || defined(_MSC_VER)
49d5d881
VZ
115 #define W_OK 2
116 #define R_OK 4
34138703
JS
117#endif
118
c801d85f 119// there is no distinction between text and binary files under Unix
246037e2 120#ifdef __UNIX__
49d5d881 121 #define O_BINARY (0)
246037e2 122#endif //__UNIX__
c801d85f 123
a3ef5bf5 124#ifdef __SALFORDC__
3f4a0c5b 125 #include <unix.h>
a3ef5bf5
JS
126#endif
127
c801d85f
KB
128// wxWindows
129#include <wx/string.h>
130#include <wx/intl.h>
131#include <wx/file.h>
132#include <wx/log.h>
133
81d66cf3 134#ifndef MAX_PATH
3f4a0c5b 135 #define MAX_PATH 512
81d66cf3 136#endif
c801d85f 137
169935ad 138#ifdef __WXMAC__
3f4a0c5b
VZ
139 char gwxMacFileName[ MAX_PATH ] ;
140 char gwxMacFileName2[ MAX_PATH ] ;
141 char gwxMacFileName3[ MAX_PATH ] ;
169935ad
SC
142#endif
143
49d5d881
VZ
144// some broken compilers don't have 3rd argument in open() and creat()
145#ifdef __SALFORDC__
146 #define ACCESS(access)
147 #define stat _stat
148#else // normal compiler
149 #define ACCESS(access) , (access)
150#endif // Salford C
151
c801d85f
KB
152// ============================================================================
153// implementation of wxFile
154// ============================================================================
155
156// ----------------------------------------------------------------------------
157// static functions
158// ----------------------------------------------------------------------------
50920146 159bool wxFile::Exists(const wxChar *name)
246037e2 160{
49d5d881 161 struct stat st;
50920146
OK
162#if wxUSE_UNICODE && wxMBFILES
163 wxCharBuffer fname = wxConv_file.cWC2MB(name);
a3ef5bf5 164
50920146
OK
165 return !access(fname, 0) &&
166 !stat(MBSTRINGCAST fname, &st) &&
167 (st.st_mode & S_IFREG);
168#else
49d5d881 169 return !access(name, 0) &&
50920146 170 !stat((wxChar*) name, &st) &&
49d5d881 171 (st.st_mode & S_IFREG);
50920146 172#endif
d1427b70
VZ
173}
174
50920146 175bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 176{
49d5d881 177 int how = 0;
d1427b70 178
49d5d881
VZ
179 switch ( mode ) {
180 case read:
181 how = R_OK;
182 break;
d1427b70 183
49d5d881
VZ
184 case write:
185 how = W_OK;
186 break;
d1427b70 187
49d5d881 188 default:
50920146 189 wxFAIL_MSG(_T("bad wxFile::Access mode parameter."));
49d5d881 190 }
d1427b70 191
50920146 192 return access(wxFNCONV(name), how) == 0;
c801d85f
KB
193}
194
195// ----------------------------------------------------------------------------
196// opening/closing
197// ----------------------------------------------------------------------------
198
199// ctors
50920146 200wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 201{
49d5d881
VZ
202 m_fd = fd_invalid;
203 m_error = FALSE;
c801d85f 204
49d5d881 205 Open(szFileName, mode);
c801d85f
KB
206}
207
208// dtor
209wxFile::~wxFile()
210{
49d5d881 211 Close();
c801d85f
KB
212}
213
214// create the file, fail if it already exists and bOverwrite
50920146 215bool 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 234bool 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 269bool 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
289off_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 307size_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
326bool 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 348off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f 349{
49d5d881
VZ
350 wxASSERT( IsOpened() );
351
352 int flag = -1;
353 switch ( mode ) {
354 case wxFromStart:
355 flag = SEEK_SET;
356 break;
357
358 case wxFromCurrent:
359 flag = SEEK_CUR;
360 break;
361
362 case wxFromEnd:
363 flag = SEEK_END;
364 break;
365
366 default:
367 wxFAIL_MSG(_("unknown seek origin"));
368 }
369
370 int iRc = lseek(m_fd, ofs, flag);
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
380off_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
394off_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?
426bool 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// ----------------------------------------------------------------------------
470wxTempFile::wxTempFile(const wxString& strName)
471{
49d5d881 472 Open(strName);
c801d85f
KB
473}
474
475bool 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
49d5d881 487#else // Windows
30a5be97
VZ
488 wxString strPath;
489 wxSplitPath(strName, &strPath, NULL, NULL);
490 if ( strPath.IsEmpty() )
50920146 491 strPath = _T('.'); // GetTempFileName will fail if we give it empty string
81d66cf3 492#ifdef __WIN32__
50920146 493 if ( !GetTempFileName(strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 494#else
49d5d881 495 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
50920146 496 if ( !GetTempFileName((BYTE) (const wxChar*) strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 497#endif
50920146 498 wxLogLastError(_T("GetTempFileName"));
30a5be97 499 m_strTemp.UngetWriteBuf();
49d5d881
VZ
500#endif // Windows/Unix
501
502 int access = wxS_DEFAULT;
503#ifdef __UNIX__
504 // create the file with the same mode as the original one under Unix
505 struct stat st;
50920146 506 if ( stat(strName.fn_str(), &st) == 0 )
49d5d881
VZ
507 {
508 // this assumes that only lower bits of st_mode contain the access
509 // rights, but it's true for at least all Unices which have S_IXXXX()
510 // macros, so should not be less portable than using (not POSIX)
511 // S_IFREG &c
512 access = st.st_mode & 0777;
513 }
514 else
515 {
50920146 516 wxLogLastError(_T("stat"));
49d5d881
VZ
517 }
518
519 // we want to create the file with exactly the same access rights as the
520 // original one, so disable the user's umask for the moment
521 mode_t umaskOld = umask(0);
522#endif // Unix
523
524 bool ok = m_file.Open(m_strTemp, wxFile::write, access);
525
526#ifdef __UNIX__
527 // restore umask now that the file is created
528 (void)umask(umaskOld);
529#endif // Unix
246037e2 530
49d5d881 531 return ok;
c801d85f
KB
532}
533
534// ----------------------------------------------------------------------------
535// destruction
536// ----------------------------------------------------------------------------
537
538wxTempFile::~wxTempFile()
539{
49d5d881
VZ
540 if ( IsOpened() )
541 Discard();
c801d85f
KB
542}
543
544bool wxTempFile::Commit()
545{
49d5d881 546 m_file.Close();
c801d85f 547
50920146 548 if ( wxFile::Exists(m_strName) && remove(m_strName.fn_str()) != 0 ) {
49d5d881
VZ
549 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
550 return FALSE;
551 }
c801d85f 552
50920146 553 if ( rename(m_strTemp.fn_str(), m_strName.fn_str()) != 0 ) {
49d5d881
VZ
554 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
555 return FALSE;
556 }
c801d85f 557
49d5d881 558 return TRUE;
c801d85f
KB
559}
560
561void wxTempFile::Discard()
562{
49d5d881 563 m_file.Close();
50920146 564 if ( remove(m_strTemp.fn_str()) != 0 )
49d5d881 565 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 566}