diff options
Diffstat (limited to '')
| -rw-r--r-- | src/file.cpp | 39 | 
1 files changed, 30 insertions, 9 deletions
diff --git a/src/file.cpp b/src/file.cpp index a0c3fd8..0566ee8 100644 --- a/src/file.cpp +++ b/src/file.cpp  | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | #include <fcntl.h> | 12 | #include <fcntl.h> | 
| 13 | #include <unistd.h> | 13 | #include <unistd.h> | 
| 14 | #include <stdlib.h> // for mkstemp | 14 | #include <stdlib.h> // for mkstemp | 
| 15 | #include <time.h> | ||
| 15 | 16 | ||
| 16 | #include "bu/config.h" | 17 | #include "bu/config.h" | 
| 17 | 18 | ||
| @@ -189,21 +190,41 @@ void Bu::File::setBlocking( bool bBlocking ) | |||
| 189 | 190 | ||
| 190 | Bu::File Bu::File::tempFile( Bu::FString &sName ) | 191 | Bu::File Bu::File::tempFile( Bu::FString &sName ) | 
| 191 | { | 192 | { | 
| 192 | #ifndef WIN32 | 193 | uint32_t iX; | 
| 193 | int afh_d = mkstemp( sName.getStr() ); | 194 | iX = time( NULL ) + getpid(); | 
| 195 | int iXes; | ||
| 196 | for( iXes = sName.getSize()-1; iXes >= 0; iXes-- ) | ||
| 197 | { | ||
| 198 | if( sName[iXes] != 'X' ) | ||
| 199 | break; | ||
| 200 | } | ||
| 201 | iXes++; | ||
| 202 | if( iXes == sName.getSize() ) | ||
| 203 | throw Bu::ExceptionBase("Invalid temporary filename template."); | ||
| 204 | for( int iter = 0; iter < 100; iter++ ) | ||
| 205 | { | ||
| 206 | for( int j = iXes; j < sName.getSize(); j++ ) | ||
| 207 | { | ||
| 208 | iX = (1103515245 * iX + 12345); | ||
| 209 | sName[j] = ('A'+(iX%26)) | ((iX&0x1000)?(0x20):(0)); | ||
| 210 | } | ||
| 194 | 211 | ||
| 195 | return Bu::File( afh_d ); | 212 | try | 
| 196 | #else | 213 | { | 
| 197 | return Bu::File( sName, Bu::File::Write|Bu::File::Create ); | 214 | return Bu::File( sName, Bu::File::Read|Bu::File::Write | 
| 198 | #endif | 215 | |Bu::File::Create|Bu::File::Exclusive ); | 
| 216 | } catch(...) { } | ||
| 217 | } | ||
| 218 | throw Bu::FileException("Failed to create unique temporary file after 100" | ||
| 219 | " iterations."); | ||
| 199 | } | 220 | } | 
| 200 | 221 | ||
| 201 | void Bu::File::setSize( long iSize ) | 222 | void Bu::File::setSize( long iSize ) | 
| 202 | { | 223 | { | 
| 203 | #ifndef WIN32 | 224 | #ifdef WIN32 | 
| 204 | ftruncate( fd, iSize ); | 225 | chsize( fd, iSize ); | 
| 205 | #else | 226 | #else | 
| 206 | #warning Bu::File::setSize not implemented on this platform | 227 | ftruncate( fd, iSize ); | 
| 207 | #endif | 228 | #endif | 
| 208 | } | 229 | } | 
| 209 | 230 | ||
