FileLister (linux): Move absolute path determination to own function
Cleans up the #ifdef hell in the code. Provide unit test for it.
This commit is contained in:
parent
316aa920eb
commit
0f299aa9bf
|
@ -255,6 +255,26 @@ bool FileLister::fileExists(const std::string &path)
|
|||
#include <limits.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
// 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<std::string> &relative,
|
||||
std::vector<std::string> &absolute,
|
||||
std::map<std::string, long> &filesizes,
|
||||
|
@ -274,38 +294,23 @@ void FileLister::recursiveAddFiles2(std::vector<std::string> &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<long>(sb.st_size);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef PATH_MAX
|
||||
free(fname);
|
||||
#endif
|
||||
} else {
|
||||
// Directory
|
||||
recursiveAddFiles2(relative, absolute, filesizes, filename);
|
||||
|
|
|
@ -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<std::string> &relative,
|
||||
std::vector<std::string> &absolute,
|
||||
std::map<std::string, long> &filesizes,
|
||||
|
|
|
@ -21,6 +21,11 @@
|
|||
#include <fstream>
|
||||
#include <algorithm>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#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<std::string> filenames;
|
||||
|
|
Loading…
Reference in New Issue