]>
git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
1 /////////////////////////////////////////////////////////////////////////////
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__)
30 #define WIN32_LEAN_AND_MEAN
52 #elif defined(__WXMSW__) && defined(__WXWINCE__)
53 #include "wx/msw/missing.h"
54 #elif (defined(__OS2__))
56 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
61 #include "wx/msw/wrapwin.h"
63 #elif defined(__DOS__)
64 #if defined(__WATCOMC__)
66 #elif defined(__DJGPP__)
71 #error "Please specify the header with file functions declarations."
73 #elif (defined(__WXSTUBS__))
74 // Have to ifdef this for different environments
76 #elif (defined(__WXMAC__))
78 int access( const char *path
, int mode
) { return 0 ; }
80 int _access( const char *path
, int mode
) { return 0 ; }
82 char* mktemp( char * path
) { return path
;}
86 #error "Please specify the header with file functions declarations."
89 #include <stdio.h> // SEEK_xxx constants
91 // Windows compilers don't have these constants
95 F_OK
= 0, // test for existence
96 X_OK
= 1, // execute permission
102 // there is no distinction between text and binary files under Unix, so define
103 // O_BINARY as 0 if the system headers don't do it already
104 #if defined(__UNIX__) && !defined(O_BINARY)
116 // some broken compilers don't have 3rd argument in open() and creat()
118 #define ACCESS(access)
120 #else // normal compiler
121 #define ACCESS(access) , (access)
126 #include "wx/string.h"
129 #endif // !WX_PRECOMP
131 #include "wx/filename.h"
133 #include "wx/filefn.h"
136 #include "wx/msw/mslu.h"
140 #include "wx/msw/private.h"
144 // ============================================================================
145 // implementation of wxFile
146 // ============================================================================
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 bool wxFile::Exists(const wxChar
*name
)
154 return wxFileExists(name
);
157 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
164 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
180 return wxAccess(name
, how
) == 0;
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
188 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
193 Open(szFileName
, mode
);
196 // create the file, fail if it already exists and bOverwrite
197 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
199 // if bOverwrite we create a new file or truncate the existing one,
200 // otherwise we only create the new file and fail if it already exists
201 #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
202 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
203 // int fd = open( szFileName , O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
204 int fd
= creat( szFileName
, accessMode
);
206 int fd
= wxOpen( szFileName
,
207 O_BINARY
| O_WRONLY
| O_CREAT
|
208 (bOverwrite
? O_TRUNC
: O_EXCL
)
209 ACCESS(accessMode
) );
213 wxLogSysError(_("can't create file '%s'"), szFileName
);
222 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
224 int flags
= O_BINARY
;
233 if ( wxFile::Exists(szFileName
) )
235 flags
|= O_WRONLY
| O_APPEND
;
238 //else: fall through as write_append is the same as write if the
239 // file doesn't exist
242 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
246 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
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__
261 int fd
= wxOpen( szFileName
, flags
ACCESS(accessMode
));
265 wxLogSysError(_("can't open file '%s'"), szFileName
);
277 if (wxClose(m_fd
) == -1)
279 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
295 ssize_t
wxFile::Read(void *pBuf
, size_t nCount
)
297 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
299 ssize_t iRc
= wxRead(m_fd
, pBuf
, nCount
);
303 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
304 return wxInvalidOffset
;
311 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
313 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
315 ssize_t iRc
= wxWrite(m_fd
, pBuf
, nCount
);
319 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
330 #if defined(__VISUALC__) || defined(HAVE_FSYNC)
331 // fsync() only works on disk files and returns errors for pipes, don't
333 if ( IsOpened() && GetKind() == wxFILE_KIND_DISK
)
335 if ( wxFsync(m_fd
) == -1 )
337 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
351 wxFileOffset
wxFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
353 wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") );
354 wxCHECK_MSG( ofs
!= wxInvalidOffset
|| mode
!= wxFromStart
,
356 _T("invalid absolute file offset") );
361 wxFAIL_MSG(_("unknown seek origin"));
376 wxFileOffset iRc
= wxSeek(m_fd
, ofs
, origin
);
377 if ( iRc
== wxInvalidOffset
)
379 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
385 // get current file offset
386 wxFileOffset
wxFile::Tell() const
388 wxASSERT( IsOpened() );
390 wxFileOffset iRc
= wxTell(m_fd
);
391 if ( iRc
== wxInvalidOffset
)
393 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
399 // get current file length
400 wxFileOffset
wxFile::Length() const
402 wxASSERT( IsOpened() );
404 wxFileOffset iRc
= Tell();
405 if ( iRc
!= wxInvalidOffset
) {
406 // have to use const_cast :-(
407 wxFileOffset iLen
= ((wxFile
*)this)->SeekEnd();
408 if ( iLen
!= wxInvalidOffset
) {
409 // restore old position
410 if ( ((wxFile
*)this)->Seek(iRc
) == wxInvalidOffset
) {
412 iLen
= wxInvalidOffset
;
419 if ( iRc
== wxInvalidOffset
)
421 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
427 // is end of file reached?
428 bool wxFile::Eof() const
430 wxASSERT( IsOpened() );
434 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
435 // @@ this doesn't work, of course, on unseekable file descriptors
436 wxFileOffset ofsCur
= Tell(),
438 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
439 iRc
= wxInvalidOffset
;
441 iRc
= ofsCur
== ofsMax
;
442 #else // Windows and "native" compiler
444 #endif // Windows/Unix
450 else if ( iRc
== wxInvalidOffset
)
451 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
453 wxFAIL_MSG(_("invalid eof() return value."));
458 // ============================================================================
459 // implementation of wxTempFile
460 // ============================================================================
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 wxTempFile::wxTempFile(const wxString
& strName
)
471 bool wxTempFile::Open(const wxString
& strName
)
473 // we must have an absolute filename because otherwise CreateTempFileName()
474 // would create the temp file in $TMP (i.e. the system standard location
475 // for the temp files) which might be on another volume/drive/mount and
476 // wxRename()ing it later to m_strName from Commit() would then fail
478 // with the absolute filename, the temp file is created in the same
479 // directory as this one which ensures that wxRename() may work later
480 wxFileName
fn(strName
);
481 if ( !fn
.IsAbsolute() )
483 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
486 m_strName
= fn
.GetFullPath();
488 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
490 if ( m_strTemp
.empty() )
492 // CreateTempFileName() failed
497 // the temp file should have the same permissions as the original one
501 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
507 // file probably didn't exist, just give it the default mode _using_
508 // user's umask (new files creation should respect umask)
509 mode_t mask
= umask(0777);
514 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
517 wxLogSysError(_("Failed to set temporary file permissions"));
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
529 wxTempFile::~wxTempFile()
535 bool wxTempFile::Commit()
539 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
540 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
544 if ( !wxRenameFile(m_strTemp
, m_strName
) ) {
545 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
552 void wxTempFile::Discard()
555 if ( wxRemove(m_strTemp
) != 0 )
556 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());