]> git.saurik.com Git - apt.git/commitdiff
factor GetTempDir out
authorMichael Vogt <mvo@debian.org>
Sun, 22 Dec 2013 21:15:52 +0000 (22:15 +0100)
committerMichael Vogt <mvo@debian.org>
Sun, 22 Dec 2013 21:15:52 +0000 (22:15 +0100)
apt-pkg/contrib/fileutl.cc
apt-pkg/contrib/fileutl.h
apt-pkg/contrib/gpgv.cc
cmdline/apt-extracttemplates.cc
cmdline/apt-get.cc

index 7fbe4d604da276033d0de7c2e0ff08231d78e02e..847d8b47ff70605d61d05a2449696c53169b3504 100644 (file)
@@ -1827,3 +1827,20 @@ std::vector<std::string> Glob(std::string const &pattern, int flags)
    return result;
 }
                                                                        /*}}}*/
+
+std::string GetTempDir()
+{
+   const char *tmpdir = getenv("TMPDIR");
+
+#ifdef P_tmpdir
+   if (!tmpdir)
+      tmpdir = P_tmpdir;
+#endif
+
+   // check that tmpdir is set and exists
+   struct stat st;
+   if (!tmpdir || stat(tmpdir, &st) != 0)
+      tmpdir = "/tmp";
+
+   return string(tmpdir);
+}
index e9a9aab28018b35c792f87e7af0061db4c581d25..e752e96211ddd54343dd05b0a08082dc1f9d85b1 100644 (file)
@@ -165,6 +165,8 @@ bool DirectoryExists(std::string const &Path) __attrib_const;
 bool CreateDirectory(std::string const &Parent, std::string const &Path);
 time_t GetModificationTime(std::string const &Path);
 
+std::string GetTempDir();
+
 /** \brief Ensure the existence of the given Path
  *
  *  \param Parent directory of the Path directory - a trailing
index 8f619fee2ce67d7b54da98742cd4ebeaf203b9a9..f57a72d8640fe099c451357eb39ba52aaaaf7327 100644 (file)
                                                                        /*}}}*/
 static char * GenerateTemporaryFileTemplate(const char *basename)      /*{{{*/
 {
-   const char *tmpdir = getenv("TMPDIR");
-
-#ifdef P_tmpdir
-   if (!tmpdir)
-      tmpdir = P_tmpdir;
-#endif
-
-   // check that tmpdir is set and exists
-   struct stat st;
-   if (!tmpdir || stat(tmpdir, &st) != 0)
-      tmpdir = "/tmp";
-
    std::string out;
-   strprintf(out,  "%s/%s.XXXXXX", tmpdir, basename);
+   std::string tmpdir = GetTempDir();
+   strprintf(out,  "%s/%s.XXXXXX", tmpdir.c_str(), basename);
    return strdup(out.c_str());
 }
                                                                        /*}}}*/
index 8fe15fdf928dc57be8323204aba42611dff856be..8e1937113ff92ac50c4a6a6d3d8784ef96026b94 100644 (file)
@@ -47,8 +47,6 @@
 
 using namespace std;
 
-#define TMPDIR         "/tmp"
-
 pkgCache *DebFile::Cache = 0;
 
 // DebFile::DebFile - Construct the DebFile object                     /*{{{*/
@@ -253,14 +251,11 @@ string WriteFile(const char *package, const char *prefix, const char *data)
 {
        char fn[512];
        static int i;
-       const char *tempdir = NULL;
-
-        tempdir = getenv("TMPDIR");
-        if (tempdir == NULL)
-             tempdir = TMPDIR;
 
+        std::string tempdir = GetTempDir();
        snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d",
-                 _config->Find("APT::ExtractTemplates::TempDir", tempdir).c_str(),
+                 _config->Find("APT::ExtractTemplates::TempDir", 
+                               tempdir.c_str()).c_str(),
                  package, prefix, getpid(), i++);
        FileFd f;
        if (data == NULL)
index 730d3ea18a2f7a8fbcdd43c090755c46ce31823b..8a0772ce26b716b6a05834ff48b3c5b263892110 100644 (file)
@@ -1536,14 +1536,12 @@ bool DoChangelog(CommandLine &CmdL)
    bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
 
    char tmpname[100];
-   char* tmpdir = NULL;
+   const char* tmpdir = NULL;
    if (downOnly == false)
    {
-      const char* const tmpDir = getenv("TMPDIR");
-      if (tmpDir != NULL && *tmpDir != '\0')
-        snprintf(tmpname, sizeof(tmpname), "%s/apt-changelog-XXXXXX", tmpDir);
-      else
-        strncpy(tmpname, "/tmp/apt-changelog-XXXXXX", sizeof(tmpname));
+      std::string systemTemp = GetTempDir();
+      snprintf(tmpname, sizeof(tmpname), "%s/apt-changelog-XXXXXX", 
+               systemTemp.c_str());
       tmpdir = mkdtemp(tmpname);
       if (tmpdir == NULL)
         return _error->Errno("mkdtemp", "mkdtemp failed");