2008-08-26 12:39:07 +00:00
|
|
|
#include "TestUtil.h"
|
2009-05-15 05:20:11 +00:00
|
|
|
|
2008-08-26 12:39:07 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2008-09-07 14:38:26 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2009-05-15 05:20:11 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "a2io.h"
|
|
|
|
#include "File.h"
|
|
|
|
#include "FatalException.h"
|
2010-10-09 14:22:49 +00:00
|
|
|
#include "Cookie.h"
|
2010-11-11 02:56:24 +00:00
|
|
|
#include "DefaultDiskWriter.h"
|
2010-11-20 09:36:14 +00:00
|
|
|
#include "fmt.h"
|
2011-10-13 12:40:07 +00:00
|
|
|
#include "util.h"
|
2012-12-19 14:03:48 +00:00
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "RequestGroup.h"
|
2012-12-22 14:37:44 +00:00
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "FileEntry.h"
|
2013-02-25 12:42:54 +00:00
|
|
|
#include "DownloadResult.h"
|
2014-04-16 23:36:19 +00:00
|
|
|
#include "message_digest_helper.h"
|
2008-08-26 12:39:07 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
void createFile(const std::string& path, size_t length)
|
|
|
|
{
|
2008-09-07 14:38:26 +00:00
|
|
|
File(File(path).getDirname()).mkdirs();
|
2012-12-15 09:28:46 +00:00
|
|
|
DefaultDiskWriter dw(path);
|
|
|
|
dw.initAndOpenFile();
|
|
|
|
dw.truncate(length);
|
2008-08-26 12:39:07 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 05:20:11 +00:00
|
|
|
std::string readFile(const std::string& path)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
std::ifstream in(path.c_str(), std::ios::binary);
|
|
|
|
char buf[4096];
|
|
|
|
while(1) {
|
|
|
|
in.read(buf, sizeof(buf));
|
|
|
|
ss.write(buf, in.gcount());
|
|
|
|
if(in.gcount() != sizeof(buf)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
std::unique_ptr<Cookie> createCookie
|
2010-10-09 14:22:49 +00:00
|
|
|
(const std::string& name,
|
|
|
|
const std::string& value,
|
|
|
|
const std::string& domain,
|
|
|
|
bool hostOnly,
|
|
|
|
const std::string& path,
|
|
|
|
bool secure)
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
return make_unique<Cookie>
|
2010-10-09 14:22:49 +00:00
|
|
|
(name, value, 0, false, domain, hostOnly, path, secure, false, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
std::unique_ptr<Cookie> createCookie
|
2010-10-09 14:22:49 +00:00
|
|
|
(const std::string& name,
|
|
|
|
const std::string& value,
|
|
|
|
time_t expiryTime,
|
|
|
|
const std::string& domain,
|
|
|
|
bool hostOnly,
|
|
|
|
const std::string& path,
|
|
|
|
bool secure)
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
return make_unique<Cookie>
|
2010-10-09 14:22:49 +00:00
|
|
|
(name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
|
|
|
|
}
|
|
|
|
|
2011-11-05 09:05:23 +00:00
|
|
|
std::string fromHex(const std::string& s)
|
|
|
|
{
|
|
|
|
return util::fromHex(s.begin(), s.end());
|
|
|
|
}
|
|
|
|
|
2013-07-02 16:13:13 +00:00
|
|
|
std::string fileHexDigest(MessageDigest* ctx, const std::string& filename)
|
2010-11-11 02:56:24 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<DiskWriter> writer(new DefaultDiskWriter(filename));
|
2010-11-11 02:56:24 +00:00
|
|
|
writer->openExistingFile();
|
2011-10-13 12:40:07 +00:00
|
|
|
return util::toHex(message_digest::digest(ctx, writer, 0, writer->size()));
|
2010-11-11 02:56:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 13:04:59 +00:00
|
|
|
WrDiskCacheEntry::DataCell* createDataCell(int64_t goff,
|
|
|
|
const char* data,
|
|
|
|
size_t offset)
|
|
|
|
{
|
|
|
|
WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
|
|
|
|
cell->goff = goff;
|
|
|
|
size_t len = strlen(data);
|
|
|
|
cell->data = new unsigned char[len];
|
|
|
|
memcpy(cell->data, data, len);
|
|
|
|
cell->offset = offset;
|
2012-12-05 17:23:32 +00:00
|
|
|
cell->len = cell->capacity = len - offset;
|
2012-11-27 13:04:59 +00:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<RequestGroup> findReservedGroup
|
2013-07-05 15:59:43 +00:00
|
|
|
(RequestGroupMan* rgman, a2_gid_t gid)
|
2012-12-19 14:03:48 +00:00
|
|
|
{
|
2013-07-05 15:59:43 +00:00
|
|
|
auto rg = rgman->findGroup(gid);
|
2012-12-19 14:03:48 +00:00
|
|
|
if(rg) {
|
|
|
|
if(rg->getState() == RequestGroup::STATE_WAITING) {
|
|
|
|
return rg;
|
|
|
|
} else {
|
|
|
|
rg.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rg;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<RequestGroup> getReservedGroup
|
2013-07-05 15:59:43 +00:00
|
|
|
(RequestGroupMan* rgman, size_t index)
|
2012-12-19 14:03:48 +00:00
|
|
|
{
|
|
|
|
assert(rgman->getReservedGroups().size() > index);
|
2013-07-05 15:59:43 +00:00
|
|
|
auto i = rgman->getReservedGroups().begin();
|
2012-12-19 14:03:48 +00:00
|
|
|
std::advance(i, index);
|
2013-03-01 14:13:24 +00:00
|
|
|
return *i;
|
2012-12-19 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<RequestGroup> createRequestGroup(int32_t pieceLength,
|
2012-12-22 14:37:44 +00:00
|
|
|
int64_t totalLength,
|
|
|
|
const std::string& path,
|
|
|
|
const std::string& uri,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Option>& opt)
|
2012-12-22 14:37:44 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<DownloadContext> dctx(new DownloadContext(pieceLength,
|
2012-12-22 14:37:44 +00:00
|
|
|
totalLength,
|
|
|
|
path));
|
|
|
|
std::vector<std::string> uris;
|
|
|
|
uris.push_back(uri);
|
|
|
|
dctx->getFirstFileEntry()->addUris(uris.begin(), uris.end());
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(), opt));
|
2012-12-22 14:37:44 +00:00
|
|
|
group->setDownloadContext(dctx);
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<DownloadResult> createDownloadResult
|
2013-02-25 12:42:54 +00:00
|
|
|
(error_code::Value result, const std::string& uri)
|
|
|
|
{
|
|
|
|
std::vector<std::string> uris;
|
|
|
|
uris.push_back(uri);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<FileEntry> entry(new FileEntry("/tmp/path", 1, 0, uris));
|
|
|
|
std::vector<std::shared_ptr<FileEntry> > entries;
|
2013-02-25 12:42:54 +00:00
|
|
|
entries.push_back(entry);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<DownloadResult> dr(new DownloadResult());
|
2013-02-25 12:42:54 +00:00
|
|
|
dr->gid = GroupId::create();
|
|
|
|
dr->fileEntries = entries;
|
|
|
|
dr->result = result;
|
|
|
|
dr->belongsTo = 0;
|
|
|
|
dr->inMemoryDownload = false;
|
2013-06-21 16:10:38 +00:00
|
|
|
dr->option = std::shared_ptr<Option>(new Option());
|
2013-02-25 12:42:54 +00:00
|
|
|
return dr;
|
|
|
|
}
|
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
} // namespace aria2
|