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