]>
git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/file.cpp
3 // Purpose: wxFile - encapsulates low-level "file descriptor"
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
27 #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
29 #define WIN32_LEAN_AND_MEAN
50 #elif defined(__WXMSW__) && defined(__WXWINCE__)
51 #include "wx/msw/missing.h"
52 #elif (defined(__OS2__))
54 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
59 #include "wx/msw/wrapwin.h"
61 #elif defined(__DOS__)
62 #if defined(__WATCOMC__)
64 #elif defined(__DJGPP__)
69 #error "Please specify the header with file functions declarations."
71 #elif (defined(__WXSTUBS__))
72 // Have to ifdef this for different environments
74 #elif (defined(__WXMAC__))
76 int access( const char *path
, int mode
) { return 0 ; }
78 int _access( const char *path
, int mode
) { return 0 ; }
80 char* mktemp( char * path
) { return path
;}
84 #error "Please specify the header with file functions declarations."
87 #include <stdio.h> // SEEK_xxx constants
89 // Windows compilers don't have these constants
93 F_OK
= 0, // test for existence
94 X_OK
= 1, // execute permission
102 #include "wx/string.h"
106 #endif // !WX_PRECOMP
108 #include "wx/filename.h"
110 #include "wx/filefn.h"
112 // there is no distinction between text and binary files under Unix, so define
113 // O_BINARY as 0 if the system headers don't do it already
114 #if defined(__UNIX__) && !defined(O_BINARY)
119 #include "wx/msw/mslu.h"
123 #include "wx/msw/private.h"
130 // ============================================================================
131 // implementation of wxFile
132 // ============================================================================
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 bool wxFile::Exists(const wxString
& name
)
140 return wxFileExists(name
);
143 bool wxFile::Access(const wxString
& name
, OpenMode mode
)
150 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
166 return wxAccess(name
, how
) == 0;
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
174 wxFile::wxFile(const wxString
& fileName
, OpenMode mode
)
179 Open(fileName
, mode
);
182 // create the file, fail if it already exists and bOverwrite
183 bool wxFile::Create(const wxString
& fileName
, bool bOverwrite
, int accessMode
)
185 // if bOverwrite we create a new file or truncate the existing one,
186 // otherwise we only create the new file and fail if it already exists
187 int fd
= wxOpen( fileName
,
188 O_BINARY
| O_WRONLY
| O_CREAT
|
189 (bOverwrite
? O_TRUNC
: O_EXCL
),
193 wxLogSysError(_("can't create file '%s'"), fileName
);
202 bool wxFile::Open(const wxString
& fileName
, OpenMode mode
, int accessMode
)
204 int flags
= O_BINARY
;
213 if ( wxFile::Exists(fileName
) )
215 flags
|= O_WRONLY
| O_APPEND
;
218 //else: fall through as write_append is the same as write if the
219 // file doesn't exist
222 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
226 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
235 // only read/write bits for "all" are supported by this function under
236 // Windows, and VC++ 8 returns EINVAL if any other bits are used in
237 // accessMode, so clear them as they have at best no effect anyhow
238 accessMode
&= wxS_IRUSR
| wxS_IWUSR
;
239 #endif // __WINDOWS__
241 int fd
= wxOpen( fileName
, flags
, accessMode
);
245 wxLogSysError(_("can't open file '%s'"), fileName
);
257 if (wxClose(m_fd
) == -1)
259 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
275 ssize_t
wxFile::Read(void *pBuf
, size_t nCount
)
277 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
279 ssize_t iRc
= wxRead(m_fd
, pBuf
, nCount
);
283 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
284 return wxInvalidOffset
;
291 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
293 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
295 ssize_t iRc
= wxWrite(m_fd
, pBuf
, nCount
);
299 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
307 bool wxFile::Write(const wxString
& s
, const wxMBConv
& conv
)
309 const wxWX2MBbuf buf
= s
.mb_str(conv
);
313 const size_t size
= strlen(buf
); // FIXME: use buf.length() when available
314 return Write(buf
, size
) == size
;
321 // fsync() only works on disk files and returns errors for pipes, don't
323 if ( IsOpened() && GetKind() == wxFILE_KIND_DISK
)
325 if ( wxFsync(m_fd
) == -1 )
327 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
341 wxFileOffset
wxFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
343 wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") );
344 wxCHECK_MSG( ofs
!= wxInvalidOffset
|| mode
!= wxFromStart
,
346 _T("invalid absolute file offset") );
351 wxFAIL_MSG(_T("unknown seek origin"));
366 wxFileOffset iRc
= wxSeek(m_fd
, ofs
, origin
);
367 if ( iRc
== wxInvalidOffset
)
369 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
375 // get current file offset
376 wxFileOffset
wxFile::Tell() const
378 wxASSERT( IsOpened() );
380 wxFileOffset iRc
= wxTell(m_fd
);
381 if ( iRc
== wxInvalidOffset
)
383 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
389 // get current file length
390 wxFileOffset
wxFile::Length() const
392 wxASSERT( IsOpened() );
394 wxFileOffset iRc
= Tell();
395 if ( iRc
!= wxInvalidOffset
) {
396 // have to use const_cast :-(
397 wxFileOffset iLen
= ((wxFile
*)this)->SeekEnd();
398 if ( iLen
!= wxInvalidOffset
) {
399 // restore old position
400 if ( ((wxFile
*)this)->Seek(iRc
) == wxInvalidOffset
) {
402 iLen
= wxInvalidOffset
;
409 if ( iRc
== wxInvalidOffset
)
411 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
417 // is end of file reached?
418 bool wxFile::Eof() const
420 wxASSERT( IsOpened() );
424 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ )
425 // @@ this doesn't work, of course, on unseekable file descriptors
426 wxFileOffset ofsCur
= Tell(),
428 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
429 iRc
= wxInvalidOffset
;
431 iRc
= ofsCur
== ofsMax
;
432 #else // Windows and "native" compiler
434 #endif // Windows/Unix
440 else if ( iRc
== wxInvalidOffset
)
441 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
443 wxFAIL_MSG(_T("invalid eof() return value."));
448 // ============================================================================
449 // implementation of wxTempFile
450 // ============================================================================
452 // ----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
456 wxTempFile::wxTempFile(const wxString
& strName
)
461 bool wxTempFile::Open(const wxString
& strName
)
463 // we must have an absolute filename because otherwise CreateTempFileName()
464 // would create the temp file in $TMP (i.e. the system standard location
465 // for the temp files) which might be on another volume/drive/mount and
466 // wxRename()ing it later to m_strName from Commit() would then fail
468 // with the absolute filename, the temp file is created in the same
469 // directory as this one which ensures that wxRename() may work later
470 wxFileName
fn(strName
);
471 if ( !fn
.IsAbsolute() )
473 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
476 m_strName
= fn
.GetFullPath();
478 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
480 if ( m_strTemp
.empty() )
482 // CreateTempFileName() failed
487 // the temp file should have the same permissions as the original one
491 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
497 // file probably didn't exist, just give it the default mode _using_
498 // user's umask (new files creation should respect umask)
499 mode_t mask
= umask(0777);
504 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
507 wxLogSysError(_("Failed to set temporary file permissions"));
515 // ----------------------------------------------------------------------------
517 // ----------------------------------------------------------------------------
519 wxTempFile::~wxTempFile()
525 bool wxTempFile::Commit()
529 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
530 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
534 if ( !wxRenameFile(m_strTemp
, m_strName
) ) {
535 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
542 void wxTempFile::Discard()
545 if ( wxRemove(m_strTemp
) != 0 )
546 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());