download_helper: Cleanup

pull/103/head
Tatsuhiro Tsujikawa 2013-06-24 23:49:49 +09:00
parent b364c2436b
commit 2f6eff3821
2 changed files with 80 additions and 86 deletions

View File

@ -74,9 +74,8 @@ namespace {
void unfoldURI void unfoldURI
(std::vector<std::string>& result, const std::vector<std::string>& args) (std::vector<std::string>& result, const std::vector<std::string>& args)
{ {
for(std::vector<std::string>::const_iterator i = args.begin(), for(const auto& i : args) {
eoi = args.end(); i != eoi; ++i) { paramed_string::expand(std::begin(i), std::end(i),
paramed_string::expand((*i).begin(), (*i).end(),
std::back_inserter(result)); std::back_inserter(result));
} }
} }
@ -92,14 +91,14 @@ void splitURI(std::vector<std::string>& result,
{ {
size_t numURIs = std::distance(begin, end); size_t numURIs = std::distance(begin, end);
if(numURIs >= numSplit) { if(numURIs >= numSplit) {
result.insert(result.end(), begin, end); result.insert(std::end(result), begin, end);
} else if(numURIs > 0) { } else if(numURIs > 0) {
size_t num = std::min(numSplit/numURIs, maxIter); size_t num = std::min(numSplit/numURIs, maxIter);
for(size_t i = 0; i < num; ++i) { for(size_t i = 0; i < num; ++i) {
result.insert(result.end(), begin, end); result.insert(std::end(result), begin, end);
} }
if(num < maxIter) { if(num < maxIter) {
result.insert(result.end(), begin, begin+(numSplit%numURIs)); result.insert(std::end(result), begin, begin+(numSplit%numURIs));
} }
} }
} }
@ -133,14 +132,14 @@ std::shared_ptr<RequestGroup> createRequestGroup
const std::vector<std::string>& uris, const std::vector<std::string>& uris,
bool useOutOption = false) bool useOutOption = false)
{ {
std::shared_ptr<Option> option = util::copy(optionTemplate); auto option = util::copy(optionTemplate);
std::shared_ptr<RequestGroup> rg(new RequestGroup(getGID(option), option)); auto rg = std::make_shared<RequestGroup>(getGID(option), option);
std::shared_ptr<DownloadContext> dctx auto dctx = std::make_shared<DownloadContext>
(new DownloadContext
(option->getAsInt(PREF_PIECE_LENGTH), (option->getAsInt(PREF_PIECE_LENGTH),
0, 0,
useOutOption&&!option->blank(PREF_OUT)? useOutOption&&!option->blank(PREF_OUT)?
util::applyDir(option->get(PREF_DIR), option->get(PREF_OUT)):A2STR::NIL)); util::applyDir(option->get(PREF_DIR),
option->get(PREF_OUT)):A2STR::NIL);
dctx->getFirstFileEntry()->setUris(uris); dctx->getFirstFileEntry()->setUris(uris);
dctx->getFirstFileEntry()->setMaxConnectionPerServer dctx->getFirstFileEntry()->setMaxConnectionPerServer
(option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER)); (option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER));
@ -148,12 +147,12 @@ std::shared_ptr<RequestGroup> createRequestGroup
const std::string& checksum = option->get(PREF_CHECKSUM); const std::string& checksum = option->get(PREF_CHECKSUM);
if(!checksum.empty()) { if(!checksum.empty()) {
std::pair<Scip, Scip> p; std::pair<Scip, Scip> p;
util::divide(p, checksum.begin(), checksum.end(), '='); util::divide(p, std::begin(checksum), std::end(checksum), '=');
std::string hashType(p.first.first, p.first.second); std::string hashType(p.first.first, p.first.second);
std::string hexDigest(p.second.first, p.second.second); std::string hexDigest(p.second.first, p.second.second);
util::lowercase(hashType); util::lowercase(hashType);
dctx->setDigest(hashType, dctx->setDigest(hashType,
util::fromHex(hexDigest.begin(), hexDigest.end())); util::fromHex(std::begin(hexDigest), std::end(hexDigest)));
} }
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
rg->setDownloadContext(dctx); rg->setDownloadContext(dctx);
@ -165,17 +164,17 @@ std::shared_ptr<RequestGroup> createRequestGroup
#if defined ENABLE_BITTORRENT || ENABLE_METALINK #if defined ENABLE_BITTORRENT || ENABLE_METALINK
namespace { namespace {
std::shared_ptr<MetadataInfo> createMetadataInfo(const std::shared_ptr<GroupId>& gid, std::shared_ptr<MetadataInfo> createMetadataInfo
const std::string& uri) (const std::shared_ptr<GroupId>& gid, const std::string& uri)
{ {
return std::shared_ptr<MetadataInfo>(new MetadataInfo(gid, uri)); return std::make_shared<MetadataInfo>(gid, uri);
} }
} // namespace } // namespace
namespace { namespace {
std::shared_ptr<MetadataInfo> createMetadataInfoDataOnly() std::shared_ptr<MetadataInfo> createMetadataInfoDataOnly()
{ {
return std::shared_ptr<MetadataInfo>(new MetadataInfo()); return std::make_shared<MetadataInfo>();
} }
} // namespace } // namespace
#endif // ENABLE_BITTORRENT || ENABLE_METALINK #endif // ENABLE_BITTORRENT || ENABLE_METALINK
@ -190,10 +189,10 @@ createBtRequestGroup(const std::string& metaInfoUri,
const std::shared_ptr<ValueBase>& torrent, const std::shared_ptr<ValueBase>& torrent,
bool adjustAnnounceUri = true) bool adjustAnnounceUri = true)
{ {
std::shared_ptr<Option> option = util::copy(optionTemplate); auto option = util::copy(optionTemplate);
std::shared_ptr<GroupId> gid = getGID(option); auto gid = getGID(option);
std::shared_ptr<RequestGroup> rg(new RequestGroup(gid, option)); auto rg = std::make_shared<RequestGroup>(gid, option);
std::shared_ptr<DownloadContext> dctx(new DownloadContext()); auto dctx = std::make_shared<DownloadContext>();
// may throw exception // may throw exception
bittorrent::loadFromMemory(torrent, dctx, option, auxUris, bittorrent::loadFromMemory(torrent, dctx, option, auxUris,
metaInfoUri.empty() ? "default" : metaInfoUri); metaInfoUri.empty() ? "default" : metaInfoUri);
@ -210,12 +209,10 @@ createBtRequestGroup(const std::string& metaInfoUri,
sgl.normalize(); sgl.normalize();
dctx->setFileFilter(sgl); dctx->setFileFilter(sgl);
std::istringstream indexOutIn(option->get(PREF_INDEX_OUT)); std::istringstream indexOutIn(option->get(PREF_INDEX_OUT));
std::vector<std::pair<size_t, std::string> > indexPaths = auto indexPaths = util::createIndexPaths(indexOutIn);
util::createIndexPaths(indexOutIn); for(const auto& i : indexPaths) {
for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
indexPaths.begin(), eoi = indexPaths.end(); i != eoi; ++i) {
dctx->setFilePathWithIndex dctx->setFilePathWithIndex
((*i).first, util::applyDir(option->get(PREF_DIR), (*i).second)); (i.first, util::applyDir(option->get(PREF_DIR), i.second));
} }
rg->setDownloadContext(dctx); rg->setDownloadContext(dctx);
rg->setPauseRequested(option->getAsBool(PREF_PAUSE)); rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
@ -233,12 +230,11 @@ createBtMagnetRequestGroup
(const std::string& magnetLink, (const std::string& magnetLink,
const std::shared_ptr<Option>& optionTemplate) const std::shared_ptr<Option>& optionTemplate)
{ {
std::shared_ptr<Option> option = util::copy(optionTemplate); auto option = util::copy(optionTemplate);
std::shared_ptr<GroupId> gid = getGID(option); auto gid = getGID(option);
std::shared_ptr<RequestGroup> rg(new RequestGroup(gid, option)); auto rg = std::make_shared<RequestGroup>(gid, option);
std::shared_ptr<DownloadContext> dctx auto dctx = std::make_shared<DownloadContext>(METADATA_PIECE_SIZE, 0,
(new DownloadContext(METADATA_PIECE_SIZE, 0, A2STR::NIL);
A2STR::NIL));
// We only know info hash. Total Length is unknown at this moment. // We only know info hash. Total Length is unknown at this moment.
dctx->markTotalLengthIsUnknown(); dctx->markTotalLengthIsUnknown();
rg->setFileAllocationEnabled(false); rg->setFileAllocationEnabled(false);
@ -249,11 +245,10 @@ createBtMagnetRequestGroup
dctx->getFirstFileEntry()->setPath(torrentAttrs->name); dctx->getFirstFileEntry()->setPath(torrentAttrs->name);
rg->setDownloadContext(dctx); rg->setDownloadContext(dctx);
rg->clearPostDownloadHandler(); rg->clearPostDownloadHandler();
std::shared_ptr<UTMetadataPostDownloadHandler> utMetadataPostHandler auto utMetadataPostHandler =
(new UTMetadataPostDownloadHandler()); std::make_shared<UTMetadataPostDownloadHandler>();
rg->addPostDownloadHandler(utMetadataPostHandler); rg->addPostDownloadHandler(utMetadataPostHandler);
rg->setDiskWriterFactory rg->setDiskWriterFactory(std::make_shared<ByteArrayDiskWriterFactory>());
(std::shared_ptr<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
rg->setMetadataInfo(createMetadataInfo(gid, magnetLink)); rg->setMetadataInfo(createMetadataInfo(gid, magnetLink));
rg->markInMemoryDownload(); rg->markInMemoryDownload();
rg->setPauseRequested(option->getAsBool(PREF_PAUSE)); rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
@ -302,8 +297,7 @@ void createRequestGroupForBitTorrent
} }
// we ignore -Z option here // we ignore -Z option here
size_t numSplit = option->getAsInt(PREF_SPLIT); size_t numSplit = option->getAsInt(PREF_SPLIT);
std::shared_ptr<RequestGroup> rg = auto rg = createBtRequestGroup(metaInfoUri, option, nargs,
createBtRequestGroup(metaInfoUri, option, nargs,
torrent, adjustAnnounceUri); torrent, adjustAnnounceUri);
rg->setNumConcurrentCommand(numSplit); rg->setNumConcurrentCommand(numSplit);
result.push_back(rg); result.push_back(rg);
@ -323,7 +317,7 @@ void createRequestGroupForMetalink
option, option,
option->get(PREF_METALINK_BASE_URI)); option->get(PREF_METALINK_BASE_URI));
} else { } else {
std::shared_ptr<ByteArrayDiskWriter> dw(new ByteArrayDiskWriter()); auto dw = std::make_shared<ByteArrayDiskWriter>();
dw->setString(metalinkData); dw->setString(metalinkData);
Metalink2RequestGroup().generate(result, dw, option, Metalink2RequestGroup().generate(result, dw, option,
option->get(PREF_METALINK_BASE_URI)); option->get(PREF_METALINK_BASE_URI));
@ -360,26 +354,23 @@ public:
for(size_t i = 0; i < num; ++i) { for(size_t i = 0; i < num; ++i) {
streamURIs.push_back(uri); streamURIs.push_back(uri);
} }
std::shared_ptr<RequestGroup> rg = createRequestGroup(option_, streamURIs); auto rg = createRequestGroup(option_, streamURIs);
rg->setNumConcurrentCommand(numSplit); rg->setNumConcurrentCommand(numSplit);
requestGroups_.push_back(rg); requestGroups_.push_back(rg);
} }
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
else if(detector_.guessTorrentMagnet(uri)) { else if(detector_.guessTorrentMagnet(uri)) {
std::shared_ptr<RequestGroup> group = requestGroups_.push_back(createBtMagnetRequestGroup(uri, option_));
createBtMagnetRequestGroup(uri, option_);
requestGroups_.push_back(group);
} else if(!ignoreLocalPath_ && detector_.guessTorrentFile(uri)) { } else if(!ignoreLocalPath_ && detector_.guessTorrentFile(uri)) {
try { try {
bittorrent::ValueBaseBencodeParser parser; bittorrent::ValueBaseBencodeParser parser;
std::shared_ptr<ValueBase> torrent = parseFile(parser, uri); auto torrent = parseFile(parser, uri);
if(!torrent) { if(!torrent) {
throw DL_ABORT_EX2("Bencode decoding failed", throw DL_ABORT_EX2("Bencode decoding failed",
error_code::BENCODE_PARSE_ERROR); error_code::BENCODE_PARSE_ERROR);
} }
requestGroups_.push_back requestGroups_.push_back
(createBtRequestGroup(uri, option_, std::vector<std::string>(), (createBtRequestGroup(uri, option_, {}, torrent));
torrent));
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
if(throwOnError_) { if(throwOnError_) {
throw; throw;
@ -443,22 +434,23 @@ void createRequestGroupForUri
} else { } else {
nargs = uris; nargs = uris;
} }
if(!ignoreForceSequential && option->get(PREF_FORCE_SEQUENTIAL) == A2_V_TRUE) { if(!ignoreForceSequential &&
std::for_each(nargs.begin(), nargs.end(), option->get(PREF_FORCE_SEQUENTIAL) == A2_V_TRUE) {
std::for_each(std::begin(nargs), std::end(nargs),
AccRequestGroup(result, option, ignoreLocalPath, AccRequestGroup(result, option, ignoreLocalPath,
throwOnError)); throwOnError));
} else { } else {
std::vector<std::string>::iterator strmProtoEnd = auto strmProtoEnd =
std::stable_partition(nargs.begin(), nargs.end(), StreamProtocolFilter()); std::stable_partition(std::begin(nargs), std::end(nargs),
StreamProtocolFilter());
// let's process http/ftp protocols first. // let's process http/ftp protocols first.
if(nargs.begin() != strmProtoEnd) { if(std::begin(nargs) != strmProtoEnd) {
size_t numIter = option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER); size_t numIter = option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
size_t numSplit = option->getAsInt(PREF_SPLIT); size_t numSplit = option->getAsInt(PREF_SPLIT);
std::vector<std::string> streamURIs; std::vector<std::string> streamURIs;
splitURI(streamURIs, nargs.begin(), strmProtoEnd, numSplit, numIter); splitURI(streamURIs, std::begin(nargs), strmProtoEnd, numSplit, numIter);
try { try {
std::shared_ptr<RequestGroup> rg = auto rg = createRequestGroup(option, streamURIs, true);
createRequestGroup(option, streamURIs, true);
rg->setNumConcurrentCommand(numSplit); rg->setNumConcurrentCommand(numSplit);
result.push_back(rg); result.push_back(rg);
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
@ -470,7 +462,7 @@ void createRequestGroupForUri
} }
} }
// process remaining URIs(local metalink, BitTorrent files) // process remaining URIs(local metalink, BitTorrent files)
std::for_each(strmProtoEnd, nargs.end(), std::for_each(strmProtoEnd, std::end(nargs),
AccRequestGroup(result, option, ignoreLocalPath, AccRequestGroup(result, option, ignoreLocalPath,
throwOnError)); throwOnError));
} }
@ -492,12 +484,12 @@ bool createRequestGroupFromUriListParser
if(uris.empty()) { if(uris.empty()) {
continue; continue;
} }
std::shared_ptr<Option> requestOption(new Option(*option)); auto requestOption = std::make_shared<Option>(*option);
requestOption->remove(PREF_OUT); requestOption->remove(PREF_OUT);
const std::shared_ptr<OptionParser>& oparser = OptionParser::getInstance(); const auto& oparser = OptionParser::getInstance();
for(size_t i = 1, len = option::countOption(); i < len; ++i) { for(size_t i = 1, len = option::countOption(); i < len; ++i) {
const Pref* pref = option::i2p(i); const auto pref = option::i2p(i);
const OptionHandler* h = oparser->find(pref); const auto h = oparser->find(pref);
if(h && h->getInitialOption() && tempOption.defined(pref)) { if(h && h->getInitialOption() && tempOption.defined(pref)) {
requestOption->put(pref, tempOption.get(pref)); requestOption->put(pref, tempOption.get(pref));
} }
@ -523,21 +515,21 @@ std::shared_ptr<UriListParser> openUriListParser(const std::string& filename)
} }
listPath = filename; listPath = filename;
} }
return std::shared_ptr<UriListParser>(new UriListParser(listPath)); return std::make_shared<UriListParser>(listPath);
} }
void createRequestGroupForUriList void createRequestGroupForUriList
(std::vector<std::shared_ptr<RequestGroup>>& result, (std::vector<std::shared_ptr<RequestGroup>>& result,
const std::shared_ptr<Option>& option) const std::shared_ptr<Option>& option)
{ {
std::shared_ptr<UriListParser> uriListParser = openUriListParser auto uriListParser = openUriListParser(option->get(PREF_INPUT_FILE));
(option->get(PREF_INPUT_FILE));
while(createRequestGroupFromUriListParser(result, option.get(), while(createRequestGroupFromUriListParser(result, option.get(),
uriListParser.get())); uriListParser.get()));
} }
std::shared_ptr<MetadataInfo> std::shared_ptr<MetadataInfo>
createMetadataInfoFromFirstFileEntry(const std::shared_ptr<GroupId>& gid, createMetadataInfoFromFirstFileEntry
(const std::shared_ptr<GroupId>& gid,
const std::shared_ptr<DownloadContext>& dctx) const std::shared_ptr<DownloadContext>& dctx)
{ {
if(dctx->getFileEntries().empty()) { if(dctx->getFileEntries().empty()) {
@ -548,7 +540,7 @@ createMetadataInfoFromFirstFileEntry(const std::shared_ptr<GroupId>& gid,
if(uris.empty()) { if(uris.empty()) {
return std::shared_ptr<MetadataInfo>(); return std::shared_ptr<MetadataInfo>();
} }
return std::shared_ptr<MetadataInfo>(new MetadataInfo(gid, uris[0])); return std::make_shared<MetadataInfo>(gid, uris[0]);
} }
} }

View File

@ -136,7 +136,8 @@ void createRequestGroupForUri
template<typename InputIterator> template<typename InputIterator>
void setMetadataInfo void setMetadataInfo
(InputIterator first, InputIterator last, const std::shared_ptr<MetadataInfo>& mi) (InputIterator first, InputIterator last,
const std::shared_ptr<MetadataInfo>& mi)
{ {
for(; first != last; ++first) { for(; first != last; ++first) {
(*first)->setMetadataInfo(mi); (*first)->setMetadataInfo(mi);
@ -144,7 +145,8 @@ void setMetadataInfo
} }
std::shared_ptr<MetadataInfo> std::shared_ptr<MetadataInfo>
createMetadataInfoFromFirstFileEntry(const std::shared_ptr<GroupId>& gid, createMetadataInfoFromFirstFileEntry
(const std::shared_ptr<GroupId>& gid,
const std::shared_ptr<DownloadContext>& dctx); const std::shared_ptr<DownloadContext>& dctx);
// Removes option value which is only effective at the first // Removes option value which is only effective at the first