Merge branch 'debian/sid' of ssh://git.debian.org/git/apt/apt into debian/sid
[ntk/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 58 if(hash.empty() == false)
32120cde 59 {
c1409d1b
MV
60 if(hash.VerifyFile(targetfile) == false)
61 {
62 _error->Error(_("HashSum Failed"));
63 Rename(targetfile, targetfile+".failed");
64 return false;
65 }
32120cde 66 }
e43a426e
MV
67 return true;
68}
69
70bool ShowHelp(CommandLine &CmdL)
71{
72 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
73 COMMON_ARCH,__DATE__,__TIME__);
74
75 if (_config->FindB("version") == true)
76 return true;
77
78 cout <<
79 _("Usage: apt-helper [options] command\n"
80 " apt-helper [options] download-file uri target-path\n"
81 "\n"
82 "apt-helper is a internal helper for apt\n"
83 "\n"
84 "Commands:\n"
85 " download-file - download the given uri to the target-path\n"
86 "\n"
87 " This APT helper has Super Meep Powers.\n");
88 return true;
89}
90
91
92int main(int argc,const char *argv[]) /*{{{*/
93{
94 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
95 {"download-file", &DoDownloadFile},
96 {0,0}};
97
98 std::vector<CommandLine::Args> Args = getCommandArgs(
99 "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
100
101 // Set up gettext support
102 setlocale(LC_ALL,"");
103 textdomain(PACKAGE);
104
105 // Parse the command line and initialize the package library
106 CommandLine CmdL(Args.data(),_config);
107 if (pkgInitConfig(*_config) == false ||
108 CmdL.Parse(argc,argv) == false ||
109 pkgInitSystem(*_config,_system) == false)
110 {
111 if (_config->FindB("version") == true)
112 ShowHelp(CmdL);
113 _error->DumpErrors();
114 return 100;
115 }
116
117 // See if the help should be shown
118 if (_config->FindB("help") == true ||
119 _config->FindB("version") == true ||
120 CmdL.FileSize() == 0)
121 {
122 ShowHelp(CmdL);
123 return 0;
124 }
125
126 InitOutput();
127
128 // Match the operation
129 CmdL.DispatchArg(Cmds);
130
131 // Print any errors or warnings found during parsing
132 bool const Errors = _error->PendingError();
133 if (_config->FindI("quiet",0) > 0)
134 _error->DumpErrors();
135 else
136 _error->DumpErrors(GlobalError::DEBUG);
137 return Errors == true ? 100 : 0;
138}
139 /*}}}*/