瀏覽代碼

2008-10-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Use seek() and SetEndOfFile() for mingw32 build instead of 
ftruncate(),
	because mingw32's ftruncate() cannot handle more than 2GB-size 
file.
	* src/AbstractDiskWriter.cc
Tatsuhiro Tsujikawa 17 年之前
父節點
當前提交
859193c50b
共有 2 個文件被更改,包括 32 次插入4 次删除
  1. 6 0
      ChangeLog
  2. 26 4
      src/AbstractDiskWriter.cc

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+2008-10-01  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Use seek() and SetEndOfFile() for mingw32 build instead of ftruncate(),
+	because mingw32's ftruncate() cannot handle more than 2GB-size file.
+	* src/AbstractDiskWriter.cc
+
 2008-10-01  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Don't set Request::supportsPersistentConnection() in HttpConnection

+ 26 - 4
src/AbstractDiskWriter.cc

@@ -33,6 +33,17 @@
  */
 /* copyright --> */
 #include "AbstractDiskWriter.h"
+
+#include <unistd.h>
+
+#include <cerrno>
+#include <cstring>
+#include <cassert>
+
+#ifdef __MINGW32__
+# include <windows.h>
+#endif // __MINGW32__
+
 #include "File.h"
 #include "Util.h"
 #include "message.h"
@@ -42,9 +53,6 @@
 #include "a2io.h"
 #include "StringFormat.h"
 #include "DownloadFailureException.h"
-#include <cerrno>
-#include <cstring>
-#include <cassert>
 
 namespace aria2 {
 
@@ -159,7 +167,21 @@ void AbstractDiskWriter::truncate(uint64_t length)
   if(fd == -1) {
     throw DlAbortEx("File not opened.");
   }
-  ftruncate(fd, length);
+#ifdef __MINGW32__
+  // Since mingw32's ftruncate cannot handle over 2GB files, we use SetEndOfFile
+  // instead.
+  HANDLE handle = LongToHandle(_get_osfhandle(fd));
+  seek(length);
+  if(SetEndOfFile(handle) == 0) {
+    throw DlAbortEx(StringFormat("SetEndOfFile failed. cause: %s",
+				 GetLastError()).str());
+  }
+#else
+  if(ftruncate(fd, length) == -1) {
+    throw DlAbortEx(StringFormat("ftruncate failed. cause: %s",
+				 strerror(errno)).str());
+  }
+#endif
 }
 
 // TODO the file descriptor fd must be opened before calling this function.