From 859193c50b1017932eda654d0a96c247fcf3b59b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 1 Oct 2008 14:48:53 +0000 Subject: [PATCH] 2008-10-01 Tatsuhiro Tsujikawa Use seek() and SetEndOfFile() for mingw32 build instead of ftruncate(), because mingw32's ftruncate() cannot handle more than 2GB-size file. * src/AbstractDiskWriter.cc --- ChangeLog | 6 ++++++ src/AbstractDiskWriter.cc | 30 ++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 99f35911..d6d0020b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-10-01 Tatsuhiro Tsujikawa + + 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 Don't set Request::supportsPersistentConnection() in HttpConnection diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index 9ef6d696..d1ce0076 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -33,6 +33,17 @@ */ /* copyright --> */ #include "AbstractDiskWriter.h" + +#include + +#include +#include +#include + +#ifdef __MINGW32__ +# include +#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 -#include -#include 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.