]> git.saurik.com Git - apt.git/blob - apt-pkg/clean.cc
* apt-pkg/contrib/configuration.cc:
[apt.git] / apt-pkg / clean.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: clean.cc,v 1.4 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
5
6 Clean - Clean out downloaded directories
7
8 ##################################################################### */
9 /*}}}*/
10 // Includes /*{{{*/
11 #include<config.h>
12
13 #include <apt-pkg/clean.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/aptconfiguration.h>
18
19 #include <dirent.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 #include <apti18n.h>
24 /*}}}*/
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 */
29 bool pkgArchiveCleaner::Go(string Dir,pkgCache &Cache)
30 {
31 bool CleanInstalled = _config->FindB("APT::Clean-Installed",true);
32
33 DIR *D = opendir(Dir.c_str());
34 if (D == 0)
35 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
36
37 string StartDir = SafeGetCWD();
38 if (chdir(Dir.c_str()) != 0)
39 {
40 closedir(D);
41 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
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)
55 {
56 chdir(StartDir.c_str());
57 closedir(D);
58 return _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
59 }
60
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;
80 string const Arch = DeQuoteString(string(Start,I-Start));
81
82 if (APT::Configuration::checkArchitecture(Arch) == false)
83 continue;
84
85 // Lookup the package
86 pkgCache::PkgIterator P = Cache.FindPkg(Pkg);
87 if (P.end() != true)
88 {
89 pkgCache::VerIterator V = P.VersionList();
90 for (; V.end() == false; ++V)
91 {
92 // See if we can fetch this version at all
93 bool IsFetchable = false;
94 for (pkgCache::VerFileIterator J = V.FileList();
95 J.end() == false; ++J)
96 {
97 if (CleanInstalled == true &&
98 (J.File()->Flags & pkgCache::Flag::NotSource) != 0)
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);
115 };
116
117 chdir(StartDir.c_str());
118 closedir(D);
119 return true;
120 }
121 /*}}}*/