13#include <OpenImageIO/filesystem.h>
14#include <OpenImageIO/strutil.h>
15#include <OpenImageIO/sysutil.h>
26# define DIR_SEP_ALT '/'
32# include <sys/types.h>
45# define FILE_MAXDIR 768
51# if defined(_MSC_VER) || defined(__MINGW64__)
53# elif defined(__MINGW32__)
59# define S_ISDIR(x) (((x) & _S_IFDIR) == _S_IFDIR)
71class directory_iterator {
75 path_info(
const string &path,
const WIN32_FIND_DATAW &find_data)
76 : path_(path), find_data_(find_data)
87 const WIN32_FIND_DATAW &find_data_;
90 directory_iterator() : path_info_(
"", find_data_), h_find_(INVALID_HANDLE_VALUE) {}
92 explicit directory_iterator(
const string &path) : path_(path), path_info_(path, find_data_)
94 string wildcard = path;
95 if (wildcard[wildcard.size() - 1] !=
DIR_SEP) {
100 if (h_find_ != INVALID_HANDLE_VALUE) {
105 ~directory_iterator()
107 if (h_find_ != INVALID_HANDLE_VALUE) {
112 directory_iterator &operator++()
118 path_info *operator->()
123 bool operator!=(
const directory_iterator &other)
125 return h_find_ != other.h_find_;
139 if (h_find_ != INVALID_HANDLE_VALUE) {
140 bool result = FindNextFileW(h_find_, &find_data_) == TRUE;
143 h_find_ = INVALID_HANDLE_VALUE;
152 while (wcscmp(find_data_.cFileName,
L".") == 0 || wcscmp(find_data_.cFileName,
L"..") == 0) {
161 path_info path_info_;
162 WIN32_FIND_DATAW find_data_;
167class directory_iterator {
171 explicit path_info(
const string &path) : path_(path) {}
178 void current_entry_set(
const struct dirent *entry)
185 const struct dirent *entry_ =
nullptr;
188 directory_iterator() : path_info_(
""), name_list_(
nullptr), num_entries_(-1), cur_entry_(-1) {}
190 explicit directory_iterator(
const string &path) : path_(path), path_info_(path_), cur_entry_(0)
192 num_entries_ = scandir(path.c_str(), &name_list_,
nullptr, alphasort);
193 if (num_entries_ < 0) {
201 ~directory_iterator()
206 directory_iterator &operator++()
212 path_info *operator->()
214 path_info_.current_entry_set(name_list_[cur_entry_]);
218 bool operator!=(
const directory_iterator &other)
220 return name_list_ != other.name_list_;
235 if (cur_entry_ >= num_entries_) {
245 while (strcmp(name_list_[cur_entry_]->d_name,
".") == 0 ||
246 strcmp(name_list_[cur_entry_]->d_name,
"..") == 0)
255 void destroy_name_list()
257 if (name_list_ ==
nullptr) {
260 for (
int i = 0; i < num_entries_; ++i) {
263 free((
void *)name_list_);
264 name_list_ =
nullptr;
268 path_info path_info_;
269 struct dirent **name_list_;
270 int num_entries_, cur_entry_;
275size_t find_last_slash(
const string &path)
277 for (
size_t i = 0; i < path.size(); ++i) {
278 const size_t index = path.size() - 1 - i;
291std::filesystem::path std_filesystem_path_from_string(
const string &p)
296 return std::filesystem::path(p);
300string std_filesystem_path_to_string(
const std::filesystem::path &p)
313 static char *
const env_shader_path = getenv(
"CYCLES_SHADER_PATH");
315 static char *
const env_source_path = getenv(
"CYCLES_KERNEL_PATH");
317 static char *ngx_lib_path =
nullptr;
318 std::string ngx_path =
"/usr/lib64";
319 std::strncpy(ngx_lib_path, ngx_path.c_str(),
FILE_MAXDIR);
321 if (env_shader_path !=
nullptr && sub ==
"shader") {
322 return env_shader_path;
324 if (env_source_path !=
nullptr && sub ==
"source") {
325 return env_source_path;
327 if (ngx_lib_path !=
nullptr && sub ==
"ngx") {
333#if defined(__linux__) || defined(__APPLE__)
334static string path_xdg_cache_get()
336 const char *home = getenv(
"XDG_CACHE_HOME");
340 home = getenv(
"HOME");
341 if (home ==
nullptr) {
342 home = getpwuid(getuid())->pw_dir;
344 return path_join(
string(home),
".cache");
351 static const string program_dir =
path_dirname(OIIO::Sysutil::this_program_path());
355void path_init(
const string &path,
const string &user_path)
364 OIIO::Filesystem::exists(path);
371 if (special !=
nullptr) {
385#if defined(__linux__) || defined(__APPLE__)
386 static const string xdg_cache_path = path_xdg_cache_get();
387 const string result =
path_join(xdg_cache_path,
"cycles");
395#if defined(__linux__) || defined(__APPLE__)
396string path_xdg_home_get(
const string &sub =
"");
401 const size_t index = find_last_slash(path);
402 if (
index != string::npos) {
405 if (
index == 0 && path.size() == 1) {
409 if (
index == path.size() - 1) {
417 return path.substr(
index + 1, path.size() -
index - 1);
424 const size_t index = find_last_slash(path);
425 if (
index != string::npos) {
427 if (
index == 0 && path.size() > 1) {
431 return path.substr(0,
index);
448 if (result[result.size() - 1] !=
DIR_SEP && result[result.size() - 1] != DIR_SEP_ALT &&
449 file[0] !=
DIR_SEP && file[0] != DIR_SEP_ALT)
460 string result = path;
467 std::string normpath = std_filesystem_path_to_string(
468 std_filesystem_path_from_string(path).lexically_normal().make_preferred());
477 return std_filesystem_path_from_string(path).is_relative();
482 const auto path = std_filesystem_path_from_string(path_);
483 const auto base = std_filesystem_path_from_string(base_);
485 if (!path.is_absolute() && base.is_absolute()) {
490 const auto root_name = path.root_name();
491 if (root_name != base.root_name()) {
497 if (root_name.empty() && path.root_directory() != base.root_directory()) {
501 return std_filesystem_path_to_string(path.lexically_relative(base));
506static string path_unc_add_slash_to_share(
const string &path)
508 size_t slash_after_server = path.find(
DIR_SEP, 2);
509 if (slash_after_server != string::npos) {
510 size_t slash_after_share = path.find(
DIR_SEP, slash_after_server + 1);
511 if (slash_after_share == string::npos) {
522static string path_unc_to_short(
const string &path)
524 size_t len = path.size();
525 if ((
len > 3) && (path[0] ==
DIR_SEP) && (path[1] ==
DIR_SEP) && (path[2] ==
'?') &&
526 ((path[3] ==
DIR_SEP) || (path[3] == DIR_SEP_ALT)))
528 if ((
len > 5) && (path[5] ==
':')) {
529 return path.substr(4,
len - 4);
531 else if ((
len > 7) && (path.substr(4, 3) ==
"UNC") &&
532 ((path[7] ==
DIR_SEP) || (path[7] == DIR_SEP_ALT)))
534 return "\\\\" + path.substr(8,
len - 8);
540static string path_cleanup_unc(
const string &path)
542 string result = path_unc_to_short(path);
543 if (path.size() > 2) {
546 return path_unc_add_slash_to_share(result);
553static string path_make_compatible(
const string &path)
561 if ((path.size() >= 3) && (path[0] ==
DIR_SEP) && (path[1] ==
DIR_SEP)) {
562 result = path_cleanup_unc(result);
565 if (
result.size() == 2 && result[1] ==
':') {
571static int path_wstat(
const wstring &path_wc,
path_stat_t *st)
573# if defined(_MSC_VER) || defined(__MINGW64__)
574 return _wstat64(path_wc.c_str(), st);
575# elif defined(__MINGW32__)
576 return _wstati64(path_wc.c_str(), st);
578 return _wstat(path_wc.c_str(), st);
585 return path_wstat(path_wc, st);
590 return stat(path.c_str(), st);
606 string fixed_path = path_make_compatible(path);
609 if (path_wstat(path_wc, &st) != 0) {
612 return st.st_mode != 0;
615 if (stat(path.c_str(), &st) != 0) {
618 return st.st_mode != 0;
643 directory_iterator it(dir);
644 const directory_iterator it_end;
646 for (; it != it_end; ++it) {
651 const string filepath = it->path();
653 hash.append((
const uint8_t *)filepath.c_str(), filepath.size());
654 hash.append_file(filepath);
667 return hash.get_hex();
678 if (!parent.empty() && parent != path) {
686 _wmkdir(path_wc.c_str());
688 mkdir(path.c_str(), 0777);
713 if (!binary.empty()) {
714 fwrite(binary.data(),
sizeof(uint8_t), binary.size(), f);
725 std::copy(text.begin(), text.end(), binary.begin());
742 if (binary.empty()) {
747 if (fread(binary.data(),
sizeof(uint8_t), binary.size(), f) != binary.size()) {
768 const size_t full_size = ZSTD_getFrameContentSize(compressed.data(), compressed.size());
770 if (full_size == ZSTD_CONTENTSIZE_ERROR) {
774 if (full_size == ZSTD_CONTENTSIZE_UNKNOWN) {
780 binary.resize(full_size);
782 const size_t err = ZSTD_decompress(
783 binary.data(), binary.size(), compressed.data(), compressed.size());
785 return ZSTD_isError(err) == 0;
796 const char *
str = (
const char *)binary.data();
797 const size_t size = binary.size();
811 const char *
str = (
const char *)binary.data();
812 const size_t size = binary.size();
829 return remove(path.c_str()) == 0;
843 const string &source_filepath,
848 const size_t line_number)
850 string unescaped_path = path;
854 const size_t base_len =
state.base.length();
855 unescaped_path = base_file +
856 unescaped_path.substr(base_len, unescaped_path.length() - base_len);
859 const size_t length = unescaped_path.length();
861 for (
size_t i = 0; i <
length; ++i) {
862 const char ch = unescaped_path[i];
863 if (strchr(
"\"\'\?\\", ch) !=
nullptr) {
864 escaped_path +=
"\\";
868 return "#line " + std::to_string(line_number) +
'"' + escaped_path +
'"';
872 const string &source_filepath,
873 const size_t line_number,
876 string result = preprocessor_line;
878 string rest_of_line =
string_strip(preprocessor_line.substr(1));
880 if (0 == strncmp(rest_of_line.c_str(),
"include", 7)) {
882 if (rest_of_line[0] ==
'"') {
883 const size_t n_start = 1;
884 const size_t n_end = rest_of_line.find(
"\"", n_start);
885 const string filename = rest_of_line.substr(n_start, n_end - n_start);
909 const string &source_filepath,
912 const string *psource = &_source;
915 auto pragma_once = _source.find(
"#pragma once");
916 if (pragma_once != string::npos) {
917 if (
state->pragma_onced.contains(source_filepath)) {
920 state->pragma_onced.insert(source_filepath);
924 source_new = _source;
925 memcpy(source_new.data() + pragma_once,
"//pr", 4);
926 psource = &source_new;
932 const SourceReplaceState::ProcessedMapping::iterator replaced_file =
state->processed_files.find(
934 if (replaced_file !=
state->processed_files.end()) {
935 return replaced_file->second;
938 const string &source = *psource;
942 const size_t source_length = source.length();
945 size_t line_number = 0;
946 size_t column_number = 1;
951 size_t token_start = 0;
952 size_t token_length = 0;
959 bool inside_preprocessor =
false;
960 string preprocessor_line;
962 while (
index < source_length) {
963 const char ch = source[
index];
966 if (inside_preprocessor) {
968 preprocessor_line, source_filepath, line_number,
state);
970 if (!block.empty()) {
977 inside_preprocessor =
false;
978 preprocessor_line =
"";
983 else if (ch ==
'#' && column_number == 1 && !inside_preprocessor) {
985 if (token_length != 0) {
986 result.append(source, token_start, token_length);
990 inside_preprocessor =
true;
993 if (inside_preprocessor) {
994 preprocessor_line += ch;
1005 if (token_length != 0) {
1006 result.append(source, token_start, token_length);
1008 if (inside_preprocessor) {
1010 preprocessor_line, source_filepath, line_number,
state);
1013 state->processed_files[source_filepath] = result;
1029 return _wfopen(path_wc.c_str(), mode_wc.c_str());
1031 return fopen(path.c_str(), mode.c_str());
1039 const std::time_t current_time = std::time(
nullptr);
1040 OIIO::Filesystem::last_write_time(path, current_time);
1053 const size_t max_old_kernel_of_same_type)
1063 directory_iterator it(dir);
1064 const directory_iterator it_end;
1067 for (; it != it_end; ++it) {
1068 const string &path = it->path();
1069 if (path == new_path) {
1073 const std::time_t last_time = OIIO::Filesystem::last_write_time(path);
1074 same_kernel_types.emplace_back(last_time, path);
1077 if (same_kernel_types.size() > max_old_kernel_of_same_type) {
1078 sort(same_kernel_types.begin(), same_kernel_types.end());
1080 for (
int i = 0; i < same_kernel_types.size() - max_old_kernel_of_same_type; i++) {
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
static DBVT_INLINE btDbvtNode * sort(btDbvtNode *n, btDbvtNode *&r)
#define CCL_NAMESPACE_END
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
ccl_device_inline bool operator!=(const float2 a, const float2 b)
size_t path_file_size(const string &path)
bool path_read_compressed_binary(const string &path, vector< uint8_t > &binary)
static string path_source_replace_includes_recursive(const string &source, const string &source_filepath, SourceReplaceState *state)
static string cached_user_path
string path_user_get(const string &sub)
string path_cache_get(const string &sub)
string path_make_relative(const string &path_, const string &base_)
bool path_is_directory(const string &path)
FILE * path_fopen(const string &path, const string &mode)
string path_dirname(const string &path)
string path_source_replace_includes(const string &source, const string &path)
static string path_source_handle_preprocessor(const string &preprocessor_line, const string &source_filepath, const size_t line_number, SourceReplaceState *state)
bool path_create_directories(const string &filepath)
static int path_stat(const string &path, path_stat_t *st)
string path_get(const string &sub)
string path_files_md5_hash(const string &dir)
bool path_is_relative(const string &path)
uint64_t path_modified_time(const string &path)
string path_join(const string &dir, const string &file)
static void path_cache_kernel_mark_used(const string &path)
bool path_exists(const string &path)
static string line_directive(const SourceReplaceState &state, const string &path, const size_t line_number)
bool path_write_text(const string &path, const string &text)
static const string & path_program_dir()
bool path_cache_kernel_exists_and_mark_used(const string &path)
string path_escape(const string &path)
string path_normalize(const string &path)
void path_cache_kernel_mark_added_and_clear_old(const string &new_path, const size_t max_old_kernel_of_same_type)
bool path_write_binary(const string &path, const vector< uint8_t > &binary)
static void path_files_md5_hash_recursive(MD5Hash &hash, const string &dir)
bool path_read_text(const string &path, string &text)
bool path_read_compressed_text(const string &path, string &text)
bool path_is_file(const string &path)
static bool create_directories_recursively(const string &path)
string path_filename(const string &path)
static char * path_specials(const string &sub)
static string cached_path
bool path_remove(const string &path)
void path_init(const string &path, const string &user_path)
bool path_read_binary(const string &path, vector< uint8_t > &binary)
string string_from_wstring(const wstring &str)
wstring string_to_wstring(const string &str)
string string_strip(const string &s)
bool string_startswith(const string_view s, const string_view start)
void string_replace(string &haystack, const string &needle, const string &other)
bool string_endswith(const string_view s, const string_view end)
map< string, string > ProcessedMapping
ProcessedMapping processed_files
set< string > pragma_onced