* debian/rules:
[ntk/apt.git] / cmdline / apt-get.cc
index b47c2ec..104baba 100644 (file)
@@ -53,6 +53,7 @@
 #include <termios.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/statfs.h>
 #include <sys/statvfs.h>
 #include <signal.h>
 #include <unistd.h>
@@ -63,6 +64,8 @@
 #include <sstream>
                                                                        /*}}}*/
 
+#define RAMFS_MAGIC     0x858458f6
+
 using namespace std;
 
 ostream c0out(0);
@@ -835,16 +838,16 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
    if (DebBytes != FetchBytes)
       ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
               SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
-   else
+   else if (DebBytes != 0)
       ioprintf(c1out,_("Need to get %sB of archives.\n"),
               SizeToStr(DebBytes).c_str());
 
    // Size delta
    if (Cache->UsrSize() >= 0)
-      ioprintf(c1out,_("After unpacking %sB of additional disk space will be used.\n"),
+      ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"),
               SizeToStr(Cache->UsrSize()).c_str());
    else
-      ioprintf(c1out,_("After unpacking %sB disk space will be freed.\n"),
+      ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"),
               SizeToStr(-1*Cache->UsrSize()).c_str());
 
    if (_error->PendingError() == true)
@@ -861,8 +864,13 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
         return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
                              OutputDir.c_str());
       if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
-        return _error->Error(_("You don't have enough free space in %s."),
-                             OutputDir.c_str());
+      {
+         struct statfs Stat;
+         if (statfs(OutputDir.c_str(),&Stat) != 0 ||
+                        unsigned(Stat.f_type) != RAMFS_MAGIC)
+            return _error->Error(_("You don't have enough free space in %s."),
+                OutputDir.c_str());
+      }
    }
    
    // Fail safe check
@@ -918,7 +926,7 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
       pkgAcquire::UriIterator I = Fetcher.UriBegin();
       for (; I != Fetcher.UriEnd(); I++)
         cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << 
-              I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
+              I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
       return true;
    }
 
@@ -1343,14 +1351,15 @@ bool DoUpdate(CommandLine &CmdL)
         return _error->Error(_("Unable to lock the list directory"));
    }
    
-   // Create the download object
+   // Create the progress
    AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
-   pkgAcquire Fetcher(&Stat);
-
-   
+      
    // Just print out the uris an exit if the --print-uris flag was used
    if (_config->FindB("APT::Get::Print-URIs") == true)
    {
+      // get a fetcher
+      pkgAcquire Fetcher(&Stat);
+
       // Populate it with the source selection and get all Indexes 
       // (GetAll=true)
       if (List.GetIndexes(&Fetcher,true) == false)
@@ -1359,58 +1368,19 @@ bool DoUpdate(CommandLine &CmdL)
       pkgAcquire::UriIterator I = Fetcher.UriBegin();
       for (; I != Fetcher.UriEnd(); I++)
         cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << 
-              I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
+              I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
       return true;
    }
 
-   // Populate it with the source selection
-   if (List.GetIndexes(&Fetcher) == false)
-        return false;
-   
-   // Run it
-   if (Fetcher.Run() == pkgAcquire::Failed)
-      return false;
-
-   bool Failed = false;
-   bool TransientNetworkFailure = false;
-   for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
-   {
-      if ((*I)->Status == pkgAcquire::Item::StatDone)
-        continue;
-
-      (*I)->Finished();
-
-      fprintf(stderr,_("Failed to fetch %s  %s\n"),(*I)->DescURI().c_str(),
-             (*I)->ErrorText.c_str());
-
-      if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError) 
-      {
-        TransientNetworkFailure = true;
-        continue;
-      }
-
-      Failed = true;
-   }
-   
-   // Clean out any old list files
-   if (!TransientNetworkFailure &&
-       _config->FindB("APT::Get::List-Cleanup",true) == true)
-   {
-      if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
-         Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
-        return false;
-   }
-   
-   // Prepare the cache.   
+   // do the work
    CacheFile Cache;
+   if (_config->FindB("APT::Get::Download",true) == true)
+       ListUpdate(Stat, List);
+
+   // Rebuild the cache.   
    if (Cache.BuildCaches() == false)
       return false;
    
-   if (TransientNetworkFailure == true)
-      _error->Warning(_("Some index files failed to download, they have been ignored, or old ones used instead."));
-   else if (Failed == true)
-      return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
-
    return true;
 }
                                                                        /*}}}*/
@@ -1421,8 +1391,8 @@ bool DoAutomaticRemove(CacheFile &Cache)
 {
    bool Debug = _config->FindI("Debug::pkgAutoRemove",false);
    bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false);
+   bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove");
    pkgDepCache::ActionGroup group(*Cache);
-   
 
    if(Debug)
       std::cout << "DoAutomaticRemove()" << std::endl;
@@ -1444,9 +1414,13 @@ bool DoAutomaticRemove(CacheFile &Cache)
         if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install())
            if(Debug)
               std::cout << "We could delete %s" <<  Pkg.Name() << std::endl;
-          
-        autoremovelist += string(Pkg.Name()) + " ";
-        autoremoveversions += string(Cache[Pkg].CandVersion) + " ";
+         
+        // only show stuff in the list that is not yet marked for removal
+        if(Cache[Pkg].Delete() == false) 
+        {
+           autoremovelist += string(Pkg.Name()) + " ";
+           autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
+        }
         if (doAutoRemove)
         {
            if(Pkg.CurrentVer() != 0 && 
@@ -1457,8 +1431,9 @@ bool DoAutomaticRemove(CacheFile &Cache)
         }
       }
    }
-   ShowList(c1out, _("The following packages were automatically installed and are no longer required:"), autoremovelist, autoremoveversions);
-   if (!doAutoRemove && autoremovelist.size() > 0)
+   if (!hideAutoRemove) 
+      ShowList(c1out, _("The following packages were automatically installed and are no longer required:"), autoremovelist, autoremoveversions);
+   if (!doAutoRemove && !hideAutoRemove && autoremovelist.size() > 0)
       c1out << _("Use 'apt-get autoremove' to remove them.") << std::endl;
 
    // Now see if we destroyed anything
@@ -1502,7 +1477,8 @@ bool DoUpgrade(CommandLine &CmdL)
 bool TryInstallTask(pkgDepCache &Cache, pkgProblemResolver &Fix, 
                    bool BrokenFix,
                    unsigned int& ExpectedInst, 
-                   const char *taskname)
+                   const char *taskname,
+                   bool Remove)
 {
    const char *start, *end;
    pkgCache::PkgIterator Pkg;
@@ -1514,26 +1490,35 @@ bool TryInstallTask(pkgDepCache &Cache, pkgProblemResolver &Fix,
 
    // build regexp for the task
    char S[300];
-   snprintf(S, sizeof(S), "^Task:.*[^a-z]%s[^a-z].*\n", taskname);
-   regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
+   snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", taskname);
+   if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0)
+      return _error->Error("Failed to compile task regexp");
    
    bool found = false;
    bool res = true;
-   for (Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
+
+   // two runs, first ignore dependencies, second install any missing
+   for(int IgnoreBroken=1; IgnoreBroken >= 0; IgnoreBroken--)
    {
-      pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
-      if(ver.end())
-        continue;
-      pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
-      parser.GetRec(start,end);
-      strncpy(buf, start, end-start);
-      buf[end-start] = 0x0;
-      if (regexec(&Pattern,buf,0,0,0) != 0)
-        continue;
-      res &= TryToInstall(Pkg,Cache,Fix,false,BrokenFix,ExpectedInst);
-      found = true;
+      for (Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
+      {
+        pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
+        if(ver.end())
+           continue;
+        pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
+        parser.GetRec(start,end);
+        strncpy(buf, start, end-start);
+        buf[end-start] = 0x0;
+        if (regexec(&Pattern,buf,0,0,0) != 0)
+           continue;
+        res &= TryToInstall(Pkg,Cache,Fix,Remove,IgnoreBroken,ExpectedInst);
+        found = true;
+      }
    }
    
+   // now let the problem resolver deal with any issues
+   Fix.Resolve(true);
+
    if(!found)
       _error->Error(_("Couldn't find task %s"),taskname);
 
@@ -1556,6 +1541,7 @@ bool DoInstall(CommandLine &CmdL)
    if (Cache->BrokenCount() != 0)
       BrokenFix = true;
    
+   unsigned int AutoMarkChanged = 0;
    unsigned int ExpectedInst = 0;
    unsigned int Packages = 0;
    pkgProblemResolver Fix(Cache);
@@ -1563,12 +1549,16 @@ bool DoInstall(CommandLine &CmdL)
    bool DefRemove = false;
    if (strcasecmp(CmdL.FileList[0],"remove") == 0)
       DefRemove = true;
+   else if (strcasecmp(CmdL.FileList[0], "purge") == 0)
+   {
+      _config->Set("APT::Get::Purge", true);
+      DefRemove = true;
+   }
    else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0)
    {
       _config->Set("APT::Get::AutomaticRemove", "true");
       DefRemove = true;
    }
-
    // new scope for the ActionGroup
    {
       pkgDepCache::ActionGroup group(Cache);
@@ -1585,6 +1575,18 @@ bool DoInstall(CommandLine &CmdL)
         bool Remove = DefRemove;
         char *VerTag = 0;
         bool VerIsRel = false;
+
+         // this is a task!
+         if (Length >= 1 && S[Length - 1] == '^')
+         {
+            S[--Length] = 0;
+            // tasks must always be confirmed
+            ExpectedInst += 1000;
+            // see if we can install it
+            TryInstallTask(Cache, Fix, BrokenFix, ExpectedInst, S, Remove);
+            continue;
+         }
+
         while (Cache->FindPkg(S).end() == true)
         {
            // Handle an optional end tag indicating what to do
@@ -1678,6 +1680,20 @@ bool DoInstall(CommandLine &CmdL)
                  return false;
            if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
               return false;
+
+           // see if we need to fix the auto-mark flag 
+           // e.g. apt-get install foo 
+           // where foo is marked automatic
+           if(!Remove && 
+              Cache[Pkg].Install() == false && 
+              (Cache[Pkg].Flags & pkgCache::Flag::Auto) &&
+              _config->FindB("APT::Get::ReInstall",false) == false)
+           {
+              ioprintf(c1out,_("%s set to manually installed.\n"),
+                       Pkg.Name());
+              Cache->MarkAuto(Pkg,false);
+              AutoMarkChanged++;
+           }
         }      
       }
 
@@ -1720,10 +1736,8 @@ bool DoInstall(CommandLine &CmdL)
         return _error->Error(_("Broken packages"));
       }   
    }
-   if (_config->FindB("APT::Get::AutomaticRemove")) {
-      if (!DoAutomaticRemove(Cache)) 
-        return false;
-   }
+   if (!DoAutomaticRemove(Cache)) 
+      return false;
 
    /* Print out a list of packages that are going to be installed extra
       to what the user asked */
@@ -1842,6 +1856,14 @@ bool DoInstall(CommandLine &CmdL)
 
    }
 
+   // if nothing changed in the cache, but only the automark information
+   // we write the StateFile here, otherwise it will be written in 
+   // cache.commit()
+   if (AutoMarkChanged > 0 &&
+       Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
+       Cache->BadCount() == 0)
+      Cache->writeStateFile(NULL);
+
    // See if we need to prompt
    if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
       return InstallPackages(Cache,false,false);
@@ -2100,6 +2122,11 @@ bool DoSource(CommandLine &CmdL)
             I->Type != "tar")
            continue;
 
+        // Dsc only mode only fetches .dsc files
+        if (_config->FindB("APT::Get::Dsc-Only",false) == true &&
+            I->Type != "dsc")
+           continue;
+
         // don't download the same uri twice (should this be moved to
         // the fetcher interface itself?)
         if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
@@ -2139,8 +2166,13 @@ bool DoSource(CommandLine &CmdL)
       return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
                           OutputDir.c_str());
    if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
-      return _error->Error(_("You don't have enough free space in %s"),
-                          OutputDir.c_str());
+     {
+       struct statfs Stat;
+       if (statfs(OutputDir.c_str(),&Stat) != 0 || 
+           unsigned(Stat.f_type) != RAMFS_MAGIC) 
+          return _error->Error(_("You don't have enough free space in %s"),
+              OutputDir.c_str());
+      }
    
    // Number of bytes
    if (DebBytes != FetchBytes)
@@ -2163,7 +2195,7 @@ bool DoSource(CommandLine &CmdL)
       pkgAcquire::UriIterator I = Fetcher.UriBegin();
       for (; I != Fetcher.UriEnd(); I++)
         cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << 
-              I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
+              I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
       return true;
    }
    
@@ -2423,6 +2455,7 @@ bool DoBuildDep(CommandLine &CmdL)
                            break;
                  }
                  if (CV.end() == true)
+                {
                   if (hasAlternatives)
                   {
                      continue;
@@ -2435,6 +2468,7 @@ bool DoBuildDep(CommandLine &CmdL)
                                            Last->BuildDepType((*D).Type),Src.c_str(),
                                            (*D).Package.c_str());
                   }
+                }
             }
             else
             {
@@ -2534,8 +2568,8 @@ bool DoMoo(CommandLine &CmdL)
 /* */
 bool ShowHelp(CommandLine &CmdL)
 {
-   ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
-           COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
+   ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
+           COMMON_ARCH,__DATE__,__TIME__);
            
    if (_config->FindB("version") == true)
    {
@@ -2593,6 +2627,8 @@ bool ShowHelp(CommandLine &CmdL)
       "   upgrade - Perform an upgrade\n"
       "   install - Install new packages (pkg is libc6 not libc6.deb)\n"
       "   remove - Remove packages\n"
+      "   autoremove - Remove automatically all unused packages\n"
+      "   purge - Remove and purge packages\n"
       "   source - Download source archives\n"
       "   build-dep - Configure build-dependencies for source packages\n"
       "   dist-upgrade - Distribution upgrade, see apt-get(8)\n"
@@ -2608,7 +2644,7 @@ bool ShowHelp(CommandLine &CmdL)
       "  -d  Download only - do NOT install or unpack archives\n"
       "  -s  No-act. Perform ordering simulation\n"
       "  -y  Assume Yes to all queries and do not prompt\n"
-      "  -f  Attempt to continue if the integrity check fails\n"
+      "  -f  Attempt to correct a system with broken dependencies in place\n"
       "  -m  Attempt to continue if archives are unlocatable\n"
       "  -u  Show a list of upgraded packages as well\n"
       "  -b  Build the source package after fetching it\n"
@@ -2682,7 +2718,8 @@ int main(int argc,const char *argv[])
       {0,"force-yes","APT::Get::force-yes",0},
       {0,"print-uris","APT::Get::Print-URIs",0},
       {0,"diff-only","APT::Get::Diff-Only",0},
-      {0,"tar-only","APT::Get::tar-Only",0},
+      {0,"tar-only","APT::Get::Tar-Only",0},
+      {0,"dsc-only","APT::Get::Dsc-Only",0},
       {0,"purge","APT::Get::Purge",0},
       {0,"list-cleanup","APT::Get::List-Cleanup",0},
       {0,"reinstall","APT::Get::ReInstall",0},
@@ -2701,7 +2738,9 @@ int main(int argc,const char *argv[])
                                    {"upgrade",&DoUpgrade},
                                    {"install",&DoInstall},
                                    {"remove",&DoInstall},
+                                   {"purge",&DoInstall},
                                   {"autoremove",&DoInstall},
+                                  {"purge",&DoInstall},
                                    {"dist-upgrade",&DoDistUpgrade},
                                    {"dselect-upgrade",&DoDSelectUpgrade},
                                   {"build-dep",&DoBuildDep},