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