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