mirror of https://github.com/aria2/aria2
mingw32: Open file using _wsopen and added --enable-mmap support
I tried CreateFile but the subsequent ReadFile fails with Access Denied if sparse file is read on NTFS. I mostly reverted previous changes and use _wsopen with read/write share enabled instead of CreateFile. This change also includes --enable-mmap support for MinGW32 build. Memory mapped file may be useful for 64-bits OS and lots of RAM. Currently, FlushViewOfFile is not called during the download, so it is slightly vulnerable against sudden power loss. I found lots of read when resuming download due to page fault. So for now it is useful for the initial download. I recommend not to use --file-allocation=prealloc with --enable-mmap for MinGW32, because it triggers page faults even in the initial download. Anyway, the option is experimental.pull/36/head
parent
b95f15b462
commit
7e59e2dbb5
|
@ -338,7 +338,10 @@ case "$host" in
|
||||||
AC_CHECK_HEADERS([windows.h \
|
AC_CHECK_HEADERS([windows.h \
|
||||||
winsock2.h \
|
winsock2.h \
|
||||||
ws2tcpip.h \
|
ws2tcpip.h \
|
||||||
mmsystem.h], [], [],
|
mmsystem.h \
|
||||||
|
io.h \
|
||||||
|
winioctl.h \
|
||||||
|
share.h], [], [],
|
||||||
[[#ifdef HAVE_WINDOWS_H
|
[[#ifdef HAVE_WINDOWS_H
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -351,7 +354,6 @@ AC_CHECK_HEADERS([argz.h \
|
||||||
fcntl.h \
|
fcntl.h \
|
||||||
float.h \
|
float.h \
|
||||||
inttypes.h \
|
inttypes.h \
|
||||||
io.h \
|
|
||||||
langinfo.h \
|
langinfo.h \
|
||||||
libintl.h \
|
libintl.h \
|
||||||
limits.h \
|
limits.h \
|
||||||
|
|
|
@ -53,21 +53,20 @@
|
||||||
#include "error_code.h"
|
#include "error_code.h"
|
||||||
#include "LogFactory.h"
|
#include "LogFactory.h"
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
|
||||||
# define A2_BAD_FILE_DESCRIPTOR INVALID_HANDLE_VALUE
|
|
||||||
#else // !__MINGW32__
|
|
||||||
# define A2_BAD_FILE_DESCRIPTOR -1
|
|
||||||
#endif // !__MINGW32__
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
AbstractDiskWriter::AbstractDiskWriter(const std::string& filename)
|
AbstractDiskWriter::AbstractDiskWriter(const std::string& filename)
|
||||||
: filename_(filename),
|
: filename_(filename),
|
||||||
fd_(A2_BAD_FILE_DESCRIPTOR),
|
fd_(-1),
|
||||||
readOnly_(false),
|
readOnly_(false),
|
||||||
enableMmap_(false),
|
enableMmap_(false),
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
hn_(INVALID_HANDLE_VALUE),
|
||||||
|
mapView_(0),
|
||||||
|
#endif // __MINGW32__
|
||||||
mapaddr_(0),
|
mapaddr_(0),
|
||||||
maplen_(0)
|
maplen_(0)
|
||||||
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
AbstractDiskWriter::~AbstractDiskWriter()
|
AbstractDiskWriter::~AbstractDiskWriter()
|
||||||
|
@ -76,6 +75,8 @@ AbstractDiskWriter::~AbstractDiskWriter()
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
// Returns error code depending on the platform. For MinGW32, return
|
||||||
|
// the value of GetLastError(). Otherwise, return errno.
|
||||||
int fileError()
|
int fileError()
|
||||||
{
|
{
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
|
@ -87,6 +88,9 @@ int fileError()
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
// Formats error message for error code errNum. For MinGW32, errNum is
|
||||||
|
// assumed to be the return value of GetLastError(). Otherwise, it is
|
||||||
|
// errno.
|
||||||
std::string fileStrerror(int errNum)
|
std::string fileStrerror(int errNum)
|
||||||
{
|
{
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
|
@ -95,11 +99,11 @@ std::string fileStrerror(int errNum)
|
||||||
0,
|
0,
|
||||||
errNum,
|
errNum,
|
||||||
// Default language
|
// Default language
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
||||||
(LPTSTR) &buf,
|
(LPTSTR) &buf,
|
||||||
sizeof(buf),
|
sizeof(buf),
|
||||||
0) == 0) {
|
0) == 0) {
|
||||||
snprintf(buf, sizeof(buf), "File IO error %x", errNum);
|
snprintf(buf, sizeof(buf), "File I/O error %x", errNum);
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
#else // !__MINGW32__
|
#else // !__MINGW32__
|
||||||
|
@ -113,13 +117,7 @@ void AbstractDiskWriter::openFile(int64_t totalLength)
|
||||||
try {
|
try {
|
||||||
openExistingFile(totalLength);
|
openExistingFile(totalLength);
|
||||||
} catch(RecoverableException& e) {
|
} catch(RecoverableException& e) {
|
||||||
if(
|
if(e.getErrNum() == ENOENT) {
|
||||||
#ifdef __MINGW32__
|
|
||||||
e.getErrNum() == ERROR_FILE_NOT_FOUND
|
|
||||||
#else // !__MINGW32__
|
|
||||||
e.getErrNum() == ENOENT
|
|
||||||
#endif // !__MINGW32__
|
|
||||||
) {
|
|
||||||
initAndOpenFile(totalLength);
|
initAndOpenFile(totalLength);
|
||||||
} else {
|
} else {
|
||||||
throw;
|
throw;
|
||||||
|
@ -129,85 +127,107 @@ void AbstractDiskWriter::openFile(int64_t totalLength)
|
||||||
|
|
||||||
void AbstractDiskWriter::closeFile()
|
void AbstractDiskWriter::closeFile()
|
||||||
{
|
{
|
||||||
#ifdef HAVE_MMAP
|
#if defined HAVE_MMAP || defined __MINGW32__
|
||||||
if(mapaddr_) {
|
if(mapaddr_) {
|
||||||
int rv = munmap(mapaddr_, maplen_);
|
int errNum = 0;
|
||||||
if(rv == -1) {
|
#ifdef __MINGW32__
|
||||||
int errNum = errno;
|
if(!UnmapViewOfFile(mapaddr_)) {
|
||||||
A2_LOG_ERROR(fmt("munmap for file %s failed: %s",
|
errNum = GetLastError();
|
||||||
filename_.c_str(), strerror(errNum)));
|
}
|
||||||
|
CloseHandle(mapView_);
|
||||||
|
mapView_ = INVALID_HANDLE_VALUE;
|
||||||
|
#else // !__MINGW32__
|
||||||
|
if(munmap(mapaddr_, maplen_) == -1) {
|
||||||
|
errNum = errno;
|
||||||
|
}
|
||||||
|
#endif // !__MINGW32__
|
||||||
|
if(errNum != 0) {
|
||||||
|
int errNum = fileError();
|
||||||
|
A2_LOG_ERROR(fmt("Unmapping file %s failed: %s",
|
||||||
|
filename_.c_str(), fileStrerror(errNum).c_str()));
|
||||||
} else {
|
} else {
|
||||||
A2_LOG_INFO(fmt("munmap for file %s succeeded", filename_.c_str()));
|
A2_LOG_INFO(fmt("Unmapping file %s succeeded", filename_.c_str()));
|
||||||
}
|
}
|
||||||
mapaddr_ = 0;
|
mapaddr_ = 0;
|
||||||
maplen_ = 0;
|
maplen_ = 0;
|
||||||
}
|
}
|
||||||
#endif // HAVE_MMAP
|
#endif // HAVE_MMAP || defined __MINGW32__
|
||||||
if(fd_ != A2_BAD_FILE_DESCRIPTOR) {
|
if(fd_ != -1) {
|
||||||
#ifdef __MINGW32__
|
|
||||||
CloseHandle(fd_);
|
|
||||||
#else // !__MINGW32__
|
|
||||||
close(fd_);
|
close(fd_);
|
||||||
#endif // !__MINGW32__
|
fd_ = -1;
|
||||||
fd_ = A2_BAD_FILE_DESCRIPTOR;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
#ifdef __MINGW32__
|
// TODO I tried CreateFile, but ReadFile fails with "Access Denied"
|
||||||
HANDLE openFileWithFlags(const std::string& filename, int flags,
|
// when reading (not fully populated) sparse files on NTFS.
|
||||||
error_code::Value errCode)
|
//
|
||||||
{
|
// HANDLE openFileWithFlags(const std::string& filename, int flags,
|
||||||
HANDLE hn;
|
// error_code::Value errCode)
|
||||||
DWORD desiredAccess = 0, sharedMode = FILE_SHARE_READ, creationDisp = 0;
|
// {
|
||||||
if(flags & O_RDONLY) {
|
// HANDLE hn;
|
||||||
desiredAccess |= GENERIC_READ;
|
// DWORD desiredAccess = 0;
|
||||||
} else if(flags & O_RDWR) {
|
// DWORD sharedMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
|
||||||
desiredAccess |= GENERIC_READ | GENERIC_WRITE;
|
// DWORD creationDisp = 0;
|
||||||
}
|
// if(flags & O_RDONLY) {
|
||||||
if(flags & O_CREAT) {
|
// desiredAccess |= GENERIC_READ;
|
||||||
if(flags & O_TRUNC) {
|
// } else if(flags & O_RDWR) {
|
||||||
creationDisp |= CREATE_ALWAYS;
|
// desiredAccess |= GENERIC_READ | GENERIC_WRITE;
|
||||||
} else {
|
// }
|
||||||
creationDisp |= CREATE_NEW;
|
// if(flags & O_CREAT) {
|
||||||
}
|
// if(flags & O_TRUNC) {
|
||||||
} else {
|
// creationDisp |= CREATE_ALWAYS;
|
||||||
creationDisp |= OPEN_EXISTING;
|
// } else {
|
||||||
}
|
// creationDisp |= CREATE_NEW;
|
||||||
hn = CreateFileW(utf8ToWChar(filename).c_str(),
|
// }
|
||||||
desiredAccess, sharedMode, 0, creationDisp,
|
// } else {
|
||||||
FILE_ATTRIBUTE_NORMAL, 0);
|
// creationDisp |= OPEN_EXISTING;
|
||||||
if(hn == INVALID_HANDLE_VALUE) {
|
// }
|
||||||
int errNum = GetLastError();
|
// hn = CreateFileW(utf8ToWChar(filename).c_str(),
|
||||||
throw DL_ABORT_EX3(errNum, fmt(EX_FILE_OPEN,
|
// desiredAccess, sharedMode, 0, creationDisp,
|
||||||
filename.c_str(),
|
// FILE_ATTRIBUTE_NORMAL, 0);
|
||||||
fileStrerror(errNum).c_str()),
|
// if(hn == INVALID_HANDLE_VALUE) {
|
||||||
errCode);
|
// int errNum = GetLastError();
|
||||||
}
|
// throw DL_ABORT_EX3(errNum, fmt(EX_FILE_OPEN,
|
||||||
return hn;
|
// filename.c_str(),
|
||||||
}
|
// fileStrerror(errNum).c_str()),
|
||||||
#else // !__MINGW32__
|
// errCode);
|
||||||
|
// }
|
||||||
|
// DWORD bytesReturned;
|
||||||
|
// if(!DeviceIoControl(hn, FSCTL_SET_SPARSE, 0, 0, 0, 0, &bytesReturned, 0)) {
|
||||||
|
// A2_LOG_WARN(fmt("Making file sparse failed or pending: %s",
|
||||||
|
// fileStrerror(GetLastError()).c_str()));
|
||||||
|
// }
|
||||||
|
// return hn;
|
||||||
int openFileWithFlags(const std::string& filename, int flags,
|
int openFileWithFlags(const std::string& filename, int flags,
|
||||||
error_code::Value errCode)
|
error_code::Value errCode)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
while((fd = a2open(filename.c_str(), flags, OPEN_MODE)) == -1
|
while((fd = a2open(utf8ToWChar(filename).c_str(), flags, OPEN_MODE)) == -1
|
||||||
&& errno == EINTR);
|
&& errno == EINTR);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
int errNum = errno;
|
int errNum = errno;
|
||||||
throw DL_ABORT_EX3(errNum, fmt(EX_FILE_OPEN,
|
throw DL_ABORT_EX3(errNum, fmt(EX_FILE_OPEN, filename.c_str(),
|
||||||
filename.c_str(),
|
util::safeStrerror(errNum).c_str()),
|
||||||
fileStrerror(errNum).c_str()),
|
|
||||||
errCode);
|
errCode);
|
||||||
}
|
}
|
||||||
|
// This may reduce memory consumption on Mac OS X. Not tested.
|
||||||
#if defined(__APPLE__) && defined(__MACH__)
|
#if defined(__APPLE__) && defined(__MACH__)
|
||||||
fcntl(fd, F_GLOBAL_NOCACHE, 1);
|
fcntl(fd, F_GLOBAL_NOCACHE, 1);
|
||||||
#endif // __APPLE__ && __MACH__
|
#endif // __APPLE__ && __MACH__
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
#endif // !__MINGW32__
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
namespace {
|
||||||
|
HANDLE getWin32Handle(int fd)
|
||||||
|
{
|
||||||
|
return reinterpret_cast<HANDLE>(_get_osfhandle(fd));
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
#endif // __MINGW32__
|
||||||
|
|
||||||
void AbstractDiskWriter::openExistingFile(int64_t totalLength)
|
void AbstractDiskWriter::openExistingFile(int64_t totalLength)
|
||||||
{
|
{
|
||||||
int flags = O_BINARY;
|
int flags = O_BINARY;
|
||||||
|
@ -217,6 +237,9 @@ void AbstractDiskWriter::openExistingFile(int64_t totalLength)
|
||||||
flags |= O_RDWR;
|
flags |= O_RDWR;
|
||||||
}
|
}
|
||||||
fd_ = openFileWithFlags(filename_, flags, error_code::FILE_OPEN_ERROR);
|
fd_ = openFileWithFlags(filename_, flags, error_code::FILE_OPEN_ERROR);
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
hn_ = getWin32Handle(fd_);
|
||||||
|
#endif // __MINGW32__
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractDiskWriter::createFile(int addFlags)
|
void AbstractDiskWriter::createFile(int addFlags)
|
||||||
|
@ -225,6 +248,17 @@ void AbstractDiskWriter::createFile(int addFlags)
|
||||||
util::mkdirs(File(filename_).getDirname());
|
util::mkdirs(File(filename_).getDirname());
|
||||||
fd_ = openFileWithFlags(filename_, O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags,
|
fd_ = openFileWithFlags(filename_, O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags,
|
||||||
error_code::FILE_CREATE_ERROR);
|
error_code::FILE_CREATE_ERROR);
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
hn_ = getWin32Handle(fd_);
|
||||||
|
// According to the documentation, the file is not sparse by default
|
||||||
|
// on NTFS.
|
||||||
|
DWORD bytesReturned;
|
||||||
|
if(!DeviceIoControl(hn_, FSCTL_SET_SPARSE, 0, 0, 0, 0, &bytesReturned, 0)) {
|
||||||
|
int errNum = GetLastError();
|
||||||
|
A2_LOG_WARN(fmt("Making file sparse failed or pending: %s",
|
||||||
|
fileStrerror(errNum).c_str()));
|
||||||
|
}
|
||||||
|
#endif // __MINGW32__
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data,
|
ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data,
|
||||||
|
@ -237,19 +271,12 @@ ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data,
|
||||||
ssize_t writtenLength = 0;
|
ssize_t writtenLength = 0;
|
||||||
seek(offset);
|
seek(offset);
|
||||||
while((size_t)writtenLength < len) {
|
while((size_t)writtenLength < len) {
|
||||||
#ifdef __MINGW32__
|
|
||||||
DWORD ret;
|
|
||||||
if(!WriteFile(fd_, data, len, &ret, 0)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#else // !__MINGW32__
|
|
||||||
ssize_t ret = 0;
|
ssize_t ret = 0;
|
||||||
while((ret = write(fd_, data+writtenLength, len-writtenLength)) == -1 &&
|
while((ret = write(fd_, data+writtenLength, len-writtenLength)) == -1 &&
|
||||||
errno == EINTR);
|
errno == EINTR);
|
||||||
if(ret == -1) {
|
if(ret == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif // !__MINGW32__
|
|
||||||
writtenLength += ret;
|
writtenLength += ret;
|
||||||
}
|
}
|
||||||
return writtenLength;
|
return writtenLength;
|
||||||
|
@ -270,15 +297,8 @@ ssize_t AbstractDiskWriter::readDataInternal(unsigned char* data, size_t len,
|
||||||
return readlen;
|
return readlen;
|
||||||
} else {
|
} else {
|
||||||
seek(offset);
|
seek(offset);
|
||||||
#ifdef __MINGW32__
|
|
||||||
DWORD ret;
|
|
||||||
if(!ReadFile(fd_, data, len, &ret, 0)) {
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
#else // !__MINGW32__
|
|
||||||
ssize_t ret = 0;
|
ssize_t ret = 0;
|
||||||
while((ret = read(fd_, data, len)) == -1 && errno == EINTR);
|
while((ret = read(fd_, data, len)) == -1 && errno == EINTR);
|
||||||
#endif // !__MINGW32__
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -288,75 +308,102 @@ void AbstractDiskWriter::seek(int64_t offset)
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
LARGE_INTEGER fileLength;
|
LARGE_INTEGER fileLength;
|
||||||
fileLength.QuadPart = offset;
|
fileLength.QuadPart = offset;
|
||||||
if(SetFilePointerEx(fd_, fileLength, 0, FILE_BEGIN) == 0) {
|
if(SetFilePointerEx(hn_, fileLength, 0, FILE_BEGIN) == 0)
|
||||||
int errNum = GetLastError();
|
|
||||||
#else // !__MINGW32__
|
#else // !__MINGW32__
|
||||||
if(a2lseek(fd_, offset, SEEK_SET) == (off_t)-1) {
|
if(a2lseek(fd_, offset, SEEK_SET) == (off_t)-1)
|
||||||
int errNum = errno;
|
|
||||||
#endif // !__MINGW32__
|
#endif // !__MINGW32__
|
||||||
throw DL_ABORT_EX2(fmt(EX_FILE_SEEK,
|
{
|
||||||
filename_.c_str(),
|
int errNum = fileError();
|
||||||
fileStrerror(errNum).c_str()),
|
throw DL_ABORT_EX2(fmt(EX_FILE_SEEK, filename_.c_str(),
|
||||||
error_code::FILE_IO_ERROR);
|
fileStrerror(errNum).c_str()),
|
||||||
}
|
error_code::FILE_IO_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset)
|
void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_MMAP
|
#if defined HAVE_MMAP || defined __MINGW32__
|
||||||
if(enableMmap_) {
|
if(enableMmap_) {
|
||||||
if(mapaddr_) {
|
if(mapaddr_) {
|
||||||
if(static_cast<int64_t>(len + offset) > maplen_) {
|
if(static_cast<int64_t>(len + offset) > maplen_) {
|
||||||
munmap(mapaddr_, maplen_);
|
int errNum = 0;
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
if(!UnmapViewOfFile(mapaddr_)) {
|
||||||
|
errNum = GetLastError();
|
||||||
|
}
|
||||||
|
CloseHandle(mapView_);
|
||||||
|
mapView_ = INVALID_HANDLE_VALUE;
|
||||||
|
#else // !__MINGW32__
|
||||||
|
if(munmap(mapaddr_, maplen_) == -1) {
|
||||||
|
errNum = errno;
|
||||||
|
}
|
||||||
|
#endif // !__MINGW32__
|
||||||
|
if(errNum != 0) {
|
||||||
|
A2_LOG_ERROR(fmt("Unmapping file %s failed: %s",
|
||||||
|
filename_.c_str(), fileStrerror(errNum).c_str()));
|
||||||
|
}
|
||||||
mapaddr_ = 0;
|
mapaddr_ = 0;
|
||||||
maplen_ = 0;
|
maplen_ = 0;
|
||||||
enableMmap_ = false;
|
enableMmap_ = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
int64_t filesize = size();
|
int64_t filesize = size();
|
||||||
|
int errNum = 0;
|
||||||
if(static_cast<int64_t>(len + offset) <= filesize) {
|
if(static_cast<int64_t>(len + offset) <= filesize) {
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
mapView_ = CreateFileMapping(hn_, 0, PAGE_READWRITE,
|
||||||
|
filesize >> 32, filesize & 0xffffffffu,
|
||||||
|
0);
|
||||||
|
if(mapView_) {
|
||||||
|
mapaddr_ = reinterpret_cast<unsigned char*>
|
||||||
|
(MapViewOfFile(mapView_, FILE_MAP_WRITE, 0, 0, 0));
|
||||||
|
if(!mapaddr_) {
|
||||||
|
errNum = GetLastError();
|
||||||
|
CloseHandle(mapView_);
|
||||||
|
mapView_ = INVALID_HANDLE_VALUE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
errNum = GetLastError();
|
||||||
|
}
|
||||||
|
#else // !__MINGW32__
|
||||||
mapaddr_ = reinterpret_cast<unsigned char*>
|
mapaddr_ = reinterpret_cast<unsigned char*>
|
||||||
(mmap(0, size(), PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0));
|
(mmap(0, size(), PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0));
|
||||||
|
if(!mapaddr_) {
|
||||||
|
errNum = errno;
|
||||||
|
}
|
||||||
|
#endif // !__MINGW32__
|
||||||
if(mapaddr_) {
|
if(mapaddr_) {
|
||||||
A2_LOG_DEBUG(fmt("mmap for file %s succeeded, length=%" PRId64 "",
|
A2_LOG_DEBUG(fmt("Mapping file %s succeeded, length=%" PRId64 "",
|
||||||
filename_.c_str(),
|
filename_.c_str(),
|
||||||
static_cast<uint64_t>(filesize)));
|
static_cast<uint64_t>(filesize)));
|
||||||
maplen_ = filesize;
|
maplen_ = filesize;
|
||||||
} else {
|
} else {
|
||||||
int errNum = errno;
|
A2_LOG_WARN(fmt("Mapping file %s failed: %s",
|
||||||
A2_LOG_INFO(fmt("mmap for file %s failed: %s",
|
filename_.c_str(), fileStrerror(errNum).c_str()));
|
||||||
filename_.c_str(), strerror(errNum)));
|
|
||||||
enableMmap_ = false;
|
enableMmap_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // HAVE_MMAP
|
#endif // HAVE_MMAP || __MINGW32__
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, int64_t offset)
|
void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, int64_t offset)
|
||||||
{
|
{
|
||||||
ensureMmapWrite(len, offset);
|
ensureMmapWrite(len, offset);
|
||||||
if(writeDataInternal(data, len, offset) < 0) {
|
if(writeDataInternal(data, len, offset) < 0) {
|
||||||
int errNum = fileError();
|
int errNum = errno;
|
||||||
// If errNum is ENOSPC(not enough space in device) (or
|
// If errNum is ENOSPC(not enough space in device), throw
|
||||||
// ERROR_HANDLE_DISK_FULL on MinGW), throw
|
|
||||||
// DownloadFailureException and abort download instantly.
|
// DownloadFailureException and abort download instantly.
|
||||||
if(
|
if(errNum == ENOSPC) {
|
||||||
#ifdef __MINGW32__
|
|
||||||
errNum == ERROR_HANDLE_DISK_FULL
|
|
||||||
#else // !__MINGW32__
|
|
||||||
errNum == ENOSPC
|
|
||||||
#endif // !__MINGW32__
|
|
||||||
) {
|
|
||||||
throw DOWNLOAD_FAILURE_EXCEPTION3
|
throw DOWNLOAD_FAILURE_EXCEPTION3
|
||||||
(errNum,
|
(errNum, fmt(EX_FILE_WRITE, filename_.c_str(),
|
||||||
fmt(EX_FILE_WRITE, filename_.c_str(), fileStrerror(errNum).c_str()),
|
util::safeStrerror(errNum).c_str()),
|
||||||
error_code::NOT_ENOUGH_DISK_SPACE);
|
error_code::NOT_ENOUGH_DISK_SPACE);
|
||||||
} else {
|
} else {
|
||||||
throw DL_ABORT_EX3
|
throw DL_ABORT_EX3
|
||||||
(errNum,
|
(errNum, fmt(EX_FILE_WRITE, filename_.c_str(),
|
||||||
fmt(EX_FILE_WRITE, filename_.c_str(), fileStrerror(errNum).c_str()),
|
util::safeStrerror(errNum).c_str()),
|
||||||
error_code::FILE_IO_ERROR);
|
error_code::FILE_IO_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -366,10 +413,10 @@ ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, int64_t of
|
||||||
{
|
{
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
if((ret = readDataInternal(data, len, offset)) < 0) {
|
if((ret = readDataInternal(data, len, offset)) < 0) {
|
||||||
int errNum = fileError();
|
int errNum = errno;
|
||||||
throw DL_ABORT_EX3
|
throw DL_ABORT_EX3
|
||||||
(errNum,
|
(errNum, fmt(EX_FILE_READ, filename_.c_str(),
|
||||||
fmt(EX_FILE_READ, filename_.c_str(), fileStrerror(errNum).c_str()),
|
util::safeStrerror(errNum).c_str()),
|
||||||
error_code::FILE_IO_ERROR);
|
error_code::FILE_IO_ERROR);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -377,42 +424,38 @@ ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, int64_t of
|
||||||
|
|
||||||
void AbstractDiskWriter::truncate(int64_t length)
|
void AbstractDiskWriter::truncate(int64_t length)
|
||||||
{
|
{
|
||||||
if(fd_ == A2_BAD_FILE_DESCRIPTOR) {
|
if(fd_ == -1) {
|
||||||
throw DL_ABORT_EX("File not yet opened.");
|
throw DL_ABORT_EX("File not yet opened.");
|
||||||
}
|
}
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
// Since mingw32's ftruncate cannot handle over 2GB files, we use
|
// Since mingw32's ftruncate cannot handle over 2GB files, we use
|
||||||
// SetEndOfFile instead.
|
// SetEndOfFile instead.
|
||||||
seek(length);
|
seek(length);
|
||||||
if(SetEndOfFile(fd_) == 0) {
|
if(SetEndOfFile(hn_) == 0)
|
||||||
throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
|
#else // !__MINGW32__
|
||||||
GetLastError()),
|
if(ftruncate(fd_, length) == -1)
|
||||||
error_code::FILE_IO_ERROR);
|
#endif // !__MINGW32__
|
||||||
}
|
{
|
||||||
#else
|
int errNum = fileError();
|
||||||
if(ftruncate(fd_, length) == -1) {
|
throw DL_ABORT_EX2(fmt("File truncation failed. cause: %s",
|
||||||
int errNum = errno;
|
fileStrerror(errNum).c_str()),
|
||||||
throw DL_ABORT_EX3(errNum,
|
error_code::FILE_IO_ERROR);
|
||||||
fmt("ftruncate failed. cause: %s",
|
}
|
||||||
util::safeStrerror(errNum).c_str()),
|
|
||||||
error_code::FILE_IO_ERROR);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractDiskWriter::allocate(int64_t offset, int64_t length)
|
void AbstractDiskWriter::allocate(int64_t offset, int64_t length)
|
||||||
{
|
{
|
||||||
if(fd_ == A2_BAD_FILE_DESCRIPTOR) {
|
if(fd_ == -1) {
|
||||||
throw DL_ABORT_EX("File not yet opened.");
|
throw DL_ABORT_EX("File not yet opened.");
|
||||||
}
|
}
|
||||||
#ifdef HAVE_SOME_FALLOCATE
|
#ifdef HAVE_SOME_FALLOCATE
|
||||||
# ifdef __MINGW32__
|
# ifdef __MINGW32__
|
||||||
seek(offset+length);
|
// TODO Remove __MINGW32__ code because the current implementation
|
||||||
if(SetEndOfFile(fd_) == 0) {
|
// does not behave like ftruncate or posix_ftruncate. Actually, it
|
||||||
throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
|
// is the same sa ftruncate.
|
||||||
GetLastError()),
|
A2_LOG_WARN("--file-allocation=falloc is now deprecated for MinGW32 build. "
|
||||||
error_code::FILE_IO_ERROR);
|
"Consider to use --file-allocation=trunc instead.");
|
||||||
}
|
truncate(offset+length);
|
||||||
# elif HAVE_FALLOCATE
|
# elif HAVE_FALLOCATE
|
||||||
// For linux, we use fallocate to detect file system supports
|
// For linux, we use fallocate to detect file system supports
|
||||||
// fallocate or not.
|
// fallocate or not.
|
||||||
|
|
|
@ -44,15 +44,18 @@ class AbstractDiskWriter : public DiskWriter {
|
||||||
private:
|
private:
|
||||||
std::string filename_;
|
std::string filename_;
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
|
||||||
HANDLE fd_;
|
|
||||||
#else // !__MINGW32__
|
|
||||||
int fd_;
|
int fd_;
|
||||||
#endif // !__MINGW32__
|
|
||||||
|
|
||||||
bool readOnly_;
|
bool readOnly_;
|
||||||
|
|
||||||
bool enableMmap_;
|
bool enableMmap_;
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
// This handle is retrieved from fd_ using _get_osfhandle(). It is
|
||||||
|
// used for Win32 API functions which only accept HANDLE.
|
||||||
|
HANDLE hn_;
|
||||||
|
// The handle for memory mapped file. mmap equivalent in Windows.
|
||||||
|
HANDLE mapView_;
|
||||||
|
#endif // __MINGW32__
|
||||||
unsigned char* mapaddr_;
|
unsigned char* mapaddr_;
|
||||||
int64_t maplen_;
|
int64_t maplen_;
|
||||||
|
|
||||||
|
|
|
@ -293,7 +293,7 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
|
||||||
op->addTag(TAG_FILE);
|
op->addTag(TAG_FILE);
|
||||||
handlers.push_back(op);
|
handlers.push_back(op);
|
||||||
}
|
}
|
||||||
#ifdef HAVE_MMAP
|
#if defined HAVE_MMAP || defined __MINGW32__
|
||||||
{
|
{
|
||||||
OptionHandler* op(new BooleanOptionHandler
|
OptionHandler* op(new BooleanOptionHandler
|
||||||
(PREF_ENABLE_MMAP,
|
(PREF_ENABLE_MMAP,
|
||||||
|
@ -307,7 +307,7 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
|
||||||
op->setChangeOptionForReserved(true);
|
op->setChangeOptionForReserved(true);
|
||||||
handlers.push_back(op);
|
handlers.push_back(op);
|
||||||
}
|
}
|
||||||
#endif // HAVE_MMAP
|
#endif // HAVE_MMAP || __MINGW32__
|
||||||
{
|
{
|
||||||
OptionHandler* op(new BooleanOptionHandler
|
OptionHandler* op(new BooleanOptionHandler
|
||||||
(PREF_ENABLE_RPC,
|
(PREF_ENABLE_RPC,
|
||||||
|
|
11
src/a2io.h
11
src/a2io.h
|
@ -46,6 +46,12 @@
|
||||||
#ifdef HAVE_IO_H
|
#ifdef HAVE_IO_H
|
||||||
# include <io.h>
|
# include <io.h>
|
||||||
#endif // HAVE_IO_H
|
#endif // HAVE_IO_H
|
||||||
|
#ifdef HAVE_WINIOCTL_H
|
||||||
|
# include <winioctl.h>
|
||||||
|
#endif // HAVE_WINIOCTL_H
|
||||||
|
#ifdef HAVE_SHARE_H
|
||||||
|
# include <share.h>
|
||||||
|
#endif // HAVE_SHARE_H
|
||||||
|
|
||||||
// in some platforms following definitions are missing:
|
// in some platforms following definitions are missing:
|
||||||
#ifndef EINPROGRESS
|
#ifndef EINPROGRESS
|
||||||
|
@ -132,8 +138,9 @@
|
||||||
# define a2unlink(path) _wunlink(path)
|
# define a2unlink(path) _wunlink(path)
|
||||||
# define a2rmdir(path) _wrmdir(path)
|
# define a2rmdir(path) _wrmdir(path)
|
||||||
# define a2rename(src, dest) _wrename(src, dest)
|
# define a2rename(src, dest) _wrename(src, dest)
|
||||||
# define a2open(path, flags, mode) _wopen(path, flags, mode)
|
// For Windows, we share files for reading and writing.
|
||||||
# define a2fopen(path, mode) _wfopen(path, mode)
|
# define a2open(path, flags, mode) _wsopen(path, flags, _SH_DENYNO, mode)
|
||||||
|
# define a2fopen(path, mode) _wfsopen(path, mode, _SH_DENYNO)
|
||||||
#else // !__MINGW32__
|
#else // !__MINGW32__
|
||||||
# define a2lseek(fd, offset, origin) lseek(fd, offset, origin)
|
# define a2lseek(fd, offset, origin) lseek(fd, offset, origin)
|
||||||
# define a2fseek(fp, offset, origin) fseek(fp, offset, origin)
|
# define a2fseek(fp, offset, origin) fseek(fp, offset, origin)
|
||||||
|
|
Loading…
Reference in New Issue