]>
Commit | Line | Data |
---|---|---|
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 | /*}}}*/ | |
34 | using namespace std; | |
35 | ||
36 | bool 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]; | |
47 | new pkgAcqFile(&Fetcher, download_uri, "", 0, "desc", "short-desc", | |
48 | "dest-dir-ignored", targetfile); | |
49 | Fetcher.Run(); | |
50 | if (!FileExists(targetfile)) | |
51 | { | |
52 | _error->Error(_("Download Failed")); | |
53 | return false; | |
54 | } | |
55 | return true; | |
56 | } | |
57 | ||
58 | bool ShowHelp(CommandLine &CmdL) | |
59 | { | |
60 | ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION, | |
61 | COMMON_ARCH,__DATE__,__TIME__); | |
62 | ||
63 | if (_config->FindB("version") == true) | |
64 | return true; | |
65 | ||
66 | cout << | |
67 | _("Usage: apt-helper [options] command\n" | |
68 | " apt-helper [options] download-file uri target-path\n" | |
69 | "\n" | |
70 | "apt-helper is a internal helper for apt\n" | |
71 | "\n" | |
72 | "Commands:\n" | |
73 | " download-file - download the given uri to the target-path\n" | |
74 | "\n" | |
75 | " This APT helper has Super Meep Powers.\n"); | |
76 | return true; | |
77 | } | |
78 | ||
79 | ||
80 | int main(int argc,const char *argv[]) /*{{{*/ | |
81 | { | |
82 | CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp}, | |
83 | {"download-file", &DoDownloadFile}, | |
84 | {0,0}}; | |
85 | ||
86 | std::vector<CommandLine::Args> Args = getCommandArgs( | |
87 | "apt-download", CommandLine::GetCommand(Cmds, argc, argv)); | |
88 | ||
89 | // Set up gettext support | |
90 | setlocale(LC_ALL,""); | |
91 | textdomain(PACKAGE); | |
92 | ||
93 | // Parse the command line and initialize the package library | |
94 | CommandLine CmdL(Args.data(),_config); | |
95 | if (pkgInitConfig(*_config) == false || | |
96 | CmdL.Parse(argc,argv) == false || | |
97 | pkgInitSystem(*_config,_system) == false) | |
98 | { | |
99 | if (_config->FindB("version") == true) | |
100 | ShowHelp(CmdL); | |
101 | _error->DumpErrors(); | |
102 | return 100; | |
103 | } | |
104 | ||
105 | // See if the help should be shown | |
106 | if (_config->FindB("help") == true || | |
107 | _config->FindB("version") == true || | |
108 | CmdL.FileSize() == 0) | |
109 | { | |
110 | ShowHelp(CmdL); | |
111 | return 0; | |
112 | } | |
113 | ||
114 | InitOutput(); | |
115 | ||
116 | // Match the operation | |
117 | CmdL.DispatchArg(Cmds); | |
118 | ||
119 | // Print any errors or warnings found during parsing | |
120 | bool const Errors = _error->PendingError(); | |
121 | if (_config->FindI("quiet",0) > 0) | |
122 | _error->DumpErrors(); | |
123 | else | |
124 | _error->DumpErrors(GlobalError::DEBUG); | |
125 | return Errors == true ? 100 : 0; | |
126 | } | |
127 | /*}}}*/ |