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