]>
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 license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
17 | #ifdef __GNUG__ | |
18 | #pragma implementation "file.h" | |
19 | #endif | |
20 | ||
21 | // For compilers that support precompilation, includes "wx.h". | |
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/defs.h" | |
30 | #endif | |
31 | ||
32 | #if wxUSE_FILE | |
33 | ||
34 | // standard | |
35 | #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__) | |
36 | #include <io.h> | |
37 | ||
38 | #ifndef __SALFORDC__ | |
39 | #define WIN32_LEAN_AND_MEAN | |
40 | #define NOSERVICE | |
41 | #define NOIME | |
42 | #define NOATOM | |
43 | #define NOGDI | |
44 | #define NOGDICAPMASKS | |
45 | #define NOMETAFILE | |
46 | #define NOMINMAX | |
47 | #define NOMSG | |
48 | #define NOOPENFILE | |
49 | #define NORASTEROPS | |
50 | #define NOSCROLL | |
51 | #define NOSOUND | |
52 | #define NOSYSMETRICS | |
53 | #define NOTEXTMETRIC | |
54 | #define NOWH | |
55 | #define NOCOMM | |
56 | #define NOKANJI | |
57 | #define NOCRYPT | |
58 | #define NOMCX | |
59 | #endif | |
60 | ||
61 | #include <windows.h> // for GetTempFileName | |
62 | #elif (defined(__UNIX__) || defined(__GNUWIN32__)) | |
63 | #include <unistd.h> | |
64 | #ifdef __GNUWIN32__ | |
65 | #include <windows.h> | |
66 | #endif | |
67 | #elif (defined(__WXSTUBS__)) | |
68 | // Have to ifdef this for different environments | |
69 | #include <io.h> | |
70 | #elif (defined(__WXMAC__)) | |
71 | int access( const char *path, int mode ) { return 0 ; } | |
72 | char* mktemp( char * path ) { return path ;} | |
73 | #include <unistd.h> | |
74 | #include <unix.h> | |
75 | #define W_OK 2 | |
76 | #define R_OK 4 | |
77 | #else | |
78 | #error "Please specify the header with file functions declarations." | |
79 | #endif //Win/UNIX | |
80 | ||
81 | #include <stdio.h> // SEEK_xxx constants | |
82 | #include <fcntl.h> // O_RDONLY &c | |
83 | ||
84 | #ifndef __MWERKS__ | |
85 | #include <sys/types.h> // needed for stat | |
86 | #include <sys/stat.h> // stat | |
87 | #endif | |
88 | ||
89 | // Microsoft compiler loves underscores, feed them to it | |
90 | #ifdef __VISUALC__ | |
91 | // functions | |
92 | #define open _open | |
93 | #define close _close | |
94 | #define read _read | |
95 | #define write _write | |
96 | #define lseek _lseek | |
97 | #define fsync _commit | |
98 | #define access _access | |
99 | #define eof _eof | |
100 | ||
101 | // types | |
102 | #define stat _stat | |
103 | ||
104 | // constants | |
105 | ||
106 | #define O_RDONLY _O_RDONLY | |
107 | #define O_WRONLY _O_WRONLY | |
108 | #define O_RDWR _O_RDWR | |
109 | #define O_EXCL _O_EXCL | |
110 | #define O_CREAT _O_CREAT | |
111 | #define O_BINARY _O_BINARY | |
112 | ||
113 | #define S_IFDIR _S_IFDIR | |
114 | #define S_IFREG _S_IFREG | |
115 | #else | |
116 | #define tell(fd) lseek(fd, 0, SEEK_CUR) | |
117 | #endif // VC++ | |
118 | ||
119 | #if defined(__BORLANDC__) || defined(_MSC_VER) | |
120 | #define W_OK 2 | |
121 | #define R_OK 4 | |
122 | #endif | |
123 | ||
124 | // there is no distinction between text and binary files under Unix | |
125 | #ifdef __UNIX__ | |
126 | #define O_BINARY (0) | |
127 | #endif //__UNIX__ | |
128 | ||
129 | #ifdef __SALFORDC__ | |
130 | #include <unix.h> | |
131 | #endif | |
132 | ||
133 | // wxWindows | |
134 | #include <wx/string.h> | |
135 | #include <wx/intl.h> | |
136 | #include <wx/file.h> | |
137 | #include <wx/log.h> | |
138 | ||
139 | #ifndef MAX_PATH | |
140 | #define MAX_PATH 512 | |
141 | #endif | |
142 | ||
143 | #ifdef __WXMAC__ | |
144 | char gwxMacFileName[ MAX_PATH ] ; | |
145 | char gwxMacFileName2[ MAX_PATH ] ; | |
146 | char gwxMacFileName3[ MAX_PATH ] ; | |
147 | #endif | |
148 | ||
149 | // some broken compilers don't have 3rd argument in open() and creat() | |
150 | #ifdef __SALFORDC__ | |
151 | #define ACCESS(access) | |
152 | #define stat _stat | |
153 | #else // normal compiler | |
154 | #define ACCESS(access) , (access) | |
155 | #endif // Salford C | |
156 | ||
157 | // ============================================================================ | |
158 | // implementation of wxFile | |
159 | // ============================================================================ | |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
162 | // static functions | |
163 | // ---------------------------------------------------------------------------- | |
164 | bool wxFile::Exists(const wxChar *name) | |
165 | { | |
166 | struct stat st; | |
167 | #if wxUSE_UNICODE && wxMBFILES | |
168 | wxCharBuffer fname = wxConvFile.cWC2MB(name); | |
169 | ||
170 | return !access(fname, 0) && | |
171 | !stat(MBSTRINGCAST fname, &st) && | |
172 | (st.st_mode & S_IFREG); | |
173 | #else | |
174 | return !access(name, 0) && | |
175 | !stat((wxChar*) name, &st) && | |
176 | (st.st_mode & S_IFREG); | |
177 | #endif | |
178 | } | |
179 | ||
180 | bool wxFile::Access(const wxChar *name, OpenMode mode) | |
181 | { | |
182 | int how = 0; | |
183 | ||
184 | switch ( mode ) { | |
185 | case read: | |
186 | how = R_OK; | |
187 | break; | |
188 | ||
189 | case write: | |
190 | how = W_OK; | |
191 | break; | |
192 | ||
193 | default: | |
194 | wxFAIL_MSG(_T("bad wxFile::Access mode parameter.")); | |
195 | } | |
196 | ||
197 | return access(wxFNCONV(name), how) == 0; | |
198 | } | |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // opening/closing | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | // ctors | |
205 | wxFile::wxFile(const wxChar *szFileName, OpenMode mode) | |
206 | { | |
207 | m_fd = fd_invalid; | |
208 | m_error = FALSE; | |
209 | ||
210 | Open(szFileName, mode); | |
211 | } | |
212 | ||
213 | // dtor | |
214 | wxFile::~wxFile() | |
215 | { | |
216 | Close(); | |
217 | } | |
218 | ||
219 | // create the file, fail if it already exists and bOverwrite | |
220 | bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode) | |
221 | { | |
222 | // if bOverwrite we create a new file or truncate the existing one, | |
223 | // otherwise we only create the new file and fail if it already exists | |
224 | int fd = open(wxFNCONV(szFileName), | |
225 | O_WRONLY | O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL) | |
226 | ACCESS(accessMode)); | |
227 | ||
228 | if ( fd == -1 ) { | |
229 | wxLogSysError(_("can't create file '%s'"), szFileName); | |
230 | return FALSE; | |
231 | } | |
232 | else { | |
233 | Attach(fd); | |
234 | return TRUE; | |
235 | } | |
236 | } | |
237 | ||
238 | // open the file | |
239 | bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode) | |
240 | { | |
241 | int flags = O_BINARY; | |
242 | ||
243 | switch ( mode ) { | |
244 | case read: | |
245 | flags |= O_RDONLY; | |
246 | break; | |
247 | ||
248 | case write: | |
249 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
250 | break; | |
251 | ||
252 | case write_append: | |
253 | flags |= O_WRONLY | O_APPEND; | |
254 | break; | |
255 | ||
256 | case read_write: | |
257 | flags |= O_RDWR; | |
258 | break; | |
259 | } | |
260 | ||
261 | int fd = open(wxFNCONV(szFileName), flags ACCESS(accessMode)); | |
262 | ||
263 | if ( fd == -1 ) { | |
264 | wxLogSysError(_("can't open file '%s'"), szFileName); | |
265 | return FALSE; | |
266 | } | |
267 | else { | |
268 | Attach(fd); | |
269 | return TRUE; | |
270 | } | |
271 | } | |
272 | ||
273 | // close | |
274 | bool wxFile::Close() | |
275 | { | |
276 | if ( IsOpened() ) { | |
277 | if ( close(m_fd) == -1 ) { | |
278 | wxLogSysError(_("can't close file descriptor %d"), m_fd); | |
279 | m_fd = fd_invalid; | |
280 | return FALSE; | |
281 | } | |
282 | else | |
283 | m_fd = fd_invalid; | |
284 | } | |
285 | ||
286 | return TRUE; | |
287 | } | |
288 | ||
289 | // ---------------------------------------------------------------------------- | |
290 | // read/write | |
291 | // ---------------------------------------------------------------------------- | |
292 | ||
293 | // read | |
294 | off_t wxFile::Read(void *pBuf, off_t nCount) | |
295 | { | |
296 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); | |
297 | ||
298 | #ifdef __MWERKS__ | |
299 | int iRc = ::read(m_fd, (char*) pBuf, nCount); | |
300 | #else | |
301 | int iRc = ::read(m_fd, pBuf, nCount); | |
302 | #endif | |
303 | if ( iRc == -1 ) { | |
304 | wxLogSysError(_("can't read from file descriptor %d"), m_fd); | |
305 | return wxInvalidOffset; | |
306 | } | |
307 | else | |
308 | return (size_t)iRc; | |
309 | } | |
310 | ||
311 | // write | |
312 | size_t wxFile::Write(const void *pBuf, size_t nCount) | |
313 | { | |
314 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); | |
315 | ||
316 | #ifdef __MWERKS__ | |
317 | int iRc = ::write(m_fd, (const char*) pBuf, nCount); | |
318 | #else | |
319 | int iRc = ::write(m_fd, pBuf, nCount); | |
320 | #endif | |
321 | if ( iRc == -1 ) { | |
322 | wxLogSysError(_("can't write to file descriptor %d"), m_fd); | |
323 | m_error = TRUE; | |
324 | return 0; | |
325 | } | |
326 | else | |
327 | return iRc; | |
328 | } | |
329 | ||
330 | // flush | |
331 | bool wxFile::Flush() | |
332 | { | |
333 | if ( IsOpened() ) { | |
334 | #if defined(__VISUALC__) || wxHAVE_FSYNC | |
335 | if ( fsync(m_fd) == -1 ) | |
336 | { | |
337 | wxLogSysError(_("can't flush file descriptor %d"), m_fd); | |
338 | return FALSE; | |
339 | } | |
340 | #else // no fsync | |
341 | // just do nothing | |
342 | #endif // fsync | |
343 | } | |
344 | ||
345 | return TRUE; | |
346 | } | |
347 | ||
348 | // ---------------------------------------------------------------------------- | |
349 | // seek | |
350 | // ---------------------------------------------------------------------------- | |
351 | ||
352 | // seek | |
353 | off_t wxFile::Seek(off_t ofs, wxSeekMode mode) | |
354 | { | |
355 | wxASSERT( IsOpened() ); | |
356 | ||
357 | int flag = -1; | |
358 | switch ( mode ) { | |
359 | case wxFromStart: | |
360 | flag = SEEK_SET; | |
361 | break; | |
362 | ||
363 | case wxFromCurrent: | |
364 | flag = SEEK_CUR; | |
365 | break; | |
366 | ||
367 | case wxFromEnd: | |
368 | flag = SEEK_END; | |
369 | break; | |
370 | ||
371 | default: | |
372 | wxFAIL_MSG(_("unknown seek origin")); | |
373 | } | |
374 | ||
375 | int iRc = lseek(m_fd, ofs, flag); | |
376 | if ( iRc == -1 ) { | |
377 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); | |
378 | return wxInvalidOffset; | |
379 | } | |
380 | else | |
381 | return (off_t)iRc; | |
382 | } | |
383 | ||
384 | // get current off_t | |
385 | off_t wxFile::Tell() const | |
386 | { | |
387 | wxASSERT( IsOpened() ); | |
388 | ||
389 | int iRc = tell(m_fd); | |
390 | if ( iRc == -1 ) { | |
391 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); | |
392 | return wxInvalidOffset; | |
393 | } | |
394 | else | |
395 | return (off_t)iRc; | |
396 | } | |
397 | ||
398 | // get current file length | |
399 | off_t wxFile::Length() const | |
400 | { | |
401 | wxASSERT( IsOpened() ); | |
402 | ||
403 | #ifdef __VISUALC__ | |
404 | int iRc = _filelength(m_fd); | |
405 | #else // !VC++ | |
406 | int iRc = tell(m_fd); | |
407 | if ( iRc != -1 ) { | |
408 | // @ have to use const_cast :-( | |
409 | int iLen = ((wxFile *)this)->SeekEnd(); | |
410 | if ( iLen != -1 ) { | |
411 | // restore old position | |
412 | if ( ((wxFile *)this)->Seek(iRc) == -1 ) { | |
413 | // error | |
414 | iLen = -1; | |
415 | } | |
416 | } | |
417 | ||
418 | iRc = iLen; | |
419 | } | |
420 | #endif // VC++ | |
421 | ||
422 | if ( iRc == -1 ) { | |
423 | wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd); | |
424 | return wxInvalidOffset; | |
425 | } | |
426 | else | |
427 | return (off_t)iRc; | |
428 | } | |
429 | ||
430 | // is end of file reached? | |
431 | bool wxFile::Eof() const | |
432 | { | |
433 | wxASSERT( IsOpened() ); | |
434 | ||
435 | int iRc; | |
436 | ||
437 | #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__) | |
438 | // @@ this doesn't work, of course, on unseekable file descriptors | |
439 | off_t ofsCur = Tell(), | |
440 | ofsMax = Length(); | |
441 | if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset ) | |
442 | iRc = -1; | |
443 | else | |
444 | iRc = ofsCur == ofsMax; | |
445 | #else // Windows and "native" compiler | |
446 | iRc = eof(m_fd); | |
447 | #endif // Windows/Unix | |
448 | ||
449 | switch ( iRc ) { | |
450 | case 1: | |
451 | break; | |
452 | ||
453 | case 0: | |
454 | return FALSE; | |
455 | ||
456 | case -1: | |
457 | wxLogSysError(_("can't determine if the end of file is reached on \ | |
458 | descriptor %d"), m_fd); | |
459 | break; | |
460 | ||
461 | default: | |
462 | wxFAIL_MSG(_("invalid eof() return value.")); | |
463 | } | |
464 | ||
465 | return TRUE; | |
466 | } | |
467 | ||
468 | // ============================================================================ | |
469 | // implementation of wxTempFile | |
470 | // ============================================================================ | |
471 | ||
472 | // ---------------------------------------------------------------------------- | |
473 | // construction | |
474 | // ---------------------------------------------------------------------------- | |
475 | wxTempFile::wxTempFile(const wxString& strName) | |
476 | { | |
477 | Open(strName); | |
478 | } | |
479 | ||
480 | bool wxTempFile::Open(const wxString& strName) | |
481 | { | |
482 | m_strName = strName; | |
483 | ||
484 | // we want to create the file in the same directory as strName because | |
485 | // otherwise rename() in Commit() might not work (if the files are on | |
486 | // different partitions for example). Unfortunately, the only standard | |
487 | // (POSIX) temp file creation function tmpnam() can't do it. | |
488 | #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ ) | |
489 | static const wxChar *szMktempSuffix = _T("XXXXXX"); | |
490 | m_strTemp << strName << szMktempSuffix; | |
491 | mktemp(MBSTRINGCAST m_strTemp.mb_str()); // will do because length doesn't change | |
492 | #else // Windows | |
493 | wxString strPath; | |
494 | wxSplitPath(strName, &strPath, NULL, NULL); | |
495 | if ( strPath.IsEmpty() ) | |
496 | strPath = _T('.'); // GetTempFileName will fail if we give it empty string | |
497 | #ifdef __WIN32__ | |
498 | if ( !GetTempFileName(strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) ) | |
499 | #else | |
500 | // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug? | |
501 | if ( !GetTempFileName((BYTE) (const wxChar*) strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) ) | |
502 | #endif | |
503 | wxLogLastError(_T("GetTempFileName")); | |
504 | m_strTemp.UngetWriteBuf(); | |
505 | #endif // Windows/Unix | |
506 | ||
507 | int access = wxS_DEFAULT; | |
508 | #ifdef __UNIX__ | |
509 | // create the file with the same mode as the original one under Unix | |
510 | mode_t umaskOld = 0; // just to suppress compiler warning | |
511 | bool changedUmask; | |
512 | ||
513 | struct stat st; | |
514 | if ( stat(strName.fn_str(), &st) == 0 ) | |
515 | { | |
516 | // this assumes that only lower bits of st_mode contain the access | |
517 | // rights, but it's true for at least all Unices which have S_IXXXX() | |
518 | // macros, so should not be less portable than using (not POSIX) | |
519 | // S_IFREG &c | |
520 | access = st.st_mode & 0777; | |
521 | ||
522 | // we want to create the file with exactly the same access rights as | |
523 | // the original one, so disable the user's umask for the moment | |
524 | umaskOld = umask(0); | |
525 | changedUmask = TRUE; | |
526 | } | |
527 | else | |
528 | { | |
529 | // file probably didn't exist, just create with default mode _using_ | |
530 | // user's umask (new files creation should respet umask) | |
531 | changedUmask = FALSE; | |
532 | } | |
533 | #endif // Unix | |
534 | ||
535 | bool ok = m_file.Open(m_strTemp, wxFile::write, access); | |
536 | ||
537 | #ifdef __UNIX__ | |
538 | if ( changedUmask ) | |
539 | { | |
540 | // restore umask now that the file is created | |
541 | (void)umask(umaskOld); | |
542 | } | |
543 | #endif // Unix | |
544 | ||
545 | return ok; | |
546 | } | |
547 | ||
548 | // ---------------------------------------------------------------------------- | |
549 | // destruction | |
550 | // ---------------------------------------------------------------------------- | |
551 | ||
552 | wxTempFile::~wxTempFile() | |
553 | { | |
554 | if ( IsOpened() ) | |
555 | Discard(); | |
556 | } | |
557 | ||
558 | bool wxTempFile::Commit() | |
559 | { | |
560 | m_file.Close(); | |
561 | ||
562 | if ( wxFile::Exists(m_strName) && remove(m_strName.fn_str()) != 0 ) { | |
563 | wxLogSysError(_("can't remove file '%s'"), m_strName.c_str()); | |
564 | return FALSE; | |
565 | } | |
566 | ||
567 | if ( rename(m_strTemp.fn_str(), m_strName.fn_str()) != 0 ) { | |
568 | wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str()); | |
569 | return FALSE; | |
570 | } | |
571 | ||
572 | return TRUE; | |
573 | } | |
574 | ||
575 | void wxTempFile::Discard() | |
576 | { | |
577 | m_file.Close(); | |
578 | if ( remove(m_strTemp.fn_str()) != 0 ) | |
579 | wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str()); | |
580 | } | |
581 | ||
582 | #endif |