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