]> git.saurik.com Git - apt.git/blame - apt-private/private-download.cc
centralize 'show' implementation of apt and apt-cache
[apt.git] / apt-private / private-download.cc
CommitLineData
866893a6
DK
1// Include Files /*{{{*/
2#include <config.h>
3
4#include <apt-pkg/acquire.h>
5#include <apt-pkg/acquire-item.h>
6#include <apt-pkg/configuration.h>
7#include <apt-pkg/error.h>
460601d5 8#include <apt-pkg/fileutl.h>
866893a6
DK
9#include <apt-pkg/strutl.h>
10
453b82a3
DK
11#include <apt-private/private-output.h>
12#include <apt-private/private-download.h>
866893a6
DK
13
14#include <fstream>
15#include <string>
16#include <vector>
17
460601d5
DK
18#include <unistd.h>
19#include <sys/types.h>
20#include <pwd.h>
21#include <fcntl.h>
9c81f8de
DK
22#include <sys/vfs.h>
23#include <sys/statvfs.h>
24#include <errno.h>
460601d5 25
866893a6
DK
26#include <apti18n.h>
27 /*}}}*/
28
29// CheckAuth - check if each download comes form a trusted source /*{{{*/
30bool CheckAuth(pkgAcquire& Fetcher, bool const PromptUser)
31{
9112f777 32 std::vector<std::string> UntrustedList;
866893a6
DK
33 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
34 if (!(*I)->IsTrusted())
9112f777 35 UntrustedList.push_back((*I)->ShortDesc());
866893a6 36
9112f777 37 if (UntrustedList.empty())
866893a6
DK
38 return true;
39
a3f1d60c
MV
40 return AuthPrompt(UntrustedList, PromptUser);
41}
42
9112f777 43bool AuthPrompt(std::vector<std::string> const &UntrustedList, bool const PromptUser)
a3f1d60c 44{
9112f777
DK
45 ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"), UntrustedList,
46 [](std::string const&) { return true; },
47 [](std::string const&str) { return str; },
48 [](std::string const&) { return ""; });
866893a6
DK
49
50 if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
51 {
52 c2out << _("Authentication warning overridden.\n");
53 return true;
54 }
55
56 if (PromptUser == false)
57 return _error->Error(_("Some packages could not be authenticated"));
58
59 if (_config->FindI("quiet",0) < 2
60 && _config->FindB("APT::Get::Assume-Yes",false) == false)
61 {
62 c2out << _("Install these packages without verification?") << std::flush;
63 if (!YnPrompt(false))
64 return _error->Error(_("Some packages could not be authenticated"));
65
66 return true;
67 }
b381a482
JAK
68 else if (_config->FindB("APT::Get::Force-Yes",false) == true) {
69 _error->Warning(_("--force-yes is deprecated, use one of the options starting with --allow instead."));
866893a6 70 return true;
b381a482 71 }
866893a6 72
b381a482 73 return _error->Error(_("There were unauthenticated packages and -y was used without --allow-unauthenticated"));
866893a6
DK
74}
75 /*}}}*/
76bool AcquireRun(pkgAcquire &Fetcher, int const PulseInterval, bool * const Failure, bool * const TransientNetworkFailure)/*{{{*/
77{
78 pkgAcquire::RunResult res;
79 if(PulseInterval > 0)
80 res = Fetcher.Run(PulseInterval);
81 else
82 res = Fetcher.Run();
83
84 if (res == pkgAcquire::Failed)
85 return false;
86
87 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
88 I != Fetcher.ItemsEnd(); ++I)
89 {
90
91 if ((*I)->Status == pkgAcquire::Item::StatDone &&
92 (*I)->Complete == true)
93 continue;
94
95 if (TransientNetworkFailure != NULL && (*I)->Status == pkgAcquire::Item::StatIdle)
96 {
97 *TransientNetworkFailure = true;
98 continue;
99 }
100
101 ::URI uri((*I)->DescURI());
102 uri.User.clear();
103 uri.Password.clear();
104 std::string descUri = std::string(uri);
105 _error->Error(_("Failed to fetch %s %s\n"), descUri.c_str(),
106 (*I)->ErrorText.c_str());
107
108 if (Failure != NULL)
109 *Failure = true;
110 }
111
112 return true;
113}
114 /*}}}*/
9c81f8de
DK
115bool CheckFreeSpaceBeforeDownload(std::string const &Dir, unsigned long long FetchBytes)/*{{{*/
116{
117 uint32_t const RAMFS_MAGIC = 0x858458f6;
118 /* Check for enough free space, but only if we are actually going to
119 download */
120 if (_config->FindB("APT::Get::Print-URIs", false) == true ||
121 _config->FindB("APT::Get::Download", true) == false)
122 return true;
123
124 struct statvfs Buf;
125 if (statvfs(Dir.c_str(),&Buf) != 0) {
126 if (errno == EOVERFLOW)
127 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
128 Dir.c_str());
129 else
130 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
131 Dir.c_str());
132 }
133 else
134 {
135 unsigned long long const FreeBlocks = _config->Find("APT::Sandbox::User").empty() ? Buf.f_bfree : Buf.f_bavail;
136 if (FreeBlocks < (FetchBytes / Buf.f_bsize))
137 {
138 struct statfs Stat;
139 if (statfs(Dir.c_str(),&Stat) != 0
140#if HAVE_STRUCT_STATFS_F_TYPE
141 || Stat.f_type != RAMFS_MAGIC
142#endif
143 )
144 return _error->Error(_("You don't have enough free space in %s."),
145 Dir.c_str());
146 }
147 }
148 return true;
149}
150 /*}}}*/