2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "AbstractDiskWriter.h"
|
2006-04-19 17:23:58 +00:00
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "File.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "message.h"
|
2007-01-08 00:13:25 +00:00
|
|
|
#include "LogFactory.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2006-08-07 16:05:00 +00:00
|
|
|
AbstractDiskWriter::AbstractDiskWriter():
|
2007-01-08 00:13:25 +00:00
|
|
|
fd(0),
|
|
|
|
fileAllocator(0),
|
|
|
|
logger(LogFactory::getInstance())
|
2006-08-07 16:05:00 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
,ctx(DIGEST_ALGO_SHA1)
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
{
|
2006-07-04 10:57:56 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestInit();
|
2006-07-04 10:57:56 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2006-03-22 15:10:03 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
AbstractDiskWriter::~AbstractDiskWriter() {
|
2006-04-19 17:23:58 +00:00
|
|
|
closeFile();
|
2006-07-04 10:57:56 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestFree();
|
2006-07-04 10:57:56 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
void AbstractDiskWriter::openFile(const string& filename, uint64_t totalLength) {
|
2006-04-16 14:38:19 +00:00
|
|
|
File f(filename);
|
|
|
|
if(f.exists()) {
|
|
|
|
openExistingFile(filename);
|
|
|
|
} else {
|
2007-01-08 00:13:25 +00:00
|
|
|
initAndOpenFile(filename, totalLength);
|
2006-04-16 14:38:19 +00:00
|
|
|
}
|
2006-04-12 13:55:43 +00:00
|
|
|
}
|
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
void AbstractDiskWriter::closeFile() {
|
2006-04-18 17:06:17 +00:00
|
|
|
if(fd >= 0) {
|
2006-02-17 13:35:04 +00:00
|
|
|
close(fd);
|
2006-04-18 17:06:17 +00:00
|
|
|
fd = -1;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-19 17:49:03 +00:00
|
|
|
void AbstractDiskWriter::openExistingFile(const string& filename) {
|
2006-04-20 15:58:05 +00:00
|
|
|
this->filename = filename;
|
2006-02-17 13:35:04 +00:00
|
|
|
File f(filename);
|
|
|
|
if(!f.isFile()) {
|
2006-04-19 17:23:58 +00:00
|
|
|
throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), "file not found");
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
if((fd = open(filename.c_str(), O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
|
2006-04-19 17:23:58 +00:00
|
|
|
throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
void AbstractDiskWriter::createFile(const string& filename, int32_t addFlags) {
|
2006-04-20 15:58:05 +00:00
|
|
|
this->filename = filename;
|
2006-02-17 13:35:04 +00:00
|
|
|
// TODO proper filename handling needed
|
|
|
|
assert(filename.size());
|
|
|
|
// if(filename.empty()) {
|
|
|
|
// filename = "index.html";
|
|
|
|
// }
|
2006-03-21 14:12:51 +00:00
|
|
|
if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|addFlags, S_IRUSR|S_IWUSR)) < 0) {
|
2006-04-19 17:23:58 +00:00
|
|
|
throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
void AbstractDiskWriter::writeDataInternal(const char* data, uint32_t len) {
|
2006-02-17 13:35:04 +00:00
|
|
|
if(write(fd, data, len) < 0) {
|
2006-04-20 15:58:05 +00:00
|
|
|
throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
int AbstractDiskWriter::readDataInternal(char* data, uint32_t len) {
|
|
|
|
int32_t ret;
|
2006-03-21 14:12:51 +00:00
|
|
|
if((ret = read(fd, data, len)) < 0) {
|
2006-04-20 15:58:05 +00:00
|
|
|
throw new DlAbortEx(EX_FILE_READ, filename.c_str(), strerror(errno));
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
string AbstractDiskWriter::sha1Sum(int64_t offset, uint64_t length) {
|
2006-07-04 10:57:56 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestReset();
|
2007-01-08 00:13:25 +00:00
|
|
|
|
|
|
|
uint32_t BUFSIZE = 16*1024;
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
for(uint64_t i = 0; i < length/BUFSIZE; i++) {
|
|
|
|
if((int32_t)BUFSIZE != readData(buf, BUFSIZE, offset)) {
|
|
|
|
throw new DlAbortEx(EX_FILE_SHA1SUM, filename.c_str(), strerror(errno));
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2007-01-08 00:13:25 +00:00
|
|
|
ctx.digestUpdate(buf, BUFSIZE);
|
|
|
|
offset += BUFSIZE;
|
|
|
|
}
|
|
|
|
uint32_t r = length%BUFSIZE;
|
|
|
|
if(r > 0) {
|
|
|
|
if((int32_t)r != readData(buf, r, offset)) {
|
|
|
|
throw new DlAbortEx(EX_FILE_SHA1SUM, filename.c_str(), strerror(errno));
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2007-01-08 00:13:25 +00:00
|
|
|
ctx.digestUpdate(buf, r);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2007-01-08 00:13:25 +00:00
|
|
|
unsigned char hashValue[20];
|
|
|
|
ctx.digestFinal(hashValue);
|
|
|
|
return Util::toHex(hashValue, 20);
|
2006-03-21 16:07:22 +00:00
|
|
|
#else
|
|
|
|
return "";
|
2006-07-04 10:57:56 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
void AbstractDiskWriter::seek(int64_t offset) {
|
2006-03-21 14:12:51 +00:00
|
|
|
if(offset != lseek(fd, offset, SEEK_SET)) {
|
2006-04-20 15:58:05 +00:00
|
|
|
throw new DlAbortEx(EX_FILE_SEEK, filename.c_str(), strerror(errno));
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
void AbstractDiskWriter::writeData(const char* data, uint32_t len, int64_t offset) {
|
2006-03-21 14:12:51 +00:00
|
|
|
seek(offset);
|
|
|
|
writeDataInternal(data, len);
|
|
|
|
}
|
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
int AbstractDiskWriter::readData(char* data, uint32_t len, int64_t offset) {
|
2006-03-21 14:12:51 +00:00
|
|
|
seek(offset);
|
|
|
|
return readDataInternal(data, len);
|
|
|
|
}
|
2007-01-08 00:13:25 +00:00
|
|
|
|