]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
f172cb82 | 2 | // Name: src/common/file.cpp |
c801d85f KB |
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> | |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
c801d85f KB |
17 | // For compilers that support precompilation, includes "wx.h". |
18 | #include "wx/wxprec.h" | |
c801d85f KB |
19 | |
20 | #ifdef __BORLANDC__ | |
ce4169a4 | 21 | #pragma hdrstop |
c801d85f KB |
22 | #endif |
23 | ||
ce4169a4 RR |
24 | #if wxUSE_FILE |
25 | ||
c801d85f | 26 | // standard |
d98a58c5 | 27 | #if defined(__WINDOWS__) && !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
30a5be97 | 28 | |
f172cb82 VZ |
29 | #define WIN32_LEAN_AND_MEAN |
30 | #define NOSERVICE | |
31 | #define NOIME | |
32 | #define NOATOM | |
33 | #define NOGDI | |
34 | #define NOGDICAPMASKS | |
35 | #define NOMETAFILE | |
36 | #define NOMINMAX | |
37 | #define NOMSG | |
38 | #define NOOPENFILE | |
39 | #define NORASTEROPS | |
40 | #define NOSCROLL | |
41 | #define NOSOUND | |
42 | #define NOSYSMETRICS | |
43 | #define NOTEXTMETRIC | |
44 | #define NOWH | |
45 | #define NOCOMM | |
46 | #define NOKANJI | |
47 | #define NOCRYPT | |
48 | #define NOMCX | |
a3ef5bf5 | 49 | |
d98a58c5 | 50 | #elif defined(__WINDOWS__) && defined(__WXWINCE__) |
d109c584 | 51 | #include "wx/msw/missing.h" |
5a3912f2 SN |
52 | #elif (defined(__OS2__)) |
53 | #include <io.h> | |
c801d85f | 54 | #elif (defined(__UNIX__) || defined(__GNUWIN32__)) |
3f4a0c5b | 55 | #include <unistd.h> |
fd938b11 | 56 | #include <time.h> |
732b8386 | 57 | #include <sys/stat.h> |
3f4a0c5b | 58 | #ifdef __GNUWIN32__ |
9ed0d735 | 59 | #include "wx/msw/wrapwin.h" |
3f4a0c5b | 60 | #endif |
d3b4d710 VS |
61 | #elif defined(__DOS__) |
62 | #if defined(__WATCOMC__) | |
63 | #include <io.h> | |
64 | #elif defined(__DJGPP__) | |
65 | #include <io.h> | |
66 | #include <unistd.h> | |
67 | #include <stdio.h> | |
68 | #else | |
69 | #error "Please specify the header with file functions declarations." | |
70 | #endif | |
34138703 | 71 | #elif (defined(__WXSTUBS__)) |
3f4a0c5b VZ |
72 | // Have to ifdef this for different environments |
73 | #include <io.h> | |
17dff81c | 74 | #elif (defined(__WXMAC__)) |
5b781a67 | 75 | #if __MSL__ < 0x6000 |
3f4a0c5b | 76 | int access( const char *path, int mode ) { return 0 ; } |
5b781a67 SC |
77 | #else |
78 | int _access( const char *path, int mode ) { return 0 ; } | |
79 | #endif | |
3f4a0c5b | 80 | char* mktemp( char * path ) { return path ;} |
5b781a67 | 81 | #include <stat.h> |
6294ac2e | 82 | #include <unistd.h> |
c801d85f | 83 | #else |
3f4a0c5b | 84 | #error "Please specify the header with file functions declarations." |
c801d85f KB |
85 | #endif //Win/UNIX |
86 | ||
87 | #include <stdio.h> // SEEK_xxx constants | |
1c193821 | 88 | |
65fe93d8 VZ |
89 | #ifndef __WXWINCE__ |
90 | #include <errno.h> | |
91 | #endif | |
92 | ||
4ea2c29f VZ |
93 | // Windows compilers don't have these constants |
94 | #ifndef W_OK | |
95 | enum | |
96 | { | |
97 | F_OK = 0, // test for existence | |
98 | X_OK = 1, // execute permission | |
99 | W_OK = 2, // write | |
100 | R_OK = 4 // read | |
101 | }; | |
102 | #endif // W_OK | |
34138703 | 103 | |
77ffb593 | 104 | // wxWidgets |
ade35f11 VZ |
105 | #ifndef WX_PRECOMP |
106 | #include "wx/string.h" | |
107 | #include "wx/intl.h" | |
ade35f11 | 108 | #include "wx/log.h" |
0bf751e7 | 109 | #include "wx/crt.h" |
ade35f11 VZ |
110 | #endif // !WX_PRECOMP |
111 | ||
112 | #include "wx/filename.h" | |
44d568b6 | 113 | #include "wx/file.h" |
4ea2c29f | 114 | #include "wx/filefn.h" |
f6bcfd97 | 115 | |
7cbe148e SN |
116 | // there is no distinction between text and binary files under Unix, so define |
117 | // O_BINARY as 0 if the system headers don't do it already | |
118 | #if defined(__UNIX__) && !defined(O_BINARY) | |
119 | #define O_BINARY (0) | |
120 | #endif //__UNIX__ | |
121 | ||
d98a58c5 | 122 | #ifdef __WINDOWS__ |
28f5082b VS |
123 | #include "wx/msw/mslu.h" |
124 | #endif | |
125 | ||
1c193821 JS |
126 | #ifdef __WXWINCE__ |
127 | #include "wx/msw/private.h" | |
128 | #endif | |
129 | ||
7cbe148e SN |
130 | #ifndef MAX_PATH |
131 | #define MAX_PATH 512 | |
132 | #endif | |
30984dea | 133 | |
c801d85f KB |
134 | // ============================================================================ |
135 | // implementation of wxFile | |
136 | // ============================================================================ | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // static functions | |
140 | // ---------------------------------------------------------------------------- | |
4ea2c29f | 141 | |
fcea31d5 | 142 | bool wxFile::Exists(const wxString& name) |
246037e2 | 143 | { |
4ea2c29f | 144 | return wxFileExists(name); |
d1427b70 VZ |
145 | } |
146 | ||
fcea31d5 | 147 | bool wxFile::Access(const wxString& name, OpenMode mode) |
d1427b70 | 148 | { |
4ea2c29f VZ |
149 | int how; |
150 | ||
151 | switch ( mode ) | |
152 | { | |
153 | default: | |
154 | wxFAIL_MSG(wxT("bad wxFile::Access mode parameter.")); | |
155 | // fall through | |
d1427b70 | 156 | |
49d5d881 VZ |
157 | case read: |
158 | how = R_OK; | |
159 | break; | |
d1427b70 | 160 | |
49d5d881 VZ |
161 | case write: |
162 | how = W_OK; | |
163 | break; | |
d1427b70 | 164 | |
4ea2c29f VZ |
165 | case read_write: |
166 | how = R_OK | W_OK; | |
167 | break; | |
49d5d881 | 168 | } |
d1427b70 | 169 | |
4ea2c29f | 170 | return wxAccess(name, how) == 0; |
c801d85f KB |
171 | } |
172 | ||
173 | // ---------------------------------------------------------------------------- | |
174 | // opening/closing | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | // ctors | |
11aac4ba | 178 | wxFile::wxFile(const wxString& fileName, OpenMode mode) |
c801d85f | 179 | { |
49d5d881 | 180 | m_fd = fd_invalid; |
65fe93d8 | 181 | m_lasterror = 0; |
c801d85f | 182 | |
11aac4ba | 183 | Open(fileName, mode); |
c801d85f KB |
184 | } |
185 | ||
65fe93d8 VZ |
186 | bool wxFile::CheckForError(wxFileOffset rc) const |
187 | { | |
188 | if ( rc != -1 ) | |
189 | return false; | |
190 | ||
191 | const_cast<wxFile *>(this)->m_lasterror = | |
192 | #ifndef __WXWINCE__ | |
193 | errno | |
194 | #else | |
195 | ::GetLastError() | |
196 | #endif | |
197 | ; | |
198 | ||
199 | return true; | |
200 | } | |
201 | ||
c801d85f | 202 | // create the file, fail if it already exists and bOverwrite |
fcea31d5 | 203 | bool wxFile::Create(const wxString& fileName, bool bOverwrite, int accessMode) |
c801d85f | 204 | { |
49d5d881 VZ |
205 | // if bOverwrite we create a new file or truncate the existing one, |
206 | // otherwise we only create the new file and fail if it already exists | |
7d1214cd | 207 | int fildes = wxOpen( fileName, |
92980e90 | 208 | O_BINARY | O_WRONLY | O_CREAT | |
f172cb82 VZ |
209 | (bOverwrite ? O_TRUNC : O_EXCL), |
210 | accessMode ); | |
7d1214cd | 211 | if ( CheckForError(fildes) ) |
92980e90 | 212 | { |
fcea31d5 | 213 | wxLogSysError(_("can't create file '%s'"), fileName); |
a62848fd | 214 | return false; |
49d5d881 | 215 | } |
769627d7 | 216 | |
7d1214cd | 217 | Attach(fildes); |
769627d7 | 218 | return true; |
c801d85f KB |
219 | } |
220 | ||
221 | // open the file | |
fcea31d5 | 222 | bool wxFile::Open(const wxString& fileName, OpenMode mode, int accessMode) |
c801d85f | 223 | { |
49d5d881 | 224 | int flags = O_BINARY; |
c801d85f | 225 | |
92980e90 RR |
226 | switch ( mode ) |
227 | { | |
49d5d881 VZ |
228 | case read: |
229 | flags |= O_RDONLY; | |
230 | break; | |
c801d85f | 231 | |
f6bcfd97 | 232 | case write_append: |
fcea31d5 | 233 | if ( wxFile::Exists(fileName) ) |
f6bcfd97 BP |
234 | { |
235 | flags |= O_WRONLY | O_APPEND; | |
236 | break; | |
237 | } | |
238 | //else: fall through as write_append is the same as write if the | |
239 | // file doesn't exist | |
240 | ||
49d5d881 VZ |
241 | case write: |
242 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
243 | break; | |
61b02744 | 244 | |
68164137 RL |
245 | case write_excl: |
246 | flags |= O_WRONLY | O_CREAT | O_EXCL; | |
247 | break; | |
248 | ||
49d5d881 VZ |
249 | case read_write: |
250 | flags |= O_RDWR; | |
251 | break; | |
252 | } | |
c801d85f | 253 | |
cc8cc54f VZ |
254 | #ifdef __WINDOWS__ |
255 | // only read/write bits for "all" are supported by this function under | |
256 | // Windows, and VC++ 8 returns EINVAL if any other bits are used in | |
257 | // accessMode, so clear them as they have at best no effect anyhow | |
258 | accessMode &= wxS_IRUSR | wxS_IWUSR; | |
259 | #endif // __WINDOWS__ | |
260 | ||
7d1214cd | 261 | int fildes = wxOpen( fileName, flags, accessMode); |
6294ac2e | 262 | |
7d1214cd | 263 | if ( CheckForError(fildes) ) |
92980e90 | 264 | { |
fcea31d5 | 265 | wxLogSysError(_("can't open file '%s'"), fileName); |
a62848fd | 266 | return false; |
49d5d881 | 267 | } |
769627d7 | 268 | |
7d1214cd | 269 | Attach(fildes); |
769627d7 | 270 | return true; |
c801d85f KB |
271 | } |
272 | ||
273 | // close | |
61b02744 | 274 | bool wxFile::Close() |
c801d85f | 275 | { |
49d5d881 | 276 | if ( IsOpened() ) { |
65fe93d8 | 277 | if ( CheckForError(wxClose(m_fd)) ) |
1c193821 | 278 | { |
49d5d881 VZ |
279 | wxLogSysError(_("can't close file descriptor %d"), m_fd); |
280 | m_fd = fd_invalid; | |
a62848fd | 281 | return false; |
49d5d881 VZ |
282 | } |
283 | else | |
284 | m_fd = fd_invalid; | |
61b02744 | 285 | } |
61b02744 | 286 | |
a62848fd | 287 | return true; |
c801d85f KB |
288 | } |
289 | ||
290 | // ---------------------------------------------------------------------------- | |
291 | // read/write | |
292 | // ---------------------------------------------------------------------------- | |
293 | ||
614108e2 VZ |
294 | bool wxFile::ReadAll(wxString *str, const wxMBConv& conv) |
295 | { | |
296 | wxCHECK_MSG( str, false, wxS("Output string must be non-NULL") ); | |
297 | ||
06a086e8 | 298 | ssize_t length = Length(); |
614108e2 VZ |
299 | wxCHECK_MSG( (wxFileOffset)length == Length(), false, wxT("huge file not supported") ); |
300 | ||
301 | wxCharBuffer buf(length); | |
302 | char* p = buf.data(); | |
303 | for ( ;; ) | |
304 | { | |
5fd78a8e | 305 | static const ssize_t READSIZE = 4096; |
614108e2 | 306 | |
7d1214cd PC |
307 | ssize_t nread = Read(p, length > READSIZE ? READSIZE : length); |
308 | if ( nread == wxInvalidOffset ) | |
614108e2 VZ |
309 | return false; |
310 | ||
7d1214cd | 311 | p += nread; |
06a086e8 VZ |
312 | if ( length <= nread ) |
313 | break; | |
314 | ||
315 | length -= nread; | |
614108e2 VZ |
316 | } |
317 | ||
318 | *p = 0; | |
319 | ||
320 | wxString strTmp(buf, conv); | |
321 | str->swap(strTmp); | |
322 | ||
323 | return true; | |
324 | } | |
325 | ||
c801d85f | 326 | // read |
f8a586e0 | 327 | ssize_t wxFile::Read(void *pBuf, size_t nCount) |
c801d85f | 328 | { |
49d5d881 | 329 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 330 | |
30984dea | 331 | ssize_t iRc = wxRead(m_fd, pBuf, nCount); |
6294ac2e | 332 | |
65fe93d8 | 333 | if ( CheckForError(iRc) ) |
02aef13c | 334 | { |
49d5d881 | 335 | wxLogSysError(_("can't read from file descriptor %d"), m_fd); |
f8a586e0 | 336 | return wxInvalidOffset; |
49d5d881 | 337 | } |
02aef13c VZ |
338 | |
339 | return iRc; | |
c801d85f KB |
340 | } |
341 | ||
342 | // write | |
30984dea | 343 | size_t wxFile::Write(const void *pBuf, size_t nCount) |
c801d85f | 344 | { |
49d5d881 | 345 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 346 | |
30984dea | 347 | ssize_t iRc = wxWrite(m_fd, pBuf, nCount); |
6294ac2e | 348 | |
65fe93d8 | 349 | if ( CheckForError(iRc) ) |
02aef13c | 350 | { |
49d5d881 | 351 | wxLogSysError(_("can't write to file descriptor %d"), m_fd); |
02aef13c | 352 | iRc = 0; |
49d5d881 | 353 | } |
02aef13c | 354 | |
30984dea | 355 | return iRc; |
c801d85f KB |
356 | } |
357 | ||
b1c67394 VZ |
358 | bool wxFile::Write(const wxString& s, const wxMBConv& conv) |
359 | { | |
360 | const wxWX2MBbuf buf = s.mb_str(conv); | |
361 | if ( !buf ) | |
362 | return false; | |
363 | ||
ae0e22dd | 364 | #if wxUSE_UNICODE |
227989f3 | 365 | const size_t size = buf.length(); |
ae0e22dd VZ |
366 | #else |
367 | const size_t size = s.length(); | |
368 | #endif | |
369 | ||
b1c67394 VZ |
370 | return Write(buf, size) == size; |
371 | } | |
372 | ||
c801d85f KB |
373 | // flush |
374 | bool wxFile::Flush() | |
375 | { | |
c8ccc915 | 376 | #ifdef HAVE_FSYNC |
608a34bf VZ |
377 | // fsync() only works on disk files and returns errors for pipes, don't |
378 | // call it then | |
379 | if ( IsOpened() && GetKind() == wxFILE_KIND_DISK ) | |
380 | { | |
65fe93d8 | 381 | if ( CheckForError(wxFsync(m_fd)) ) |
09914df7 VZ |
382 | { |
383 | wxLogSysError(_("can't flush file descriptor %d"), m_fd); | |
a62848fd | 384 | return false; |
09914df7 | 385 | } |
49d5d881 | 386 | } |
c8ccc915 | 387 | #endif // HAVE_FSYNC |
c801d85f | 388 | |
a62848fd | 389 | return true; |
c801d85f KB |
390 | } |
391 | ||
392 | // ---------------------------------------------------------------------------- | |
393 | // seek | |
394 | // ---------------------------------------------------------------------------- | |
395 | ||
396 | // seek | |
30984dea | 397 | wxFileOffset wxFile::Seek(wxFileOffset ofs, wxSeekMode mode) |
c801d85f | 398 | { |
9a83f860 | 399 | wxASSERT_MSG( IsOpened(), wxT("can't seek on closed file") ); |
686a3ee0 VZ |
400 | wxCHECK_MSG( ofs != wxInvalidOffset || mode != wxFromStart, |
401 | wxInvalidOffset, | |
9a83f860 | 402 | wxT("invalid absolute file offset") ); |
49d5d881 | 403 | |
a1b82138 | 404 | int origin; |
49d5d881 | 405 | switch ( mode ) { |
a1b82138 | 406 | default: |
9a83f860 | 407 | wxFAIL_MSG(wxT("unknown seek origin")); |
a1b82138 | 408 | |
49d5d881 | 409 | case wxFromStart: |
a1b82138 | 410 | origin = SEEK_SET; |
49d5d881 VZ |
411 | break; |
412 | ||
413 | case wxFromCurrent: | |
a1b82138 | 414 | origin = SEEK_CUR; |
49d5d881 VZ |
415 | break; |
416 | ||
417 | case wxFromEnd: | |
a1b82138 | 418 | origin = SEEK_END; |
49d5d881 | 419 | break; |
49d5d881 VZ |
420 | } |
421 | ||
30984dea | 422 | wxFileOffset iRc = wxSeek(m_fd, ofs, origin); |
65fe93d8 | 423 | if ( CheckForError(iRc) ) |
02aef13c | 424 | { |
49d5d881 | 425 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); |
49d5d881 | 426 | } |
02aef13c VZ |
427 | |
428 | return iRc; | |
c801d85f KB |
429 | } |
430 | ||
02aef13c | 431 | // get current file offset |
30984dea | 432 | wxFileOffset wxFile::Tell() const |
c801d85f | 433 | { |
49d5d881 VZ |
434 | wxASSERT( IsOpened() ); |
435 | ||
30984dea | 436 | wxFileOffset iRc = wxTell(m_fd); |
65fe93d8 | 437 | if ( CheckForError(iRc) ) |
02aef13c | 438 | { |
49d5d881 | 439 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); |
49d5d881 | 440 | } |
02aef13c VZ |
441 | |
442 | return iRc; | |
c801d85f KB |
443 | } |
444 | ||
445 | // get current file length | |
30984dea | 446 | wxFileOffset wxFile::Length() const |
c801d85f | 447 | { |
49d5d881 | 448 | wxASSERT( IsOpened() ); |
c801d85f | 449 | |
41f6f17d VZ |
450 | // we use a special method for Linux systems where files in sysfs (i.e. |
451 | // those under /sys typically) return length of 4096 bytes even when | |
452 | // they're much smaller -- this is a problem as it results in errors later | |
453 | // when we try reading 4KB from them | |
454 | #ifdef __LINUX__ | |
455 | struct stat st; | |
456 | if ( fstat(m_fd, &st) == 0 ) | |
457 | { | |
458 | // returning 0 for the special files indicates to the caller that they | |
459 | // are not seekable | |
460 | return st.st_blocks ? st.st_size : 0; | |
461 | } | |
462 | //else: failed to stat, try the normal method | |
463 | #endif // __LINUX__ | |
464 | ||
30984dea | 465 | wxFileOffset iRc = Tell(); |
02aef13c | 466 | if ( iRc != wxInvalidOffset ) { |
f48a1159 | 467 | wxFileOffset iLen = const_cast<wxFile *>(this)->SeekEnd(); |
02aef13c | 468 | if ( iLen != wxInvalidOffset ) { |
49d5d881 | 469 | // restore old position |
02aef13c | 470 | if ( ((wxFile *)this)->Seek(iRc) == wxInvalidOffset ) { |
49d5d881 | 471 | // error |
02aef13c | 472 | iLen = wxInvalidOffset; |
49d5d881 | 473 | } |
c801d85f | 474 | } |
c801d85f | 475 | |
49d5d881 VZ |
476 | iRc = iLen; |
477 | } | |
49d5d881 | 478 | |
02aef13c VZ |
479 | if ( iRc == wxInvalidOffset ) |
480 | { | |
65fe93d8 | 481 | // last error was already set by Tell() |
49d5d881 | 482 | wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd); |
c801d85f | 483 | } |
02aef13c VZ |
484 | |
485 | return iRc; | |
c801d85f KB |
486 | } |
487 | ||
488 | // is end of file reached? | |
489 | bool wxFile::Eof() const | |
490 | { | |
49d5d881 | 491 | wxASSERT( IsOpened() ); |
c801d85f | 492 | |
30984dea | 493 | wxFileOffset iRc; |
61b02744 | 494 | |
2415cf67 | 495 | #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) |
61b02744 | 496 | // @@ this doesn't work, of course, on unseekable file descriptors |
30984dea | 497 | wxFileOffset ofsCur = Tell(), |
49d5d881 | 498 | ofsMax = Length(); |
1678ad78 | 499 | if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset ) |
02aef13c | 500 | iRc = wxInvalidOffset; |
61b02744 | 501 | else |
49d5d881 VZ |
502 | iRc = ofsCur == ofsMax; |
503 | #else // Windows and "native" compiler | |
6294ac2e | 504 | iRc = wxEof(m_fd); |
49d5d881 | 505 | #endif // Windows/Unix |
c801d85f | 506 | |
43b2d5e7 | 507 | if ( iRc == 0 ) |
b9daf00a | 508 | return false; |
43b2d5e7 VZ |
509 | |
510 | if ( iRc == wxInvalidOffset ) | |
511 | { | |
b9daf00a | 512 | wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd); |
43b2d5e7 VZ |
513 | } |
514 | else if ( iRc != 1 ) | |
515 | { | |
9a83f860 | 516 | wxFAIL_MSG(wxT("invalid eof() return value.")); |
43b2d5e7 | 517 | } |
c801d85f | 518 | |
a62848fd | 519 | return true; |
c801d85f KB |
520 | } |
521 | ||
522 | // ============================================================================ | |
523 | // implementation of wxTempFile | |
524 | // ============================================================================ | |
525 | ||
526 | // ---------------------------------------------------------------------------- | |
527 | // construction | |
528 | // ---------------------------------------------------------------------------- | |
44b62d54 | 529 | |
c801d85f KB |
530 | wxTempFile::wxTempFile(const wxString& strName) |
531 | { | |
49d5d881 | 532 | Open(strName); |
c801d85f KB |
533 | } |
534 | ||
535 | bool wxTempFile::Open(const wxString& strName) | |
536 | { | |
44b62d54 VZ |
537 | // we must have an absolute filename because otherwise CreateTempFileName() |
538 | // would create the temp file in $TMP (i.e. the system standard location | |
539 | // for the temp files) which might be on another volume/drive/mount and | |
540 | // wxRename()ing it later to m_strName from Commit() would then fail | |
541 | // | |
542 | // with the absolute filename, the temp file is created in the same | |
543 | // directory as this one which ensures that wxRename() may work later | |
544 | wxFileName fn(strName); | |
545 | if ( !fn.IsAbsolute() ) | |
546 | { | |
547 | fn.Normalize(wxPATH_NORM_ABSOLUTE); | |
548 | } | |
549 | ||
550 | m_strName = fn.GetFullPath(); | |
246037e2 | 551 | |
44b62d54 | 552 | m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file); |
ade35f11 VZ |
553 | |
554 | if ( m_strTemp.empty() ) | |
555 | { | |
556 | // CreateTempFileName() failed | |
a62848fd | 557 | return false; |
ade35f11 | 558 | } |
49d5d881 | 559 | |
49d5d881 | 560 | #ifdef __UNIX__ |
ade35f11 VZ |
561 | // the temp file should have the same permissions as the original one |
562 | mode_t mode; | |
a62848fd | 563 | |
f6bcfd97 | 564 | wxStructStat st; |
ca11abde | 565 | if ( stat( (const char*) m_strName.fn_str(), &st) == 0 ) |
49d5d881 | 566 | { |
ade35f11 | 567 | mode = st.st_mode; |
49d5d881 VZ |
568 | } |
569 | else | |
570 | { | |
ade35f11 | 571 | // file probably didn't exist, just give it the default mode _using_ |
68164137 | 572 | // user's umask (new files creation should respect umask) |
ade35f11 VZ |
573 | mode_t mask = umask(0777); |
574 | mode = 0666 & ~mask; | |
575 | umask(mask); | |
49d5d881 | 576 | } |
49d5d881 | 577 | |
ca11abde | 578 | if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 ) |
fe99e285 | 579 | { |
5a3912f2 | 580 | #ifndef __OS2__ |
ade35f11 | 581 | wxLogSysError(_("Failed to set temporary file permissions")); |
5a3912f2 | 582 | #endif |
fe99e285 | 583 | } |
49d5d881 | 584 | #endif // Unix |
246037e2 | 585 | |
a62848fd | 586 | return true; |
c801d85f KB |
587 | } |
588 | ||
589 | // ---------------------------------------------------------------------------- | |
590 | // destruction | |
591 | // ---------------------------------------------------------------------------- | |
592 | ||
593 | wxTempFile::~wxTempFile() | |
594 | { | |
49d5d881 VZ |
595 | if ( IsOpened() ) |
596 | Discard(); | |
c801d85f KB |
597 | } |
598 | ||
599 | bool wxTempFile::Commit() | |
600 | { | |
49d5d881 | 601 | m_file.Close(); |
c801d85f | 602 | |
f6bcfd97 | 603 | if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) { |
49d5d881 | 604 | wxLogSysError(_("can't remove file '%s'"), m_strName.c_str()); |
a62848fd | 605 | return false; |
49d5d881 | 606 | } |
c801d85f | 607 | |
f35746ce | 608 | if ( !wxRenameFile(m_strTemp, m_strName) ) { |
49d5d881 | 609 | wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str()); |
a62848fd | 610 | return false; |
49d5d881 | 611 | } |
c801d85f | 612 | |
a62848fd | 613 | return true; |
c801d85f KB |
614 | } |
615 | ||
616 | void wxTempFile::Discard() | |
617 | { | |
49d5d881 | 618 | m_file.Close(); |
f6bcfd97 | 619 | if ( wxRemove(m_strTemp) != 0 ) |
af588446 | 620 | { |
49d5d881 | 621 | wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str()); |
af588446 | 622 | } |
c801d85f | 623 | } |
ce4169a4 | 624 | |
ade35f11 | 625 | #endif // wxUSE_FILE |
cc985fac | 626 |