Blender V5.3
path.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/path.h"
6#include "util/algorithm.h"
7#include "util/map.h"
8#include "util/md5.h"
9#include "util/set.h"
10#include "util/string.h"
11#include "util/vector.h"
12
13#include <OpenImageIO/filesystem.h>
14#include <OpenImageIO/strutil.h>
15#include <OpenImageIO/sysutil.h>
16
17#include <cstdio>
18#include <filesystem>
19
20#include <sys/stat.h>
21
22#include <zstd.h>
23
24#if defined(_WIN32)
25# define DIR_SEP '\\'
26# define DIR_SEP_ALT '/'
27# include <direct.h>
28#else
29# define DIR_SEP '/'
30# include <dirent.h>
31# include <pwd.h>
32# include <sys/types.h>
33# include <unistd.h>
34#endif
35
36#ifdef HAVE_SHLWAPI_H
37# include <shlwapi.h>
38#endif
39
40#ifdef _WIN32
41# include "util/windows.h"
42#endif
43
44#ifndef FILE_MAXDIR
45# define FILE_MAXDIR 768
46#endif
47
49
50#ifdef _WIN32
51# if defined(_MSC_VER) || defined(__MINGW64__)
52typedef struct _stat64 path_stat_t;
53# elif defined(__MINGW32__)
54typedef struct _stati64 path_stat_t;
55# else
56typedef struct _stat path_stat_t;
57# endif
58# ifndef S_ISDIR
59# define S_ISDIR(x) (((x) & _S_IFDIR) == _S_IFDIR)
60# endif
61#else
62using path_stat_t = struct stat;
63#endif
64
65static string cached_path;
66static string cached_user_path;
67
68namespace {
69
70#ifdef _WIN32
71class directory_iterator {
72 public:
73 class path_info {
74 public:
75 path_info(const string &path, const WIN32_FIND_DATAW &find_data)
76 : path_(path), find_data_(find_data)
77 {
78 }
79
80 string path()
81 {
82 return path_join(path_, string_from_wstring(find_data_.cFileName));
83 }
84
85 protected:
86 const string &path_;
87 const WIN32_FIND_DATAW &find_data_;
88 };
89
90 directory_iterator() : path_info_("", find_data_), h_find_(INVALID_HANDLE_VALUE) {}
91
92 explicit directory_iterator(const string &path) : path_(path), path_info_(path, find_data_)
93 {
94 string wildcard = path;
95 if (wildcard[wildcard.size() - 1] != DIR_SEP) {
96 wildcard += DIR_SEP;
97 }
98 wildcard += "*";
99 h_find_ = FindFirstFileW(string_to_wstring(wildcard).c_str(), &find_data_);
100 if (h_find_ != INVALID_HANDLE_VALUE) {
101 skip_dots();
102 }
103 }
104
105 ~directory_iterator()
106 {
107 if (h_find_ != INVALID_HANDLE_VALUE) {
108 FindClose(h_find_);
109 }
110 }
111
112 directory_iterator &operator++()
113 {
114 step();
115 return *this;
116 }
117
118 path_info *operator->()
119 {
120 return &path_info_;
121 }
122
123 bool operator!=(const directory_iterator &other)
124 {
125 return h_find_ != other.h_find_;
126 }
127
128 protected:
129 bool step()
130 {
131 if (do_step()) {
132 return skip_dots();
133 }
134 return false;
135 }
136
137 bool do_step()
138 {
139 if (h_find_ != INVALID_HANDLE_VALUE) {
140 bool result = FindNextFileW(h_find_, &find_data_) == TRUE;
141 if (!result) {
142 FindClose(h_find_);
143 h_find_ = INVALID_HANDLE_VALUE;
144 }
145 return result;
146 }
147 return false;
148 }
149
150 bool skip_dots()
151 {
152 while (wcscmp(find_data_.cFileName, L".") == 0 || wcscmp(find_data_.cFileName, L"..") == 0) {
153 if (!do_step()) {
154 return false;
155 }
156 }
157 return true;
158 }
159
160 string path_;
161 path_info path_info_;
162 WIN32_FIND_DATAW find_data_;
163 HANDLE h_find_;
164};
165#else /* _WIN32 */
166
167class directory_iterator {
168 public:
169 class path_info {
170 public:
171 explicit path_info(const string &path) : path_(path) {}
172
173 string path()
174 {
175 return path_join(path_, entry_->d_name);
176 }
177
178 void current_entry_set(const struct dirent *entry)
179 {
180 entry_ = entry;
181 }
182
183 protected:
184 const string &path_;
185 const struct dirent *entry_ = nullptr;
186 };
187
188 directory_iterator() : path_info_(""), name_list_(nullptr), num_entries_(-1), cur_entry_(-1) {}
189
190 explicit directory_iterator(const string &path) : path_(path), path_info_(path_), cur_entry_(0)
191 {
192 num_entries_ = scandir(path.c_str(), &name_list_, nullptr, alphasort);
193 if (num_entries_ < 0) {
194 perror("scandir");
195 }
196 else {
197 skip_dots();
198 }
199 }
200
201 ~directory_iterator()
202 {
203 destroy_name_list();
204 }
205
206 directory_iterator &operator++()
207 {
208 step();
209 return *this;
210 }
211
212 path_info *operator->()
213 {
214 path_info_.current_entry_set(name_list_[cur_entry_]);
215 return &path_info_;
216 }
217
218 bool operator!=(const directory_iterator &other)
219 {
220 return name_list_ != other.name_list_;
221 }
222
223 protected:
224 bool step()
225 {
226 if (do_step()) {
227 return skip_dots();
228 }
229 return false;
230 }
231
232 bool do_step()
233 {
234 ++cur_entry_;
235 if (cur_entry_ >= num_entries_) {
236 destroy_name_list();
237 return false;
238 }
239 return true;
240 }
241
242 /* Skip . and .. folders. */
243 bool skip_dots()
244 {
245 while (strcmp(name_list_[cur_entry_]->d_name, ".") == 0 ||
246 strcmp(name_list_[cur_entry_]->d_name, "..") == 0)
247 {
248 if (!step()) {
249 return false;
250 }
251 }
252 return true;
253 }
254
255 void destroy_name_list()
256 {
257 if (name_list_ == nullptr) {
258 return;
259 }
260 for (int i = 0; i < num_entries_; ++i) {
261 free(name_list_[i]);
262 }
263 free((void *)name_list_);
264 name_list_ = nullptr;
265 }
266
267 string path_;
268 path_info path_info_;
269 struct dirent **name_list_;
270 int num_entries_, cur_entry_;
271};
272
273#endif /* _WIN32 */
274
275size_t find_last_slash(const string &path)
276{
277 for (size_t i = 0; i < path.size(); ++i) {
278 const size_t index = path.size() - 1 - i;
279#ifdef _WIN32
280 if (path[index] == DIR_SEP || path[index] == DIR_SEP_ALT)
281#else
282 if (path[index] == DIR_SEP)
283#endif
284 {
285 return index;
286 }
287 }
288 return string::npos;
289}
290
291std::filesystem::path std_filesystem_path_from_string(const string &p)
292{
293#ifdef _WIN32
294 return std::filesystem::path(string_to_wstring(p));
295#else
296 return std::filesystem::path(p);
297#endif
298}
299
300string std_filesystem_path_to_string(const std::filesystem::path &p)
301{
302#ifdef _WIN32
303 return string_from_wstring(p.native());
304#else
305 return p.string();
306#endif
307}
308
309} /* namespace */
310
311static char *path_specials(const string &sub)
312{
313 static char *const env_shader_path = getenv("CYCLES_SHADER_PATH");
314 /* NOTE: It is KERNEL in env variable for compatibility reasons. */
315 static char *const env_source_path = getenv("CYCLES_KERNEL_PATH");
316
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);
320
321 if (env_shader_path != nullptr && sub == "shader") {
322 return env_shader_path;
323 }
324 if (env_source_path != nullptr && sub == "source") {
325 return env_source_path;
326 }
327 if (ngx_lib_path != nullptr && sub == "ngx") {
328 return ngx_lib_path;
329 }
330 return nullptr;
331}
332
333#if defined(__linux__) || defined(__APPLE__)
334static string path_xdg_cache_get()
335{
336 const char *home = getenv("XDG_CACHE_HOME");
337 if (home) {
338 return string(home);
339 }
340 home = getenv("HOME");
341 if (home == nullptr) {
342 home = getpwuid(getuid())->pw_dir;
343 }
344 return path_join(string(home), ".cache");
345}
346#endif
347
348/* Fallback for when #path_init was not called to initialize #cached_path. */
349static const string &path_program_dir()
350{
351 static const string program_dir = path_dirname(OIIO::Sysutil::this_program_path());
352 return program_dir;
353}
354
355void path_init(const string &path, const string &user_path)
356{
357 cached_path = path;
358 cached_user_path = user_path;
359
360#ifdef _MSC_VER
361 // workaround for https://svn.boost.org/trac/boost/ticket/6320
362 // indirectly init boost codec here since it's not thread safe, and can
363 // cause crashes when it happens in multithreaded image load
364 OIIO::Filesystem::exists(path);
365#endif
366}
367
368string path_get(const string &sub)
369{
370 char *special = path_specials(sub);
371 if (special != nullptr) {
372 return special;
373 }
374
375 return path_join(cached_path.empty() ? path_program_dir() : cached_path, sub);
376}
377
378string path_user_get(const string &sub)
379{
381}
382
383string path_cache_get(const string &sub)
384{
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");
388 return path_join(result, sub);
389#else
390 /* TODO(sergey): What that should be on Windows? */
391 return path_user_get(path_join("cache", sub));
392#endif
393}
394
395#if defined(__linux__) || defined(__APPLE__)
396string path_xdg_home_get(const string &sub = "");
397#endif
398
399string path_filename(const string &path)
400{
401 const size_t index = find_last_slash(path);
402 if (index != string::npos) {
403 /* Corner cases to match boost behavior. */
404#ifndef _WIN32
405 if (index == 0 && path.size() == 1) {
406 return path;
407 }
408#endif
409 if (index == path.size() - 1) {
410#ifdef _WIN32
411 if (index == 2) {
412 return string(1, DIR_SEP);
413 }
414#endif
415 return ".";
416 }
417 return path.substr(index + 1, path.size() - index - 1);
418 }
419 return path;
420}
421
422string path_dirname(const string &path)
423{
424 const size_t index = find_last_slash(path);
425 if (index != string::npos) {
426#ifndef _WIN32
427 if (index == 0 && path.size() > 1) {
428 return string(1, DIR_SEP);
429 }
430#endif
431 return path.substr(0, index);
432 }
433 return "";
434}
435
436string path_join(const string &dir, const string &file)
437{
438 if (dir.empty()) {
439 return file;
440 }
441 if (file.empty()) {
442 return dir;
443 }
444 string result = dir;
445#ifndef _WIN32
446 if (result[result.size() - 1] != DIR_SEP && file[0] != DIR_SEP)
447#else
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)
450#endif
451 {
452 result += DIR_SEP;
453 }
454 result += file;
455 return result;
456}
457
458string path_escape(const string &path)
459{
460 string result = path;
461 string_replace(result, " ", "\\ ");
462 return result;
463}
464
465string path_normalize(const string &path)
466{
467 std::string normpath = std_filesystem_path_to_string(
468 std_filesystem_path_from_string(path).lexically_normal().make_preferred());
469#ifdef _WIN32
470 string_replace(normpath, "\\", "/");
471#endif
472 return normpath;
473}
474
475bool path_is_relative(const string &path)
476{
477 return std_filesystem_path_from_string(path).is_relative();
478}
479
480string path_make_relative(const string &path_, const string &base_)
481{
482 const auto path = std_filesystem_path_from_string(path_);
483 const auto base = std_filesystem_path_from_string(base_);
484
485 if (!path.is_absolute() && base.is_absolute()) {
486 return path_;
487 }
488
489 /* Don't make relative between different drives. */
490 const auto root_name = path.root_name();
491 if (root_name != base.root_name()) {
492 return path_;
493 }
494
495 /* Also don't make relative if root directories are different. This prevents
496 * typical cases like making relative to /tmp. */
497 if (root_name.empty() && path.root_directory() != base.root_directory()) {
498 return path_;
499 }
500
501 return std_filesystem_path_to_string(path.lexically_relative(base));
502}
503
504#ifdef _WIN32
505/* Add a slash if the UNC path points to a share. */
506static string path_unc_add_slash_to_share(const string &path)
507{
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) {
512 return path + DIR_SEP;
513 }
514 }
515 return path;
516}
517
518/* Convert:
519 * \\?\UNC\server\share\folder\... to \\server\share\folder\...
520 * \\?\C:\ to C:\ and \\?\C:\folder\... to C:\folder\...
521 */
522static string path_unc_to_short(const string &path)
523{
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)))
527 {
528 if ((len > 5) && (path[5] == ':')) {
529 return path.substr(4, len - 4);
530 }
531 else if ((len > 7) && (path.substr(4, 3) == "UNC") &&
532 ((path[7] == DIR_SEP) || (path[7] == DIR_SEP_ALT)))
533 {
534 return "\\\\" + path.substr(8, len - 8);
535 }
536 }
537 return path;
538}
539
540static string path_cleanup_unc(const string &path)
541{
542 string result = path_unc_to_short(path);
543 if (path.size() > 2) {
544 /* It's possible path is now a non-UNC. */
545 if (result[0] == DIR_SEP && result[1] == DIR_SEP) {
546 return path_unc_add_slash_to_share(result);
547 }
548 }
549 return result;
550}
551
552/* Make path compatible for stat() functions. */
553static string path_make_compatible(const string &path)
554{
555 string result = path;
556 /* In Windows stat() doesn't recognize dir ending on a slash. */
557 if (result.size() > 3 && result[result.size() - 1] == DIR_SEP) {
558 result.resize(result.size() - 1);
559 }
560 /* Clean up UNC path. */
561 if ((path.size() >= 3) && (path[0] == DIR_SEP) && (path[1] == DIR_SEP)) {
562 result = path_cleanup_unc(result);
563 }
564 /* Make sure volume-only path ends up wit a directory separator. */
565 if (result.size() == 2 && result[1] == ':') {
566 result += DIR_SEP;
567 }
568 return result;
569}
570
571static int path_wstat(const wstring &path_wc, path_stat_t *st)
572{
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);
577# else
578 return _wstat(path_wc.c_str(), st);
579# endif
580}
581
582static int path_stat(const string &path, path_stat_t *st)
583{
584 wstring path_wc = string_to_wstring(path);
585 return path_wstat(path_wc, st);
586}
587#else /* _WIN32 */
588static int path_stat(const string &path, path_stat_t *st)
589{
590 return stat(path.c_str(), st);
591}
592#endif /* _WIN32 */
593
594size_t path_file_size(const string &path)
595{
596 path_stat_t st;
597 if (path_stat(path, &st) != 0) {
598 return -1;
599 }
600 return st.st_size;
601}
602
603bool path_exists(const string &path)
604{
605#ifdef _WIN32
606 string fixed_path = path_make_compatible(path);
607 wstring path_wc = string_to_wstring(fixed_path);
608 path_stat_t st;
609 if (path_wstat(path_wc, &st) != 0) {
610 return false;
611 }
612 return st.st_mode != 0;
613#else /* _WIN32 */
614 struct stat st;
615 if (stat(path.c_str(), &st) != 0) {
616 return false;
617 }
618 return st.st_mode != 0;
619#endif /* _WIN32 */
620}
621
622bool path_is_directory(const string &path)
623{
624 path_stat_t st;
625 if (path_stat(path, &st) != 0) {
626 return false;
627 }
628 return S_ISDIR(st.st_mode);
629}
630
631bool path_is_file(const string &path)
632{
633 path_stat_t st;
634 if (path_stat(path, &st) != 0) {
635 return false;
636 }
637 return !S_ISDIR(st.st_mode);
638}
639
640static void path_files_md5_hash_recursive(MD5Hash &hash, const string &dir)
641{
642 if (path_exists(dir)) {
643 directory_iterator it(dir);
644 const directory_iterator it_end;
645
646 for (; it != it_end; ++it) {
647 if (path_is_directory(it->path())) {
649 }
650 else {
651 const string filepath = it->path();
652
653 hash.append((const uint8_t *)filepath.c_str(), filepath.size());
654 hash.append_file(filepath);
655 }
656 }
657 }
658}
659
660string path_files_md5_hash(const string &dir)
661{
662 /* computes md5 hash of all files in the directory */
664
666
667 return hash.get_hex();
668}
669
670static bool create_directories_recursively(const string &path)
671{
672 if (path_exists(path)) {
673 /* Either directory exists and there is nothing to do, or it's a file and we fail. */
674 return path_is_directory(path);
675 }
676
677 const string parent = path_dirname(path);
678 if (!parent.empty() && parent != path) {
679 if (!create_directories_recursively(parent)) {
680 return false;
681 }
682 }
683
684#ifdef _WIN32
685 wstring path_wc = string_to_wstring(path);
686 _wmkdir(path_wc.c_str());
687#else
688 mkdir(path.c_str(), 0777);
689#endif
690
691 /* If another thread creates this in the meantime mkdir will return an error,
692 * so instead check if the directory exists. */
693 return path_is_directory(path);
694}
695
696bool path_create_directories(const string &filepath)
697{
698 const string path = path_dirname(filepath);
700}
701
702bool path_write_binary(const string &path, const vector<uint8_t> &binary)
703{
705
706 /* write binary file from memory */
707 FILE *f = path_fopen(path, "wb");
708
709 if (!f) {
710 return false;
711 }
712
713 if (!binary.empty()) {
714 fwrite(binary.data(), sizeof(uint8_t), binary.size(), f);
715 }
716
717 fclose(f);
718
719 return true;
720}
721
722bool path_write_text(const string &path, const string &text)
723{
724 vector<uint8_t> binary(text.length(), 0);
725 std::copy(text.begin(), text.end(), binary.begin());
726
727 return path_write_binary(path, binary);
728}
729
730bool path_read_binary(const string &path, vector<uint8_t> &binary)
731{
732 /* read binary file into memory */
733 FILE *f = path_fopen(path, "rb");
734
735 if (!f) {
736 binary.resize(0);
737 return false;
738 }
739
740 binary.resize(path_file_size(path));
741
742 if (binary.empty()) {
743 fclose(f);
744 return false;
745 }
746
747 if (fread(binary.data(), sizeof(uint8_t), binary.size(), f) != binary.size()) {
748 fclose(f);
749 return false;
750 }
751
752 fclose(f);
753
754 return true;
755}
756
757bool path_read_compressed_binary(const string &path, vector<uint8_t> &binary)
758{
759 if (!string_endswith(path, ".zst")) {
760 return path_read_binary(path, binary);
761 }
762
763 vector<uint8_t> compressed;
764 if (!path_read_binary(path, compressed)) {
765 return false;
766 }
767
768 const size_t full_size = ZSTD_getFrameContentSize(compressed.data(), compressed.size());
769
770 if (full_size == ZSTD_CONTENTSIZE_ERROR) {
771 /* Potentially corrupted file? */
772 return false;
773 }
774 if (full_size == ZSTD_CONTENTSIZE_UNKNOWN) {
775 /* Technically this is an optional field, but we can expect it to be set for now.
776 * Otherwise we'd need streaming decompression and repeated resizing of the vector. */
777 return false;
778 }
779
780 binary.resize(full_size);
781
782 const size_t err = ZSTD_decompress(
783 binary.data(), binary.size(), compressed.data(), compressed.size());
784
785 return ZSTD_isError(err) == 0;
786}
787
788bool path_read_text(const string &path, string &text)
789{
790 vector<uint8_t> binary;
791
792 if (!path_exists(path) || !path_read_binary(path, binary)) {
793 return false;
794 }
795
796 const char *str = (const char *)binary.data();
797 const size_t size = binary.size();
798 text = string(str, size);
799
800 return true;
801}
802
803bool path_read_compressed_text(const string &path, string &text)
804{
805 vector<uint8_t> binary;
806
807 if (!path_exists(path) || !path_read_compressed_binary(path, binary)) {
808 return false;
809 }
810
811 const char *str = (const char *)binary.data();
812 const size_t size = binary.size();
813 text = string(str, size);
814
815 return true;
816}
817
818uint64_t path_modified_time(const string &path)
819{
820 path_stat_t st;
821 if (path_stat(path, &st) != 0) {
822 return 0;
823 }
824 return st.st_mtime;
825}
826
827bool path_remove(const string &path)
828{
829 return remove(path.c_str()) == 0;
830}
831
833 using ProcessedMapping = map<string, string>;
834 /* Base director for all relative include headers. */
835 string base;
836 /* Result of processed files. */
838 /* Set of files containing #pragma once which have been included. */
839 set<string> pragma_onced;
840};
841
842static string path_source_replace_includes_recursive(const string &source,
843 const string &source_filepath,
845
847 const string &path,
848 const size_t line_number)
849{
850 string unescaped_path = path;
851 /* First we make path relative. */
852 if (string_startswith(unescaped_path, state.base)) {
853 const string base_file = path_filename(state.base);
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);
857 }
858 /* Second, we replace all unsafe characters. */
859 const size_t length = unescaped_path.length();
860 string escaped_path;
861 for (size_t i = 0; i < length; ++i) {
862 const char ch = unescaped_path[i];
863 if (strchr("\"\'\?\\", ch) != nullptr) {
864 escaped_path += "\\";
865 }
866 escaped_path += ch;
867 }
868 return "#line " + std::to_string(line_number) + '"' + escaped_path + '"';
869}
870
871static string path_source_handle_preprocessor(const string &preprocessor_line,
872 const string &source_filepath,
873 const size_t line_number,
875{
876 string result = preprocessor_line;
877
878 string rest_of_line = string_strip(preprocessor_line.substr(1));
879
880 if (0 == strncmp(rest_of_line.c_str(), "include", 7)) {
881 rest_of_line = string_strip(rest_of_line.substr(8));
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);
886
887 string filepath = path_join(state->base, filename);
888 if (!path_exists(filepath)) {
889 filepath = path_join(path_dirname(source_filepath), filename);
890 }
891 string text;
892 if (path_read_text(filepath, text)) {
893 text = path_source_replace_includes_recursive(text, filepath, state);
894 /* Use line directives for better error messages. */
895 result = line_directive(*state, filepath, 1) + "\n" + text + "\n" +
896 line_directive(*state, source_filepath, line_number + 1);
897 }
898 }
899 }
900
901 return result;
902}
903
904/* Our own little c preprocessor that replaces #includes with the file
905 * contents, to work around issue of OpenCL drivers not supporting
906 * include paths with spaces in them.
907 */
908static string path_source_replace_includes_recursive(const string &_source,
909 const string &source_filepath,
911{
912 const string *psource = &_source;
913 string source_new;
914
915 auto pragma_once = _source.find("#pragma once");
916 if (pragma_once != string::npos) {
917 if (state->pragma_onced.contains(source_filepath)) {
918 return "";
919 }
920 state->pragma_onced.insert(source_filepath);
921
922 // "#pragma once"
923 // "//prgma once"
924 source_new = _source;
925 memcpy(source_new.data() + pragma_once, "//pr", 4);
926 psource = &source_new;
927 }
928
929 /* Try to re-use processed file without spending time on replacing all
930 * include directives again.
931 */
932 const SourceReplaceState::ProcessedMapping::iterator replaced_file = state->processed_files.find(
933 source_filepath);
934 if (replaced_file != state->processed_files.end()) {
935 return replaced_file->second;
936 }
937
938 const string &source = *psource;
939
940 /* Perform full file processing. */
941 string result;
942 const size_t source_length = source.length();
943 size_t index = 0;
944 /* Information about where we are in the source. */
945 size_t line_number = 0;
946 size_t column_number = 1;
947 /* Currently gathered non-preprocessor token.
948 * Store as start/length rather than token itself to avoid overhead of
949 * memory re-allocations on each character concatenation.
950 */
951 size_t token_start = 0;
952 size_t token_length = 0;
953 /* Denotes whether we're inside of preprocessor line, together with
954 * preprocessor line itself.
955 *
956 * TODO(sergey): Investigate whether using token start/end position
957 * gives measurable speedup.
958 */
959 bool inside_preprocessor = false;
960 string preprocessor_line;
961 /* Actual loop over the whole source. */
962 while (index < source_length) {
963 const char ch = source[index];
964
965 if (ch == '\n') {
966 if (inside_preprocessor) {
967 const string block = path_source_handle_preprocessor(
968 preprocessor_line, source_filepath, line_number, state);
969
970 if (!block.empty()) {
971 result += block;
972 }
973
974 /* Start gathering net part of the token. */
975 token_start = index;
976 token_length = 0;
977 inside_preprocessor = false;
978 preprocessor_line = "";
979 }
980 column_number = 0;
981 ++line_number;
982 }
983 else if (ch == '#' && column_number == 1 && !inside_preprocessor) {
984 /* Append all possible non-preprocessor token to the result. */
985 if (token_length != 0) {
986 result.append(source, token_start, token_length);
987 token_start = index;
988 token_length = 0;
989 }
990 inside_preprocessor = true;
991 }
992
993 if (inside_preprocessor) {
994 preprocessor_line += ch;
995 }
996 else {
997 ++token_length;
998 }
999 ++index;
1000 ++column_number;
1001 }
1002 /* Append possible tokens which happened before special events handled
1003 * above.
1004 */
1005 if (token_length != 0) {
1006 result.append(source, token_start, token_length);
1007 }
1008 if (inside_preprocessor) {
1010 preprocessor_line, source_filepath, line_number, state);
1011 }
1012 /* Store result for further reuse. */
1013 state->processed_files[source_filepath] = result;
1014 return result;
1015}
1016
1017string path_source_replace_includes(const string &source, const string &path)
1018{
1020 state.base = path;
1021 return path_source_replace_includes_recursive(source, path, &state);
1022}
1023
1024FILE *path_fopen(const string &path, const string &mode)
1025{
1026#ifdef _WIN32
1027 wstring path_wc = string_to_wstring(path);
1028 wstring mode_wc = string_to_wstring(mode);
1029 return _wfopen(path_wc.c_str(), mode_wc.c_str());
1030#else
1031 return fopen(path.c_str(), mode.c_str());
1032#endif
1033}
1034
1035/* LRU Cache for Kernels */
1036
1037static void path_cache_kernel_mark_used(const string &path)
1038{
1039 const std::time_t current_time = std::time(nullptr);
1040 OIIO::Filesystem::last_write_time(path, current_time);
1041}
1042
1044{
1045 if (path_exists(path)) {
1047 return true;
1048 }
1049 return false;
1050}
1051
1053 const size_t max_old_kernel_of_same_type)
1054{
1056
1057 const string dir = path_dirname(new_path);
1058 if (!path_exists(dir)) {
1059 return;
1060 }
1061
1062 /* Remove older kernels within the same directory. */
1063 directory_iterator it(dir);
1064 const directory_iterator it_end;
1065 vector<pair<std::time_t, string>> same_kernel_types;
1066
1067 for (; it != it_end; ++it) {
1068 const string &path = it->path();
1069 if (path == new_path) {
1070 continue;
1071 }
1072
1073 const std::time_t last_time = OIIO::Filesystem::last_write_time(path);
1074 same_kernel_types.emplace_back(last_time, path);
1075 }
1076
1077 if (same_kernel_types.size() > max_old_kernel_of_same_type) {
1078 sort(same_kernel_types.begin(), same_kernel_types.end());
1079
1080 for (int i = 0; i < same_kernel_types.size() - max_old_kernel_of_same_type; i++) {
1081 path_remove(same_kernel_types[i].second);
1082 }
1083 }
1084}
1085
#define FILE_MAXDIR
#define S_ISDIR(x)
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static DBVT_INLINE btDbvtNode * sort(btDbvtNode *n, btDbvtNode *&r)
Definition btDbvt.cpp:418
Definition md5.h:19
#define CCL_NAMESPACE_END
#define str(s)
#define index(i)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
const int state
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline bool operator!=(const float2 a, const float2 b)
#define L
#define hash
Definition noise_c.cc:156
size_t path_file_size(const string &path)
Definition path.cpp:594
bool path_read_compressed_binary(const string &path, vector< uint8_t > &binary)
Definition path.cpp:757
static string path_source_replace_includes_recursive(const string &source, const string &source_filepath, SourceReplaceState *state)
Definition path.cpp:908
static string cached_user_path
Definition path.cpp:66
string path_user_get(const string &sub)
Definition path.cpp:378
string path_cache_get(const string &sub)
Definition path.cpp:383
string path_make_relative(const string &path_, const string &base_)
Definition path.cpp:480
bool path_is_directory(const string &path)
Definition path.cpp:622
FILE * path_fopen(const string &path, const string &mode)
Definition path.cpp:1024
string path_dirname(const string &path)
Definition path.cpp:422
string path_source_replace_includes(const string &source, const string &path)
Definition path.cpp:1017
static string path_source_handle_preprocessor(const string &preprocessor_line, const string &source_filepath, const size_t line_number, SourceReplaceState *state)
Definition path.cpp:871
bool path_create_directories(const string &filepath)
Definition path.cpp:696
static int path_stat(const string &path, path_stat_t *st)
Definition path.cpp:588
string path_get(const string &sub)
Definition path.cpp:368
string path_files_md5_hash(const string &dir)
Definition path.cpp:660
bool path_is_relative(const string &path)
Definition path.cpp:475
uint64_t path_modified_time(const string &path)
Definition path.cpp:818
string path_join(const string &dir, const string &file)
Definition path.cpp:436
#define DIR_SEP
Definition path.cpp:29
static void path_cache_kernel_mark_used(const string &path)
Definition path.cpp:1037
bool path_exists(const string &path)
Definition path.cpp:603
static string line_directive(const SourceReplaceState &state, const string &path, const size_t line_number)
Definition path.cpp:846
bool path_write_text(const string &path, const string &text)
Definition path.cpp:722
static const string & path_program_dir()
Definition path.cpp:349
bool path_cache_kernel_exists_and_mark_used(const string &path)
Definition path.cpp:1043
string path_escape(const string &path)
Definition path.cpp:458
string path_normalize(const string &path)
Definition path.cpp:465
void path_cache_kernel_mark_added_and_clear_old(const string &new_path, const size_t max_old_kernel_of_same_type)
Definition path.cpp:1052
bool path_write_binary(const string &path, const vector< uint8_t > &binary)
Definition path.cpp:702
static void path_files_md5_hash_recursive(MD5Hash &hash, const string &dir)
Definition path.cpp:640
bool path_read_text(const string &path, string &text)
Definition path.cpp:788
bool path_read_compressed_text(const string &path, string &text)
Definition path.cpp:803
bool path_is_file(const string &path)
Definition path.cpp:631
static bool create_directories_recursively(const string &path)
Definition path.cpp:670
string path_filename(const string &path)
Definition path.cpp:399
struct stat path_stat_t
Definition path.cpp:62
static char * path_specials(const string &sub)
Definition path.cpp:311
static string cached_path
Definition path.cpp:65
bool path_remove(const string &path)
Definition path.cpp:827
void path_init(const string &path, const string &user_path)
Definition path.cpp:355
bool path_read_binary(const string &path, vector< uint8_t > &binary)
Definition path.cpp:730
string string_from_wstring(const wstring &str)
Definition string.cpp:286
wstring string_to_wstring(const string &str)
Definition string.cpp:261
string string_strip(const string &s)
Definition string.cpp:139
bool string_startswith(const string_view s, const string_view start)
Definition string.cpp:106
void string_replace(string &haystack, const string &needle, const string &other)
Definition string.cpp:147
bool string_endswith(const string_view s, const string_view end)
Definition string.cpp:122
map< string, string > ProcessedMapping
Definition path.cpp:833
ProcessedMapping processed_files
Definition path.cpp:837
set< string > pragma_onced
Definition path.cpp:839