]> git.saurik.com Git - apt.git/commitdiff
* apt-pkg/contrib/fileutl.cc:
authorDavid Kalnischkies <kalnischkies@gmail.com>
Wed, 7 Apr 2010 14:38:18 +0000 (16:38 +0200)
committerDavid Kalnischkies <kalnischkies@gmail.com>
Wed, 7 Apr 2010 14:38:18 +0000 (16:38 +0200)
  - add a parent-guarded "mkdir -p" as CreateDirectory()
* apt-pkg/acquire.{cc,h}:
  - add a delayed constructor with Setup() for success reporting
  - check for and create directories in Setup if needed instead of
    error out unfriendly in the Constructor (Closes: #523920, #525783)
  - optional handle a lock file in Setup()
* cmdline/apt-get.cc:
  - remove the lock file handling and let Acquire take care of it instead

apt-pkg/acquire.cc
apt-pkg/acquire.h
apt-pkg/algorithms.cc
apt-pkg/contrib/fileutl.cc
apt-pkg/contrib/fileutl.h
apt-pkg/pkgcachegen.cc
cmdline/apt-get.cc
debian/changelog

index 74510ae21aaaef46ff13aeb9578c8f4b295c99f7..d83d80fac155d14b68e25e19ecb246f844731abf 100644 (file)
@@ -19,6 +19,7 @@
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/fileutl.h>
 
 #include <apti18n.h>
 
@@ -29,7 +30,6 @@
 #include <dirent.h>
 #include <sys/time.h>
 #include <errno.h>
-#include <sys/stat.h>
                                                                        /*}}}*/
 
 using namespace std;
@@ -37,32 +37,72 @@ using namespace std;
 // Acquire::pkgAcquire - Constructor                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* We grab some runtime state from the configuration space */
-pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log)
+pkgAcquire::pkgAcquire() : Queues(0), Workers(0), Configs(0), Log(NULL), ToFetch(0),
+                          Debug(_config->FindB("Debug::pkgAcquire",false)),
+                          Running(false), LockFD(-1)
 {
-   Queues = 0;
-   Configs = 0;
-   Workers = 0;
-   ToFetch = 0;
-   Running = false;
-   
-   string Mode = _config->Find("Acquire::Queue-Mode","host");
+   string const Mode = _config->Find("Acquire::Queue-Mode","host");
+   if (strcasecmp(Mode.c_str(),"host") == 0)
+      QueueMode = QueueHost;
+   if (strcasecmp(Mode.c_str(),"access") == 0)
+      QueueMode = QueueAccess;
+}
+pkgAcquire::pkgAcquire(pkgAcquireStatus *Progress) : Queues(0), Workers(0),
+                          Configs(0), Log(Progress), ToFetch(0),
+                          Debug(_config->FindB("Debug::pkgAcquire",false)),
+                          Running(false), LockFD(-1)
+{
+   string const Mode = _config->Find("Acquire::Queue-Mode","host");
    if (strcasecmp(Mode.c_str(),"host") == 0)
       QueueMode = QueueHost;
    if (strcasecmp(Mode.c_str(),"access") == 0)
-      QueueMode = QueueAccess;   
+      QueueMode = QueueAccess;
+   Setup(Progress, "");
+}
+                                                                       /*}}}*/
+// Acquire::Setup - Delayed Constructor                                        /*{{{*/
+// ---------------------------------------------------------------------
+/* Do everything needed to be a complete Acquire object and report the
+   success (or failure) back so the user knows that something is wrong… */
+bool pkgAcquire::Setup(pkgAcquireStatus *Progress, string const &Lock)
+{
+   Log = Progress;
 
-   Debug = _config->FindB("Debug::pkgAcquire",false);
-   
-   // This is really a stupid place for this
-   struct stat St;
-   if (stat((_config->FindDir("Dir::State::lists") + "partial/").c_str(),&St) != 0 ||
-       S_ISDIR(St.st_mode) == 0)
-      _error->Error(_("Lists directory %spartial is missing."),
-                   _config->FindDir("Dir::State::lists").c_str());
-   if (stat((_config->FindDir("Dir::Cache::Archives") + "partial/").c_str(),&St) != 0 ||
-       S_ISDIR(St.st_mode) == 0)
-      _error->Error(_("Archive directory %spartial is missing."),
-                   _config->FindDir("Dir::Cache::Archives").c_str());
+   // check for existence and possibly create auxiliary directories
+   if (CheckDirectory(_config->FindDir("Dir::State"), _config->FindDir("Dir::State::lists") + "partial/") == false ||
+       CheckDirectory(_config->FindDir("Dir::Cache"), _config->FindDir("Dir::Cache::Archives") + "partial/") == false)
+      return false;
+
+   if (Lock.empty() == true || _config->FindB("Debug::NoLocking", false) == true)
+      return true;
+
+   // Lock the directory this acquire object will work in
+   LockFD = GetLock(flCombine(Lock, "lock"));
+   if (LockFD == -1)
+      return _error->Error(_("Unable to lock directory %s"), Lock.c_str());
+
+   return true;
+}
+                                                                       /*}}}*/
+// Acquire::CheckDirectory - ensure that the given directory exists    /*{{{*/
+// ---------------------------------------------------------------------
+/* a small wrapper around CreateDirectory to check if it exists and to
+   remove the trailing "/apt/" from the parent directory if needed */
+bool pkgAcquire::CheckDirectory(string const &Parent, string const &Path) const
+{
+   if (DirectoryExists(Path) == true)
+      return true;
+
+   size_t const len = Parent.size();
+   if (len > 5 && Parent.find("/apt/", len - 6, 5) != len - 5)
+   {
+      if (CreateDirectory(Parent.substr(0,len-5), Path) == true)
+        return true;
+   }
+   else if (CreateDirectory(Parent, Path) == true)
+      return true;
+
+   return _error->Errno("Acquire", _("Directory %s can't be created."), Path.c_str());
 }
                                                                        /*}}}*/
 // Acquire::~pkgAcquire        - Destructor                                    /*{{{*/
@@ -71,7 +111,10 @@ pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log)
 pkgAcquire::~pkgAcquire()
 {
    Shutdown();
-   
+
+   if (LockFD != -1)
+      close(LockFD);
+
    while (Configs != 0)
    {
       MethodConfig *Jnk = Configs;
index 6c130c1b32163d9ce5bb535960fec118bb60eef0..9e91a9f67c7fc26ca5b38bfed736d160b0410958 100644 (file)
@@ -66,6 +66,8 @@
 #ifndef PKGLIB_ACQUIRE_H
 #define PKGLIB_ACQUIRE_H
 
+#include <apt-pkg/macros.h>
+
 #include <vector>
 #include <string>
 
@@ -161,7 +163,7 @@ class pkgAcquire
      QueueAccess} QueueMode;
 
    /** \brief If \b true, debugging information will be dumped to std::clog. */
-   bool Debug;
+   bool const Debug;
    /** \brief If \b true, a download is currently in progress. */
    bool Running;
 
@@ -332,15 +334,22 @@ class pkgAcquire
     */
    double PartialPresent();
 
-   /** \brief Construct a new pkgAcquire.
+   /** \brief Delayed constructor
     *
-    *  \param Log The progress indicator associated with this
-    *  download, or \b NULL for none.  This object is not owned by the
+    *  \param Progress indicator associated with this download or
+    *  \b NULL for none.  This object is not owned by the
     *  download process and will not be deleted when the pkgAcquire
     *  object is destroyed.  Naturally, it should live for at least as
     *  long as the pkgAcquire object does.
+    *  \param Lock defines a lock file that should be acquired to ensure
+    *  only one Acquire class is in action at the time or an empty string
+    *  if no lock file should be used.
     */
-   pkgAcquire(pkgAcquireStatus *Log = 0);
+   bool Setup(pkgAcquireStatus *Progress = NULL, string const &Lock = "");
+
+   /** \brief Construct a new pkgAcquire. */
+   pkgAcquire(pkgAcquireStatus *Log) __deprecated;
+   pkgAcquire();
 
    /** \brief Destroy this pkgAcquire object.
     *
@@ -348,6 +357,18 @@ class pkgAcquire
     *  this download.
     */
    virtual ~pkgAcquire();
+
+   private:
+   /** \brief FD of the Lock file we acquire in Setup (if any) */
+   int LockFD;
+
+   /** \brief Ensure the existence of the given Path
+    *
+    *  \param Parent directory of the Path directory - a trailing
+    *  /apt/ will be removed before CreateDirectory call.
+    *  \param Path which should exist after (successful) call
+    */
+   bool CheckDirectory(string const &Parent, string const &Path) const;
 };
 
 /** \brief Represents a single download source from which an item
index f8a9e210c48f24a36f3e14aeebcb816876b1919d..f1e51131a363adefe89f780c90dae5375b19e6dd 100644 (file)
@@ -1398,7 +1398,9 @@ bool ListUpdate(pkgAcquireStatus &Stat,
                int PulseInterval)
 {
    pkgAcquire::RunResult res;
-   pkgAcquire Fetcher(&Stat);
+   pkgAcquire Fetcher;
+   if (Fetcher.Setup(&Stat, _config->FindDir("Dir::State::Lists")) == false)
+      return false;
 
    // Populate it with the source selection
    if (List.GetIndexes(&Fetcher) == false)
index 75adce305818fefd90de8daa75e430ee53063dc6..16f7ce929ca0598b10580cc08f3f3c0768f72c4b 100644 (file)
@@ -18,6 +18,7 @@
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
 #include <apt-pkg/fileutl.h>
+#include <apt-pkg/strutl.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/sptr.h>
 #include <apt-pkg/configuration.h>
@@ -197,6 +198,57 @@ bool FileExists(string File)
    return true;
 }
                                                                        /*}}}*/
+// DirectoryExists - Check if a directory exists and is really one     /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool DirectoryExists(string const &Path)
+{
+   struct stat Buf;
+   if (stat(Path.c_str(),&Buf) != 0)
+      return false;
+   return ((Buf.st_mode & S_IFDIR) != 0);
+}
+                                                                       /*}}}*/
+// CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
+// ---------------------------------------------------------------------
+/* This method will create all directories needed for path in good old
+   mkdir -p style but refuses to do this if Parent is not a prefix of
+   this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
+   so it will create apt/archives if /var/cache exists - on the other
+   hand if the parent is /var/lib the creation will fail as this path
+   is not a parent of the path to be generated. */
+bool CreateDirectory(string const &Parent, string const &Path)
+{
+   if (Parent.empty() == true || Path.empty() == true)
+      return false;
+
+   if (DirectoryExists(Path) == true)
+      return true;
+
+   if (DirectoryExists(Parent) == false)
+      return false;
+
+   // we are not going to create directories "into the blue"
+   if (Path.find(Parent, 0) != 0)
+      return false;
+
+   vector<string> const dirs = VectorizeString(Path.substr(Parent.size()), '/');
+   string progress = Parent;
+   for (vector<string>::const_iterator d = dirs.begin(); d != dirs.end(); ++d)
+   {
+      if (d->empty() == true)
+        continue;
+
+      progress.append("/").append(*d);
+      if (DirectoryExists(progress) == true)
+        continue;
+
+      if (mkdir(progress.c_str(), 0755) != 0)
+        return false;
+   }
+   return true;
+}
+                                                                       /*}}}*/
 // GetListOfFilesInDir - returns a vector of files in the given dir    /*{{{*/
 // ---------------------------------------------------------------------
 /* If an extension is given only files with this extension are included
index 351d53c5e089abeb197357ee6454b593ffc3d59d..003bd9b83f499ab41592f60373d48be9d217b316 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef PKGLIB_FILEUTL_H
 #define PKGLIB_FILEUTL_H
 
+#include <apt-pkg/macros.h>
 
 #include <string>
 #include <vector>
@@ -82,6 +83,8 @@ bool RunScripts(const char *Cnf);
 bool CopyFile(FileFd &From,FileFd &To);
 int GetLock(string File,bool Errors = true);
 bool FileExists(string File);
+bool DirectoryExists(string const &Path) __attrib_const;
+bool CreateDirectory(string const &Parent, string const &Path);
 std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
                                        bool const &SortList, bool const &AllowNoExt=false);
 std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
index 21240b9516fc451f86c6910c78273bd50bba990c..114c9d5ed36b727686fad86752471ce0d52f3c1f 100644 (file)
@@ -1003,7 +1003,20 @@ bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
    // Decide if we can write to the files..
    string const CacheFile = _config->FindFile("Dir::Cache::pkgcache");
    string const SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
-   
+
+   // ensure the cache directory exists
+   if (CacheFile.empty() == false || SrcCacheFile.empty() == false)
+   {
+      string dir = _config->FindDir("Dir::Cache");
+      size_t const len = dir.size();
+      if (len > 5 && dir.find("/apt/", len - 6, 5) == len - 5)
+        dir = dir.substr(0, len - 5);
+      if (CacheFile.empty() == false)
+        CreateDirectory(dir, flNotFile(CacheFile));
+      if (SrcCacheFile.empty() == false)
+        CreateDirectory(dir, flNotFile(SrcCacheFile));
+   }
+
    // Decide if we can write to the cache
    bool Writeable = false;
    if (CacheFile.empty() == false)
index 62f712c391614cb25a94d6658cf3cd7b28cea2fc..416d316da5fbce28211e6a530566c0d09d67dac8 100644 (file)
@@ -811,20 +811,13 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
    pkgRecords Recs(Cache);
    if (_error->PendingError() == true)
       return false;
-   
-   // Lock the archive directory
-   FileFd Lock;
-   if (_config->FindB("Debug::NoLocking",false) == false &&
-       _config->FindB("APT::Get::Print-URIs") == false)
-   {
-      Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
-      if (_error->PendingError() == true)
-        return _error->Error(_("Unable to lock the download directory"));
-   }
-   
+
    // Create the download object
+   pkgAcquire Fetcher;
    AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));   
-   pkgAcquire Fetcher(&Stat);
+   if (Fetcher.Setup(&Stat, _config->FindB("APT::Get::Print-URIs", false)
+       ? "" : _config->FindDir("Dir::Cache::Archives")) == false)
+      return false;
 
    // Read the source list
    pkgSourceList List;
@@ -1442,15 +1435,6 @@ bool DoUpdate(CommandLine &CmdL)
    if (List.ReadMainList() == false)
       return false;
 
-   // Lock the list directory
-   FileFd Lock;
-   if (_config->FindB("Debug::NoLocking",false) == false)
-   {
-      Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
-      if (_error->PendingError() == true)
-        return _error->Error(_("Unable to lock the list directory"));
-   }
-   
    // Create the progress
    AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
       
@@ -1458,7 +1442,9 @@ bool DoUpdate(CommandLine &CmdL)
    if (_config->FindB("APT::Get::Print-URIs") == true)
    {
       // get a fetcher
-      pkgAcquire Fetcher(&Stat);
+      pkgAcquire Fetcher;
+      if (Fetcher.Setup(&Stat) == false)
+        return false;
 
       // Populate it with the source selection and get all Indexes 
       // (GetAll=true)
@@ -2207,7 +2193,9 @@ bool DoSource(CommandLine &CmdL)
 
    // Create the download object
    AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));   
-   pkgAcquire Fetcher(&Stat);
+   pkgAcquire Fetcher;
+   if (Fetcher.Setup(&Stat) == false)
+      return false;
 
    DscFile *Dsc = new DscFile[CmdL.FileSize()];
    
@@ -2464,7 +2452,9 @@ bool DoBuildDep(CommandLine &CmdL)
 
    // Create the download object
    AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));   
-   pkgAcquire Fetcher(&Stat);
+   pkgAcquire Fetcher;
+   if (Fetcher.Setup(&Stat) == false)
+      return false;
 
    unsigned J = 0;
    for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
index 71aeb1504a75fa615c5908e9da9eb8496cb39f55..ffab08eb107a8b471930ac3e9a60880747c48b29 100644 (file)
@@ -4,6 +4,15 @@ apt (0.7.26~exp4) experimental; urgency=low
     - "reinstall" the correct version for a killed pseudo package
   * apt-pkg/packagemanager.cc:
     - don't try to "unpack" pseudo packages twice
+  * apt-pkg/contrib/fileutl.cc:
+    - add a parent-guarded "mkdir -p" as CreateDirectory()
+  * apt-pkg/acquire.{cc,h}:
+    - add a delayed constructor with Setup() for success reporting
+    - check for and create directories in Setup if needed instead of
+      error out unfriendly in the Constructor (Closes: #523920, #525783)
+    - optional handle a lock file in Setup()
+  * cmdline/apt-get.cc:
+    - remove the lock file handling and let Acquire take care of it instead
 
  -- David Kalnischkies <kalnischkies@gmail.com>  Sat, 03 Apr 2010 14:58:39 +0200