apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

clean.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  Clean - Clean out downloaded directories
6 
7  ##################################################################### */
8  /*}}}*/
9 // Includes /*{{{*/
10 #include <config.h>
11 
13 #include <apt-pkg/clean.h>
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/pkgcache.h>
18 #include <apt-pkg/strutl.h>
19 
20 #include <string>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.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  // non-existing directories are always clean
42  // we do not check for a directory explicitly to support symlinks
43  if (FileExists(Dir) == false)
44  return true;
45 
46  int const dirfd = open(Dir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
47  if (dirfd == -1)
48  return _error->Errno("open",_("Unable to read %s"),Dir.c_str());
49  DIR * const D = fdopendir(dirfd);
50  if (D == nullptr)
51  return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
52 
53  for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
54  {
55  // Skip some files..
56  if (strcmp(Dir->d_name, "lock") == 0 ||
57  strcmp(Dir->d_name, "partial") == 0 ||
58  strcmp(Dir->d_name, "auxfiles") == 0 ||
59  strcmp(Dir->d_name, "lost+found") == 0 ||
60  strcmp(Dir->d_name, ".") == 0 ||
61  strcmp(Dir->d_name, "..") == 0)
62  continue;
63 
64  struct stat St;
65  if (fstatat(dirfd, Dir->d_name,&St, 0) != 0)
66  {
67  _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
68  closedir(D);
69  return false;
70  }
71 
72  // Grab the package name
73  const char *I = Dir->d_name;
74  for (; *I != 0 && *I != '_';I++);
75  if (*I != '_')
76  continue;
77  std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
78 
79  // Grab the version
80  const char *Start = I + 1;
81  for (I = Start; *I != 0 && *I != '_';I++);
82  if (*I != '_')
83  continue;
84  std::string Ver = DeQuoteString(std::string(Start,I-Start));
85 
86  // Grab the arch
87  Start = I + 1;
88  for (I = Start; *I != 0 && *I != '.' ;I++);
89  if (*I != '.')
90  continue;
91  std::string const Arch = DeQuoteString(std::string(Start,I-Start));
92 
93  // ignore packages of unconfigured architectures
94  if (APT::Configuration::checkArchitecture(Arch) == false)
95  continue;
96 
97  // Lookup the package
98  pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
99  if (P.end() != true)
100  {
101  pkgCache::VerIterator V = P.VersionList();
102  for (; V.end() == false; ++V)
103  {
104  // See if we can fetch this version at all
105  bool IsFetchable = false;
106  for (pkgCache::VerFileIterator J = V.FileList();
107  J.end() == false; ++J)
108  {
109  if (CleanInstalled == true &&
110  J.File().Flagged(pkgCache::Flag::NotSource))
111  continue;
112  IsFetchable = true;
113  break;
114  }
115 
116  // See if this version matches the file
117  if (IsFetchable == true && Ver == V.VerStr())
118  break;
119  }
120 
121  // We found a match, keep the file
122  if (V.end() == false)
123  continue;
124  }
125 
126  Erase(dirfd, Dir->d_name, Pkg, Ver, St);
127  }
128  closedir(D);
129  return true;
130 }
131  /*}}}*/
132 
static bool std::string const metaIndex const *const pkgAcqMetaClearSig *const pkgAcquire::Item *const I
bool FindB(const char *Name, bool const &Default=false) const
bool Go(std::string Dir, pkgCache &Cache)
Definition: clean.cc:34
virtual ~pkgArchiveCleaner()
Definition: clean.cc:134
virtual void Erase(int const dirfd, char const *const File, std::string const &Pkg, std::string const &Ver, struct stat const &St)=0
PkgIterator FindPkg(APT::StringView Arch=APT::StringView("any", 3)) const
DIR
Configuration * _config
bool FileExists(string File)
Definition: fileutl.cc:326
APT_PUBLIC bool checkArchitecture(std::string const &Arch)
Are we interested in the given Architecture?
pkgCache - Structure definitions for the cache file
string DeQuoteString(const string &Str)
Definition: strutl.cc:404