]> git.saurik.com Git - apt.git/blame - apt-pkg/clean.cc
abstract version hash comparison a bit
[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);
b2e465d6
AL
37
38 DIR *D = opendir(Dir.c_str());
1bc849af 39 if (D == 0)
b2e465d6
AL
40 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
41
8f3ba4e8 42 std::string StartDir = SafeGetCWD();
1bc849af
AL
43 if (chdir(Dir.c_str()) != 0)
44 {
45 closedir(D);
b2e465d6 46 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
1bc849af
AL
47 }
48
49 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
50 {
51 // Skip some files..
52 if (strcmp(Dir->d_name,"lock") == 0 ||
53 strcmp(Dir->d_name,"partial") == 0 ||
54 strcmp(Dir->d_name,".") == 0 ||
55 strcmp(Dir->d_name,"..") == 0)
56 continue;
57
58 struct stat St;
59 if (stat(Dir->d_name,&St) != 0)
b2e465d6 60 {
31bda500 61 _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
b2e465d6 62 closedir(D);
31bda500
DK
63 if (chdir(StartDir.c_str()) != 0)
64 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
65 return false;
b2e465d6
AL
66 }
67
1bc849af
AL
68 // Grab the package name
69 const char *I = Dir->d_name;
70 for (; *I != 0 && *I != '_';I++);
71 if (*I != '_')
72 continue;
8f3ba4e8 73 std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
1bc849af
AL
74
75 // Grab the version
76 const char *Start = I + 1;
77 for (I = Start; *I != 0 && *I != '_';I++);
78 if (*I != '_')
79 continue;
8f3ba4e8 80 std::string Ver = DeQuoteString(std::string(Start,I-Start));
1bc849af
AL
81
82 // Grab the arch
83 Start = I + 1;
84 for (I = Start; *I != 0 && *I != '.' ;I++);
85 if (*I != '.')
86 continue;
8f3ba4e8 87 std::string const Arch = DeQuoteString(std::string(Start,I-Start));
469b0278
DK
88
89 // ignore packages of unconfigured architectures
5dd4c8b8 90 if (APT::Configuration::checkArchitecture(Arch) == false)
b2e465d6
AL
91 continue;
92
1bc849af 93 // Lookup the package
469b0278 94 pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
1bc849af
AL
95 if (P.end() != true)
96 {
97 pkgCache::VerIterator V = P.VersionList();
f7f0d6c7 98 for (; V.end() == false; ++V)
1bc849af
AL
99 {
100 // See if we can fetch this version at all
101 bool IsFetchable = false;
102 for (pkgCache::VerFileIterator J = V.FileList();
f7f0d6c7 103 J.end() == false; ++J)
1bc849af 104 {
c98b1307
AL
105 if (CleanInstalled == true &&
106 (J.File()->Flags & pkgCache::Flag::NotSource) != 0)
1bc849af
AL
107 continue;
108 IsFetchable = true;
109 break;
110 }
111
1e3f4083 112 // See if this version matches the file
1bc849af
AL
113 if (IsFetchable == true && Ver == V.VerStr())
114 break;
115 }
116
117 // We found a match, keep the file
118 if (V.end() == false)
119 continue;
120 }
121
122 Erase(Dir->d_name,Pkg,Ver,St);
1bc849af
AL
123 };
124
1bc849af 125 closedir(D);
31bda500
DK
126 if (chdir(StartDir.c_str()) != 0)
127 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
128 return true;
1bc849af
AL
129}
130 /*}}}*/