Changeset 7115
- Timestamp:
- Nov 15, 2008, 7:59:18 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/fdlimit.c
r7099 r7115 96 96 ***/ 97 97 98 #ifndef O_LARGEFILE 99 #define O_LARGEFILE 0 100 #endif 101 98 102 static int 99 preallocateFile( int fd UNUSED, uint64_t length UNUSED ) 100 { 101 #ifdef HAVE_FALLOCATE 102 103 return fallocate( fd, FALLOC_FL_KEEP_SIZE, 0, length ); 104 105 #elif defined(HAVE_POSIX_FALLOCATE) 106 107 return posix_fallocate( fd, 0, length ); 108 109 #elif defined(SYS_DARWIN) 110 111 fstore_t fst; 112 fst.fst_flags = F_ALLOCATECONTIG; 113 fst.fst_posmode = F_PEOFPOSMODE; 114 fst.fst_offset = 0; 115 fst.fst_length = length; 116 fst.fst_bytesalloc = 0; 117 return fcntl( fd, F_PREALLOCATE, &fst ); 103 preallocateFile( const char * filename, uint64_t length ) 104 { 105 int success = 0; 106 107 #ifdef WIN32 108 109 HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 ); 110 if( hFile != INVALID_HANDLE_VALUE ) 111 { 112 LARGE_INTEGER li; 113 li.QuadPart = desiredFileSize; 114 success = SetFilePointerEx( hFile, desiredFileSize, NULL, FILE_BEGIN ) 115 && SetEndOfFile( hFile ); 116 CloseHandle( hFile ); 117 } 118 118 119 119 #else 120 120 121 #warning no known method to preallocate files on this platform 122 return -1; 123 124 #endif 121 int flags = O_RDWR | O_CREAT | O_LARGEFILE; 122 int fd = open( filename, flags, 0666 ); 123 if( fd >= 0 ) 124 { 125 126 # ifdef HAVE_FALLOCATE 127 128 success = !fallocate( fd, FALLOC_FL_KEEP_SIZE, 0, length ); 129 130 # elif defined(HAVE_POSIX_FALLOCATE) 131 132 success = !posix_fallocate( fd, 0, length ); 133 134 # elif defined(SYS_DARWIN) 135 136 fstore_t fst; 137 fst.fst_flags = F_ALLOCATECONTIG; 138 fst.fst_posmode = F_PEOFPOSMODE; 139 fst.fst_offset = 0; 140 fst.fst_length = length; 141 fst.fst_bytesalloc = 0; 142 success = !fcntl( fd, F_PREALLOCATE, &fst ); 143 144 # else 145 146 #warning no known method to preallocate files on this platform 147 success = 0; 148 149 # endif 150 151 close( fd ); 152 } 153 154 #endif 155 156 return success; 125 157 } 126 158 … … 162 194 163 195 alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode ); 196 197 if( doWrite && !alreadyExisted && doPreallocate ) 198 if( preallocateFile( filename, desiredFileSize ) ) 199 tr_inf( _( "Preallocated file \"%s\"" ), filename ); 164 200 165 201 /* open the file */ … … 181 217 } 182 218 183 if( ( file->fd >= 0 ) && !alreadyExisted && doPreallocate )184 if( !preallocateFile( file->fd, desiredFileSize ) )185 tr_inf( _( "Preallocated file \"%s\"" ), filename );186 187 219 tr_free( filename ); 188 220 return 0;
Note: See TracChangeset
for help on using the changeset viewer.