Skip to content

Commit

Permalink
don't recheck fs for inputfile::isDir
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Oct 14, 2023
1 parent c9e91d2 commit c4950d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
38 changes: 12 additions & 26 deletions Caprica/common/CapricaInputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,8 @@ bool IInputFile::exists() const {
return std::filesystem::exists(resolved_absolute());
}

bool IInputFile::isDir() const {
return std::filesystem::is_directory(resolved_absolute());
}

bool IInputFile::requiresRemap() const {
return requiresPreParse;
}

static constexpr char const curDir[3] = { '.', FSUtils::SEP, 0 };
static constexpr char const parent[4] = { '.', '.', FSUtils::SEP, 0 };

std::filesystem::path getCorrectBaseDir(const std::filesystem::path& normalPath,
const std::filesystem::path& absBaseDir) {
// for every 2 ..s in the path, remove a directory from the base dir
Expand All @@ -92,47 +83,42 @@ std::filesystem::path getCorrectBaseDir(const std::filesystem::path& normalPath,
return ret;
}

InputFile::InputFile(const std::filesystem::path& _path, bool noRecurse, const std::filesystem::path& _cwd)
: IInputFile(_path, noRecurse, _cwd) {
requiresPreParse = true; // we always require pre-parse for non-PCompiler-compatible input files
}

bool InputFile::resolve() {
auto normalPath = FSUtils::normalize(rawPath);
if (!normalPath.is_absolute()) {
absPath = FSUtils::canonicalFS(cwd / normalPath);
absBaseDir = find_import_dir(absPath);
if (absBaseDir.empty()) {
absBaseDir = getCorrectBaseDir(normalPath, cwd);
requiresPreParse = true;
}
if (std::filesystem::exists(absPath)) {
resolved = true;
return true;
} else {
return false;
}
} else {
absPath = FSUtils::canonicalFS(normalPath);
absBaseDir = find_import_dir(absPath);
if (absBaseDir.empty()) {
absBaseDir = absPath.parent_path();
requiresPreParse = true;
}
}

if (std::filesystem::exists(absPath)) {
if (std::filesystem::is_directory(absPath))
isFolder = true;
resolved = true;
return true;
}

return false;
}

InputFile::InputFile(const std::filesystem::path& _path, bool noRecurse, const std::filesystem::path& _cwd)
: IInputFile(_path, noRecurse, _cwd) {
requiresPreParse = true; // we always require pre-parse for non-PCompiler-compatible input files
}

ImportDir::ImportDir(const std::filesystem::path& _path, bool noRecurse, const std::filesystem::path& _cwd)
: IInputFile(_path, noRecurse, _cwd) {
requiresPreParse = true; // we always require pre-parse for import dirs
import = true;
isFolder = true;
resolve(); // we resolve import dirs immediately
}

Expand All @@ -154,12 +140,12 @@ PCompInputFile::PCompInputFile(const std::filesystem::path& _path,
bool isFolder,
const std::filesystem::path& _cwd)
: IInputFile(_path, noRecurse, _cwd) {
__isFolder = isFolder;
isFolder = isFolder;
}

bool PCompInputFile::resolve() {
std::filesystem::path normalPath = FSUtils::objectNameToPath(rawPath.string());
if (!__isFolder && normalPath.extension().empty())
if (!isFolder && normalPath.extension().empty())
normalPath.replace_extension(".psc");
normalPath = FSUtils::normalize(normalPath);
std::string str = normalPath.string();
Expand All @@ -175,7 +161,7 @@ bool PCompInputFile::resolve() {
}

// if this is a relative folder path, and the folder is in the cwd, use cwd as the base dir
if (__isFolder && !normalPath.is_absolute() && dirContains(normalPath, cwd))
if (isFolder && !normalPath.is_absolute() && dirContains(normalPath, cwd))
absBaseDir = getCorrectBaseDir(normalPath, cwd);
else
absBaseDir = find_import_dir(normalPath);
Expand All @@ -190,7 +176,7 @@ bool PCompInputFile::resolve() {

if (!std::filesystem::exists(absPath))
return false;
if (__isFolder && !std::filesystem::is_directory(absPath))
if (isFolder && !std::filesystem::is_directory(absPath))
return false;

resolved = true;
Expand Down
12 changes: 4 additions & 8 deletions Caprica/common/CapricaInputFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ struct IInputFile {
bool isRecursive() const { return !noRecurse; }
bool isImport() const { return import; }
bool isResolved() const { return resolved; }
virtual bool exists() const;
virtual bool isDir() const;
bool requiresRemap() const;
bool isDir() const { return isFolder; }
bool exists() const;
bool requiresRemap() const { return requiresPreParse; }
virtual bool resolve() = 0;
IInputFile(const std::filesystem::path& _path, bool noRecurse = true, const std::filesystem::path& _cwd = "");
virtual ~IInputFile() = default;
Expand All @@ -30,6 +30,7 @@ struct IInputFile {
bool resolved = false;
bool import = false;
bool requiresPreParse = false;
bool isFolder = false;
};

struct InputFile : public IInputFile {
Expand All @@ -43,16 +44,11 @@ struct PCompInputFile : public IInputFile {
bool noRecurse = true,
bool isFolder = false,
const std::filesystem::path& _cwd = "");
virtual bool isDir() const override { return __isFolder; }
virtual bool resolve() override;

private:
bool __isFolder = false;
};

struct ImportDir : public IInputFile {
ImportDir(const std::filesystem::path& _path, bool noRecurse = true, const std::filesystem::path& _cwd = "");
virtual bool isDir() const override { return true; }
virtual bool resolve() override;

private:
Expand Down

0 comments on commit c4950d1

Please sign in to comment.