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