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