Clean support
authorArch Librarian <arch@canonical.com>
Mon, 20 Sep 2004 16:52:40 +0000 (16:52 +0000)
committerArch Librarian <arch@canonical.com>
Mon, 20 Sep 2004 16:52:40 +0000 (16:52 +0000)
Author: jgg
Date: 1999-02-01 08:11:57 GMT
Clean support

apt-pkg/acquire-item.cc
apt-pkg/clean.cc [new file with mode: 0644]
apt-pkg/clean.h [new file with mode: 0644]
apt-pkg/contrib/strutl.cc
apt-pkg/contrib/strutl.h
apt-pkg/makefile
cmdline/acqprogress.cc
cmdline/apt-get.cc

index beb4260..ebef611 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: acquire-item.cc,v 1.22 1999/02/01 02:22:11 jgg Exp $
+// $Id: acquire-item.cc,v 1.23 1999/02/01 08:11:57 jgg Exp $
 /* ######################################################################
 
    Acquire Item - Item to acquire
@@ -363,7 +363,7 @@ pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
    // Generate the final file name as: package_version_arch.deb
    StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
                    QuoteString(Version.VerStr(),"_:") + '_' +
-                   QuoteString(Version.Arch(),"_:") + ".deb";
+                   QuoteString(Version.Arch(),"_:.") + ".deb";
    
    // Select a source
    if (QueueNext() == false && _error->PendingError() == false)
diff --git a/apt-pkg/clean.cc b/apt-pkg/clean.cc
new file mode 100644 (file)
index 0000000..db47b1c
--- /dev/null
@@ -0,0 +1,111 @@
+// -*- mode: cpp; mode: fold -*-
+// Description                                                         /*{{{*/
+// $Id: clean.cc,v 1.1 1999/02/01 08:11:57 jgg Exp $
+/* ######################################################################
+
+   Clean - Clean out downloaded directories
+   
+   ##################################################################### */
+                                                                       /*}}}*/
+// Includes                                                            /*{{{*/
+#ifdef __GNUG__
+#pragma implementation "apt-pkg/clean.h"
+#endif
+
+#include <apt-pkg/clean.h>
+#include <apt-pkg/strutl.h>
+#include <apt-pkg/error.h>
+
+#include <dirent.h>
+#include <sys/stat.h>
+#include <unistd.h>
+                                                                       /*}}}*/
+
+// ArchiveCleaner::Go - Perform smart cleanup of the archive           /*{{{*/
+// ---------------------------------------------------------------------
+/* Scan the directory for files to erase, we check the version information
+   against our database to see if it is interesting */
+bool pkgArchiveCleaner::Go(string Dir,pkgCache &Cache)
+{
+   DIR *D = opendir(Dir.c_str());   
+   if (D == 0)
+      return _error->Errno("opendir","Unable to read %s",Dir.c_str());
+   
+   string StartDir = SafeGetCWD();
+   if (chdir(Dir.c_str()) != 0)
+   {
+      closedir(D);
+      return _error->Errno("chdir","Unable to change to ",Dir.c_str());
+   }
+   
+   for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
+   {
+      // Skip some files..
+      if (strcmp(Dir->d_name,"lock") == 0 ||
+         strcmp(Dir->d_name,"partial") == 0 ||
+         strcmp(Dir->d_name,".") == 0 ||
+         strcmp(Dir->d_name,"..") == 0)
+        continue;
+
+      struct stat St;
+      if (stat(Dir->d_name,&St) != 0)
+        return _error->Errno("stat","Unable to stat %s.",Dir->d_name);
+
+      // Grab the package name
+      const char *I = Dir->d_name;
+      for (; *I != 0 && *I != '_';I++);
+      if (*I != '_')
+        continue;
+      string Pkg = DeQuoteString(string(Dir->d_name,I-Dir->d_name));
+
+      // Grab the version
+      const char *Start = I + 1;
+      for (I = Start; *I != 0 && *I != '_';I++);
+      if (*I != '_')
+        continue;
+      string Ver = DeQuoteString(string(Start,I-Start));
+  
+      // Grab the arch
+      Start = I + 1;
+      for (I = Start; *I != 0 && *I != '.' ;I++);
+      if (*I != '.')
+        continue;
+      string Arch = DeQuoteString(string(Start,I-Start));
+            
+      // Lookup the package
+      pkgCache::PkgIterator P = Cache.FindPkg(Pkg);
+      if (P.end() != true)
+      {
+        pkgCache::VerIterator V = P.VersionList();
+        for (; V.end() == false; V++)
+        {
+           // See if we can fetch this version at all
+           bool IsFetchable = false;
+           for (pkgCache::VerFileIterator J = V.FileList(); 
+                J.end() == false; J++)
+           {
+              if ((J.File()->Flags & pkgCache::Flag::NotSource) != 0)
+                 continue;
+              IsFetchable = true;
+              break;
+           }
+           
+           // See if this verison matches the file
+           if (IsFetchable == true && Ver == V.VerStr())
+              break;
+        }
+        
+        // We found a match, keep the file
+        if (V.end() == false)
+           continue;
+      }
+            
+      Erase(Dir->d_name,Pkg,Ver,St);
+      unlink(Dir->d_name);
+   };
+   
+   chdir(StartDir.c_str());
+   closedir(D);
+   return true;   
+}
+                                                                       /*}}}*/
diff --git a/apt-pkg/clean.h b/apt-pkg/clean.h
new file mode 100644 (file)
index 0000000..afb5aa4
--- /dev/null
@@ -0,0 +1,30 @@
+// -*- mode: cpp; mode: fold -*-
+// Description                                                         /*{{{*/
+// $Id: clean.h,v 1.1 1999/02/01 08:11:57 jgg Exp $
+/* ######################################################################
+
+   Clean - Clean out downloaded directories
+   
+   ##################################################################### */
+                                                                       /*}}}*/
+#ifndef APTPKG_CLEAN_H
+#define APTPKG_CLEAN_H
+
+#ifdef __GNUG__
+#pragma interface "apt-pkg/clean.h"
+#endif 
+
+#include <apt-pkg/pkgcache.h>
+
+class pkgArchiveCleaner
+{
+   protected:
+   
+   virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St) {};
+
+   public:   
+   
+   bool Go(string Dir,pkgCache &Cache);
+};
+
+#endif
index e0188e3..b5086be 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: strutl.cc,v 1.18 1999/01/27 02:48:52 jgg Exp $
+// $Id: strutl.cc,v 1.19 1999/02/01 08:11:57 jgg Exp $
 /* ######################################################################
 
    String Util - Some usefull string functions.
@@ -116,7 +116,7 @@ bool ParseQuoteWord(const char *&String,string &Res)
       {
         Tmp[0] = Start[1];
         Tmp[1] = Start[2];
-        Tmp[3] = 0;
+        Tmp[2] = 0;
         *I = (char)strtol(Tmp,0,16);
         Start += 3;
         continue;
@@ -177,6 +177,30 @@ bool ParseCWord(const char *String,string &Res)
    return true;
 }
                                                                        /*}}}*/
+// DeQuoteString - Convert a string from quoted from                   /*{{{*/
+// ---------------------------------------------------------------------
+/* This undoes QuoteString */
+string DeQuoteString(string Str)
+{
+   string Res;
+   for (string::iterator I = Str.begin(); I != Str.end(); I++)
+   {
+      if (*I == '%' && I + 2 < Str.end())
+      {
+        char Tmp[3];
+        Tmp[0] = I[1];
+        Tmp[1] = I[2];
+        Tmp[2] = 0;
+        Res += (char)strtol(Tmp,0,16);
+        I += 2;
+        continue;
+      }
+      else
+        Res += *I;
+   }
+   return Res;
+}
+                                                                       /*}}}*/
 // QuoteString - Convert a string into quoted from                     /*{{{*/
 // ---------------------------------------------------------------------
 /* */
index e9a31f1..a79f28e 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: strutl.h,v 1.11 1999/01/27 02:48:53 jgg Exp $
+// $Id: strutl.h,v 1.12 1999/02/01 08:11:57 jgg Exp $
 /* ######################################################################
 
    String Util - These are some usefull string functions
@@ -30,6 +30,7 @@ char *_strtabexpand(char *String,size_t Len);
 bool ParseQuoteWord(const char *&String,string &Res);
 bool ParseCWord(const char *String,string &Res);
 string QuoteString(string Str,const char *Bad);
+string DeQuoteString(string Str);
 string SizeToStr(double Bytes);
 string TimeToStr(unsigned long Sec);
 string SubstVar(string Str,string Subst,string Contents);
index 08b8b29..87a5e46 100644 (file)
@@ -24,9 +24,9 @@ SOURCE = contrib/mmap.cc contrib/error.cc contrib/strutl.cc \
 SOURCE+= pkgcache.cc version.cc fileutl.cc pkgcachegen.cc depcache.cc \
          orderlist.cc tagfile.cc sourcelist.cc packagemanager.cc \
         pkgrecords.cc algorithms.cc acquire.cc acquire-item.cc \
-        acquire-worker.cc acquire-method.cc init.cc templates.cc
+        acquire-worker.cc acquire-method.cc init.cc clean.cc templates.cc
 
-# Source code for the debian specific components        
+# Source code for the debian specific components
 SOURCE+= deb/deblistparser.cc deb/debrecords.cc deb/dpkgpm.cc deb/dpkginit.cc
 
 # Public apt-pkg header files
@@ -35,7 +35,7 @@ HEADERS = algorithms.h depcache.h mmap.h pkgcachegen.h cacheiterators.h \
           packagemanager.h tagfile.h deblistparser.h init.h pkgcache.h \
           version.h progress.h pkgrecords.h debrecords.h cmndline.h \
          acquire.h acquire-worker.h acquire-item.h acquire-method.h md5.h \
-         dpkgpm.h dpkginit.h cdromutl.h strutl.h
+         dpkgpm.h dpkginit.h cdromutl.h strutl.h clean.h
 
 HEADERS := $(addprefix apt-pkg/,$(HEADERS))
 
index 7b23f30..2b4d295 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: acqprogress.cc,v 1.8 1999/01/31 22:25:34 jgg Exp $
+// $Id: acqprogress.cc,v 1.9 1999/02/01 08:11:57 jgg Exp $
 /* ######################################################################
 
    Acquire Progress - Command line progress meter 
@@ -46,7 +46,7 @@ void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
    
    cout << "Hit " << Itm.Description;
    if (Itm.Owner->FileSize != 0)
-      cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']';
+      cout << " [" << SizeToStr(Itm.Owner->FileSize) << "b]";
    cout << endl;
    Update = true;
 };
@@ -70,7 +70,7 @@ void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
    
    cout << "Get:" << hex << Itm.Owner->ID << dec << ' ' << Itm.Description;
    if (Itm.Owner->FileSize != 0)
-      cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']';
+      cout << " [" << SizeToStr(Itm.Owner->FileSize) << "b]";
    cout << endl;
 };
                                                                        /*}}}*/
@@ -120,9 +120,9 @@ void AcqTextStatus::Stop()
       cout << '\r' << BlankLine << '\r';
    
    if (FetchedBytes != 0)
-      cout << "Fetched " << SizeToStr(FetchedBytes) << " in " << 
+      cout << "Fetched " << SizeToStr(FetchedBytes) << "b in " <<
          TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) << 
-         "/s)" << endl;
+         "b/s)" << endl;
 }
                                                                        /*}}}*/
 // AcqTextStatus::Pulse - Regular event pulse                          /*{{{*/
@@ -187,7 +187,7 @@ void AcqTextStatus::Pulse(pkgAcquire *Owner)
       else
       {
         if (Mode == Medium || I->TotalSize == 0)
-           snprintf(S,End-S," %s",SizeToStr(I->CurrentSize).c_str());
+           snprintf(S,End-S," %sb",SizeToStr(I->CurrentSize).c_str());
       }
       S += strlen(S);
       
@@ -198,7 +198,7 @@ void AcqTextStatus::Pulse(pkgAcquire *Owner)
            snprintf(S,End-S," %u%%",
                     long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
         else
-           snprintf(S,End-S,"/%s %u%%",SizeToStr(I->TotalSize).c_str(),
+           snprintf(S,End-S,"/%sb %u%%",SizeToStr(I->TotalSize).c_str(),
                     long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
       }      
       S += strlen(S);
@@ -214,7 +214,7 @@ void AcqTextStatus::Pulse(pkgAcquire *Owner)
    {      
       char Tmp[300];
       unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
-      sprintf(Tmp," %s/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
+      sprintf(Tmp," %sb/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
       unsigned int Len = strlen(Buffer);
       unsigned int LenT = strlen(Tmp);
       if (Len + LenT < ScreenWidth)
index bdc9c06..1225ece 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: apt-get.cc,v 1.37 1999/01/30 06:07:24 jgg Exp $
+// $Id: apt-get.cc,v 1.38 1999/02/01 08:11:57 jgg Exp $
 /* ######################################################################
    
    apt-get - Cover for dpkg
@@ -36,6 +36,7 @@
 #include <apt-pkg/dpkgpm.h>
 #include <apt-pkg/dpkginit.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/clean.h>
 
 #include <config.h>
 
@@ -44,6 +45,7 @@
 #include <fstream.h>
 #include <termios.h>
 #include <sys/ioctl.h>
+#include <sys/stat.h>
 #include <signal.h>
 #include <stdio.h>
                                                                        /*}}}*/
@@ -534,17 +536,17 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true)
    // Number of bytes
    c2out << "Need to get ";
    if (DebBytes != FetchBytes)
-      c2out << SizeToStr(FetchBytes) << '/' << SizeToStr(DebBytes);
+      c2out << SizeToStr(FetchBytes) << "b/" << SizeToStr(DebBytes) << 'b';
    else
-      c2out << SizeToStr(DebBytes);
+      c2out << SizeToStr(DebBytes) << 'b';
       
    c1out << " of archives. After unpacking ";
    
    // Size delta
    if (Cache->UsrSize() >= 0)
-      c2out << SizeToStr(Cache->UsrSize()) << " will be used." << endl;
+      c2out << SizeToStr(Cache->UsrSize()) << "b will be used." << endl;
    else
-      c2out << SizeToStr(-1*Cache->UsrSize()) << " will be freed." << endl;
+      c2out << SizeToStr(-1*Cache->UsrSize()) << "b will be freed." << endl;
 
    if (_error->PendingError() == true)
       return false;
@@ -993,6 +995,29 @@ bool DoClean(CommandLine &CmdL)
    return true;
 }
                                                                        /*}}}*/
+// DoAutoClean - Smartly remove downloaded archives                    /*{{{*/
+// ---------------------------------------------------------------------
+/* This is similar to clean but it only purges things that cannot be 
+   downloaded, that is old versions of cached packages. */
+bool DoAutoClean(CommandLine &CmdL)
+{
+   CacheFile Cache;
+   if (Cache.Open(true) == false)
+      return false;
+   
+   class LogCleaner : public pkgArchiveCleaner
+   {
+      protected:
+      virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St) 
+      {
+        cout << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "b]" << endl;
+      };
+   } Cleaner;
+   
+   return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
+      Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
+}
+                                                                       /*}}}*/
 // DoCheck - Perform the check operation                               /*{{{*/
 // ---------------------------------------------------------------------
 /* Opening automatically checks the system, this command is mostly used
@@ -1031,6 +1056,7 @@ bool ShowHelp(CommandLine &CmdL)
    cout << "   dist-upgrade - Distribution upgrade, see apt-get(8)" << endl;
    cout << "   dselect-upgrade - Follow dselect selections" << endl;
    cout << "   clean - Erase downloaded archive files" << endl;
+   cout << "   autoclean - Erase old downloaded archive files" << endl;
    cout << "   check - Verify that there are no broken dependencies" << endl;
    cout << endl;
    cout << "Options:" << endl;
@@ -1111,6 +1137,7 @@ int main(int argc,const char *argv[])
                                    {"dist-upgrade",&DoDistUpgrade},
                                    {"dselect-upgrade",&DoDSelectUpgrade},
                                    {"clean",&DoClean},
+                                   {"autoclean",&DoAutoClean},
                                    {"check",&DoCheck},
                                   {"help",&ShowHelp},
                                    {0,0}};