]> git.saurik.com Git - apt.git/commitdiff
add flAbsPath() as a wrapper to realpath()
authorMichael Vogt <mvo@ubuntu.com>
Mon, 28 Apr 2014 15:24:35 +0000 (17:24 +0200)
committerMichael Vogt <mvo@ubuntu.com>
Mon, 28 Apr 2014 15:24:35 +0000 (17:24 +0200)
apt-pkg/contrib/fileutl.cc
apt-pkg/contrib/fileutl.h
test/libapt/fileutl_test.cc

index de73a7fd8a8f9e593d3301e6db76aff30bab5e22..abc0a5fb2294e826e48f5c648392a43c8beab7cf 100644 (file)
@@ -659,6 +659,22 @@ string flCombine(string Dir,string File)
    return Dir + '/' + File;
 }
                                                                        /*}}}*/
+// flAbsPath - Return the absolute path of the filename                        /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+string flAbsPath(string File)
+{
+   char *p = realpath(File.c_str(), NULL);
+   if (p == NULL)
+   {
+      _error->Errno("realpath", "flAbsPath failed");
+      return "";
+   }
+   std::string AbsPath(p);
+   free(p);
+   return AbsPath;
+}
+                                                                       /*}}}*/
 // SetCloseExec - Set the close on exec flag                           /*{{{*/
 // ---------------------------------------------------------------------
 /* */
index cc1a98eae02c227b47962dff72421baf0e3f5e6d..ab944071ac7fafa294ebc967bf47b119ff1f86b4 100644 (file)
@@ -198,6 +198,10 @@ std::string flNoLink(std::string File);
 std::string flExtension(std::string File);
 std::string flCombine(std::string Dir,std::string File);
 
+/** \brief Takes a file path and returns the absolute path
+ */
+std::string flAbsPath(std::string File);
+
 // simple c++ glob
 std::vector<std::string> Glob(std::string const &pattern, int flags=0);
 
index 643c02297fbc2e8851775cceb84fb36582beea9f..9c7e1630a3d22d8f8d1decceb58fdccb01acf390 100644 (file)
@@ -224,3 +224,10 @@ TEST(FileUtlTest, GetTempDir)
    if (old_tmpdir.empty() == false)
       setenv("TMPDIR", old_tmpdir.c_str(), 1);
 }
+TEST(FileUtlTest, flAbsPath)
+{
+   int res = chdir("/bin/");
+   EXPECT_EQ(res, 0);
+   std::string p = flAbsPath("ls");
+   EXPECT_EQ(p, "/bin/ls");
+}