]> git.saurik.com Git - apt.git/blame - apt-pkg/clean.cc
* lp:~mvo/apt/config-clear:
[apt.git] / apt-pkg / clean.cc
CommitLineData
1bc849af
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b2e465d6 3// $Id: clean.cc,v 1.4 2001/02/20 07:03:17 jgg Exp $
1bc849af
AL
4/* ######################################################################
5
6 Clean - Clean out downloaded directories
7
8 ##################################################################### */
9 /*}}}*/
10// Includes /*{{{*/
ea542140
DK
11#include<config.h>
12
1bc849af
AL
13#include <apt-pkg/clean.h>
14#include <apt-pkg/strutl.h>
15#include <apt-pkg/error.h>
c98b1307 16#include <apt-pkg/configuration.h>
5dd4c8b8 17#include <apt-pkg/aptconfiguration.h>
472ff00e 18#include <apt-pkg/fileutl.h>
1bc849af
AL
19
20#include <dirent.h>
21#include <sys/stat.h>
22#include <unistd.h>
ea542140
DK
23
24#include <apti18n.h>
1bc849af 25 /*}}}*/
1bc849af
AL
26// ArchiveCleaner::Go - Perform smart cleanup of the archive /*{{{*/
27// ---------------------------------------------------------------------
28/* Scan the directory for files to erase, we check the version information
29 against our database to see if it is interesting */
8f3ba4e8 30bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
1bc849af 31{
c98b1307 32 bool CleanInstalled = _config->FindB("APT::Clean-Installed",true);
b2e465d6
AL
33
34 DIR *D = opendir(Dir.c_str());
1bc849af 35 if (D == 0)
b2e465d6
AL
36 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
37
8f3ba4e8 38 std::string StartDir = SafeGetCWD();
1bc849af
AL
39 if (chdir(Dir.c_str()) != 0)
40 {
41 closedir(D);
b2e465d6 42 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
1bc849af
AL
43 }
44
45 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
46 {
47 // Skip some files..
48 if (strcmp(Dir->d_name,"lock") == 0 ||
49 strcmp(Dir->d_name,"partial") == 0 ||
50 strcmp(Dir->d_name,".") == 0 ||
51 strcmp(Dir->d_name,"..") == 0)
52 continue;
53
54 struct stat St;
55 if (stat(Dir->d_name,&St) != 0)
b2e465d6 56 {
31bda500 57 _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
b2e465d6 58 closedir(D);
31bda500
DK
59 if (chdir(StartDir.c_str()) != 0)
60 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
61 return false;
b2e465d6
AL
62 }
63
1bc849af
AL
64 // Grab the package name
65 const char *I = Dir->d_name;
66 for (; *I != 0 && *I != '_';I++);
67 if (*I != '_')
68 continue;
8f3ba4e8 69 std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
1bc849af
AL
70
71 // Grab the version
72 const char *Start = I + 1;
73 for (I = Start; *I != 0 && *I != '_';I++);
74 if (*I != '_')
75 continue;
8f3ba4e8 76 std::string Ver = DeQuoteString(std::string(Start,I-Start));
1bc849af
AL
77
78 // Grab the arch
79 Start = I + 1;
80 for (I = Start; *I != 0 && *I != '.' ;I++);
81 if (*I != '.')
82 continue;
8f3ba4e8 83 std::string const Arch = DeQuoteString(std::string(Start,I-Start));
469b0278
DK
84
85 // ignore packages of unconfigured architectures
5dd4c8b8 86 if (APT::Configuration::checkArchitecture(Arch) == false)
b2e465d6
AL
87 continue;
88
1bc849af 89 // Lookup the package
469b0278 90 pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
1bc849af
AL
91 if (P.end() != true)
92 {
93 pkgCache::VerIterator V = P.VersionList();
f7f0d6c7 94 for (; V.end() == false; ++V)
1bc849af
AL
95 {
96 // See if we can fetch this version at all
97 bool IsFetchable = false;
98 for (pkgCache::VerFileIterator J = V.FileList();
f7f0d6c7 99 J.end() == false; ++J)
1bc849af 100 {
c98b1307
AL
101 if (CleanInstalled == true &&
102 (J.File()->Flags & pkgCache::Flag::NotSource) != 0)
1bc849af
AL
103 continue;
104 IsFetchable = true;
105 break;
106 }
107
108 // See if this verison matches the file
109 if (IsFetchable == true && Ver == V.VerStr())
110 break;
111 }
112
113 // We found a match, keep the file
114 if (V.end() == false)
115 continue;
116 }
117
118 Erase(Dir->d_name,Pkg,Ver,St);
1bc849af
AL
119 };
120
1bc849af 121 closedir(D);
31bda500
DK
122 if (chdir(StartDir.c_str()) != 0)
123 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
124 return true;
1bc849af
AL
125}
126 /*}}}*/