2014-06-14 19:25:17 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
/* Based on luafilesystem
|
|
|
|
Copyright © 2003-2014 Kepler Project.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
obtaining a copy of this software and associated documentation
|
|
|
|
files (the "Software"), to deal in the Software without
|
|
|
|
restriction, including without limitation the rights to use, copy,
|
|
|
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-07-16 09:42:39 -06:00
|
|
|
#include <algorithm>
|
2014-06-14 19:25:17 -06:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "modules/Filesystem.h"
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
bool Filesystem::chdir (std::string path)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
2017-07-21 12:30:05 -06:00
|
|
|
return ::chdir(path.c_str()) == 0;
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
std::string Filesystem::getcwd ()
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
|
|
|
char *path;
|
2015-01-28 15:15:09 -07:00
|
|
|
char buf[FILENAME_MAX];
|
2014-06-19 20:58:17 -06:00
|
|
|
std::string result = "";
|
2014-06-19 20:34:40 -06:00
|
|
|
#ifdef _WIN32
|
2015-01-28 15:15:09 -07:00
|
|
|
if ((path = ::_getcwd(buf, FILENAME_MAX)) != NULL)
|
2014-06-19 20:34:40 -06:00
|
|
|
#else
|
2015-01-28 15:15:09 -07:00
|
|
|
if ((path = ::getcwd(buf, FILENAME_MAX)) != NULL)
|
2014-06-19 20:34:40 -06:00
|
|
|
#endif
|
2015-01-28 15:15:09 -07:00
|
|
|
result = buf;
|
2014-06-19 20:58:17 -06:00
|
|
|
return result;
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
bool Filesystem::mkdir (std::string path)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
|
|
|
int fail;
|
|
|
|
#ifdef _WIN32
|
|
|
|
fail = ::_mkdir(path.c_str());
|
|
|
|
#else
|
|
|
|
fail = ::mkdir(path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
|
|
|
|
S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
|
|
|
#endif
|
2017-07-21 12:30:05 -06:00
|
|
|
return fail == 0;
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
|
|
|
|
2020-07-15 22:35:21 -06:00
|
|
|
static bool mkdir_recursive_impl (std::string path)
|
|
|
|
{
|
|
|
|
size_t last_slash = path.find_last_of("/");
|
|
|
|
if (last_slash != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string parent_path = path.substr(0, last_slash);
|
|
|
|
bool parent_exists = mkdir_recursive_impl(parent_path);
|
|
|
|
if (!parent_exists)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 23:02:12 -06:00
|
|
|
return (Filesystem::mkdir(path) || errno == EEXIST) && Filesystem::isdir(path);
|
2020-07-15 22:35:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Filesystem::mkdir_recursive (std::string path)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
// normalize to forward slashes
|
|
|
|
std::replace(path.begin(), path.end(), '\\', '/');
|
|
|
|
#endif
|
|
|
|
|
2020-07-16 09:33:23 -06:00
|
|
|
if (path.size() > FILENAME_MAX)
|
2020-07-15 22:35:21 -06:00
|
|
|
{
|
|
|
|
// path too long
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mkdir_recursive_impl(path);
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
bool Filesystem::rmdir (std::string path)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
|
|
|
int fail;
|
|
|
|
#ifdef _WIN32
|
|
|
|
fail = ::_rmdir(path.c_str());
|
|
|
|
#else
|
|
|
|
fail = ::rmdir(path.c_str());
|
|
|
|
#endif
|
2017-07-21 12:30:05 -06:00
|
|
|
return fail == 0;
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2014-06-19 19:24:16 -06:00
|
|
|
_filetype mode2type (unsigned short mode) {
|
2014-06-14 19:25:17 -06:00
|
|
|
#else
|
|
|
|
_filetype mode2type (mode_t mode) {
|
|
|
|
#endif
|
|
|
|
if (S_ISREG(mode))
|
|
|
|
return FILETYPE_FILE;
|
|
|
|
else if (S_ISDIR(mode))
|
|
|
|
return FILETYPE_DIRECTORY;
|
|
|
|
else if (S_ISLNK(mode))
|
|
|
|
return FILETYPE_LINK;
|
|
|
|
else if (S_ISSOCK(mode))
|
|
|
|
return FILETYPE_SOCKET;
|
|
|
|
else if (S_ISCHR(mode))
|
|
|
|
return FILETYPE_CHAR_DEVICE;
|
|
|
|
else if (S_ISBLK(mode))
|
|
|
|
return FILETYPE_BLOCK_DEVICE;
|
|
|
|
else
|
|
|
|
return FILETYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
bool Filesystem::stat (std::string path, STAT_STRUCT &info)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
2017-07-21 12:30:05 -06:00
|
|
|
return (STAT_FUNC(path.c_str(), &info)) == 0;
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
bool Filesystem::exists (std::string path)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
|
|
|
STAT_STRUCT info;
|
2017-07-21 12:30:05 -06:00
|
|
|
return Filesystem::stat(path, info);
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
_filetype Filesystem::filetype (std::string path)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
|
|
|
STAT_STRUCT info;
|
2015-09-19 15:34:58 -06:00
|
|
|
if (!Filesystem::stat(path, info))
|
|
|
|
return FILETYPE_NONE;
|
2014-06-14 19:25:17 -06:00
|
|
|
return mode2type(info.st_mode);
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
bool Filesystem::isfile (std::string path)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
2015-09-19 15:34:58 -06:00
|
|
|
return Filesystem::exists(path) && Filesystem::filetype(path) == FILETYPE_FILE;
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:18:06 -07:00
|
|
|
bool Filesystem::isdir (std::string path)
|
2014-06-14 19:25:17 -06:00
|
|
|
{
|
2015-09-19 15:34:58 -06:00
|
|
|
return Filesystem::exists(path) && Filesystem::filetype(path) == FILETYPE_DIRECTORY;
|
2015-01-28 16:18:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DEFINE_STAT_TIME_WRAPPER(attr) \
|
|
|
|
int64_t Filesystem::attr (std::string path) \
|
|
|
|
{ \
|
|
|
|
STAT_STRUCT info; \
|
2015-01-28 17:15:58 -07:00
|
|
|
if (!Filesystem::stat(path, info)) \
|
2015-01-28 16:18:06 -07:00
|
|
|
return -1; \
|
|
|
|
return (int64_t)info.st_##attr; \
|
2014-06-14 19:25:17 -06:00
|
|
|
}
|
2015-01-28 16:18:06 -07:00
|
|
|
|
2015-01-28 17:58:32 -07:00
|
|
|
DEFINE_STAT_TIME_WRAPPER(atime)
|
|
|
|
DEFINE_STAT_TIME_WRAPPER(ctime)
|
|
|
|
DEFINE_STAT_TIME_WRAPPER(mtime)
|
2015-01-28 16:18:06 -07:00
|
|
|
|
|
|
|
#undef DEFINE_STAT_TIME_WRAPPER
|
2015-01-28 17:15:58 -07:00
|
|
|
|
|
|
|
int Filesystem::listdir (std::string dir, std::vector<std::string> &files)
|
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *dirp;
|
|
|
|
if((dp = opendir(dir.c_str())) == NULL)
|
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
while ((dirp = readdir(dp)) != NULL) {
|
|
|
|
files.push_back(std::string(dirp->d_name));
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-18 23:22:37 -06:00
|
|
|
// prefix is the top-level dir where we started recursing
|
|
|
|
// path is the relative path under the prefix; must be empty or end in a '/'
|
|
|
|
// files is the output list of files and directories (bool == true for dir)
|
|
|
|
// depth is the remaining dir depth to recurse into. function returns -1 if
|
|
|
|
// we haven't finished recursing when we run out of depth.
|
|
|
|
// include_prefix controls whether the directory where we started recursing is
|
|
|
|
// included in the filenames returned in files.
|
|
|
|
static int listdir_recursive_impl (std::string prefix, std::string path,
|
|
|
|
std::map<std::string, bool> &files, int depth, bool include_prefix)
|
2015-01-30 15:29:17 -07:00
|
|
|
{
|
|
|
|
if (depth < 0)
|
|
|
|
return -1;
|
2020-07-18 23:22:37 -06:00
|
|
|
std::string prefixed_path = prefix + "/" + path;
|
|
|
|
std::vector<std::string> curdir_files;
|
|
|
|
int err = Filesystem::listdir(prefixed_path, curdir_files);
|
2015-01-30 15:29:17 -07:00
|
|
|
if (err)
|
|
|
|
return err;
|
2020-07-18 23:22:37 -06:00
|
|
|
for (auto file = curdir_files.begin(); file != curdir_files.end(); ++file)
|
2015-01-30 15:29:17 -07:00
|
|
|
{
|
|
|
|
if (*file == "." || *file == "..")
|
|
|
|
continue;
|
2020-07-18 23:22:37 -06:00
|
|
|
std::string prefixed_file = prefixed_path + *file;
|
|
|
|
std::string path_file = path + *file;
|
|
|
|
if (Filesystem::isdir(prefixed_file))
|
2015-01-30 15:29:17 -07:00
|
|
|
{
|
2020-07-18 23:22:37 -06:00
|
|
|
files.insert(std::pair<std::string, bool>(include_prefix ? prefixed_file : path_file, true));
|
|
|
|
err = listdir_recursive_impl(prefix, path_file + "/", files, depth - 1, include_prefix);
|
2015-01-30 15:29:17 -07:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-18 23:22:37 -06:00
|
|
|
files.insert(std::pair<std::string, bool>(include_prefix ? prefixed_file : path_file, false));
|
2015-01-30 15:29:17 -07:00
|
|
|
}
|
|
|
|
}
|
2015-01-31 20:28:17 -07:00
|
|
|
return 0;
|
2015-01-30 15:29:17 -07:00
|
|
|
}
|
2020-07-18 23:22:37 -06:00
|
|
|
|
|
|
|
int Filesystem::listdir_recursive (std::string dir, std::map<std::string, bool> &files,
|
|
|
|
int depth /* = 10 */, bool include_prefix /* = true */)
|
|
|
|
{
|
|
|
|
return listdir_recursive_impl(dir, "", files, depth, include_prefix);
|
|
|
|
}
|