]>
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 | #ifdef __SALFORDC__ | |
103 | #include <unix.h> | |
104 | #endif | |
105 | ||
106 | // some broken compilers don't have 3rd argument in open() and creat() | |
107 | #ifdef __SALFORDC__ | |
108 | #define ACCESS(access) | |
109 | #define stat _stat | |
110 | #else // normal compiler | |
111 | #define ACCESS(access) , (access) | |
112 | #endif // Salford C | |
113 | ||
114 | // wxWidgets | |
115 | #ifndef WX_PRECOMP | |
116 | #include "wx/string.h" | |
117 | #include "wx/intl.h" | |
118 | #include "wx/log.h" | |
119 | #include "wx/crt.h" | |
120 | #endif // !WX_PRECOMP | |
121 | ||
122 | #include "wx/filename.h" | |
123 | #include "wx/file.h" | |
124 | #include "wx/filefn.h" | |
125 | ||
126 | // there is no distinction between text and binary files under Unix, so define | |
127 | // O_BINARY as 0 if the system headers don't do it already | |
128 | #if defined(__UNIX__) && !defined(O_BINARY) | |
129 | #define O_BINARY (0) | |
130 | #endif //__UNIX__ | |
131 | ||
132 | #ifdef __WXMSW__ | |
133 | #include "wx/msw/mslu.h" | |
134 | #endif | |
135 | ||
136 | #ifdef __WXWINCE__ | |
137 | #include "wx/msw/private.h" | |
138 | #endif | |
139 | ||
140 | #ifndef MAX_PATH | |
141 | #define MAX_PATH 512 | |
142 | #endif | |
143 | ||
144 | // ============================================================================ | |
145 | // implementation of wxFile | |
146 | // ============================================================================ | |
147 | ||
148 | // ---------------------------------------------------------------------------- | |
149 | // static functions | |
150 | // ---------------------------------------------------------------------------- | |
151 | ||
152 | bool wxFile::Exists(const wxString& name) | |
153 | { | |
154 | return wxFileExists(name); | |
155 | } | |
156 | ||
157 | bool wxFile::Access(const wxString& 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 wxString& fileName, OpenMode mode) | |
189 | { | |
190 | m_fd = fd_invalid; | |
191 | m_error = false; | |
192 | ||
193 | Open(fileName, mode); | |
194 | } | |
195 | ||
196 | // create the file, fail if it already exists and bOverwrite | |
197 | bool wxFile::Create(const wxString& fileName, 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 | int fd = wxOpen( fileName, | |
202 | O_BINARY | O_WRONLY | O_CREAT | | |
203 | (bOverwrite ? O_TRUNC : O_EXCL) | |
204 | ACCESS(accessMode) ); | |
205 | if ( fd == -1 ) | |
206 | { | |
207 | wxLogSysError(_("can't create file '%s'"), fileName); | |
208 | return false; | |
209 | } | |
210 | ||
211 | Attach(fd); | |
212 | return true; | |
213 | } | |
214 | ||
215 | // open the file | |
216 | bool wxFile::Open(const wxString& fileName, OpenMode mode, int accessMode) | |
217 | { | |
218 | int flags = O_BINARY; | |
219 | ||
220 | switch ( mode ) | |
221 | { | |
222 | case read: | |
223 | flags |= O_RDONLY; | |
224 | break; | |
225 | ||
226 | case write_append: | |
227 | if ( wxFile::Exists(fileName) ) | |
228 | { | |
229 | flags |= O_WRONLY | O_APPEND; | |
230 | break; | |
231 | } | |
232 | //else: fall through as write_append is the same as write if the | |
233 | // file doesn't exist | |
234 | ||
235 | case write: | |
236 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
237 | break; | |
238 | ||
239 | case write_excl: | |
240 | flags |= O_WRONLY | O_CREAT | O_EXCL; | |
241 | break; | |
242 | ||
243 | case read_write: | |
244 | flags |= O_RDWR; | |
245 | break; | |
246 | } | |
247 | ||
248 | #ifdef __WINDOWS__ | |
249 | // only read/write bits for "all" are supported by this function under | |
250 | // Windows, and VC++ 8 returns EINVAL if any other bits are used in | |
251 | // accessMode, so clear them as they have at best no effect anyhow | |
252 | accessMode &= wxS_IRUSR | wxS_IWUSR; | |
253 | #endif // __WINDOWS__ | |
254 | ||
255 | int fd = wxOpen( fileName, flags ACCESS(accessMode)); | |
256 | ||
257 | if ( fd == -1 ) | |
258 | { | |
259 | wxLogSysError(_("can't open file '%s'"), fileName); | |
260 | return false; | |
261 | } | |
262 | ||
263 | Attach(fd); | |
264 | return true; | |
265 | } | |
266 | ||
267 | // close | |
268 | bool wxFile::Close() | |
269 | { | |
270 | if ( IsOpened() ) { | |
271 | if (wxClose(m_fd) == -1) | |
272 | { | |
273 | wxLogSysError(_("can't close file descriptor %d"), m_fd); | |
274 | m_fd = fd_invalid; | |
275 | return false; | |
276 | } | |
277 | else | |
278 | m_fd = fd_invalid; | |
279 | } | |
280 | ||
281 | return true; | |
282 | } | |
283 | ||
284 | // ---------------------------------------------------------------------------- | |
285 | // read/write | |
286 | // ---------------------------------------------------------------------------- | |
287 | ||
288 | // read | |
289 | ssize_t wxFile::Read(void *pBuf, size_t nCount) | |
290 | { | |
291 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); | |
292 | ||
293 | ssize_t iRc = wxRead(m_fd, pBuf, nCount); | |
294 | ||
295 | if ( iRc == -1 ) | |
296 | { | |
297 | wxLogSysError(_("can't read from file descriptor %d"), m_fd); | |
298 | return wxInvalidOffset; | |
299 | } | |
300 | ||
301 | return iRc; | |
302 | } | |
303 | ||
304 | // write | |
305 | size_t wxFile::Write(const void *pBuf, size_t nCount) | |
306 | { | |
307 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); | |
308 | ||
309 | ssize_t iRc = wxWrite(m_fd, pBuf, nCount); | |
310 | ||
311 | if ( iRc == -1 ) | |
312 | { | |
313 | wxLogSysError(_("can't write to file descriptor %d"), m_fd); | |
314 | m_error = true; | |
315 | iRc = 0; | |
316 | } | |
317 | ||
318 | return iRc; | |
319 | } | |
320 | ||
321 | bool wxFile::Write(const wxString& s, const wxMBConv& conv) | |
322 | { | |
323 | const wxWX2MBbuf buf = s.mb_str(conv); | |
324 | if ( !buf ) | |
325 | return false; | |
326 | ||
327 | const size_t size = strlen(buf); // FIXME: use buf.length() when available | |
328 | return Write(buf, size) == size; | |
329 | } | |
330 | ||
331 | // flush | |
332 | bool wxFile::Flush() | |
333 | { | |
334 | #ifdef HAVE_FSYNC | |
335 | // fsync() only works on disk files and returns errors for pipes, don't | |
336 | // call it then | |
337 | if ( IsOpened() && GetKind() == wxFILE_KIND_DISK ) | |
338 | { | |
339 | if ( wxFsync(m_fd) == -1 ) | |
340 | { | |
341 | wxLogSysError(_("can't flush file descriptor %d"), m_fd); | |
342 | return false; | |
343 | } | |
344 | } | |
345 | #endif // HAVE_FSYNC | |
346 | ||
347 | return true; | |
348 | } | |
349 | ||
350 | // ---------------------------------------------------------------------------- | |
351 | // seek | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | // seek | |
355 | wxFileOffset wxFile::Seek(wxFileOffset ofs, wxSeekMode mode) | |
356 | { | |
357 | wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") ); | |
358 | wxCHECK_MSG( ofs != wxInvalidOffset || mode != wxFromStart, | |
359 | wxInvalidOffset, | |
360 | _T("invalid absolute file offset") ); | |
361 | ||
362 | int origin; | |
363 | switch ( mode ) { | |
364 | default: | |
365 | wxFAIL_MSG(_T("unknown seek origin")); | |
366 | ||
367 | case wxFromStart: | |
368 | origin = SEEK_SET; | |
369 | break; | |
370 | ||
371 | case wxFromCurrent: | |
372 | origin = SEEK_CUR; | |
373 | break; | |
374 | ||
375 | case wxFromEnd: | |
376 | origin = SEEK_END; | |
377 | break; | |
378 | } | |
379 | ||
380 | wxFileOffset iRc = wxSeek(m_fd, ofs, origin); | |
381 | if ( iRc == wxInvalidOffset ) | |
382 | { | |
383 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); | |
384 | } | |
385 | ||
386 | return iRc; | |
387 | } | |
388 | ||
389 | // get current file offset | |
390 | wxFileOffset wxFile::Tell() const | |
391 | { | |
392 | wxASSERT( IsOpened() ); | |
393 | ||
394 | wxFileOffset iRc = wxTell(m_fd); | |
395 | if ( iRc == wxInvalidOffset ) | |
396 | { | |
397 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); | |
398 | } | |
399 | ||
400 | return iRc; | |
401 | } | |
402 | ||
403 | // get current file length | |
404 | wxFileOffset wxFile::Length() const | |
405 | { | |
406 | wxASSERT( IsOpened() ); | |
407 | ||
408 | wxFileOffset iRc = Tell(); | |
409 | if ( iRc != wxInvalidOffset ) { | |
410 | // have to use const_cast :-( | |
411 | wxFileOffset iLen = ((wxFile *)this)->SeekEnd(); | |
412 | if ( iLen != wxInvalidOffset ) { | |
413 | // restore old position | |
414 | if ( ((wxFile *)this)->Seek(iRc) == wxInvalidOffset ) { | |
415 | // error | |
416 | iLen = wxInvalidOffset; | |
417 | } | |
418 | } | |
419 | ||
420 | iRc = iLen; | |
421 | } | |
422 | ||
423 | if ( iRc == wxInvalidOffset ) | |
424 | { | |
425 | wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd); | |
426 | } | |
427 | ||
428 | return iRc; | |
429 | } | |
430 | ||
431 | // is end of file reached? | |
432 | bool wxFile::Eof() const | |
433 | { | |
434 | wxASSERT( IsOpened() ); | |
435 | ||
436 | wxFileOffset iRc; | |
437 | ||
438 | #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__) | |
439 | // @@ this doesn't work, of course, on unseekable file descriptors | |
440 | wxFileOffset ofsCur = Tell(), | |
441 | ofsMax = Length(); | |
442 | if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset ) | |
443 | iRc = wxInvalidOffset; | |
444 | else | |
445 | iRc = ofsCur == ofsMax; | |
446 | #else // Windows and "native" compiler | |
447 | iRc = wxEof(m_fd); | |
448 | #endif // Windows/Unix | |
449 | ||
450 | if ( iRc == 1) | |
451 | {} | |
452 | else if ( iRc == 0 ) | |
453 | return false; | |
454 | else if ( iRc == wxInvalidOffset ) | |
455 | wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd); | |
456 | else | |
457 | wxFAIL_MSG(_T("invalid eof() return value.")); | |
458 | ||
459 | return true; | |
460 | } | |
461 | ||
462 | // ============================================================================ | |
463 | // implementation of wxTempFile | |
464 | // ============================================================================ | |
465 | ||
466 | // ---------------------------------------------------------------------------- | |
467 | // construction | |
468 | // ---------------------------------------------------------------------------- | |
469 | ||
470 | wxTempFile::wxTempFile(const wxString& strName) | |
471 | { | |
472 | Open(strName); | |
473 | } | |
474 | ||
475 | bool wxTempFile::Open(const wxString& strName) | |
476 | { | |
477 | // we must have an absolute filename because otherwise CreateTempFileName() | |
478 | // would create the temp file in $TMP (i.e. the system standard location | |
479 | // for the temp files) which might be on another volume/drive/mount and | |
480 | // wxRename()ing it later to m_strName from Commit() would then fail | |
481 | // | |
482 | // with the absolute filename, the temp file is created in the same | |
483 | // directory as this one which ensures that wxRename() may work later | |
484 | wxFileName fn(strName); | |
485 | if ( !fn.IsAbsolute() ) | |
486 | { | |
487 | fn.Normalize(wxPATH_NORM_ABSOLUTE); | |
488 | } | |
489 | ||
490 | m_strName = fn.GetFullPath(); | |
491 | ||
492 | m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file); | |
493 | ||
494 | if ( m_strTemp.empty() ) | |
495 | { | |
496 | // CreateTempFileName() failed | |
497 | return false; | |
498 | } | |
499 | ||
500 | #ifdef __UNIX__ | |
501 | // the temp file should have the same permissions as the original one | |
502 | mode_t mode; | |
503 | ||
504 | wxStructStat st; | |
505 | if ( stat( (const char*) m_strName.fn_str(), &st) == 0 ) | |
506 | { | |
507 | mode = st.st_mode; | |
508 | } | |
509 | else | |
510 | { | |
511 | // file probably didn't exist, just give it the default mode _using_ | |
512 | // user's umask (new files creation should respect umask) | |
513 | mode_t mask = umask(0777); | |
514 | mode = 0666 & ~mask; | |
515 | umask(mask); | |
516 | } | |
517 | ||
518 | if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 ) | |
519 | { | |
520 | #ifndef __OS2__ | |
521 | wxLogSysError(_("Failed to set temporary file permissions")); | |
522 | #endif | |
523 | } | |
524 | #endif // Unix | |
525 | ||
526 | return true; | |
527 | } | |
528 | ||
529 | // ---------------------------------------------------------------------------- | |
530 | // destruction | |
531 | // ---------------------------------------------------------------------------- | |
532 | ||
533 | wxTempFile::~wxTempFile() | |
534 | { | |
535 | if ( IsOpened() ) | |
536 | Discard(); | |
537 | } | |
538 | ||
539 | bool wxTempFile::Commit() | |
540 | { | |
541 | m_file.Close(); | |
542 | ||
543 | if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) { | |
544 | wxLogSysError(_("can't remove file '%s'"), m_strName.c_str()); | |
545 | return false; | |
546 | } | |
547 | ||
548 | if ( !wxRenameFile(m_strTemp, m_strName) ) { | |
549 | wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str()); | |
550 | return false; | |
551 | } | |
552 | ||
553 | return true; | |
554 | } | |
555 | ||
556 | void wxTempFile::Discard() | |
557 | { | |
558 | m_file.Close(); | |
559 | if ( wxRemove(m_strTemp) != 0 ) | |
560 | wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str()); | |
561 | } | |
562 | ||
563 | #endif // wxUSE_FILE | |
564 |