use mkdtemp() instead of tempnam, free mkdtemp() data afterwards, return true in...
[ntk/apt.git] / cmdline / apt-get.cc
index 7cf760c..0d67585 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
+#define _LARGEFILE_SOURCE
+#define _LARGEFILE64_SOURCE
+
+#include <apt-pkg/aptconfiguration.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/init.h>
@@ -37,6 +41,7 @@
 #include <apt-pkg/srcrecords.h>
 #include <apt-pkg/version.h>
 #include <apt-pkg/cachefile.h>
+#include <apt-pkg/cacheset.h>
 #include <apt-pkg/sptr.h>
 #include <apt-pkg/md5.h>
 #include <apt-pkg/versionmatch.h>
@@ -45,7 +50,6 @@
 #include <apti18n.h>
 
 #include "acqprogress.h"
-#include "cacheset.h"
 
 #include <set>
 #include <locale.h>
@@ -63,6 +67,9 @@
 #include <regex.h>
 #include <sys/wait.h>
 #include <sstream>
+
+#define statfs statfs64
+#define statvfs statvfs64
                                                                        /*}}}*/
 
 #define RAMFS_MAGIC     0x858458f6
@@ -837,9 +844,11 @@ struct TryToRemove {
    pkgCacheFile* Cache;
    pkgProblemResolver* Fix;
    bool FixBroken;
+   bool PurgePkgs;
    unsigned long AutoMarkChanged;
 
-   TryToRemove(pkgCacheFile &Cache, pkgProblemResolver &PM) : Cache(&Cache), Fix(&PM) {};
+   TryToRemove(pkgCacheFile &Cache, pkgProblemResolver &PM) : Cache(&Cache), Fix(&PM),
+                               PurgePkgs(_config->FindB("APT::Get::Purge", false)) {};
 
    void operator() (pkgCache::VerIterator const &Ver)
    {
@@ -849,10 +858,11 @@ struct TryToRemove {
       Fix->Protect(Pkg);
       Fix->Remove(Pkg);
 
-      if (Pkg->CurrentVer == 0)
+      if ((Pkg->CurrentVer == 0 && PurgePkgs == false) ||
+         (PurgePkgs == true && Pkg->CurrentState == pkgCache::State::NotInstalled))
         ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.FullName(true).c_str());
       else
-        Cache->GetDepCache()->MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
+        Cache->GetDepCache()->MarkDelete(Pkg, PurgePkgs);
    }
 };
                                                                        /*}}}*/
@@ -1074,13 +1084,13 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
       return false;
 
    // Read the source list
-   pkgSourceList List;
-   if (List.ReadMainList() == false)
-      return _error->Error(_("The list of sources could not be read."));
+   if (Cache.BuildSourceList() == false)
+      return false;
+   pkgSourceList *List = Cache.GetSourceList();
    
    // Create the package manager and prepare to download
    SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
-   if (PM->GetArchives(&Fetcher,&List,&Recs) == false || 
+   if (PM->GetArchives(&Fetcher,List,&Recs) == false || 
        _error->PendingError() == true)
       return false;
 
@@ -1096,17 +1106,25 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
    
    // Number of bytes
    if (DebBytes != FetchBytes)
+      //TRANSLATOR: The required space between number and unit is already included
+      // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
       ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
               SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
    else if (DebBytes != 0)
+      //TRANSLATOR: The required space between number and unit is already included
+      // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
       ioprintf(c1out,_("Need to get %sB of archives.\n"),
               SizeToStr(DebBytes).c_str());
 
    // Size delta
    if (Cache->UsrSize() >= 0)
+      //TRANSLATOR: The required space between number and unit is already included
+      // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
       ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"),
               SizeToStr(Cache->UsrSize()).c_str());
    else
+      //TRANSLATOR: The required space between number and unit is already included
+      // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
       ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"),
               SizeToStr(-1*Cache->UsrSize()).c_str());
 
@@ -1296,7 +1314,7 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
       
       // Reload the fetcher object and loop again for media swapping
       Fetcher.Shutdown();
-      if (PM->GetArchives(&Fetcher,&List,&Recs) == false)
+      if (PM->GetArchives(&Fetcher,List,&Recs) == false)
         return false;
       
       _system->Lock();
@@ -1532,11 +1550,13 @@ bool DoUpdate(CommandLine &CmdL)
 {
    if (CmdL.FileSize() != 1)
       return _error->Error(_("The update command takes no arguments"));
-   
+
+   CacheFile Cache;
+
    // Get the source list
-   pkgSourceList List;
-   if (List.ReadMainList() == false)
+   if (Cache.BuildSourceList() == false)
       return false;
+   pkgSourceList *List = Cache.GetSourceList();
 
    // Create the progress
    AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
@@ -1554,7 +1574,7 @@ bool DoUpdate(CommandLine &CmdL)
 
       // Populate it with the source selection and get all Indexes 
       // (GetAll=true)
-      if (List.GetIndexes(&Fetcher,true) == false)
+      if (List->GetIndexes(&Fetcher,true) == false)
         return false;
 
       pkgAcquire::UriIterator I = Fetcher.UriBegin();
@@ -1565,9 +1585,8 @@ bool DoUpdate(CommandLine &CmdL)
    }
 
    // do the work
-   CacheFile Cache;
    if (_config->FindB("APT::Get::Download",true) == true)
-       ListUpdate(Stat, List);
+       ListUpdate(Stat, *List);
 
    // Rebuild the cache.   
    if (Cache.BuildCaches() == false)
@@ -1644,7 +1663,7 @@ bool DoAutomaticRemove(CacheFile &Cache)
    if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0))
    {
       if (smallList == false)
-        ShowList(c1out, P_("The following package is automatically installed and is no longer required:",
+        ShowList(c1out, P_("The following package was automatically installed and is no longer required:",
                  "The following packages were automatically installed and are no longer required:",
                  autoRemoveCount), autoremovelist, autoremoveversions);
       else
@@ -2179,13 +2198,13 @@ bool DoSource(CommandLine &CmdL)
       return _error->Error(_("Must specify at least one package to fetch source for"));
    
    // Read the source list
-   pkgSourceList List;
-   if (List.ReadMainList() == false)
-      return _error->Error(_("The list of sources could not be read."));
+   if (Cache.BuildSourceList() == false)
+      return false;
+   pkgSourceList *List = Cache.GetSourceList();
    
    // Create the text record parsers
    pkgRecords Recs(Cache);
-   pkgSrcRecords SrcRecs(List);
+   pkgSrcRecords SrcRecs(*List);
    if (_error->PendingError() == true)
       return false;
 
@@ -2329,9 +2348,13 @@ bool DoSource(CommandLine &CmdL)
    
    // Number of bytes
    if (DebBytes != FetchBytes)
+      //TRANSLATOR: The required space between number and unit is already included
+      // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
       ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
               SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
    else
+      //TRANSLATOR: The required space between number and unit is already included
+      // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
       ioprintf(c1out,_("Need to get %sB of source archives.\n"),
               SizeToStr(DebBytes).c_str());
    
@@ -2470,13 +2493,13 @@ bool DoBuildDep(CommandLine &CmdL)
       return _error->Error(_("Must specify at least one package to check builddeps for"));
    
    // Read the source list
-   pkgSourceList List;
-   if (List.ReadMainList() == false)
-      return _error->Error(_("The list of sources could not be read."));
+   if (Cache.BuildSourceList() == false)
+      return false;
+   pkgSourceList *List = Cache.GetSourceList();
    
    // Create the text record parsers
    pkgRecords Recs(Cache);
-   pkgSrcRecords SrcRecs(List);
+   pkgSrcRecords SrcRecs(*List);
    if (_error->PendingError() == true)
       return false;
 
@@ -2487,6 +2510,7 @@ bool DoBuildDep(CommandLine &CmdL)
       return false;
 
    unsigned J = 0;
+   bool const StripMultiArch = APT::Configuration::getArchitectures().size() <= 1;
    for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
    {
       string Src;
@@ -2496,7 +2520,7 @@ bool DoBuildDep(CommandLine &CmdL)
             
       // Process the build-dependencies
       vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
-      if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only",true)) == false)
+      if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
        return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
    
       // Also ensure that build-essential packages are present
@@ -2709,6 +2733,114 @@ bool DoBuildDep(CommandLine &CmdL)
    return true;
 }
                                                                        /*}}}*/
+// DownloadChangelog - Download the changelog                          /*{{{*/
+// ---------------------------------------------------------------------
+string DownloadChangelog(CacheFile &CacheFile, pkgAcquire &Fetcher, pkgCache::VerIterator V)
+{
+   string uri;
+   string srcpkg;
+   string prefix;
+   string descr;
+   string src_section;
+   string verstr;
+
+   // data structures we need
+   pkgRecords Recs(CacheFile);
+   pkgCache::PkgIterator Pkg = V.ParentPkg();
+   pkgRecords::Parser &rec=Recs.Lookup(V.FileList());
+
+   // build uri
+   srcpkg = rec.SourcePkg().empty() ? Pkg.Name() : rec.SourcePkg();
+   strprintf(descr, _("Changelog for %s"), srcpkg.c_str());
+   // FIXME: we actually need the source section here
+   src_section= Pkg.Section();
+   if(src_section.find('/')!=src_section.npos)
+      src_section=string(src_section, 0, src_section.find('/'));
+   else
+      src_section="main";
+
+   prefix+=srcpkg[0];
+   if(srcpkg.size()>3 && srcpkg[0]=='l' && srcpkg[1]=='i' && srcpkg[2]=='b')
+      prefix=std::string("lib")+srcpkg[3];
+
+   verstr = V.VerStr();
+   if(verstr.find(':')!=verstr.npos)
+      verstr=string(verstr, verstr.find(':')+1);
+
+   strprintf(uri, "http://packages.debian.org/changelogs/pool/%s/%s/%s/%s_%s/changelog", src_section.c_str(), prefix.c_str(), srcpkg.c_str(), srcpkg.c_str(), verstr.c_str());
+
+   AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
+   Fetcher.Setup(&Stat);
+
+   // temp file
+   char *tmpdir = mkdtemp(strdup("apt-changelog-XXXXXX"));
+   if (tmpdir == NULL) {
+      _error->Errno("mkdtemp", "mkdtemp failed");
+      return "";
+   }
+   string targetfile = string(tmpdir) + "changelog";
+
+   // get it
+   new pkgAcqFile(&Fetcher, uri, "", 0, descr, srcpkg, tmpdir);
+   int res = Fetcher.Run();
+   free(tmpdir);
+
+   if (FileExists(targetfile))
+      return targetfile;
+
+   // error
+   return "";
+}
+                                                                       /*}}}*/
+// DisplayFileInPager - Display File with pager                                /*{{{*/
+void DisplayFileInPager(string filename)
+{
+   pid_t Process = ExecFork();
+   if (Process == 0)
+   {
+      const char *Args[3];
+      Args[0] = "/usr/bin/sensible-pager";
+      Args[1] = filename.c_str();
+      Args[2] = 0;
+      execvp(Args[0],(char **)Args);
+      exit(100);
+   }
+         
+   // Wait for the subprocess
+   ExecWait(Process, "sensible-pager", false);
+}
+                                                                       /*}}}*/
+// DoChangelog - Get changelog from the command line                   /*{{{*/
+// ---------------------------------------------------------------------
+bool DoChangelog(CommandLine &CmdL)
+{
+   CacheFile Cache;
+   if (Cache.ReadOnlyOpen() == false)
+      return false;
+   
+   APT::CacheSetHelper helper(c0out);
+   APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
+               CmdL.FileList + 1, APT::VersionSet::CANDIDATE, helper);
+   pkgAcquire Fetcher;
+
+   if (verset.empty() == true)
+      return false;
+   for (APT::VersionSet::const_iterator Ver = verset.begin(); 
+        Ver != verset.end(); 
+        ++Ver) 
+   {
+      string changelogfile = DownloadChangelog(Cache, Fetcher, Ver);
+      if (changelogfile.size() > 0)
+      {
+         DisplayFileInPager(changelogfile);
+         // cleanup
+         unlink(changelogfile.c_str());
+         rmdir(flNotFile(changelogfile).c_str());
+      }
+   }
+   return true;
+}
+                                                                       /*}}}*/
 // DoMoo - Never Ask, Never Tell                                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -2822,22 +2954,6 @@ bool ShowHelp(CommandLine &CmdL)
    return true;
 }
                                                                        /*}}}*/
-// GetInitialize - Initialize things for apt-get                       /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-void GetInitialize()
-{
-   _config->Set("quiet",0);
-   _config->Set("help",false);
-   _config->Set("APT::Get::Download-Only",false);
-   _config->Set("APT::Get::Simulate",false);
-   _config->Set("APT::Get::Assume-Yes",false);
-   _config->Set("APT::Get::Fix-Broken",false);
-   _config->Set("APT::Get::Force-Yes",false);
-   _config->Set("APT::Get::List-Cleanup",true);
-   _config->Set("APT::Get::AutomaticRemove",false);
-}
-                                                                       /*}}}*/
 // SigWinch - Window size change signal handler                                /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -2915,6 +3031,7 @@ int main(int argc,const char *argv[])                                     /*{{{*/
                                    {"autoclean",&DoAutoClean},
                                    {"check",&DoCheck},
                                   {"source",&DoSource},
+                                   {"changelog",&DoChangelog},
                                   {"moo",&DoMoo},
                                   {"help",&ShowHelp},
                                    {0,0}};