1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
4 apt-helper - cmdline helpers
5 ##################################################################### */
7 // Include Files /*{{{*/
10 #include <apt-pkg/configuration.h>
11 #include <apt-pkg/cmndline.h>
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/init.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/pkgsystem.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/acquire.h>
18 #include <apt-pkg/acquire-item.h>
19 #include <apt-pkg/proxy.h>
21 #include <apt-private/acqprogress.h>
22 #include <apt-private/private-output.h>
23 #include <apt-private/private-download.h>
24 #include <apt-private/private-cmndline.h>
25 #include <apt-private/private-main.h>
26 #include <apt-pkg/srvrec.h>
37 static bool DoAutoDetectProxy(CommandLine
&CmdL
) /*{{{*/
39 if (CmdL
.FileSize() != 2)
40 return _error
->Error(_("Need one URL as argument"));
41 URI
ServerURL(CmdL
.FileList
[1]);
42 if (AutoDetectProxy(ServerURL
) == false)
44 std::string SpecificProxy
= _config
->Find("Acquire::"+ServerURL
.Access
+"::Proxy::" + ServerURL
.Host
);
45 ioprintf(std::cout
, "Using proxy '%s' for URL '%s'\n",
46 SpecificProxy
.c_str(), std::string(ServerURL
).c_str());
51 static bool DoDownloadFile(CommandLine
&CmdL
) /*{{{*/
53 if (CmdL
.FileSize() <= 2)
54 return _error
->Error(_("Must specify at least one pair url/filename"));
56 aptAcquireWithTextStatus Fetcher
;
58 std::vector
<std::string
> targetfiles
;
59 while (fileind
+ 2 <= CmdL
.FileSize())
61 std::string download_uri
= CmdL
.FileList
[fileind
+ 1];
62 std::string targetfile
= CmdL
.FileList
[fileind
+ 2];
64 if (CmdL
.FileSize() > fileind
+ 3)
65 hash
= CmdL
.FileList
[fileind
+ 3];
66 // we use download_uri as descr and targetfile as short-descr
67 new pkgAcqFile(&Fetcher
, download_uri
, hash
, 0, download_uri
, targetfile
,
68 "dest-dir-ignored", targetfile
);
69 targetfiles
.push_back(targetfile
);
74 if (AcquireRun(Fetcher
, 0, &Failed
, NULL
) == false || Failed
== true)
75 return _error
->Error(_("Download Failed"));
76 if (targetfiles
.empty() == false)
77 for (std::vector
<std::string
>::const_iterator f
= targetfiles
.begin(); f
!= targetfiles
.end(); ++f
)
78 if (FileExists(*f
) == false)
79 return _error
->Error(_("Download Failed"));
84 static bool DoSrvLookup(CommandLine
&CmdL
) /*{{{*/
86 if (CmdL
.FileSize() <= 1)
87 return _error
->Error("Must specify at least one SRV record");
89 for(size_t i
= 1; CmdL
.FileList
[i
] != NULL
; ++i
)
91 std::vector
<SrvRec
> srv_records
;
92 std::string
const name
= CmdL
.FileList
[i
];
93 c0out
<< "# Target\tPriority\tWeight\tPort # for " << name
<< std::endl
;
94 size_t const found
= name
.find(":");
95 if (found
!= std::string::npos
)
97 std::string
const host
= name
.substr(0, found
);
98 size_t const port
= atoi(name
.c_str() + found
+ 1);
99 if(GetSrvRecords(host
, port
, srv_records
) == false)
100 _error
->Error(_("GetSrvRec failed for %s"), name
.c_str());
102 else if(GetSrvRecords(name
, srv_records
) == false)
103 _error
->Error(_("GetSrvRec failed for %s"), name
.c_str());
105 for (SrvRec
const &I
: srv_records
)
106 ioprintf(c1out
, "%s\t%d\t%d\t%d\n", I
.target
.c_str(), I
.priority
, I
.weight
, I
.port
);
111 static const APT::Configuration::Compressor
*FindCompressor(std::vector
<APT::Configuration::Compressor
> const & compressors
, std::string name
) /*{{{*/
113 APT::Configuration::Compressor
const * compressor
= NULL
;
114 for (auto const & c
: compressors
)
116 if (compressor
!= NULL
&& c
.Cost
>= compressor
->Cost
)
118 if (c
.Name
== name
|| c
.Extension
== name
|| (!c
.Extension
.empty() && c
.Extension
.substr(1) == name
))
125 static bool DoCatFile(CommandLine
&CmdL
) /*{{{*/
129 std::string
const compressorName
= _config
->Find("Apt-Helper::Cat-File::Compress", "");
131 if (compressorName
.empty() == false)
134 auto const compressors
= APT::Configuration::getCompressors();
135 auto const compressor
= FindCompressor(compressors
, compressorName
);
137 if (compressor
== NULL
)
138 return _error
->Error("Could not find compressor: %s", compressorName
.c_str());
140 if (out
.OpenDescriptor(STDOUT_FILENO
, FileFd::WriteOnly
, *compressor
) == false)
144 if (out
.OpenDescriptor(STDOUT_FILENO
, FileFd::WriteOnly
) == false)
148 if (CmdL
.FileSize() <= 1)
150 if (fd
.OpenDescriptor(STDIN_FILENO
, FileFd::ReadOnly
) == false)
152 if (CopyFile(fd
, out
) == false)
157 for(size_t i
= 1; CmdL
.FileList
[i
] != NULL
; ++i
)
159 std::string
const name
= CmdL
.FileList
[i
];
163 if (fd
.Open(name
, FileFd::ReadOnly
, FileFd::Extension
) == false)
168 if (fd
.OpenDescriptor(STDIN_FILENO
, FileFd::ReadOnly
) == false)
172 if (CopyFile(fd
, out
) == false)
178 static bool ShowHelp(CommandLine
&) /*{{{*/
181 _("Usage: apt-helper [options] command\n"
182 " apt-helper [options] cat-file file ...\n"
183 " apt-helper [options] download-file uri target-path\n"
185 "apt-helper bundles a variety of commands for shell scripts to use\n"
186 "e.g. the same proxy configuration or acquire system as APT would.\n");
190 static std::vector
<aptDispatchWithHelp
> GetCommands() /*{{{*/
193 {"download-file", &DoDownloadFile
, _("download the given uri to the target-path")},
194 {"srv-lookup", &DoSrvLookup
, _("lookup a SRV record (e.g. _http._tcp.ftp.debian.org)")},
195 {"cat-file", &DoCatFile
, _("concatenate files, with automatic decompression")},
196 {"auto-detect-proxy", &DoAutoDetectProxy
, _("detect proxy using apt.conf")},
197 {nullptr, nullptr, nullptr}
201 int main(int argc
,const char *argv
[]) /*{{{*/
204 auto const Cmds
= ParseCommandLine(CmdL
, APT_CMD::APT_HELPER
, &_config
, &_system
, argc
, argv
, &ShowHelp
, &GetCommands
);
208 return DispatchCommandLine(CmdL
, Cmds
);