Browse Source

sanitize paths

master
Martin Dørum 3 years ago
parent
commit
dd956a6dfa
4 changed files with 28 additions and 3 deletions
  1. 4
    1
      src/CompileStep.h
  2. 21
    0
      src/sys.cc
  3. 1
    0
      src/sys.h
  4. 2
    2
      src/toolchain.cc

+ 4
- 1
src/CompileStep.h View File

@@ -2,6 +2,7 @@

#include "DepNode.h"
#include "BXParser.h"
#include "sys.h"
#include "toolchain.h"

class CompileStep: public DepNode {
@@ -32,5 +33,7 @@ private:
std::vector<std::string> compileCommand_;
std::vector<std::string> &compileCommand(const std::string &outDir);

std::string confPath(const std::string &outDir) { return outDir + '/' + path_ + ".bx"; }
std::string confPath(const std::string &outDir) {
return outDir + '/' + sys::sanitizePath(path_) + ".bx";
}
};

+ 21
- 0
src/sys.cc View File

@@ -20,6 +20,27 @@ namespace sys {

static thread_local std::unordered_set<std::string> mkdirp_set;

std::string sanitizePath(const std::string &path) {
std::string npath;
size_t idx = 0;
while (idx < path.size()) {
if (idx >= path.size() - 2) {
npath += path[idx++];
} else if (path[idx] == '.' && path[idx + 1] == '.' && path[idx + 2] == '/') {
npath += "__parent__/";
idx += 3;
} else {
npath += path[idx++];
}
}

if (npath[npath.size() - 1] == '.' && npath[npath.size() - 2] == '.') {
npath.replace(npath.size() - 2, 2, "__parent__");
}

return npath;
}

FileInfo fileInfo(const std::string &path) {
FileInfo finfo;


+ 1
- 0
src/sys.h View File

@@ -17,6 +17,7 @@ struct FileInfo {
}
};

std::string sanitizePath(const std::string &path);
FileInfo fileInfo(const std::string &path);
bool fileExists(const std::string &path);
void mkdirp(const std::string &path);

+ 2
- 2
src/toolchain.cc View File

@@ -103,14 +103,14 @@ static void parseWhitespaceSeparated(
}

std::string objectFilePath(const std::string &path, const std::string &outDir) {
return outDir + '/' + path + ".o";
return outDir + '/' + sys::sanitizePath(path) + ".o";
}

std::string targetFilePath(
TargetType type,
const std::string &path,
const std::string &outDir) {
std::string base = outDir + '/' + path;
std::string base = outDir + '/' + sys::sanitizePath(path);

switch (type) {
case TargetType::BINARY:

Loading…
Cancel
Save