]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
Temporary fix for wxFileName under Mac - ignore volumes.
[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"
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
fac3b423 31#if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__)
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
c801d85f 57#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b
VZ
58 #include <unistd.h>
59 #ifdef __GNUWIN32__
60 #include <windows.h>
61 #endif
c2ff79b1
DW
62#elif (defined(__WXPM__))
63 #include <io.h>
c2ff79b1
DW
64 #define W_OK 2
65 #define R_OK 4
34138703 66#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
67 // Have to ifdef this for different environments
68 #include <io.h>
17dff81c 69#elif (defined(__WXMAC__))
5b781a67 70#if __MSL__ < 0x6000
3f4a0c5b 71 int access( const char *path, int mode ) { return 0 ; }
5b781a67
SC
72#else
73 int _access( const char *path, int mode ) { return 0 ; }
74#endif
3f4a0c5b 75 char* mktemp( char * path ) { return path ;}
5b781a67 76 #include <stat.h>
3f4a0c5b
VZ
77 #define W_OK 2
78 #define R_OK 4
5b781a67 79 #include <unistd.h>
c801d85f 80#else
3f4a0c5b 81 #error "Please specify the header with file functions declarations."
c801d85f
KB
82#endif //Win/UNIX
83
84#include <stdio.h> // SEEK_xxx constants
85#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 86
469e1e5c 87#ifndef __MWERKS__
3f4a0c5b
VZ
88 #include <sys/types.h> // needed for stat
89 #include <sys/stat.h> // stat
de85a884
VZ
90#elif ( defined(__MWERKS__) && defined(__WXMSW__) )
91 #include <sys/types.h> // needed for stat
92 #include <sys/stat.h> // stat
469e1e5c 93#endif
c801d85f 94
3f4a0c5b 95#if defined(__BORLANDC__) || defined(_MSC_VER)
49d5d881
VZ
96 #define W_OK 2
97 #define R_OK 4
34138703
JS
98#endif
99
b12915c1
VZ
100// there is no distinction between text and binary files under Unix, so define
101// O_BINARY as 0 if the system headers don't do it already
102#if defined(__UNIX__) && !defined(O_BINARY)
49d5d881 103 #define O_BINARY (0)
246037e2 104#endif //__UNIX__
c801d85f 105
a3ef5bf5 106#ifdef __SALFORDC__
3f4a0c5b 107 #include <unix.h>
a3ef5bf5
JS
108#endif
109
81d66cf3 110#ifndef MAX_PATH
3f4a0c5b 111 #define MAX_PATH 512
81d66cf3 112#endif
c801d85f 113
49d5d881
VZ
114// some broken compilers don't have 3rd argument in open() and creat()
115#ifdef __SALFORDC__
116 #define ACCESS(access)
117 #define stat _stat
118#else // normal compiler
119 #define ACCESS(access) , (access)
120#endif // Salford C
121
f6bcfd97 122// wxWindows
ade35f11
VZ
123#ifndef WX_PRECOMP
124 #include "wx/string.h"
125 #include "wx/intl.h"
ade35f11
VZ
126 #include "wx/log.h"
127#endif // !WX_PRECOMP
128
129#include "wx/filename.h"
44d568b6 130#include "wx/file.h"
f6bcfd97 131
c801d85f
KB
132// ============================================================================
133// implementation of wxFile
134// ============================================================================
135
136// ----------------------------------------------------------------------------
137// static functions
138// ----------------------------------------------------------------------------
50920146 139bool wxFile::Exists(const wxChar *name)
246037e2 140{
f6bcfd97 141 wxStructStat st;
50920146 142#if wxUSE_UNICODE && wxMBFILES
dcf924a3 143 wxCharBuffer fname = wxConvFile.cWC2MB(name);
a3ef5bf5 144
f6bcfd97
BP
145 return !wxAccess(fname, 0) &&
146 !wxStat(wxMBSTRINGCAST fname, &st) &&
50920146 147 (st.st_mode & S_IFREG);
bedaf53e 148
50920146 149#else
f6bcfd97
BP
150 return !wxAccess(name, 0) &&
151 !wxStat(name, &st) &&
49d5d881 152 (st.st_mode & S_IFREG);
50920146 153#endif
d1427b70
VZ
154}
155
50920146 156bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 157{
49d5d881 158 int how = 0;
d1427b70 159
49d5d881
VZ
160 switch ( mode ) {
161 case read:
162 how = R_OK;
163 break;
d1427b70 164
49d5d881
VZ
165 case write:
166 how = W_OK;
167 break;
d1427b70 168
49d5d881 169 default:
223d09f6 170 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
49d5d881 171 }
d1427b70 172
f6bcfd97 173 return wxAccess(wxFNCONV(name), how) == 0;
c801d85f
KB
174}
175
176// ----------------------------------------------------------------------------
177// opening/closing
178// ----------------------------------------------------------------------------
179
180// ctors
50920146 181wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 182{
49d5d881
VZ
183 m_fd = fd_invalid;
184 m_error = FALSE;
c801d85f 185
49d5d881 186 Open(szFileName, mode);
c801d85f
KB
187}
188
c801d85f 189// create the file, fail if it already exists and bOverwrite
50920146 190bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 191{
49d5d881
VZ
192 // if bOverwrite we create a new file or truncate the existing one,
193 // otherwise we only create the new file and fail if it already exists
5fde6fcc 194#if defined(__WXMAC__) && !defined(__UNIX__)
5b781a67
SC
195 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
196 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
bedaf53e 197 int fd = creat( szFileName , accessMode);
7c74e7fe 198#else
f6bcfd97
BP
199 int fd = wxOpen(wxFNCONV(szFileName),
200 O_BINARY | O_WRONLY | O_CREAT |
201 (bOverwrite ? O_TRUNC : O_EXCL)
202 ACCESS(accessMode));
7c74e7fe 203#endif
49d5d881
VZ
204 if ( fd == -1 ) {
205 wxLogSysError(_("can't create file '%s'"), szFileName);
206 return FALSE;
207 }
208 else {
209 Attach(fd);
210 return TRUE;
211 }
c801d85f
KB
212}
213
214// open the file
50920146 215bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 216{
49d5d881 217 int flags = O_BINARY;
c801d85f 218
49d5d881
VZ
219 switch ( mode ) {
220 case read:
221 flags |= O_RDONLY;
222 break;
c801d85f 223
f6bcfd97
BP
224 case write_append:
225 if ( wxFile::Exists(szFileName) )
226 {
227 flags |= O_WRONLY | O_APPEND;
228 break;
229 }
230 //else: fall through as write_append is the same as write if the
231 // file doesn't exist
232
49d5d881
VZ
233 case write:
234 flags |= O_WRONLY | O_CREAT | O_TRUNC;
235 break;
61b02744 236
68164137
RL
237 case write_excl:
238 flags |= O_WRONLY | O_CREAT | O_EXCL;
239 break;
240
49d5d881
VZ
241 case read_write:
242 flags |= O_RDWR;
243 break;
244 }
c801d85f 245
f6bcfd97 246 int fd = wxOpen(wxFNCONV(szFileName), flags ACCESS(accessMode));
49d5d881
VZ
247 if ( fd == -1 ) {
248 wxLogSysError(_("can't open file '%s'"), szFileName);
249 return FALSE;
250 }
251 else {
252 Attach(fd);
253 return TRUE;
254 }
c801d85f
KB
255}
256
257// close
61b02744 258bool wxFile::Close()
c801d85f 259{
49d5d881
VZ
260 if ( IsOpened() ) {
261 if ( close(m_fd) == -1 ) {
262 wxLogSysError(_("can't close file descriptor %d"), m_fd);
263 m_fd = fd_invalid;
264 return FALSE;
265 }
266 else
267 m_fd = fd_invalid;
61b02744 268 }
61b02744 269
49d5d881 270 return TRUE;
c801d85f
KB
271}
272
273// ----------------------------------------------------------------------------
274// read/write
275// ----------------------------------------------------------------------------
276
277// read
278off_t wxFile::Read(void *pBuf, off_t nCount)
279{
49d5d881 280 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 281
469e1e5c 282#ifdef __MWERKS__
49d5d881 283 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 284#else
49d5d881 285 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 286#endif
49d5d881
VZ
287 if ( iRc == -1 ) {
288 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
289 return wxInvalidOffset;
290 }
291 else
292 return (size_t)iRc;
c801d85f
KB
293}
294
295// write
c86f1403 296size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 297{
49d5d881 298 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 299
469e1e5c 300#ifdef __MWERKS__
5b781a67
SC
301#if __MSL__ >= 0x6000
302 int iRc = ::write(m_fd, (void*) pBuf, nCount);
303#else
49d5d881 304 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
5b781a67 305#endif
469e1e5c 306#else
49d5d881 307 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 308#endif
49d5d881
VZ
309 if ( iRc == -1 ) {
310 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
311 m_error = TRUE;
312 return 0;
313 }
314 else
315 return iRc;
c801d85f
KB
316}
317
318// flush
319bool wxFile::Flush()
320{
49d5d881
VZ
321 if ( IsOpened() ) {
322#if defined(__VISUALC__) || wxHAVE_FSYNC
f6bcfd97 323 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
324 {
325 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
326 return FALSE;
327 }
49d5d881 328#else // no fsync
09914df7 329 // just do nothing
49d5d881
VZ
330#endif // fsync
331 }
c801d85f 332
49d5d881 333 return TRUE;
c801d85f
KB
334}
335
336// ----------------------------------------------------------------------------
337// seek
338// ----------------------------------------------------------------------------
339
340// seek
79c3e0e1 341off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f 342{
49d5d881
VZ
343 wxASSERT( IsOpened() );
344
a1b82138 345 int origin;
49d5d881 346 switch ( mode ) {
a1b82138
VZ
347 default:
348 wxFAIL_MSG(_("unknown seek origin"));
349
49d5d881 350 case wxFromStart:
a1b82138 351 origin = SEEK_SET;
49d5d881
VZ
352 break;
353
354 case wxFromCurrent:
a1b82138 355 origin = SEEK_CUR;
49d5d881
VZ
356 break;
357
358 case wxFromEnd:
a1b82138 359 origin = SEEK_END;
49d5d881 360 break;
49d5d881
VZ
361 }
362
a1b82138 363 int iRc = lseek(m_fd, ofs, origin);
49d5d881
VZ
364 if ( iRc == -1 ) {
365 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
366 return wxInvalidOffset;
367 }
368 else
369 return (off_t)iRc;
c801d85f
KB
370}
371
372// get current off_t
373off_t wxFile::Tell() const
374{
49d5d881
VZ
375 wxASSERT( IsOpened() );
376
f6bcfd97 377 int iRc = wxTell(m_fd);
49d5d881
VZ
378 if ( iRc == -1 ) {
379 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
380 return wxInvalidOffset;
381 }
382 else
383 return (off_t)iRc;
c801d85f
KB
384}
385
386// get current file length
387off_t wxFile::Length() const
388{
49d5d881 389 wxASSERT( IsOpened() );
c801d85f 390
49d5d881 391#ifdef __VISUALC__
c801d85f 392 int iRc = _filelength(m_fd);
49d5d881 393#else // !VC++
f6bcfd97 394 int iRc = wxTell(m_fd);
c801d85f 395 if ( iRc != -1 ) {
49d5d881
VZ
396 // @ have to use const_cast :-(
397 int iLen = ((wxFile *)this)->SeekEnd();
398 if ( iLen != -1 ) {
399 // restore old position
400 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
401 // error
402 iLen = -1;
403 }
c801d85f 404 }
c801d85f 405
49d5d881
VZ
406 iRc = iLen;
407 }
408#endif // VC++
409
410 if ( iRc == -1 ) {
411 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
412 return wxInvalidOffset;
c801d85f 413 }
49d5d881
VZ
414 else
415 return (off_t)iRc;
c801d85f
KB
416}
417
418// is end of file reached?
419bool wxFile::Eof() const
420{
49d5d881 421 wxASSERT( IsOpened() );
c801d85f 422
49d5d881 423 int iRc;
61b02744 424
49d5d881 425#if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744
VZ
426 // @@ this doesn't work, of course, on unseekable file descriptors
427 off_t ofsCur = Tell(),
49d5d881 428 ofsMax = Length();
1678ad78 429 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 430 iRc = -1;
61b02744 431 else
49d5d881
VZ
432 iRc = ofsCur == ofsMax;
433#else // Windows and "native" compiler
61b02744 434 iRc = eof(m_fd);
49d5d881 435#endif // Windows/Unix
c801d85f 436
49d5d881
VZ
437 switch ( iRc ) {
438 case 1:
439 break;
c801d85f 440
49d5d881
VZ
441 case 0:
442 return FALSE;
c801d85f 443
49d5d881 444 case -1:
8e3f1261 445 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
49d5d881 446 break;
c801d85f 447
49d5d881
VZ
448 default:
449 wxFAIL_MSG(_("invalid eof() return value."));
450 }
c801d85f 451
49d5d881 452 return TRUE;
c801d85f
KB
453}
454
455// ============================================================================
456// implementation of wxTempFile
457// ============================================================================
458
459// ----------------------------------------------------------------------------
460// construction
461// ----------------------------------------------------------------------------
462wxTempFile::wxTempFile(const wxString& strName)
463{
49d5d881 464 Open(strName);
c801d85f
KB
465}
466
467bool wxTempFile::Open(const wxString& strName)
468{
49d5d881 469 m_strName = strName;
246037e2 470
ade35f11
VZ
471 m_strTemp = wxFileName::CreateTempFileName(strName);
472
473 if ( m_strTemp.empty() )
474 {
475 // CreateTempFileName() failed
476 return FALSE;
477 }
49d5d881 478
3f247475
VZ
479 // actually open the file now (it must already exist)
480 if ( !m_file.Open(m_strTemp, wxFile::write) )
481 {
482 // opening existing file failed?
483 return FALSE;
484 }
485
49d5d881 486#ifdef __UNIX__
ade35f11
VZ
487 // the temp file should have the same permissions as the original one
488 mode_t mode;
fe99e285 489
f6bcfd97 490 wxStructStat st;
50920146 491 if ( stat(strName.fn_str(), &st) == 0 )
49d5d881 492 {
ade35f11 493 mode = st.st_mode;
49d5d881
VZ
494 }
495 else
496 {
ade35f11 497 // file probably didn't exist, just give it the default mode _using_
68164137 498 // user's umask (new files creation should respect umask)
ade35f11
VZ
499 mode_t mask = umask(0777);
500 mode = 0666 & ~mask;
501 umask(mask);
49d5d881 502 }
49d5d881 503
ade35f11 504 if ( chmod(m_strTemp.mb_str(), mode) == -1 )
fe99e285 505 {
ade35f11 506 wxLogSysError(_("Failed to set temporary file permissions"));
fe99e285 507 }
49d5d881 508#endif // Unix
246037e2 509
ade35f11 510 return TRUE;
c801d85f
KB
511}
512
513// ----------------------------------------------------------------------------
514// destruction
515// ----------------------------------------------------------------------------
516
517wxTempFile::~wxTempFile()
518{
49d5d881
VZ
519 if ( IsOpened() )
520 Discard();
c801d85f
KB
521}
522
523bool wxTempFile::Commit()
524{
49d5d881 525 m_file.Close();
c801d85f 526
f6bcfd97 527 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881
VZ
528 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
529 return FALSE;
530 }
c801d85f 531
f6bcfd97 532 if ( wxRename(m_strTemp, m_strName) != 0 ) {
49d5d881
VZ
533 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
534 return FALSE;
535 }
c801d85f 536
49d5d881 537 return TRUE;
c801d85f
KB
538}
539
540void wxTempFile::Discard()
541{
49d5d881 542 m_file.Close();
f6bcfd97 543 if ( wxRemove(m_strTemp) != 0 )
49d5d881 544 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 545}
ce4169a4 546
ade35f11 547#endif // wxUSE_FILE
cc985fac 548