2017-11-15 15:59:39 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
|
|
|
|
2018-02-08 18:27:44 +00:00
|
|
|
#include "Settings.h"
|
2017-11-15 15:59:39 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
#if defined(_WIN32)
|
2017-11-15 15:59:39 +00:00
|
|
|
// because windows...
|
|
|
|
#include <direct.h>
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
#include <Windows.h>
|
2018-01-27 17:04:28 +00:00
|
|
|
#define getcwd _getcwd
|
2017-11-15 15:59:39 +00:00
|
|
|
#define mkdir(x,y) _mkdir(x)
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#define unlink _unlink
|
|
|
|
#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
|
|
|
|
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
|
|
|
|
#else // _WIN32
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif // _WIN32
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
namespace FileSystem
|
|
|
|
{
|
2018-01-29 22:50:10 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
static std::string convertFromWideString(const std::wstring wstring)
|
|
|
|
{
|
|
|
|
int numBytes = WideCharToMultiByte(CP_UTF8, 0, wstring.c_str(), (int)wstring.length(), nullptr, 0, nullptr, nullptr);
|
|
|
|
std::string string;
|
|
|
|
|
|
|
|
string.resize(numBytes);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wstring.c_str(), (int)wstring.length(), (char*)string.c_str(), numBytes, nullptr, nullptr);
|
|
|
|
|
|
|
|
return std::string(string);
|
|
|
|
|
|
|
|
} // convertFromWideString
|
|
|
|
#endif // _WIN32
|
|
|
|
|
2018-01-27 20:04:08 +00:00
|
|
|
stringList getDirContent(const std::string& _path, const bool _recursive)
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
stringList contentList;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// only parse the directory, if it's a directory
|
|
|
|
if(isDirectory(path))
|
|
|
|
{
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
#if defined(_WIN32)
|
2018-01-29 22:50:10 +00:00
|
|
|
WIN32_FIND_DATAW findData;
|
|
|
|
std::string wildcard = path + "/*";
|
|
|
|
HANDLE hFind = FindFirstFileW(std::wstring(wildcard.begin(), wildcard.end()).c_str(), &findData);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
if(hFind != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
// loop over all files in the directory
|
|
|
|
do
|
|
|
|
{
|
2018-01-29 22:50:10 +00:00
|
|
|
std::string name = convertFromWideString(findData.cFileName);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// ignore "." and ".."
|
|
|
|
if((name != ".") && (name != ".."))
|
2018-01-27 20:04:08 +00:00
|
|
|
{
|
|
|
|
std::string fullName(path + "/" + name);
|
|
|
|
contentList.push_back(fullName);
|
|
|
|
|
|
|
|
if(_recursive && isDirectory(fullName))
|
|
|
|
contentList.merge(getDirContent(fullName, true));
|
|
|
|
}
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
}
|
2018-01-29 22:50:10 +00:00
|
|
|
while(FindNextFileW(hFind, &findData));
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
#else // _WIN32
|
|
|
|
DIR* dir = opendir(path.c_str());
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
if(dir != NULL)
|
|
|
|
{
|
|
|
|
struct dirent* entry;
|
|
|
|
|
|
|
|
// loop over all files in the directory
|
|
|
|
while((entry = readdir(dir)) != NULL)
|
|
|
|
{
|
|
|
|
std::string name(entry->d_name);
|
|
|
|
|
|
|
|
// ignore "." and ".."
|
|
|
|
if((name != ".") && (name != ".."))
|
2018-01-27 20:04:08 +00:00
|
|
|
{
|
|
|
|
std::string fullName(path + "/" + name);
|
|
|
|
contentList.push_back(fullName);
|
|
|
|
|
|
|
|
if(_recursive && isDirectory(fullName))
|
|
|
|
contentList.merge(getDirContent(fullName, true));
|
|
|
|
}
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
}
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the content list
|
|
|
|
contentList.sort();
|
|
|
|
|
|
|
|
// return the content list
|
|
|
|
return contentList;
|
|
|
|
|
|
|
|
} // getDirContent
|
|
|
|
|
2018-01-29 22:50:10 +00:00
|
|
|
stringList getPathList(const std::string& _path)
|
|
|
|
{
|
|
|
|
stringList pathList;
|
|
|
|
std::string path = getGenericPath(_path);
|
|
|
|
size_t start = 0;
|
|
|
|
size_t end = 0;
|
|
|
|
|
|
|
|
// split at '/'
|
|
|
|
while((end = path.find("/", start)) != std::string::npos)
|
|
|
|
{
|
|
|
|
if(end != start)
|
|
|
|
pathList.push_back(std::string(path, start, end - start));
|
|
|
|
|
|
|
|
start = end + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add last folder / file to pathList
|
|
|
|
if(start != path.size())
|
|
|
|
pathList.push_back(std::string(path, start, path.size() - start));
|
|
|
|
|
|
|
|
// return the path list
|
|
|
|
return pathList;
|
|
|
|
|
|
|
|
} // getPathList
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
std::string getHomePath()
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
static std::string path;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// only construct the homepath once
|
|
|
|
if(!path.length())
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// this should give us something like "/home/YOUR_USERNAME" on Linux and "C:/Users/YOUR_USERNAME/" on Windows
|
|
|
|
std::string envHome(getenv("HOME"));
|
|
|
|
if(envHome.length())
|
2018-01-09 22:55:09 +00:00
|
|
|
path = getGenericPath(envHome);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// but does not seem to work for Windows XP or Vista, so try something else
|
|
|
|
if(!path.length())
|
|
|
|
{
|
|
|
|
std::string envDir(getenv("HOMEDRIVE"));
|
|
|
|
std::string envPath(getenv("HOMEPATH"));
|
|
|
|
if(envDir.length() && envPath.length())
|
2018-01-09 22:55:09 +00:00
|
|
|
path = getGenericPath(envDir + "/" + envPath);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
2017-11-15 15:59:39 +00:00
|
|
|
}
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// return constructed homepath
|
|
|
|
return path;
|
|
|
|
|
|
|
|
} // getHomePath
|
|
|
|
|
|
|
|
std::string getCWDPath()
|
|
|
|
{
|
|
|
|
char temp[512];
|
|
|
|
|
|
|
|
// return current working directory path
|
2018-01-09 22:55:09 +00:00
|
|
|
return (getcwd(temp, 512) ? getGenericPath(temp) : "");
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
} // getCWDPath
|
|
|
|
|
2018-02-08 18:27:44 +00:00
|
|
|
std::string getExePath()
|
|
|
|
{
|
|
|
|
static std::string path;
|
|
|
|
|
|
|
|
// only construct the exepath once
|
|
|
|
if(!path.length())
|
|
|
|
{
|
|
|
|
path = getCanonicalPath(Settings::getInstance()->getString("ExePath"));
|
|
|
|
|
|
|
|
if(isRegularFile(path))
|
|
|
|
{
|
|
|
|
path = getParent(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return constructed exepath
|
|
|
|
return path;
|
|
|
|
|
|
|
|
} // getExePath
|
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string getGenericPath(const std::string& _path)
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
{
|
|
|
|
std::string path = _path;
|
|
|
|
size_t offset = std::string::npos;
|
|
|
|
|
|
|
|
// remove "\\\\?\\"
|
|
|
|
if((path.find("\\\\?\\")) == 0)
|
|
|
|
path.erase(0, 4);
|
|
|
|
|
|
|
|
// convert '\\' to '/'
|
|
|
|
while((offset = path.find('\\')) != std::string::npos)
|
|
|
|
path.replace(offset, 1 ,"/");
|
|
|
|
|
|
|
|
// remove double '/'
|
|
|
|
while((offset = path.find("//")) != std::string::npos)
|
|
|
|
path.erase(offset, 1);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// return generic path
|
|
|
|
return path;
|
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
} // getGenericPath
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string getEscapedPath(const std::string& _path)
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
#if defined(_WIN32)
|
2017-11-15 15:59:39 +00:00
|
|
|
// windows escapes stuff by just putting everything in quotes
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
return '"' + path + '"';
|
|
|
|
#else // _WIN32
|
2017-11-15 15:59:39 +00:00
|
|
|
// insert a backslash before most characters that would mess up a bash path
|
|
|
|
const char* invalidChars = "\\ '\"!$^&*(){}[]?;<>";
|
2017-11-22 21:29:52 +00:00
|
|
|
const char* invalidChar = invalidChars;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
while(*invalidChar)
|
|
|
|
{
|
2018-02-02 15:45:21 +00:00
|
|
|
size_t start = 0;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
2018-01-26 18:53:19 +00:00
|
|
|
while((offset = path.find(*invalidChar, start)) != std::string::npos)
|
|
|
|
{
|
|
|
|
start = offset + 1;
|
|
|
|
|
|
|
|
if((offset == 0) || (path[offset - 1] != '\\'))
|
|
|
|
{
|
|
|
|
path.insert(offset, 1, '\\');
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
}
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
++invalidChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return escaped path
|
|
|
|
return path;
|
|
|
|
#endif // _WIN32
|
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
} // getEscapedPath
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string getCanonicalPath(const std::string& _path)
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
{
|
2018-01-26 18:53:19 +00:00
|
|
|
// temporary hack for builtin resources
|
|
|
|
if((_path[0] == ':') && (_path[1] == '/'))
|
|
|
|
return _path;
|
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = exists(_path) ? getAbsolutePath(_path) : getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
// cleanup path
|
|
|
|
bool scan = true;
|
|
|
|
while(scan)
|
|
|
|
{
|
2018-01-29 22:50:10 +00:00
|
|
|
stringList pathList = getPathList(path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
path.clear();
|
|
|
|
scan = false;
|
|
|
|
|
|
|
|
for(stringList::const_iterator it = pathList.cbegin(); it != pathList.cend(); ++it)
|
|
|
|
{
|
|
|
|
// ignore empty
|
|
|
|
if((*it).empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// remove "/./"
|
|
|
|
if((*it) == ".")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// resolve "/../"
|
|
|
|
if((*it) == "..")
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
path = getParent(path);
|
|
|
|
continue;
|
2017-11-15 15:59:39 +00:00
|
|
|
}
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// append folder to path
|
|
|
|
path += (path.size() == 0) ? (*it) : ("/" + (*it));
|
|
|
|
#else // _WIN32
|
|
|
|
// append folder to path
|
|
|
|
path += ("/" + (*it));
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
// resolve symlink
|
|
|
|
if(isSymlink(path))
|
|
|
|
{
|
|
|
|
std::string resolved = resolveSymlink(path);
|
|
|
|
|
|
|
|
if(resolved.empty())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if(isAbsolute(resolved))
|
|
|
|
path = resolved;
|
|
|
|
else
|
|
|
|
path = getParent(path) + "/" + resolved;
|
|
|
|
|
2018-01-27 17:04:28 +00:00
|
|
|
for(++it; it != pathList.cend(); ++it)
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
path += (path.size() == 0) ? (*it) : ("/" + (*it));
|
|
|
|
|
|
|
|
scan = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-15 15:59:39 +00:00
|
|
|
}
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// return canonical path
|
|
|
|
return path;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
} // getCanonicalPath
|
2017-11-15 15:59:39 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string getAbsolutePath(const std::string& _path, const std::string& _base)
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
|
|
|
std::string base = isAbsolute(_base) ? getGenericPath(_base) : getAbsolutePath(_base);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// return absolute path
|
2018-01-09 22:55:09 +00:00
|
|
|
return isAbsolute(path) ? path : getGenericPath(base + "/" + path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
} // getAbsolutePath
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
std::string getParent(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
size_t offset = std::string::npos;
|
|
|
|
|
|
|
|
// find last '/' and erase it
|
|
|
|
if((offset = path.find_last_of('/')) != std::string::npos)
|
|
|
|
return path.erase(offset);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
// no parent found
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
return path;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
} // getParent
|
|
|
|
|
2017-11-22 21:29:52 +00:00
|
|
|
std::string getFileName(const std::string& _path)
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
size_t offset = std::string::npos;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
// find last '/' and return the filename
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
if((offset = path.find_last_of('/')) != std::string::npos)
|
|
|
|
return ((path[offset + 1] == 0) ? "." : std::string(path, offset + 1));
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
// no '/' found, entire path is a filename
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
return path;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
} // getFileName
|
|
|
|
|
2017-11-22 21:29:52 +00:00
|
|
|
std::string getStem(const std::string& _path)
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
2017-11-22 21:29:52 +00:00
|
|
|
std::string fileName = getFileName(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
size_t offset = std::string::npos;
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
// empty fileName
|
|
|
|
if(fileName == ".")
|
|
|
|
return fileName;
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
// find last '.' and erase the extension
|
|
|
|
if((offset = fileName.find_last_of('.')) != std::string::npos)
|
|
|
|
return fileName.erase(offset);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
// no '.' found, filename has no extension
|
|
|
|
return fileName;
|
|
|
|
|
|
|
|
} // getStem
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
std::string getExtension(const std::string& _path)
|
|
|
|
{
|
|
|
|
std::string fileName = getFileName(_path);
|
|
|
|
size_t offset = std::string::npos;
|
|
|
|
|
|
|
|
// empty fileName
|
|
|
|
if(fileName == ".")
|
|
|
|
return fileName;
|
|
|
|
|
|
|
|
// find last '.' and return the extension
|
|
|
|
if((offset = fileName.find_last_of('.')) != std::string::npos)
|
|
|
|
return std::string(fileName, offset);
|
|
|
|
|
|
|
|
// no '.' found, filename has no extension
|
|
|
|
return ".";
|
|
|
|
|
|
|
|
} // getExtension
|
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string resolveRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome)
|
|
|
|
{
|
2018-01-27 17:04:28 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string relativeTo = isDirectory(_relativeTo) ? getGenericPath(_relativeTo) : getParent(_relativeTo);
|
|
|
|
|
|
|
|
// nothing to resolve
|
2018-01-27 17:04:28 +00:00
|
|
|
if(!path.length())
|
2018-01-09 22:55:09 +00:00
|
|
|
return path;
|
|
|
|
|
|
|
|
// replace '.' with relativeTo
|
2018-01-29 22:50:10 +00:00
|
|
|
if((path[0] == '.') && (path[1] == '/'))
|
|
|
|
return (relativeTo + &(path[1]));
|
2018-01-09 22:55:09 +00:00
|
|
|
|
|
|
|
// replace '~' with homePath
|
2018-01-29 22:50:10 +00:00
|
|
|
if(_allowHome && (path[0] == '~') && (path[1] == '/'))
|
|
|
|
return (getHomePath() + &(path[1]));
|
2018-01-09 22:55:09 +00:00
|
|
|
|
|
|
|
// nothing to resolve
|
|
|
|
return path;
|
|
|
|
|
|
|
|
} // resolveRelativePath
|
|
|
|
|
|
|
|
std::string createRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome)
|
|
|
|
{
|
2018-01-27 17:04:28 +00:00
|
|
|
bool contains = false;
|
|
|
|
std::string path = removeCommonPath(_path, _relativeTo, contains);
|
2018-01-09 22:55:09 +00:00
|
|
|
|
2018-01-27 17:04:28 +00:00
|
|
|
if(contains)
|
2018-01-09 22:55:09 +00:00
|
|
|
{
|
|
|
|
// success
|
2018-01-29 22:50:10 +00:00
|
|
|
return ("./" + path);
|
2018-01-09 22:55:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 17:04:28 +00:00
|
|
|
if(_allowHome)
|
2018-01-09 22:55:09 +00:00
|
|
|
{
|
2018-01-27 17:04:28 +00:00
|
|
|
path = removeCommonPath(_path, getHomePath(), contains);
|
2018-01-09 22:55:09 +00:00
|
|
|
|
2018-01-27 17:04:28 +00:00
|
|
|
if(contains)
|
2018-01-09 22:55:09 +00:00
|
|
|
{
|
|
|
|
// success
|
2018-01-29 22:50:10 +00:00
|
|
|
return ("~/" + path);
|
2018-01-09 22:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing to resolve
|
|
|
|
return path;
|
|
|
|
|
|
|
|
} // createRelativePath
|
|
|
|
|
|
|
|
std::string removeCommonPath(const std::string& _path, const std::string& _common, bool& _contains)
|
|
|
|
{
|
2018-01-27 17:04:28 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string common = isDirectory(_common) ? getGenericPath(_common) : getParent(_common);
|
|
|
|
|
|
|
|
// check if path contains common
|
2018-01-27 17:04:28 +00:00
|
|
|
if(path.find_first_of(common) == 0)
|
2018-01-09 22:55:09 +00:00
|
|
|
{
|
|
|
|
_contains = true;
|
|
|
|
return path.substr(common.length() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// it didn't
|
|
|
|
_contains = false;
|
|
|
|
return path;
|
|
|
|
|
|
|
|
} // removeCommonPath
|
|
|
|
|
|
|
|
std::string resolveSymlink(const std::string& _path)
|
|
|
|
{
|
|
|
|
std::string path = getGenericPath(_path);
|
|
|
|
std::string resolved;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
HANDLE hFile = CreateFile(path.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
|
|
|
|
2018-01-27 17:04:28 +00:00
|
|
|
if(hFile != INVALID_HANDLE_VALUE)
|
2018-01-09 22:55:09 +00:00
|
|
|
{
|
|
|
|
resolved.resize(GetFinalPathNameByHandle(hFile, nullptr, 0, FILE_NAME_NORMALIZED) + 1);
|
2018-01-27 17:04:28 +00:00
|
|
|
if(GetFinalPathNameByHandle(hFile, (LPSTR)resolved.data(), (DWORD)resolved.size(), FILE_NAME_NORMALIZED) > 0)
|
2018-01-09 22:55:09 +00:00
|
|
|
{
|
|
|
|
resolved.resize(resolved.size() - 1);
|
|
|
|
resolved = getGenericPath(resolved);
|
|
|
|
}
|
|
|
|
CloseHandle(hFile);
|
|
|
|
}
|
|
|
|
#else // _WIN32
|
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
// check if lstat succeeded
|
2018-01-27 17:04:28 +00:00
|
|
|
if(lstat(path.c_str(), &info) == 0)
|
2018-01-09 22:55:09 +00:00
|
|
|
{
|
|
|
|
resolved.resize(info.st_size);
|
2018-01-27 17:04:28 +00:00
|
|
|
if(readlink(path.c_str(), (char*)resolved.data(), resolved.size()) > 0)
|
2018-01-09 22:55:09 +00:00
|
|
|
resolved = getGenericPath(resolved);
|
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
// return resolved path
|
|
|
|
return resolved;
|
|
|
|
|
|
|
|
} // resolveSymlink
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
bool removeFile(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
// don't remove if it doesn't exists
|
|
|
|
if(!exists(path))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// try to remove file
|
|
|
|
return (unlink(path.c_str()) == 0);
|
|
|
|
|
|
|
|
} // removeFile
|
|
|
|
|
|
|
|
bool createDirectory(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
// don't create if it already exists
|
|
|
|
if(exists(path))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// try to create directory
|
|
|
|
if(mkdir(path.c_str(), 0755) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// failed to create directory, try to create the parent
|
|
|
|
std::string parent = getParent(path);
|
|
|
|
|
|
|
|
// only try to create parent if it's not identical to path
|
|
|
|
if(parent != path)
|
|
|
|
createDirectory(parent);
|
|
|
|
|
|
|
|
// try to create directory again now that the parent should exist
|
|
|
|
return (mkdir(path.c_str(), 0755) == 0);
|
|
|
|
|
|
|
|
} // createDirectory
|
|
|
|
|
2017-11-22 21:29:52 +00:00
|
|
|
bool exists(const std::string& _path)
|
2017-11-15 15:59:39 +00:00
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
2017-11-15 15:59:39 +00:00
|
|
|
struct stat info;
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
// check if stat succeeded
|
|
|
|
return (stat(path.c_str(), &info) == 0);
|
2017-11-15 15:59:39 +00:00
|
|
|
|
|
|
|
} // exists
|
|
|
|
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
bool isAbsolute(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return ((path.size() > 1) && (path[1] == ':'));
|
|
|
|
#else // _WIN32
|
|
|
|
return ((path.size() > 0) && (path[0] == '/'));
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
} // isAbsolute
|
|
|
|
|
|
|
|
bool isRegularFile(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
// check if stat succeeded
|
|
|
|
if(stat(path.c_str(), &info) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check for S_IFREG attribute
|
|
|
|
return (S_ISREG(info.st_mode));
|
|
|
|
|
|
|
|
} // isRegularFile
|
|
|
|
|
|
|
|
bool isDirectory(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
// check if stat succeeded
|
|
|
|
if(stat(path.c_str(), &info) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check for S_IFDIR attribute
|
|
|
|
return (S_ISDIR(info.st_mode));
|
|
|
|
|
|
|
|
} // isDirectory
|
|
|
|
|
|
|
|
bool isSymlink(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// check for symlink attribute
|
|
|
|
const DWORD Attributes = GetFileAttributes(path.c_str());
|
|
|
|
if((Attributes != INVALID_FILE_ATTRIBUTES) && (Attributes & FILE_ATTRIBUTE_REPARSE_POINT))
|
|
|
|
return true;
|
|
|
|
#else // _WIN32
|
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
// check if lstat succeeded
|
|
|
|
if(lstat(path.c_str(), &info) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check for S_IFLNK attribute
|
|
|
|
return (S_ISLNK(info.st_mode));
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
// not a symlink
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} // isSymlink
|
|
|
|
|
|
|
|
bool isHidden(const std::string& _path)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path = getGenericPath(_path);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// check for hidden attribute
|
|
|
|
const DWORD Attributes = GetFileAttributes(path.c_str());
|
|
|
|
if((Attributes != INVALID_FILE_ATTRIBUTES) && (Attributes & FILE_ATTRIBUTE_HIDDEN))
|
|
|
|
return true;
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
// filenames starting with . are hidden in linux, we do this check for windows as well
|
|
|
|
if(getFileName(path)[0] == '.')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// not hidden
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} // isHidden
|
|
|
|
|
|
|
|
bool isEquivalent(const std::string& _path1, const std::string& _path2)
|
|
|
|
{
|
2018-01-09 22:55:09 +00:00
|
|
|
std::string path1 = getGenericPath(_path1);
|
|
|
|
std::string path2 = getGenericPath(_path2);
|
Add getDirContent, getHomePath, getCWDPath, canonicalPath, absolutePath, resolvePath, resolveSymlink, getExtension, removeFile, isAbsolute, isRegularFile, isDirectory, isSymlink, isHidden and isEquivalent
Rename makeGeneric to genericPath and escapePath to escapedPath
Add toUpper
2017-12-04 23:32:04 +00:00
|
|
|
struct stat info1;
|
|
|
|
struct stat info2;
|
|
|
|
|
|
|
|
// check if stat succeeded
|
|
|
|
if((stat(path1.c_str(), &info1) != 0) || (stat(path2.c_str(), &info2) != 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check if attributes are identical
|
|
|
|
return ((info1.st_dev == info2.st_dev) && (info1.st_ino == info2.st_ino) && (info1.st_size == info2.st_size) && (info1.st_mtime == info2.st_mtime));
|
|
|
|
|
|
|
|
} // isEquivalent
|
|
|
|
|
2017-11-15 15:59:39 +00:00
|
|
|
} // FileSystem::
|
|
|
|
|
|
|
|
} // Utils::
|