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