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