From 0f299aa9bfaa49536ceaeb1de46832787736cb64 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Sat, 29 Oct 2011 19:30:33 +0200 Subject: [PATCH] FileLister (linux): Move absolute path determination to own function Cleans up the #ifdef hell in the code. Provide unit test for it. --- cli/filelister.cpp | 47 +++++++++++++++++++++++------------------ cli/filelister.h | 2 ++ test/testfilelister.cpp | 18 ++++++++++++++++ 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/cli/filelister.cpp b/cli/filelister.cpp index 4ae936805..0c063721b 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -255,6 +255,26 @@ bool FileLister::fileExists(const std::string &path) #include #include +// Get absolute path. Returns empty string if path does not exist or other error. +std::string FileLister::getAbsolutePath(const std::string& path) +{ + std::string absolute_path; + +#ifdef PATH_MAX + char buf[PATH_MAX]; + if (realpath(path.c_str(), buf) != NULL) + absolute_path = buf; +#else + char *dynamic_buf; + if ((dynamic_buf = realpath(path.c_str(), NULL)) != NULL) { + absolute_path = dynamic_buf; + free(dynamic_buf); + } +#endif + + return absolute_path; +} + void FileLister::recursiveAddFiles2(std::vector &relative, std::vector &absolute, std::map &filesizes, @@ -274,38 +294,23 @@ void FileLister::recursiveAddFiles2(std::vector &relative, if (filename[filename.length()-1] != '/') { // File -#ifdef PATH_MAX - char fname[PATH_MAX]; - if (realpath(filename.c_str(), fname) == NULL) -#else - char *fname; - if ((fname = realpath(filename.c_str(), NULL)) == NULL) -#endif - { + std::string absolute_path = getAbsolutePath(filename); + if (absolute_path.empty()) continue; - } - // Does absolute path exist? then bail out - if (std::find(absolute.begin(), absolute.end(), std::string(fname)) != absolute.end()) { -#ifndef PATH_MAX - free(fname); -#endif + // Did we already see this file? Then bail out + if (std::find(absolute.begin(), absolute.end(), absolute_path) != absolute.end()) continue; - } if (Path::sameFileName(path,filename) || FileLister::acceptFile(filename)) { relative.push_back(filename); - absolute.push_back(fname); + absolute.push_back(absolute_path); struct stat sb; - if (stat(fname, &sb) == 0) { + if (stat(absolute_path.c_str(), &sb) == 0) { // Limitation: file sizes are assumed to fit in a 'long' filesizes[filename] = static_cast(sb.st_size); } } - -#ifndef PATH_MAX - free(fname); -#endif } else { // Directory recursiveAddFiles2(relative, absolute, filesizes, filename); diff --git a/cli/filelister.h b/cli/filelister.h index 1d0d59b49..9ee319b18 100644 --- a/cli/filelister.h +++ b/cli/filelister.h @@ -63,6 +63,8 @@ public: static bool fileExists(const std::string &path); #ifndef _WIN32 + static std::string getAbsolutePath(const std::string& path); + static void recursiveAddFiles2(std::vector &relative, std::vector &absolute, std::map &filesizes, diff --git a/test/testfilelister.cpp b/test/testfilelister.cpp index 7382dbe7e..95c700dd0 100644 --- a/test/testfilelister.cpp +++ b/test/testfilelister.cpp @@ -21,6 +21,11 @@ #include #include +#ifndef _WIN32 +#include +#include +#endif + class TestFileLister: public TestFixture { public: TestFileLister() @@ -37,6 +42,9 @@ private: } TEST_CASE(isDirectory); +#ifndef _WIN32 + TEST_CASE(absolutePath); +#endif TEST_CASE(recursiveAddFiles); } @@ -45,6 +53,16 @@ private: ASSERT_EQUALS(true, FileLister::isDirectory("lib")); } +#ifndef _WIN32 + void absolutePath() { + char current_dir[PATH_MAX]; + getcwd(current_dir, sizeof(current_dir)); + + std::string absolute_path = FileLister::getAbsolutePath("."); + ASSERT_EQUALS(current_dir, absolute_path); + } +#endif + void recursiveAddFiles() { // Recursively add add files.. std::vector filenames;