]> git.saurik.com Git - apt.git/blame - cmdline/apt-helper.cc
add hashsum support in apt-file download and add more tests
[apt.git] / cmdline / apt-helper.cc
CommitLineData
e43a426e
MV
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* #####################################################################
4 apt-helper - cmdline helpers
5 ##################################################################### */
6 /*}}}*/
7// Include Files /*{{{*/
8#include <config.h>
9
10#include <apt-pkg/cmndline.h>
11#include <apt-pkg/error.h>
12#include <apt-pkg/init.h>
13#include <apt-pkg/strutl.h>
14#include <apt-pkg/pkgsystem.h>
15#include <apt-pkg/fileutl.h>
16#include <apt-pkg/acquire.h>
17#include <apt-pkg/acquire-item.h>
18
19#include <apt-private/acqprogress.h>
20#include <apt-private/private-output.h>
21#include <apt-private/private-cmndline.h>
22
23#include <errno.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/wait.h>
28#include <fcntl.h>
29
30
31
32#include <apti18n.h>
33 /*}}}*/
34using namespace std;
35
36bool DoDownloadFile(CommandLine &CmdL)
37{
38 if (CmdL.FileSize() <= 2)
39 return _error->Error(_("Must specify at least one pair url/filename"));
40
41
42 pkgAcquire Fetcher;
43 AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
44 Fetcher.Setup(&Stat);
45 std::string download_uri = CmdL.FileList[1];
46 std::string targetfile = CmdL.FileList[2];
c1409d1b
MV
47 HashString hash;
48 if (CmdL.FileSize() > 3)
49 hash = HashString(CmdL.FileList[3]);
e43a426e
MV
50 new pkgAcqFile(&Fetcher, download_uri, "", 0, "desc", "short-desc",
51 "dest-dir-ignored", targetfile);
52 Fetcher.Run();
53 if (!FileExists(targetfile))
54 {
55 _error->Error(_("Download Failed"));
56 return false;
57 }
c1409d1b
MV
58 if(hash.empty() == false)
59 if(hash.VerifyFile(targetfile) == false)
60 {
61 _error->Error(_("HashSum Failed"));
62 Rename(targetfile, targetfile+".failed");
63 return false;
64 }
65
e43a426e
MV
66 return true;
67}
68
69bool ShowHelp(CommandLine &CmdL)
70{
71 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
72 COMMON_ARCH,__DATE__,__TIME__);
73
74 if (_config->FindB("version") == true)
75 return true;
76
77 cout <<
78 _("Usage: apt-helper [options] command\n"
79 " apt-helper [options] download-file uri target-path\n"
80 "\n"
81 "apt-helper is a internal helper for apt\n"
82 "\n"
83 "Commands:\n"
84 " download-file - download the given uri to the target-path\n"
85 "\n"
86 " This APT helper has Super Meep Powers.\n");
87 return true;
88}
89
90
91int main(int argc,const char *argv[]) /*{{{*/
92{
93 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
94 {"download-file", &DoDownloadFile},
95 {0,0}};
96
97 std::vector<CommandLine::Args> Args = getCommandArgs(
98 "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
99
100 // Set up gettext support
101 setlocale(LC_ALL,"");
102 textdomain(PACKAGE);
103
104 // Parse the command line and initialize the package library
105 CommandLine CmdL(Args.data(),_config);
106 if (pkgInitConfig(*_config) == false ||
107 CmdL.Parse(argc,argv) == false ||
108 pkgInitSystem(*_config,_system) == false)
109 {
110 if (_config->FindB("version") == true)
111 ShowHelp(CmdL);
112 _error->DumpErrors();
113 return 100;
114 }
115
116 // See if the help should be shown
117 if (_config->FindB("help") == true ||
118 _config->FindB("version") == true ||
119 CmdL.FileSize() == 0)
120 {
121 ShowHelp(CmdL);
122 return 0;
123 }
124
125 InitOutput();
126
127 // Match the operation
128 CmdL.DispatchArg(Cmds);
129
130 // Print any errors or warnings found during parsing
131 bool const Errors = _error->PendingError();
132 if (_config->FindI("quiet",0) > 0)
133 _error->DumpErrors();
134 else
135 _error->DumpErrors(GlobalError::DEBUG);
136 return Errors == true ? 100 : 0;
137}
138 /*}}}*/