]> git.saurik.com Git - apt.git/blob - apt-private/private-download.cc
ensure FileFd doesn't try to open /dev/null as atomic and co
[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/cacheset.h>
7 #include <apt-pkg/cmndline.h>
8 #include <apt-pkg/clean.h>
9 #include <apt-pkg/configuration.h>
10 #include <apt-pkg/error.h>
11 #include <apt-pkg/fileutl.h>
12 #include <apt-pkg/strutl.h>
13
14 #include <apt-private/private-cachefile.h>
15 #include <apt-private/private-download.h>
16 #include <apt-private/private-output.h>
17 #include <apt-private/private-utils.h>
18 #include <apt-private/acqprogress.h>
19
20 #include <fstream>
21 #include <string>
22 #include <vector>
23
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <pwd.h>
27 #include <fcntl.h>
28 #include <sys/vfs.h>
29 #include <sys/statvfs.h>
30 #include <errno.h>
31
32 #include <apti18n.h>
33 /*}}}*/
34
35 // CheckAuth - check if each download comes form a trusted source /*{{{*/
36 bool CheckAuth(pkgAcquire& Fetcher, bool const PromptUser)
37 {
38 std::vector<std::string> UntrustedList;
39 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
40 if (!(*I)->IsTrusted())
41 UntrustedList.push_back((*I)->ShortDesc());
42
43 if (UntrustedList.empty())
44 return true;
45
46 return AuthPrompt(UntrustedList, PromptUser);
47 }
48 /*}}}*/
49 bool AuthPrompt(std::vector<std::string> const &UntrustedList, bool const PromptUser)/*{{{*/
50 {
51 ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"), UntrustedList,
52 [](std::string const&) { return true; },
53 [](std::string const&str) { return str; },
54 [](std::string const&) { return ""; });
55
56 if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
57 {
58 c2out << _("Authentication warning overridden.\n");
59 return true;
60 }
61
62 if (PromptUser == false)
63 return _error->Error(_("Some packages could not be authenticated"));
64
65 if (_config->FindI("quiet",0) < 2
66 && _config->FindB("APT::Get::Assume-Yes",false) == false)
67 {
68 c2out << _("Install these packages without verification?") << std::flush;
69 if (!YnPrompt(false))
70 return _error->Error(_("Some packages could not be authenticated"));
71
72 return true;
73 }
74 else if (_config->FindB("APT::Get::Force-Yes",false) == true) {
75 _error->Warning(_("--force-yes is deprecated, use one of the options starting with --allow instead."));
76 return true;
77 }
78
79 return _error->Error(_("There were unauthenticated packages and -y was used without --allow-unauthenticated"));
80 }
81 /*}}}*/
82 bool AcquireRun(pkgAcquire &Fetcher, int const PulseInterval, bool * const Failure, bool * const TransientNetworkFailure)/*{{{*/
83 {
84 pkgAcquire::RunResult res;
85 if(PulseInterval > 0)
86 res = Fetcher.Run(PulseInterval);
87 else
88 res = Fetcher.Run();
89
90 if (res == pkgAcquire::Failed)
91 return false;
92
93 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
94 I != Fetcher.ItemsEnd(); ++I)
95 {
96
97 if ((*I)->Status == pkgAcquire::Item::StatDone &&
98 (*I)->Complete == true)
99 continue;
100
101 if (TransientNetworkFailure != NULL && (*I)->Status == pkgAcquire::Item::StatIdle)
102 {
103 *TransientNetworkFailure = true;
104 continue;
105 }
106
107 ::URI uri((*I)->DescURI());
108 uri.User.clear();
109 uri.Password.clear();
110 std::string descUri = std::string(uri);
111 _error->Error(_("Failed to fetch %s %s\n"), descUri.c_str(),
112 (*I)->ErrorText.c_str());
113
114 if (Failure != NULL)
115 *Failure = true;
116 }
117
118 return true;
119 }
120 /*}}}*/
121 bool CheckFreeSpaceBeforeDownload(std::string const &Dir, unsigned long long FetchBytes)/*{{{*/
122 {
123 uint32_t const RAMFS_MAGIC = 0x858458f6;
124 /* Check for enough free space, but only if we are actually going to
125 download */
126 if (_config->FindB("APT::Get::Print-URIs", false) == true ||
127 _config->FindB("APT::Get::Download", true) == false)
128 return true;
129
130 struct statvfs Buf;
131 if (statvfs(Dir.c_str(),&Buf) != 0) {
132 if (errno == EOVERFLOW)
133 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
134 Dir.c_str());
135 else
136 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
137 Dir.c_str());
138 }
139 else
140 {
141 unsigned long long const FreeBlocks = _config->Find("APT::Sandbox::User").empty() ? Buf.f_bfree : Buf.f_bavail;
142 if (FreeBlocks < (FetchBytes / Buf.f_bsize))
143 {
144 struct statfs Stat;
145 if (statfs(Dir.c_str(),&Stat) != 0
146 #if HAVE_STRUCT_STATFS_F_TYPE
147 || Stat.f_type != RAMFS_MAGIC
148 #endif
149 )
150 return _error->Error(_("You don't have enough free space in %s."),
151 Dir.c_str());
152 }
153 }
154 return true;
155 }
156 /*}}}*/
157
158 aptAcquireWithTextStatus::aptAcquireWithTextStatus() : pkgAcquire::pkgAcquire(),
159 Stat(std::cout, ScreenWidth, _config->FindI("quiet",0))
160 {
161 SetLog(&Stat);
162 }
163
164 // DoDownload - download a binary /*{{{*/
165 bool DoDownload(CommandLine &CmdL)
166 {
167 CacheFile Cache;
168 if (Cache.ReadOnlyOpen() == false)
169 return false;
170
171 APT::CacheSetHelper helper;
172 APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
173 CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
174
175 if (verset.empty() == true)
176 return false;
177
178 pkgRecords Recs(Cache);
179 pkgSourceList *SrcList = Cache.GetSourceList();
180
181 // reuse the usual acquire methods for deb files, but don't drop them into
182 // the usual directories - keep everything in the current directory
183 aptAcquireWithTextStatus Fetcher;
184 std::vector<std::string> storefile(verset.size());
185 std::string const cwd = SafeGetCWD();
186 _config->Set("Dir::Cache::Archives", cwd);
187 int i = 0;
188 for (APT::VersionSet::const_iterator Ver = verset.begin();
189 Ver != verset.end(); ++Ver, ++i)
190 {
191 pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]);
192 if (storefile[i].empty())
193 continue;
194 std::string const filename = cwd + flNotDir(storefile[i]);
195 storefile[i].assign(filename);
196 I->DestFile.assign(filename);
197 }
198
199 // Just print out the uris and exit if the --print-uris flag was used
200 if (_config->FindB("APT::Get::Print-URIs") == true)
201 {
202 pkgAcquire::UriIterator I = Fetcher.UriBegin();
203 for (; I != Fetcher.UriEnd(); ++I)
204 std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
205 I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
206 return true;
207 }
208
209 if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false)
210 return false;
211
212 bool Failed = false;
213 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false)
214 return false;
215
216 // copy files in local sources to the current directory
217 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
218 {
219 std::string const filename = cwd + flNotDir((*I)->DestFile);
220 if ((*I)->Local == true &&
221 filename != (*I)->DestFile &&
222 (*I)->Status == pkgAcquire::Item::StatDone)
223 {
224 std::ifstream src((*I)->DestFile.c_str(), std::ios::binary);
225 std::ofstream dst(filename.c_str(), std::ios::binary);
226 dst << src.rdbuf();
227 }
228 }
229 return Failed == false;
230 }
231 /*}}}*/
232 // DoChangelog - Get changelog from the command line /*{{{*/
233 bool DoChangelog(CommandLine &CmdL)
234 {
235 CacheFile Cache;
236 if (Cache.ReadOnlyOpen() == false)
237 return false;
238
239 APT::CacheSetHelper helper;
240 APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
241 CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
242 if (verset.empty() == true)
243 return false;
244
245 bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
246 bool const printOnly = _config->FindB("APT::Get::Print-URIs", false);
247
248 aptAcquireWithTextStatus Fetcher;
249 for (APT::VersionList::const_iterator Ver = verset.begin();
250 Ver != verset.end();
251 ++Ver)
252 {
253 if (printOnly)
254 new pkgAcqChangelog(&Fetcher, Ver, "/dev/null");
255 else if (downOnly)
256 new pkgAcqChangelog(&Fetcher, Ver, ".");
257 else
258 new pkgAcqChangelog(&Fetcher, Ver);
259 }
260
261 if (printOnly == false)
262 {
263 bool Failed = false;
264 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
265 return false;
266 }
267
268 if (downOnly == false || printOnly == true)
269 {
270 bool Failed = false;
271 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
272 {
273 if (printOnly)
274 {
275 if ((*I)->ErrorText.empty() == false)
276 {
277 Failed = true;
278 _error->Error("%s", (*I)->ErrorText.c_str());
279 }
280 else
281 std::cout << '\'' << (*I)->DescURI() << "' " << flNotDir((*I)->DestFile) << std::endl;
282 }
283 else
284 DisplayFileInPager((*I)->DestFile);
285 }
286 return Failed == false;
287 }
288
289 return true;
290 }
291 /*}}}*/
292
293 // DoClean - Remove download archives /*{{{*/
294 bool DoClean(CommandLine &)
295 {
296 std::string const archivedir = _config->FindDir("Dir::Cache::archives");
297 std::string const listsdir = _config->FindDir("Dir::state::lists");
298
299 if (_config->FindB("APT::Get::Simulate") == true)
300 {
301 std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
302 std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
303 std::cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< std::endl
304 << "Del " << listsdir << "partial/*" << std::endl
305 << "Del " << pkgcache << " " << srcpkgcache << std::endl;
306 return true;
307 }
308
309 pkgAcquire Fetcher;
310 Fetcher.GetLock(archivedir);
311 Fetcher.Clean(archivedir);
312 Fetcher.Clean(archivedir + "partial/");
313
314 Fetcher.GetLock(listsdir);
315 Fetcher.Clean(listsdir + "partial/");
316
317 pkgCacheFile::RemoveCaches();
318
319 return true;
320 }
321 /*}}}*/
322 // DoAutoClean - Smartly remove downloaded archives /*{{{*/
323 // ---------------------------------------------------------------------
324 /* This is similar to clean but it only purges things that cannot be
325 downloaded, that is old versions of cached packages. */
326 class LogCleaner : public pkgArchiveCleaner
327 {
328 protected:
329 virtual void Erase(const char *File, std::string Pkg, std::string Ver,struct stat &St) APT_OVERRIDE
330 {
331 c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << std::endl;
332
333 if (_config->FindB("APT::Get::Simulate") == false)
334 unlink(File);
335 };
336 };
337 bool DoAutoClean(CommandLine &)
338 {
339 // Lock the archive directory
340 FileFd Lock;
341 if (_config->FindB("Debug::NoLocking",false) == false)
342 {
343 int lock_fd = GetLock(_config->FindDir("Dir::Cache::Archives") + "lock");
344 if (lock_fd < 0)
345 return _error->Error(_("Unable to lock the download directory"));
346 Lock.Fd(lock_fd);
347 }
348
349 CacheFile Cache;
350 if (Cache.Open() == false)
351 return false;
352
353 LogCleaner Cleaner;
354
355 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
356 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
357 }
358 /*}}}*/