]> git.saurik.com Git - apt.git/blob - apt-pkg/clean.cc
fix bashism (local outside function) and releasefile creation in testcases
[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 #include <apt-pkg/aptconfiguration.h>
16
17 #include <apti18n.h>
18
19 #include <dirent.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
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
31 DIR *D = opendir(Dir.c_str());
32 if (D == 0)
33 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
34
35 string StartDir = SafeGetCWD();
36 if (chdir(Dir.c_str()) != 0)
37 {
38 closedir(D);
39 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
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)
53 {
54 chdir(StartDir.c_str());
55 closedir(D);
56 return _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
57 }
58
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 const Arch = DeQuoteString(string(Start,I-Start));
79
80 if (APT::Configuration::checkArchitecture(Arch) == false)
81 continue;
82
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 {
95 if (CleanInstalled == true &&
96 (J.File()->Flags & pkgCache::Flag::NotSource) != 0)
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);
113 };
114
115 chdir(StartDir.c_str());
116 closedir(D);
117 return true;
118 }
119 /*}}}*/