]> git.saurik.com Git - apt.git/blob - apt-pkg/clean.cc
Store tags in the cache (they are very useful :/).
[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 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/cacheiterators.h>
21
22 #include <string>
23 #include <string.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include <apti18n.h>
29 /*}}}*/
30 // ArchiveCleaner::Go - Perform smart cleanup of the archive /*{{{*/
31 // ---------------------------------------------------------------------
32 /* Scan the directory for files to erase, we check the version information
33 against our database to see if it is interesting */
34 bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
35 {
36 bool CleanInstalled = _config->FindB("APT::Clean-Installed",true);
37
38 if(Dir == "/")
39 return _error->Error(_("Clean of %s is not supported"), Dir.c_str());
40
41 // non-existing directories are always clean
42 // we do not check for a directory explicitly to support symlinks
43 if (FileExists(Dir) == false)
44 return true;
45
46 DIR *D = opendir(Dir.c_str());
47 if (D == 0)
48 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
49
50 std::string StartDir = SafeGetCWD();
51 if (chdir(Dir.c_str()) != 0)
52 {
53 closedir(D);
54 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
55 }
56
57 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
58 {
59 // Skip some files..
60 if (strcmp(Dir->d_name,"lock") == 0 ||
61 strcmp(Dir->d_name,"partial") == 0 ||
62 strcmp(Dir->d_name,"lost+found") == 0 ||
63 strcmp(Dir->d_name,".") == 0 ||
64 strcmp(Dir->d_name,"..") == 0)
65 continue;
66
67 struct stat St;
68 if (stat(Dir->d_name,&St) != 0)
69 {
70 _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
71 closedir(D);
72 if (chdir(StartDir.c_str()) != 0)
73 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
74 return false;
75 }
76
77 // Grab the package name
78 const char *I = Dir->d_name;
79 for (; *I != 0 && *I != '_';I++);
80 if (*I != '_')
81 continue;
82 std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
83
84 // Grab the version
85 const char *Start = I + 1;
86 for (I = Start; *I != 0 && *I != '_';I++);
87 if (*I != '_')
88 continue;
89 std::string Ver = DeQuoteString(std::string(Start,I-Start));
90
91 // Grab the arch
92 Start = I + 1;
93 for (I = Start; *I != 0 && *I != '.' ;I++);
94 if (*I != '.')
95 continue;
96 std::string const Arch = DeQuoteString(std::string(Start,I-Start));
97
98 // ignore packages of unconfigured architectures
99 if (APT::Configuration::checkArchitecture(Arch) == false)
100 continue;
101
102 // Lookup the package
103 pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
104 if (P.end() != true)
105 {
106 pkgCache::VerIterator V = P.VersionList();
107 for (; V.end() == false; ++V)
108 {
109 // See if we can fetch this version at all
110 bool IsFetchable = false;
111 for (pkgCache::VerFileIterator J = V.FileList();
112 J.end() == false; ++J)
113 {
114 if (CleanInstalled == true &&
115 J.File().Flagged(pkgCache::Flag::NotSource))
116 continue;
117 IsFetchable = true;
118 break;
119 }
120
121 // See if this version matches the file
122 if (IsFetchable == true && Ver == V.VerStr())
123 break;
124 }
125
126 // We found a match, keep the file
127 if (V.end() == false)
128 continue;
129 }
130
131 Erase(Dir->d_name,Pkg,Ver,St);
132 };
133
134 closedir(D);
135 if (chdir(StartDir.c_str()) != 0)
136 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
137 return true;
138 }
139 /*}}}*/
140
141 pkgArchiveCleaner::pkgArchiveCleaner() : d(NULL) {}
142 APT_CONST pkgArchiveCleaner::~pkgArchiveCleaner() {}