]> git.saurik.com Git - apt.git/blob - apt-pkg/clean.cc
use free() instead of delete() when realloc is used
[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 DIR *D = opendir(Dir.c_str());
39 if (D == 0)
40 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
41
42 std::string StartDir = SafeGetCWD();
43 if (chdir(Dir.c_str()) != 0)
44 {
45 closedir(D);
46 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
47 }
48
49 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
50 {
51 // Skip some files..
52 if (strcmp(Dir->d_name,"lock") == 0 ||
53 strcmp(Dir->d_name,"partial") == 0 ||
54 strcmp(Dir->d_name,".") == 0 ||
55 strcmp(Dir->d_name,"..") == 0)
56 continue;
57
58 struct stat St;
59 if (stat(Dir->d_name,&St) != 0)
60 {
61 _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
62 closedir(D);
63 if (chdir(StartDir.c_str()) != 0)
64 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
65 return false;
66 }
67
68 // Grab the package name
69 const char *I = Dir->d_name;
70 for (; *I != 0 && *I != '_';I++);
71 if (*I != '_')
72 continue;
73 std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
74
75 // Grab the version
76 const char *Start = I + 1;
77 for (I = Start; *I != 0 && *I != '_';I++);
78 if (*I != '_')
79 continue;
80 std::string Ver = DeQuoteString(std::string(Start,I-Start));
81
82 // Grab the arch
83 Start = I + 1;
84 for (I = Start; *I != 0 && *I != '.' ;I++);
85 if (*I != '.')
86 continue;
87 std::string const Arch = DeQuoteString(std::string(Start,I-Start));
88
89 // ignore packages of unconfigured architectures
90 if (APT::Configuration::checkArchitecture(Arch) == false)
91 continue;
92
93 // Lookup the package
94 pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
95 if (P.end() != true)
96 {
97 pkgCache::VerIterator V = P.VersionList();
98 for (; V.end() == false; ++V)
99 {
100 // See if we can fetch this version at all
101 bool IsFetchable = false;
102 for (pkgCache::VerFileIterator J = V.FileList();
103 J.end() == false; ++J)
104 {
105 if (CleanInstalled == true &&
106 (J.File()->Flags & pkgCache::Flag::NotSource) != 0)
107 continue;
108 IsFetchable = true;
109 break;
110 }
111
112 // See if this version matches the file
113 if (IsFetchable == true && Ver == V.VerStr())
114 break;
115 }
116
117 // We found a match, keep the file
118 if (V.end() == false)
119 continue;
120 }
121
122 Erase(Dir->d_name,Pkg,Ver,St);
123 };
124
125 closedir(D);
126 if (chdir(StartDir.c_str()) != 0)
127 return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
128 return true;
129 }
130 /*}}}*/