]> git.saurik.com Git - apt.git/blame - apt-pkg/clean.cc
review apt(8) manpage
[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>
453b82a3
DK
19#include <apt-pkg/pkgcache.h>
20#include <apt-pkg/cacheiterators.h>
1bc849af 21
453b82a3
DK
22#include <string>
23#include <string.h>
1bc849af
AL
24#include <dirent.h>
25#include <sys/stat.h>
26#include <unistd.h>
ea542140
DK
27
28#include <apti18n.h>
1bc849af 29 /*}}}*/
1bc849af
AL
30// ArchiveCleaner::Go - Perform smart cleanup of the archive /*{{{*/
31// ---------------------------------------------------------------------
32/* Scan the directory for files to erase, we check the version information
33 against our database to see if it is interesting */
8f3ba4e8 34bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
1bc849af 35{
c98b1307 36 bool CleanInstalled = _config->FindB("APT::Clean-Installed",true);
10ecfe4f
MV
37
38 if(Dir == "/")
39 return _error->Error(_("Clean of %s is not supported"), Dir.c_str());
40
b2e465d6 41 DIR *D = opendir(Dir.c_str());
1bc849af 42 if (D == 0)
b2e465d6
AL
43 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
44
8f3ba4e8 45 std::string StartDir = SafeGetCWD();
1bc849af
AL
46 if (chdir(Dir.c_str()) != 0)
47 {
48 closedir(D);
b2e465d6 49 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
1bc849af
AL
50 }
51
52 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
53 {
54 // Skip some files..
55 if (strcmp(Dir->d_name,"lock") == 0 ||
56 strcmp(Dir->d_name,"partial") == 0 ||
6aef1942 57 strcmp(Dir->d_name,"lost+found") == 0 ||
1bc849af
AL
58 strcmp(Dir->d_name,".") == 0 ||
59 strcmp(Dir->d_name,"..") == 0)
60 continue;
61
62 struct stat St;
63 if (stat(Dir->d_name,&St) != 0)
b2e465d6 64 {
31bda500 65 _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
b2e465d6 66 closedir(D);
31bda500
DK
67 if (chdir(StartDir.c_str()) != 0)
68 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
69 return false;
b2e465d6
AL
70 }
71
1bc849af
AL
72 // Grab the package name
73 const char *I = Dir->d_name;
74 for (; *I != 0 && *I != '_';I++);
75 if (*I != '_')
76 continue;
8f3ba4e8 77 std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
1bc849af
AL
78
79 // Grab the version
80 const char *Start = I + 1;
81 for (I = Start; *I != 0 && *I != '_';I++);
82 if (*I != '_')
83 continue;
8f3ba4e8 84 std::string Ver = DeQuoteString(std::string(Start,I-Start));
1bc849af
AL
85
86 // Grab the arch
87 Start = I + 1;
88 for (I = Start; *I != 0 && *I != '.' ;I++);
89 if (*I != '.')
90 continue;
8f3ba4e8 91 std::string const Arch = DeQuoteString(std::string(Start,I-Start));
469b0278
DK
92
93 // ignore packages of unconfigured architectures
5dd4c8b8 94 if (APT::Configuration::checkArchitecture(Arch) == false)
b2e465d6
AL
95 continue;
96
1bc849af 97 // Lookup the package
469b0278 98 pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
1bc849af
AL
99 if (P.end() != true)
100 {
101 pkgCache::VerIterator V = P.VersionList();
f7f0d6c7 102 for (; V.end() == false; ++V)
1bc849af
AL
103 {
104 // See if we can fetch this version at all
105 bool IsFetchable = false;
106 for (pkgCache::VerFileIterator J = V.FileList();
f7f0d6c7 107 J.end() == false; ++J)
1bc849af 108 {
c98b1307 109 if (CleanInstalled == true &&
b07aeb1a 110 J.File().Flagged(pkgCache::Flag::NotSource))
1bc849af
AL
111 continue;
112 IsFetchable = true;
113 break;
114 }
115
1e3f4083 116 // See if this version matches the file
1bc849af
AL
117 if (IsFetchable == true && Ver == V.VerStr())
118 break;
119 }
120
121 // We found a match, keep the file
122 if (V.end() == false)
123 continue;
124 }
125
126 Erase(Dir->d_name,Pkg,Ver,St);
1bc849af
AL
127 };
128
1bc849af 129 closedir(D);
31bda500
DK
130 if (chdir(StartDir.c_str()) != 0)
131 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
132 return true;
1bc849af
AL
133}
134 /*}}}*/
862bafea 135
6c55f07a 136pkgArchiveCleaner::pkgArchiveCleaner() : d(NULL) {}
9d653a6d 137APT_CONST pkgArchiveCleaner::~pkgArchiveCleaner() {}