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