From 0b9282a779867459fe5babfff300795c343c46ea Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 12 Oct 2014 12:11:57 +0200 Subject: Added make_temp_{file,dir}() and remove_directory() APIs --- kernel/yosys.cc | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) (limited to 'kernel/yosys.cc') diff --git a/kernel/yosys.cc b/kernel/yosys.cc index 50da13ae..4af3ff9c 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -34,6 +34,7 @@ #include #include +#include #include YOSYS_NAMESPACE_BEGIN @@ -204,7 +205,96 @@ int run_command(const std::string &command, std::function> 17, x ^= x << 5; + template_str[pos+i] = y[x % y.size()]; + } + if (access(template_str.c_str(), F_OK) != 0) + break; + } +#else + size_t pos = template_str.rfind("XXXXXX"); + log_assert(pos != std::string::npos); + + int suffixlen = GetSize(template_str) - pos - 6; + + char *p = strdup(template_str.c_str()); + close(mkstemps(p, suffixlen)); + template_str = p; + free(p); +#endif + + return template_str; +} + +std::string make_temp_dir(std::string template_str) +{ +#ifdef _WIN32 + template_str = make_temp_file(template_str); + mkdir(template_str.c_str()); + return template_str; +#else + size_t pos = template_str.rfind("XXXXXX"); + log_assert(pos != std::string::npos); + + int suffixlen = GetSize(template_str) - pos - 6; + log_assert(suffixlen == 0); + + char *p = strdup(template_str.c_str()); + mkdtemp(p, suffixlen); + template_str = p; + free(p); + + return template_str; +#endif +} + +void remove_directory(std::string dirname) +{ +#ifdef _WIN32 + run_command(stringf("rmdir /s /q \"%s\"", dirname.c_str())); +#else + struct stat stbuf; + struct dirent **namelist; + int n = scandir(dirname.c_str(), &namelist, nullptr, alphasort); + log_assert(n >= 0); + for (int i = 0; i < n; i++) { + if (strcmp(namelist[i]->d_name, ".") && strcmp(namelist[i]->d_name, "..")) { + buffer = stringf("%s/%s", dirname.c_str(), namelist[i]->d_name); + if (!stat(buffer.c_str(), &stbuf) && S_ISREG(stbuf.st_mode)) { + log("Removing `%s'.\n", buffer.c_str()); + remove(buffer.c_str()); + } else + remove_directory(buffer); + } + free(namelist[i]); + } + free(namelist); + log("Removing `%s'.\n", dirname.c_str()); + rmdir(dirname.c_str()); +#endif } int GetSize(RTLIL::Wire *wire) -- cgit v1.2.3