download_helper.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "download_helper.h"
  36. #include <iostream>
  37. #include <fstream>
  38. #include <algorithm>
  39. #include <sstream>
  40. #include "RequestGroup.h"
  41. #include "Option.h"
  42. #include "prefs.h"
  43. #include "Metalink2RequestGroup.h"
  44. #include "ProtocolDetector.h"
  45. #include "ParameterizedStringParser.h"
  46. #include "PStringBuildVisitor.h"
  47. #include "UriListParser.h"
  48. #include "DownloadContext.h"
  49. #include "RecoverableException.h"
  50. #include "DlAbortEx.h"
  51. #include "message.h"
  52. #include "fmt.h"
  53. #include "FileEntry.h"
  54. #include "LogFactory.h"
  55. #include "File.h"
  56. #include "util.h"
  57. #include "array_fun.h"
  58. #include "OptionHandler.h"
  59. #include "ByteArrayDiskWriter.h"
  60. #include "a2functional.h"
  61. #include "ByteArrayDiskWriterFactory.h"
  62. #include "MetadataInfo.h"
  63. #ifdef ENABLE_BITTORRENT
  64. # include "bittorrent_helper.h"
  65. # include "BtConstants.h"
  66. # include "UTMetadataPostDownloadHandler.h"
  67. #endif // ENABLE_BITTORRENT
  68. namespace aria2 {
  69. const std::set<std::string>& listRequestOptions()
  70. {
  71. static const std::string REQUEST_OPTIONS[] = {
  72. PREF_DIR,
  73. PREF_CHECK_INTEGRITY,
  74. PREF_CONTINUE,
  75. PREF_ALL_PROXY,
  76. PREF_ALL_PROXY_USER,
  77. PREF_ALL_PROXY_PASSWD,
  78. PREF_CONNECT_TIMEOUT,
  79. PREF_DRY_RUN,
  80. PREF_LOWEST_SPEED_LIMIT,
  81. PREF_MAX_FILE_NOT_FOUND,
  82. PREF_MAX_TRIES,
  83. PREF_NO_PROXY,
  84. PREF_OUT,
  85. PREF_PROXY_METHOD,
  86. PREF_REMOTE_TIME,
  87. PREF_SPLIT,
  88. PREF_TIMEOUT,
  89. PREF_HTTP_AUTH_CHALLENGE,
  90. PREF_HTTP_NO_CACHE,
  91. PREF_HTTP_USER,
  92. PREF_HTTP_PASSWD,
  93. PREF_HTTP_PROXY,
  94. PREF_HTTP_PROXY_USER,
  95. PREF_HTTP_PROXY_PASSWD,
  96. PREF_HTTPS_PROXY,
  97. PREF_HTTPS_PROXY_USER,
  98. PREF_HTTPS_PROXY_PASSWD,
  99. PREF_REFERER,
  100. PREF_ENABLE_HTTP_KEEP_ALIVE,
  101. PREF_ENABLE_HTTP_PIPELINING,
  102. PREF_HEADER,
  103. PREF_USE_HEAD,
  104. PREF_USER_AGENT,
  105. PREF_FTP_USER,
  106. PREF_FTP_PASSWD,
  107. PREF_FTP_PASV,
  108. PREF_FTP_PROXY,
  109. PREF_FTP_PROXY_USER,
  110. PREF_FTP_PROXY_PASSWD,
  111. PREF_FTP_TYPE,
  112. PREF_FTP_REUSE_CONNECTION,
  113. PREF_NO_NETRC,
  114. PREF_REUSE_URI,
  115. PREF_SELECT_FILE,
  116. PREF_BT_ENABLE_LPD,
  117. PREF_BT_EXTERNAL_IP,
  118. PREF_BT_HASH_CHECK_SEED,
  119. PREF_BT_MAX_OPEN_FILES,
  120. PREF_BT_MAX_PEERS,
  121. PREF_BT_METADATA_ONLY,
  122. PREF_BT_MIN_CRYPTO_LEVEL,
  123. PREF_BT_PRIORITIZE_PIECE,
  124. PREF_BT_REQUIRE_CRYPTO,
  125. PREF_BT_REQUEST_PEER_SPEED_LIMIT,
  126. PREF_BT_SAVE_METADATA,
  127. PREF_BT_SEED_UNVERIFIED,
  128. PREF_BT_STOP_TIMEOUT,
  129. PREF_BT_TRACKER_INTERVAL,
  130. PREF_BT_TRACKER_TIMEOUT,
  131. PREF_BT_TRACKER_CONNECT_TIMEOUT,
  132. PREF_ENABLE_PEER_EXCHANGE,
  133. PREF_FOLLOW_TORRENT,
  134. PREF_INDEX_OUT,
  135. PREF_MAX_UPLOAD_LIMIT,
  136. PREF_SEED_RATIO,
  137. PREF_SEED_TIME,
  138. PREF_FOLLOW_METALINK,
  139. PREF_METALINK_SERVERS,
  140. PREF_METALINK_LANGUAGE,
  141. PREF_METALINK_LOCATION,
  142. PREF_METALINK_OS,
  143. PREF_METALINK_VERSION,
  144. PREF_METALINK_PREFERRED_PROTOCOL,
  145. PREF_METALINK_ENABLE_UNIQUE_PROTOCOL,
  146. PREF_ALLOW_OVERWRITE,
  147. PREF_ALLOW_PIECE_LENGTH_CHANGE,
  148. PREF_ASYNC_DNS,
  149. PREF_AUTO_FILE_RENAMING,
  150. PREF_FILE_ALLOCATION,
  151. PREF_MAX_DOWNLOAD_LIMIT,
  152. PREF_NO_FILE_ALLOCATION_LIMIT,
  153. PREF_PARAMETERIZED_URI,
  154. PREF_REALTIME_CHUNK_CHECKSUM,
  155. PREF_REMOVE_CONTROL_FILE,
  156. PREF_ALWAYS_RESUME,
  157. PREF_MAX_RESUME_FAILURE_TRIES,
  158. PREF_HTTP_ACCEPT_GZIP,
  159. PREF_MAX_CONNECTION_PER_SERVER,
  160. PREF_MIN_SPLIT_SIZE,
  161. PREF_CONDITIONAL_GET,
  162. PREF_ENABLE_ASYNC_DNS6,
  163. PREF_BT_TRACKER,
  164. PREF_BT_EXCLUDE_TRACKER,
  165. PREF_RETRY_WAIT,
  166. PREF_METALINK_BASE_URI,
  167. PREF_PAUSE
  168. };
  169. static std::set<std::string> requestOptions
  170. (vbegin(REQUEST_OPTIONS), vend(REQUEST_OPTIONS));
  171. return requestOptions;
  172. }
  173. namespace {
  174. void unfoldURI
  175. (std::vector<std::string>& result, const std::vector<std::string>& args)
  176. {
  177. ParameterizedStringParser p;
  178. PStringBuildVisitor v;
  179. for(std::vector<std::string>::const_iterator itr = args.begin(),
  180. eoi = args.end(); itr != eoi; ++itr) {
  181. v.reset();
  182. p.parse(*itr)->accept(v);
  183. result.insert(result.end(), v.getURIs().begin(), v.getURIs().end());
  184. }
  185. }
  186. } // namespace
  187. namespace {
  188. template<typename InputIterator>
  189. void splitURI(std::vector<std::string>& result,
  190. InputIterator begin,
  191. InputIterator end,
  192. size_t numSplit,
  193. size_t maxIter)
  194. {
  195. size_t numURIs = std::distance(begin, end);
  196. if(numURIs >= numSplit) {
  197. result.insert(result.end(), begin, end);
  198. } else if(numURIs > 0) {
  199. size_t num = std::min(numSplit/numURIs, maxIter);
  200. for(size_t i = 0; i < num; ++i) {
  201. result.insert(result.end(), begin, end);
  202. }
  203. if(num < maxIter) {
  204. result.insert(result.end(), begin, begin+(numSplit%numURIs));
  205. }
  206. }
  207. }
  208. } // namespace
  209. namespace {
  210. SharedHandle<RequestGroup> createRequestGroup
  211. (const SharedHandle<Option>& option, const std::vector<std::string>& uris,
  212. bool useOutOption = false)
  213. {
  214. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  215. SharedHandle<DownloadContext> dctx
  216. (new DownloadContext
  217. (option->getAsInt(PREF_SEGMENT_SIZE),
  218. 0,
  219. useOutOption&&!option->blank(PREF_OUT)?
  220. util::applyDir(option->get(PREF_DIR), option->get(PREF_OUT)):A2STR::NIL));
  221. dctx->getFirstFileEntry()->setUris(uris);
  222. dctx->getFirstFileEntry()->setMaxConnectionPerServer
  223. (option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER));
  224. rg->setDownloadContext(dctx);
  225. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  226. removeOneshotOption(rg->getOption());
  227. return rg;
  228. }
  229. } // namespace
  230. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  231. namespace {
  232. SharedHandle<MetadataInfo> createMetadataInfo(const std::string& uri)
  233. {
  234. return SharedHandle<MetadataInfo>(new MetadataInfo(uri));
  235. }
  236. } // namespace
  237. namespace {
  238. SharedHandle<MetadataInfo> createMetadataInfoDataOnly()
  239. {
  240. return SharedHandle<MetadataInfo>(new MetadataInfo());
  241. }
  242. } // namespace
  243. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  244. #ifdef ENABLE_BITTORRENT
  245. namespace {
  246. SharedHandle<RequestGroup>
  247. createBtRequestGroup(const std::string& torrentFilePath,
  248. const SharedHandle<Option>& option,
  249. const std::vector<std::string>& auxUris,
  250. const std::string& torrentData = "",
  251. bool adjustAnnounceUri = true)
  252. {
  253. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  254. SharedHandle<DownloadContext> dctx(new DownloadContext());
  255. if(torrentData.empty()) {
  256. // may throw exception
  257. bittorrent::load(torrentFilePath, dctx, option, auxUris);
  258. rg->setMetadataInfo(createMetadataInfo(torrentFilePath));
  259. } else {
  260. // may throw exception
  261. bittorrent::loadFromMemory(torrentData, dctx, option, auxUris, "default");
  262. rg->setMetadataInfo(createMetadataInfoDataOnly());
  263. }
  264. if(adjustAnnounceUri) {
  265. bittorrent::adjustAnnounceUri(bittorrent::getTorrentAttrs(dctx), option);
  266. }
  267. dctx->setFileFilter(util::parseIntRange(option->get(PREF_SELECT_FILE)));
  268. std::istringstream indexOutIn(option->get(PREF_INDEX_OUT));
  269. std::map<size_t, std::string> indexPathMap =
  270. util::createIndexPathMap(indexOutIn);
  271. for(std::map<size_t, std::string>::const_iterator i = indexPathMap.begin(),
  272. eoi = indexPathMap.end(); i != eoi; ++i) {
  273. dctx->setFilePathWithIndex
  274. ((*i).first, util::applyDir(option->get(PREF_DIR), (*i).second));
  275. }
  276. rg->setDownloadContext(dctx);
  277. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  278. // Remove "metalink" from Accept Type list to avoid server from
  279. // responding Metalink file for web-seeding URIs.
  280. util::removeMetalinkContentTypes(rg);
  281. removeOneshotOption(rg->getOption());
  282. return rg;
  283. }
  284. } // namespace
  285. namespace {
  286. SharedHandle<RequestGroup>
  287. createBtMagnetRequestGroup(const std::string& magnetLink,
  288. const SharedHandle<Option>& option)
  289. {
  290. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  291. SharedHandle<DownloadContext> dctx
  292. (new DownloadContext(METADATA_PIECE_SIZE, 0,
  293. A2STR::NIL));
  294. // We only know info hash. Total Length is unknown at this moment.
  295. dctx->markTotalLengthIsUnknown();
  296. rg->setFileAllocationEnabled(false);
  297. rg->setPreLocalFileCheckEnabled(false);
  298. bittorrent::loadMagnet(magnetLink, dctx);
  299. SharedHandle<TorrentAttribute> torrentAttrs =
  300. bittorrent::getTorrentAttrs(dctx);
  301. bittorrent::adjustAnnounceUri(torrentAttrs, rg->getOption());
  302. dctx->getFirstFileEntry()->setPath(torrentAttrs->name);
  303. rg->setDownloadContext(dctx);
  304. rg->clearPostDownloadHandler();
  305. SharedHandle<UTMetadataPostDownloadHandler> utMetadataPostHandler
  306. (new UTMetadataPostDownloadHandler());
  307. rg->addPostDownloadHandler(utMetadataPostHandler);
  308. rg->setDiskWriterFactory
  309. (SharedHandle<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
  310. rg->setMetadataInfo(createMetadataInfo(magnetLink));
  311. rg->markInMemoryDownload();
  312. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  313. removeOneshotOption(rg->getOption());
  314. return rg;
  315. }
  316. } // namespace
  317. void createRequestGroupForBitTorrent
  318. (std::vector<SharedHandle<RequestGroup> >& result,
  319. const SharedHandle<Option>& option,
  320. const std::vector<std::string>& uris,
  321. const std::string& torrentData,
  322. bool adjustAnnounceUri)
  323. {
  324. std::vector<std::string> nargs;
  325. if(option->get(PREF_PARAMETERIZED_URI) == A2_V_TRUE) {
  326. unfoldURI(nargs, uris);
  327. } else {
  328. nargs = uris;
  329. }
  330. // we ignore -Z option here
  331. size_t numSplit = option->getAsInt(PREF_SPLIT);
  332. SharedHandle<RequestGroup> rg =
  333. createBtRequestGroup(option->get(PREF_TORRENT_FILE), option, nargs,
  334. torrentData, adjustAnnounceUri);
  335. rg->setNumConcurrentCommand(numSplit);
  336. result.push_back(rg);
  337. }
  338. #endif // ENABLE_BITTORRENT
  339. #ifdef ENABLE_METALINK
  340. void createRequestGroupForMetalink
  341. (std::vector<SharedHandle<RequestGroup> >& result,
  342. const SharedHandle<Option>& option,
  343. const std::string& metalinkData)
  344. {
  345. if(metalinkData.empty()) {
  346. Metalink2RequestGroup().generate(result,
  347. option->get(PREF_METALINK_FILE),
  348. option,
  349. option->get(PREF_METALINK_BASE_URI));
  350. } else {
  351. SharedHandle<ByteArrayDiskWriter> dw(new ByteArrayDiskWriter());
  352. dw->setString(metalinkData);
  353. Metalink2RequestGroup().generate(result, dw, option,
  354. option->get(PREF_METALINK_BASE_URI));
  355. }
  356. }
  357. #endif // ENABLE_METALINK
  358. namespace {
  359. class AccRequestGroup {
  360. private:
  361. std::vector<SharedHandle<RequestGroup> >& requestGroups_;
  362. ProtocolDetector detector_;
  363. SharedHandle<Option> option_;
  364. bool ignoreLocalPath_;
  365. bool throwOnError_;
  366. public:
  367. AccRequestGroup(std::vector<SharedHandle<RequestGroup> >& requestGroups,
  368. const SharedHandle<Option>& option,
  369. bool ignoreLocalPath = false,
  370. bool throwOnError = false):
  371. requestGroups_(requestGroups), option_(option),
  372. ignoreLocalPath_(ignoreLocalPath),
  373. throwOnError_(throwOnError)
  374. {}
  375. void
  376. operator()(const std::string& uri)
  377. {
  378. if(detector_.isStreamProtocol(uri)) {
  379. std::vector<std::string> streamURIs;
  380. size_t numIter = option_->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
  381. size_t numSplit = option_->getAsInt(PREF_SPLIT);
  382. size_t num = std::min(numIter, numSplit);
  383. for(size_t i = 0; i < num; ++i) {
  384. streamURIs.push_back(uri);
  385. }
  386. SharedHandle<RequestGroup> rg = createRequestGroup(option_, streamURIs);
  387. rg->setNumConcurrentCommand(numSplit);
  388. requestGroups_.push_back(rg);
  389. }
  390. #ifdef ENABLE_BITTORRENT
  391. else if(detector_.guessTorrentMagnet(uri)) {
  392. SharedHandle<RequestGroup> group =
  393. createBtMagnetRequestGroup(uri, option_);
  394. requestGroups_.push_back(group);
  395. } else if(!ignoreLocalPath_ && detector_.guessTorrentFile(uri)) {
  396. try {
  397. requestGroups_.push_back
  398. (createBtRequestGroup(uri, option_, std::vector<std::string>()));
  399. } catch(RecoverableException& e) {
  400. if(throwOnError_) {
  401. throw;
  402. } else {
  403. // error occurred while parsing torrent file.
  404. // We simply ignore it.
  405. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  406. }
  407. }
  408. }
  409. #endif // ENABLE_BITTORRENT
  410. #ifdef ENABLE_METALINK
  411. else if(!ignoreLocalPath_ && detector_.guessMetalinkFile(uri)) {
  412. try {
  413. Metalink2RequestGroup().generate(requestGroups_, uri, option_,
  414. option_->get(PREF_METALINK_BASE_URI));
  415. } catch(RecoverableException& e) {
  416. if(throwOnError_) {
  417. throw;
  418. } else {
  419. // error occurred while parsing metalink file.
  420. // We simply ignore it.
  421. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  422. }
  423. }
  424. }
  425. #endif // ENABLE_METALINK
  426. else {
  427. if(throwOnError_) {
  428. throw DL_ABORT_EX(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
  429. } else {
  430. A2_LOG_ERROR(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
  431. }
  432. }
  433. }
  434. };
  435. } // namespace
  436. namespace {
  437. class StreamProtocolFilter {
  438. private:
  439. ProtocolDetector detector_;
  440. public:
  441. bool operator()(const std::string& uri) {
  442. return detector_.isStreamProtocol(uri);
  443. }
  444. };
  445. } // namespace
  446. void createRequestGroupForUri
  447. (std::vector<SharedHandle<RequestGroup> >& result,
  448. const SharedHandle<Option>& option,
  449. const std::vector<std::string>& uris,
  450. bool ignoreForceSequential,
  451. bool ignoreLocalPath,
  452. bool throwOnError)
  453. {
  454. std::vector<std::string> nargs;
  455. if(option->get(PREF_PARAMETERIZED_URI) == A2_V_TRUE) {
  456. unfoldURI(nargs, uris);
  457. } else {
  458. nargs = uris;
  459. }
  460. if(!ignoreForceSequential && option->get(PREF_FORCE_SEQUENTIAL) == A2_V_TRUE) {
  461. std::for_each(nargs.begin(), nargs.end(),
  462. AccRequestGroup(result, option, ignoreLocalPath,
  463. throwOnError));
  464. } else {
  465. std::vector<std::string>::iterator strmProtoEnd =
  466. std::stable_partition(nargs.begin(), nargs.end(), StreamProtocolFilter());
  467. // let's process http/ftp protocols first.
  468. if(nargs.begin() != strmProtoEnd) {
  469. size_t numIter = option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
  470. size_t numSplit = option->getAsInt(PREF_SPLIT);
  471. std::vector<std::string> streamURIs;
  472. splitURI(streamURIs, nargs.begin(), strmProtoEnd, numSplit, numIter);
  473. SharedHandle<RequestGroup> rg =
  474. createRequestGroup(option, streamURIs, true);
  475. rg->setNumConcurrentCommand(numSplit);
  476. result.push_back(rg);
  477. }
  478. // process remaining URIs(local metalink, BitTorrent files)
  479. std::for_each(strmProtoEnd, nargs.end(),
  480. AccRequestGroup(result, option, ignoreLocalPath,
  481. throwOnError));
  482. }
  483. }
  484. namespace {
  485. void createRequestGroupForUriList
  486. (std::vector<SharedHandle<RequestGroup> >& result,
  487. const SharedHandle<Option>& option,
  488. std::istream& in)
  489. {
  490. UriListParser p(in);
  491. while(p.hasNext()) {
  492. std::vector<std::string> uris;
  493. SharedHandle<Option> tempOption(new Option());
  494. p.parseNext(uris, *tempOption.get());
  495. if(uris.empty()) {
  496. continue;
  497. }
  498. SharedHandle<Option> requestOption(new Option(*option.get()));
  499. for(std::set<std::string>::const_iterator i =
  500. listRequestOptions().begin(), eoi = listRequestOptions().end();
  501. i != eoi; ++i) {
  502. if(tempOption->defined(*i)) {
  503. requestOption->put(*i, tempOption->get(*i));
  504. }
  505. }
  506. createRequestGroupForUri(result, requestOption, uris);
  507. }
  508. }
  509. } // namespace
  510. void createRequestGroupForUriList
  511. (std::vector<SharedHandle<RequestGroup> >& result,
  512. const SharedHandle<Option>& option)
  513. {
  514. if(option->get(PREF_INPUT_FILE) == "-") {
  515. createRequestGroupForUriList(result, option, std::cin);
  516. } else {
  517. if(!File(option->get(PREF_INPUT_FILE)).isFile()) {
  518. throw DL_ABORT_EX
  519. (fmt(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(),
  520. "No such file"));
  521. }
  522. std::ifstream f(option->get(PREF_INPUT_FILE).c_str(), std::ios::binary);
  523. createRequestGroupForUriList(result, option, f);
  524. }
  525. }
  526. SharedHandle<MetadataInfo>
  527. createMetadataInfoFromFirstFileEntry(const SharedHandle<DownloadContext>& dctx)
  528. {
  529. if(dctx->getFileEntries().empty()) {
  530. return SharedHandle<MetadataInfo>();
  531. } else {
  532. std::vector<std::string> uris;
  533. dctx->getFileEntries()[0]->getUris(uris);
  534. if(uris.empty()) {
  535. return SharedHandle<MetadataInfo>();
  536. }
  537. return SharedHandle<MetadataInfo>(new MetadataInfo(uris[0]));
  538. }
  539. }
  540. void removeOneshotOption(const SharedHandle<Option>& option)
  541. {
  542. option->remove(PREF_PAUSE);
  543. }
  544. } // namespace aria2