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