]>
git.saurik.com Git - apt.git/blob - apt-pkg/clean.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: clean.cc,v 1.4 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
6 Clean - Clean out downloaded directories
8 ##################################################################### */
11 #include <apt-pkg/clean.h>
12 #include <apt-pkg/strutl.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/configuration.h>
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
)
29 bool CleanInstalled
= _config
->FindB("APT::Clean-Installed",true);
30 string MyArch
= _config
->Find("APT::Architecture");
32 DIR *D
= opendir(Dir
.c_str());
34 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
36 string StartDir
= SafeGetCWD();
37 if (chdir(Dir
.c_str()) != 0)
40 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
43 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
46 if (strcmp(Dir
->d_name
,"lock") == 0 ||
47 strcmp(Dir
->d_name
,"partial") == 0 ||
48 strcmp(Dir
->d_name
,".") == 0 ||
49 strcmp(Dir
->d_name
,"..") == 0)
53 if (stat(Dir
->d_name
,&St
) != 0)
55 chdir(StartDir
.c_str());
57 return _error
->Errno("stat",_("Unable to stat %s."),Dir
->d_name
);
60 // Grab the package name
61 const char *I
= Dir
->d_name
;
62 for (; *I
!= 0 && *I
!= '_';I
++);
65 string Pkg
= DeQuoteString(string(Dir
->d_name
,I
-Dir
->d_name
));
68 const char *Start
= I
+ 1;
69 for (I
= Start
; *I
!= 0 && *I
!= '_';I
++);
72 string Ver
= DeQuoteString(string(Start
,I
-Start
));
76 for (I
= Start
; *I
!= 0 && *I
!= '.' ;I
++);
79 string Arch
= DeQuoteString(string(Start
,I
-Start
));
81 if (Arch
!= "all" && Arch
!= MyArch
)
85 pkgCache::PkgIterator P
= Cache
.FindPkg(Pkg
);
88 pkgCache::VerIterator V
= P
.VersionList();
89 for (; V
.end() == false; V
++)
91 // See if we can fetch this version at all
92 bool IsFetchable
= false;
93 for (pkgCache::VerFileIterator J
= V
.FileList();
94 J
.end() == false; J
++)
96 if (CleanInstalled
== true &&
97 (J
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
103 // See if this verison matches the file
104 if (IsFetchable
== true && Ver
== V
.VerStr())
108 // We found a match, keep the file
109 if (V
.end() == false)
113 Erase(Dir
->d_name
,Pkg
,Ver
,St
);
116 chdir(StartDir
.c_str());