Welcome to mirror list, hosted at ThFree Co, Russian Federation.

appdir.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 965f48d1e5148399d28c3b2bb640d3b7e581555b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup bke
 *
 * Access to application level directories.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "BLI_fileops.h"
#include "BLI_fileops_types.h"
#include "BLI_listbase.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_string_utils.h"
#include "BLI_utildefines.h"

#include "BKE_appdir.h" /* own include */
#include "BKE_blender_version.h"

#include "BLT_translation.h"

#include "GHOST_Path-api.h"

#include "MEM_guardedalloc.h"

#include "CLG_log.h"

#ifdef WIN32
#  include "utf_winfunc.h"
#  include "utfconv.h"
#  include <io.h>
#  ifdef _WIN32_IE
#    undef _WIN32_IE
#  endif
#  define _WIN32_IE 0x0501
#  include "BLI_winstuff.h"
#  include <shlobj.h>
#  include <windows.h>
#else /* non windows */
#  ifdef WITH_BINRELOC
#    include "binreloc.h"
#  endif
/* #mkdtemp on OSX (and probably all *BSD?), not worth making specific check for this OS. */
#  include <unistd.h>
#endif /* WIN32 */

static const char _str_null[] = "(null)";
#define STR_OR_FALLBACK(a) ((a) ? (a) : _str_null)

/* -------------------------------------------------------------------- */
/** \name Local Variables
 * \{ */

/* local */
static CLG_LogRef LOG = {"bke.appdir"};

static struct {
  /** Full path to program executable. */
  char program_filepath[FILE_MAX];
  /** Full path to directory in which executable is located. */
  char program_dirname[FILE_MAX];
  /** Persistent temporary directory (defined by the preferences or OS). */
  char temp_dirname_base[FILE_MAX];
  /** Volatile temporary directory (owned by Blender, removed on exit). */
  char temp_dirname_session[FILE_MAX];
} g_app = {
    .temp_dirname_session = "",
};

/** \} */

/* -------------------------------------------------------------------- */
/** \name Initialization
 * \{ */

#ifndef NDEBUG
static bool is_appdir_init = false;
#  define ASSERT_IS_INIT() BLI_assert(is_appdir_init)
#else
#  define ASSERT_IS_INIT() ((void)0)
#endif

void BKE_appdir_init(void)
{
#ifndef NDEBUG
  BLI_assert(is_appdir_init == false);
  is_appdir_init = true;
#endif
}

void BKE_appdir_exit(void)
{
#ifndef NDEBUG
  BLI_assert(is_appdir_init == true);
  is_appdir_init = false;
#endif
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Internal Utilities
 * \{ */

/**
 * \returns a formatted representation of the specified version number. Non-re-entrant!
 */
static char *blender_version_decimal(const int version)
{
  static char version_str[5];
  BLI_assert(version < 1000);
  BLI_snprintf(version_str, sizeof(version_str), "%d.%d", version / 100, version % 100);
  return version_str;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Default Directories
 * \{ */

const char *BKE_appdir_folder_default(void)
{
#ifndef WIN32
  return BLI_getenv("HOME");
#else  /* Windows */
  static char documentfolder[MAXPATHLEN];

  if (BKE_appdir_folder_documents(documentfolder)) {
    return documentfolder;
  }

  return NULL;
#endif /* WIN32 */
}

const char *BKE_appdir_folder_root(void)
{
#ifndef WIN32
  return "/";
#else
  static char root[4];
  BLI_windows_get_default_root_dir(root);
  return root;
#endif
}

const char *BKE_appdir_folder_default_or_root(void)
{
  const char *path = BKE_appdir_folder_default();
  if (path == NULL) {
    path = BKE_appdir_folder_root();
  }
  return path;
}

const char *BKE_appdir_folder_home(void)
{
#ifdef WIN32
  return BLI_getenv("userprofile");
#elif defined(__APPLE__)
  return BLI_expand_tilde("~/");
#else
  return BLI_getenv("HOME");
#endif
}

bool BKE_appdir_folder_documents(char *dir)
{
  dir[0] = '\0';

  const char *documents_path = GHOST_getUserSpecialDir(GHOST_kUserSpecialDirDocuments);

  /* Usual case: Ghost gave us the documents path. We're done here. */
  if (documents_path && BLI_is_dir(documents_path)) {
    BLI_strncpy(dir, documents_path, FILE_MAXDIR);
    return true;
  }

  /* Ghost couldn't give us a documents path, let's try if we can find it ourselves. */

  const char *home_path = BKE_appdir_folder_home();
  if (!home_path || !BLI_is_dir(home_path)) {
    return false;
  }

  char try_documents_path[FILE_MAXDIR];
  /* Own attempt at getting a valid Documents path. */
  BLI_path_join(try_documents_path, sizeof(try_documents_path), home_path, N_("Documents"));
  if (!BLI_is_dir(try_documents_path)) {
    return false;
  }

  BLI_strncpy(dir, try_documents_path, FILE_MAXDIR);
  return true;
}

bool BKE_appdir_folder_caches(char *r_path, const size_t path_len)
{
  r_path[0] = '\0';

  const char *caches_root_path = GHOST_getUserSpecialDir(GHOST_kUserSpecialDirCaches);
  if (caches_root_path == NULL || !BLI_is_dir(caches_root_path)) {
    caches_root_path = BKE_tempdir_base();
  }
  if (caches_root_path == NULL || !BLI_is_dir(caches_root_path)) {
    return false;
  }

#ifdef WIN32
  BLI_path_join(
      r_path, path_len, caches_root_path, "Blender Foundation", "Blender", "Cache", SEP_STR);
#elif defined(__APPLE__)
  BLI_path_join(r_path, path_len, caches_root_path, "Blender", SEP_STR);
#else /* __linux__ */
  BLI_path_join(r_path, path_len, caches_root_path, "blender", SEP_STR);
#endif

  return true;
}

bool BKE_appdir_font_folder_default(char *dir)
{
  char test_dir[FILE_MAXDIR];
  test_dir[0] = '\0';

#ifdef WIN32
  wchar_t wpath[FILE_MAXDIR];
  if (SHGetSpecialFolderPathW(0, wpath, CSIDL_FONTS, 0)) {
    wcscat(wpath, L"\\");
    BLI_strncpy_wchar_as_utf8(test_dir, wpath, sizeof(test_dir));
  }
#elif defined(__APPLE__)
  STRNCPY(test_dir, BLI_expand_tilde("~/Library/Fonts/"));
  BLI_path_slash_ensure(test_dir);
#else
  STRNCPY(test_dir, "/usr/share/fonts");
#endif

  if (test_dir[0] && BLI_exists(test_dir)) {
    BLI_strncpy(dir, test_dir, FILE_MAXDIR);
    return true;
  }
  return false;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Path Presets (Internal Helpers)
 * \{ */

/**
 * Concatenates paths into \a targetpath,
 * returning true if result points to a directory.
 *
 * \param path_base: Path base, never NULL.
 * \param folder_name: First sub-directory (optional).
 * \param subfolder_name: Second sub-directory (optional).
 * \param check_is_dir: When false, return true even if the path doesn't exist.
 *
 * \note The names for optional paths only follow other usage in this file,
 * the names don't matter for this function.
 *
 * \note If it's useful we could take an arbitrary number of paths.
 * For now usage is limited and we don't need this.
 */
static bool test_path(char *targetpath,
                      size_t targetpath_len,
                      const bool check_is_dir,
                      const char *path_base,
                      const char *folder_name,
                      const char *subfolder_name)
{
  ASSERT_IS_INIT();

  /* Only the last argument should be NULL. */
  BLI_assert(!(folder_name == NULL && (subfolder_name != NULL)));
  const char *path_array[] = {path_base, folder_name, subfolder_name};
  const int path_array_num = (folder_name ? (subfolder_name ? 3 : 2) : 1);
  BLI_path_join_array(targetpath, targetpath_len, path_array, path_array_num);
  if (check_is_dir == false) {
    CLOG_INFO(&LOG, 3, "using without test: '%s'", targetpath);
    return true;
  }

  if (BLI_is_dir(targetpath)) {
    CLOG_INFO(&LOG, 3, "found '%s'", targetpath);
    return true;
  }

  CLOG_INFO(&LOG, 3, "missing '%s'", targetpath);

  /* Path not found, don't accidentally use it,
   * otherwise call this function with `check_is_dir` set to false. */
  targetpath[0] = '\0';
  return false;
}

/**
 * Puts the value of the specified environment variable into \a path if it exists.
 *
 * \param check_is_dir: When true, checks if it points at a directory.
 *
 * \returns true when the value of the environment variable is stored
 * at the address \a path points to.
 */
static bool test_env_path(char *path, const char *envvar, const bool check_is_dir)
{
  ASSERT_IS_INIT();

  const char *env_path = envvar ? BLI_getenv(envvar) : NULL;
  if (!env_path) {
    return false;
  }

  BLI_strncpy(path, env_path, FILE_MAX);

  if (check_is_dir == false) {
    CLOG_INFO(&LOG, 3, "using env '%s' without test: '%s'", envvar, env_path);
    return true;
  }

  if (BLI_is_dir(env_path)) {
    CLOG_INFO(&LOG, 3, "env '%s' found: %s", envvar, env_path);
    return true;
  }

  CLOG_INFO(&LOG, 3, "env '%s' missing: %s", envvar, env_path);

  /* Path not found, don't accidentally use it,
   * otherwise call this function with `check_is_dir` set to false. */
  path[0] = '\0';
  return false;
}

/**
 * Constructs in \a targetpath the name of a directory relative to a version-specific
 * sub-directory in the parent directory of the Blender executable.
 *
 * \param targetpath: String to return path.
 * \param folder_name: Optional folder name within version-specific directory.
 * \param subfolder_name: Optional sub-folder name within folder_name.
 *
 * \param version: To construct name of version-specific directory within #g_app.program_dirname.
 * \param check_is_dir: When false, return true even if the path doesn't exist.
 * \return true if such a directory exists.
 */
static bool get_path_local_ex(char *targetpath,
                              size_t targetpath_len,
                              const char *folder_name,
                              const char *subfolder_name,
                              const int version,
                              const bool check_is_dir)
{
  char relfolder[FILE_MAX];

  CLOG_INFO(&LOG,
            3,
            "folder='%s', subfolder='%s'",
            STR_OR_FALLBACK(folder_name),
            STR_OR_FALLBACK(subfolder_name));

  if (folder_name) { /* `subfolder_name` may be NULL. */
    const char *path_array[] = {folder_name, subfolder_name};
    const int path_array_num = subfolder_name ? 2 : 1;
    BLI_path_join_array(relfolder, sizeof(relfolder), path_array, path_array_num);
  }
  else {
    relfolder[0] = '\0';
  }

  /* Try `{g_app.program_dirname}/3.xx/{folder_name}` the default directory
   * for a portable distribution. See `WITH_INSTALL_PORTABLE` build-option. */
  const char *path_base = g_app.program_dirname;
#if defined(__APPLE__) && !defined(WITH_PYTHON_MODULE)
  /* Due new code-sign situation in OSX > 10.9.5
   * we must move the blender_version dir with contents to Resources.
   * Add 4 + 9 for the temporary `/../` path & `Resources`. */
  char osx_resourses[FILE_MAX + 4 + 9];
  BLI_path_join(osx_resourses, sizeof(osx_resourses), g_app.program_dirname, "..", "Resources");
  /* Remove the '/../' added above. */
  BLI_path_normalize(NULL, osx_resourses);
  path_base = osx_resourses;
#endif
  return test_path(targetpath,
                   targetpath_len,
                   check_is_dir,
                   path_base,
                   blender_version_decimal(version),
                   relfolder);
}
static bool get_path_local(char *targetpath,
                           size_t targetpath_len,
                           const char *folder_name,
                           const char *subfolder_name)
{
  const int version = BLENDER_VERSION;
  const bool check_is_dir = true;
  return get_path_local_ex(
      targetpath, targetpath_len, folder_name, subfolder_name, version, check_is_dir);
}

bool BKE_appdir_app_is_portable_install(void)
{
  /* Detect portable install by the existence of `config` folder. */
  char path[FILE_MAX];
  return get_path_local(path, sizeof(path), "config", NULL);
}

/**
 * Returns the path of a folder from environment variables.
 *
 * \param targetpath: String to return path.
 * \param subfolder_name: optional name of sub-folder within folder.
 * \param envvar: name of environment variable to check folder_name.
 * \param check_is_dir: When false, return true even if the path doesn't exist.
 * \return true if it was able to construct such a path and the path exists.
 */
static bool get_path_environment_ex(char *targetpath,
                                    size_t targetpath_len,
                                    const char *subfolder_name,
                                    const char *envvar,
                                    const bool check_is_dir)
{
  char user_path[FILE_MAX];

  if (test_env_path(user_path, envvar, check_is_dir)) {
    /* Note that `subfolder_name` may be NULL, in this case we use `user_path` as-is. */
    return test_path(targetpath, targetpath_len, check_is_dir, user_path, subfolder_name, NULL);
  }
  return false;
}
static bool get_path_environment(char *targetpath,
                                 size_t targetpath_len,
                                 const char *subfolder_name,
                                 const char *envvar)
{
  const bool check_is_dir = true;
  return get_path_environment_ex(targetpath, targetpath_len, subfolder_name, envvar, check_is_dir);
}

/**
 * Returns the path of a folder within the user-files area.
 *
 * \param targetpath: String to return path.
 * \param folder_name: default name of folder within user area.
 * \param subfolder_name: optional name of sub-folder within folder.
 * \param version: Blender version, used to construct a sub-directory name.
 * \param check_is_dir: When false, return true even if the path doesn't exist.
 * \return true if it was able to construct such a path.
 */
static bool get_path_user_ex(char *targetpath,
                             size_t targetpath_len,
                             const char *folder_name,
                             const char *subfolder_name,
                             const int version,
                             const bool check_is_dir)
{
  char user_path[FILE_MAX];

  if (test_env_path(user_path, "BLENDER_USER_RESOURCES", check_is_dir)) {
    /* Pass. */
  }
  else {
    /* for portable install, user path is always local */
    if (BKE_appdir_app_is_portable_install()) {
      return get_path_local_ex(
          targetpath, targetpath_len, folder_name, subfolder_name, version, check_is_dir);
    }
    user_path[0] = '\0';

    const char *user_base_path = GHOST_getUserDir(version, blender_version_decimal(version));
    if (user_base_path) {
      BLI_strncpy(user_path, user_base_path, FILE_MAX);
    }
  }

  if (!user_path[0]) {
    return false;
  }

  CLOG_INFO(&LOG,
            3,
            "'%s', folder='%s', subfolder='%s'",
            user_path,
            STR_OR_FALLBACK(folder_name),
            STR_OR_FALLBACK(subfolder_name));

  /* `subfolder_name` may be NULL. */
  return test_path(
      targetpath, targetpath_len, check_is_dir, user_path, folder_name, subfolder_name);
}
static bool get_path_user(char *targetpath,
                          size_t targetpath_len,
                          const char *folder_name,
                          const char *subfolder_name)
{
  const int version = BLENDER_VERSION;
  const bool check_is_dir = true;
  return get_path_user_ex(
      targetpath, targetpath_len, folder_name, subfolder_name, version, check_is_dir);
}

/**
 * Returns the path of a folder within the Blender installation directory.
 *
 * \param targetpath: String to return path.
 * \param folder_name: default name of folder within installation area.
 * \param subfolder_name: optional name of sub-folder within folder.
 * \param version: Blender version, used to construct a sub-directory name.
 * \param check_is_dir: When false, return true even if the path doesn't exist.
 * \return true if it was able to construct such a path.
 */
static bool get_path_system_ex(char *targetpath,
                               size_t targetpath_len,
                               const char *folder_name,
                               const char *subfolder_name,
                               const int version,
                               const bool check_is_dir)
{
  char system_path[FILE_MAX];
  char relfolder[FILE_MAX];

  if (folder_name) { /* `subfolder_name` may be NULL. */
    const char *path_array[] = {folder_name, subfolder_name};
    const int path_array_num = subfolder_name ? 2 : 1;
    BLI_path_join_array(relfolder, sizeof(relfolder), path_array, path_array_num);
  }
  else {
    relfolder[0] = '\0';
  }

  if (test_env_path(system_path, "BLENDER_SYSTEM_RESOURCES", check_is_dir)) {
    /* Pass. */
  }
  else {
    system_path[0] = '\0';
    const char *system_base_path = GHOST_getSystemDir(version, blender_version_decimal(version));
    if (system_base_path) {
      BLI_strncpy(system_path, system_base_path, FILE_MAX);
    }
  }

  if (!system_path[0]) {
    return false;
  }

  CLOG_INFO(&LOG,
            3,
            "'%s', folder='%s', subfolder='%s'",
            system_path,
            STR_OR_FALLBACK(folder_name),
            STR_OR_FALLBACK(subfolder_name));

  /* Try `$BLENDERPATH/folder_name/subfolder_name`, `subfolder_name` may be NULL. */
  return test_path(
      targetpath, targetpath_len, check_is_dir, system_path, folder_name, subfolder_name);
}

static bool get_path_system(char *targetpath,
                            size_t targetpath_len,
                            const char *folder_name,
                            const char *subfolder_name)
{
  const int version = BLENDER_VERSION;
  const bool check_is_dir = true;
  return get_path_system_ex(
      targetpath, targetpath_len, folder_name, subfolder_name, version, check_is_dir);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Path Presets API
 * \{ */

bool BKE_appdir_folder_id_ex(const int folder_id,
                             const char *subfolder,
                             char *path,
                             size_t path_len)
{
  switch (folder_id) {
    case BLENDER_DATAFILES: /* general case */
      if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES")) {
        break;
      }
      if (get_path_user(path, path_len, "datafiles", subfolder)) {
        break;
      }
      if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_DATAFILES")) {
        break;
      }
      if (get_path_local(path, path_len, "datafiles", subfolder)) {
        break;
      }
      if (get_path_system(path, path_len, "datafiles", subfolder)) {
        break;
      }
      return false;

    case BLENDER_USER_DATAFILES:
      if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES")) {
        break;
      }
      if (get_path_user(path, path_len, "datafiles", subfolder)) {
        break;
      }
      return false;

    case BLENDER_SYSTEM_DATAFILES:
      if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_DATAFILES")) {
        break;
      }
      if (get_path_system(path, path_len, "datafiles", subfolder)) {
        break;
      }
      if (get_path_local(path, path_len, "datafiles", subfolder)) {
        break;
      }
      return false;

    case BLENDER_USER_AUTOSAVE:
      if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES")) {
        break;
      }
      if (get_path_user(path, path_len, "autosave", subfolder)) {
        break;
      }
      return false;

    case BLENDER_USER_CONFIG:
      if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_CONFIG")) {
        break;
      }
      if (get_path_user(path, path_len, "config", subfolder)) {
        break;
      }
      return false;

    case BLENDER_USER_SCRIPTS:
      if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_SCRIPTS")) {
        break;
      }
      if (get_path_user(path, path_len, "scripts", subfolder)) {
        break;
      }
      return false;

    case BLENDER_SYSTEM_SCRIPTS:
      if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_SCRIPTS")) {
        break;
      }
      if (get_path_system(path, path_len, "scripts", subfolder)) {
        break;
      }
      if (get_path_local(path, path_len, "scripts", subfolder)) {
        break;
      }
      return false;

    case BLENDER_SYSTEM_PYTHON:
      if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_PYTHON")) {
        break;
      }
      if (get_path_system(path, path_len, "python", subfolder)) {
        break;
      }
      if (get_path_local(path, path_len, "python", subfolder)) {
        break;
      }
      return false;

    default:
      BLI_assert_unreachable();
      break;
  }

  return true;
}

const char *BKE_appdir_folder_id(const int folder_id, const char *subfolder)
{
  static char path[FILE_MAX] = "";
  if (BKE_appdir_folder_id_ex(folder_id, subfolder, path, sizeof(path))) {
    return path;
  }
  return NULL;
}

const char *BKE_appdir_folder_id_user_notest(const int folder_id, const char *subfolder)
{
  const int version = BLENDER_VERSION;
  static char path[FILE_MAX] = "";
  const bool check_is_dir = false;

  switch (folder_id) {
    case BLENDER_USER_DATAFILES:
      if (get_path_environment_ex(
              path, sizeof(path), subfolder, "BLENDER_USER_DATAFILES", check_is_dir)) {
        break;
      }
      get_path_user_ex(path, sizeof(path), "datafiles", subfolder, version, check_is_dir);
      break;
    case BLENDER_USER_CONFIG:
      if (get_path_environment_ex(
              path, sizeof(path), subfolder, "BLENDER_USER_CONFIG", check_is_dir)) {
        break;
      }
      get_path_user_ex(path, sizeof(path), "config", subfolder, version, check_is_dir);
      break;
    case BLENDER_USER_AUTOSAVE:
      if (get_path_environment_ex(
              path, sizeof(path), subfolder, "BLENDER_USER_AUTOSAVE", check_is_dir)) {
        break;
      }
      get_path_user_ex(path, sizeof(path), "autosave", subfolder, version, check_is_dir);
      break;
    case BLENDER_USER_SCRIPTS:
      if (get_path_environment_ex(
              path, sizeof(path), subfolder, "BLENDER_USER_SCRIPTS", check_is_dir)) {
        break;
      }
      get_path_user_ex(path, sizeof(path), "scripts", subfolder, version, check_is_dir);
      break;
    default:
      BLI_assert_unreachable();
      break;
  }

  if ('\0' == path[0]) {
    return NULL;
  }
  return path;
}

const char *BKE_appdir_folder_id_create(const int folder_id, const char *subfolder)
{
  const char *path;

  /* Only for user folders. */
  if (!ELEM(folder_id,
            BLENDER_USER_DATAFILES,
            BLENDER_USER_CONFIG,
            BLENDER_USER_SCRIPTS,
            BLENDER_USER_AUTOSAVE)) {
    BLI_assert_unreachable();
    return NULL;
  }

  path = BKE_appdir_folder_id(folder_id, subfolder);

  if (!path) {
    path = BKE_appdir_folder_id_user_notest(folder_id, subfolder);
    if (path) {
      BLI_dir_create_recursive(path);
    }
  }

  return path;
}

const char *BKE_appdir_resource_path_id_with_version(const int folder_id,
                                                     const bool check_is_dir,
                                                     const int version)
{
  static char path[FILE_MAX] = "";
  bool ok;
  switch (folder_id) {
    case BLENDER_RESOURCE_PATH_USER:
      ok = get_path_user_ex(path, sizeof(path), NULL, NULL, version, check_is_dir);
      break;
    case BLENDER_RESOURCE_PATH_LOCAL:
      ok = get_path_local_ex(path, sizeof(path), NULL, NULL, version, check_is_dir);
      break;
    case BLENDER_RESOURCE_PATH_SYSTEM:
      ok = get_path_system_ex(path, sizeof(path), NULL, NULL, version, check_is_dir);
      break;
    default:
      path[0] = '\0'; /* in case check_is_dir is false */
      ok = false;
      BLI_assert_msg(0, "incorrect ID");
      break;
  }
  return ok ? path : NULL;
}

const char *BKE_appdir_resource_path_id(const int folder_id, const bool check_is_dir)
{
  return BKE_appdir_resource_path_id_with_version(folder_id, check_is_dir, BLENDER_VERSION);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Program Path Queries
 *
 * Access locations of Blender & Python.
 * \{ */

#ifndef WITH_PYTHON_MODULE
/**
 * Checks if name is a fully qualified filename to an executable.
 * If not it searches `$PATH` for the file. On Windows it also
 * adds the correct extension (`.com` `.exe` etc) from
 * `$PATHEXT` if necessary. Also on Windows it translates
 * the name to its 8.3 version to prevent problems with
 * spaces and stuff. Final result is returned in \a fullname.
 *
 * \param fullname: The full path and full name of the executable
 * (must be #FILE_MAX minimum)
 * \param name: The name of the executable (usually `argv[0]`) to be checked
 */
static void where_am_i(char *fullname, const size_t maxlen, const char *name)
{
#  ifdef WITH_BINRELOC
  /* Linux uses `binreloc` since `argv[0]` is not reliable, call `br_init(NULL)` first. */
  {
    const char *path = NULL;
    path = br_find_exe(NULL);
    if (path) {
      BLI_strncpy(fullname, path, maxlen);
      free((void *)path);
      return;
    }
  }
#  endif

#  ifdef _WIN32
  {
    wchar_t *fullname_16 = MEM_mallocN(maxlen * sizeof(wchar_t), "ProgramPath");
    if (GetModuleFileNameW(0, fullname_16, maxlen)) {
      conv_utf_16_to_8(fullname_16, fullname, maxlen);
      if (!BLI_exists(fullname)) {
        CLOG_ERROR(&LOG, "path can't be found: \"%.*s\"", (int)maxlen, fullname);
        MessageBox(
            NULL, "path contains invalid characters or is too long (see console)", "Error", MB_OK);
      }
      MEM_freeN(fullname_16);
      return;
    }

    MEM_freeN(fullname_16);
  }
#  endif

  /* Unix and non Linux. */
  if (name && name[0]) {

    BLI_strncpy(fullname, name, maxlen);
    if (name[0] == '.') {
      BLI_path_abs_from_cwd(fullname, maxlen);
#  ifdef _WIN32
      BLI_path_program_extensions_add_win32(fullname, maxlen);
#  endif
    }
    else if (BLI_path_slash_rfind(name)) {
      /* Full path. */
      BLI_strncpy(fullname, name, maxlen);
#  ifdef _WIN32
      BLI_path_program_extensions_add_win32(fullname, maxlen);
#  endif
    }
    else {
      BLI_path_program_search(fullname, maxlen, name);
    }
    /* Remove "/./" and "/../" so string comparisons can be used on the path. */
    BLI_path_normalize(NULL, fullname);

#  if defined(DEBUG)
    if (!STREQ(name, fullname)) {
      CLOG_INFO(&LOG, 2, "guessing '%s' == '%s'", name, fullname);
    }
#  endif
  }
}
#endif /* WITH_PYTHON_MODULE */

void BKE_appdir_program_path_init(const char *argv0)
{
#ifdef WITH_PYTHON_MODULE
  /* NOTE(@campbellbarton): Always use `argv[0]` as is, when building as a Python module.
   * Otherwise other methods of detecting the binary that override this argument
   * which must point to the Python module for data-files to be detected. */
  STRNCPY(g_app.program_filepath, argv0);
  BLI_path_abs_from_cwd(g_app.program_filepath, sizeof(g_app.program_filepath));
  BLI_path_normalize(NULL, g_app.program_filepath);

  if (g_app.program_dirname[0] == '\0') {
    /* First time initializing, the file binary path isn't valid from a Python module.
     * Calling again must set the `filepath` and leave the directory as-is. */
    BLI_split_dir_part(
        g_app.program_filepath, g_app.program_dirname, sizeof(g_app.program_dirname));
    g_app.program_filepath[0] = '\0';
  }
#else
  where_am_i(g_app.program_filepath, sizeof(g_app.program_filepath), argv0);
  BLI_split_dir_part(g_app.program_filepath, g_app.program_dirname, sizeof(g_app.program_dirname));
#endif
}

const char *BKE_appdir_program_path(void)
{
#ifndef WITH_PYTHON_MODULE /* Default's to empty when building as a Python module. */
  BLI_assert(g_app.program_filepath[0]);
#endif
  return g_app.program_filepath;
}

const char *BKE_appdir_program_dir(void)
{
  BLI_assert(g_app.program_dirname[0]);
  return g_app.program_dirname;
}

bool BKE_appdir_program_python_search(char *fullpath,
                                      const size_t fullpath_len,
                                      const int version_major,
                                      const int version_minor)
{
  ASSERT_IS_INIT();

#ifdef PYTHON_EXECUTABLE_NAME
  /* Passed in from the build-systems 'PYTHON_EXECUTABLE'. */
  const char *python_build_def = STRINGIFY(PYTHON_EXECUTABLE_NAME);
#endif
  const char *basename = "python";
#if defined(WIN32) && !defined(NDEBUG)
  const char *basename_debug = "python_d";
#endif
  char python_version[16];
  /* Check both possible names. */
  const char *python_names[] = {
#ifdef PYTHON_EXECUTABLE_NAME
    python_build_def,
#endif
#if defined(WIN32) && !defined(NDEBUG)
    basename_debug,
#endif
    python_version,
    basename,
  };
  bool is_found = false;

  SNPRINTF(python_version, "%s%d.%d", basename, version_major, version_minor);

  {
    const char *python_bin_dir = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON, "bin");
    if (python_bin_dir) {

      for (int i = 0; i < ARRAY_SIZE(python_names); i++) {
        BLI_path_join(fullpath, fullpath_len, python_bin_dir, python_names[i]);

        if (
#ifdef _WIN32
            BLI_path_program_extensions_add_win32(fullpath, fullpath_len)
#else
            BLI_exists(fullpath)
#endif
        ) {
          is_found = true;
          break;
        }
      }
    }
  }

  if (is_found == false) {
    for (int i = 0; i < ARRAY_SIZE(python_names); i++) {
      if (BLI_path_program_search(fullpath, fullpath_len, python_names[i])) {
        is_found = true;
        break;
      }
    }
  }

  if (is_found == false) {
    *fullpath = '\0';
  }

  return is_found;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Application Templates
 * \{ */

/** Keep in sync with `bpy.utils.app_template_paths()` */
static const char *app_template_directory_search[2] = {
    "startup" SEP_STR "bl_app_templates_user",
    "startup" SEP_STR "bl_app_templates_system",
};

static const int app_template_directory_id[2] = {
    /* Only 'USER' */
    BLENDER_USER_SCRIPTS,
    /* Covers 'LOCAL' & 'SYSTEM'. */
    BLENDER_SYSTEM_SCRIPTS,
};

bool BKE_appdir_app_template_any(void)
{
  char temp_dir[FILE_MAX];
  for (int i = 0; i < ARRAY_SIZE(app_template_directory_id); i++) {
    if (BKE_appdir_folder_id_ex(app_template_directory_id[i],
                                app_template_directory_search[i],
                                temp_dir,
                                sizeof(temp_dir))) {
      return true;
    }
  }
  return false;
}

bool BKE_appdir_app_template_id_search(const char *app_template, char *path, size_t path_len)
{
  for (int i = 0; i < ARRAY_SIZE(app_template_directory_id); i++) {
    char subdir[FILE_MAX];
    BLI_path_join(subdir, sizeof(subdir), app_template_directory_search[i], app_template);
    if (BKE_appdir_folder_id_ex(app_template_directory_id[i], subdir, path, path_len)) {
      return true;
    }
  }
  return false;
}

bool BKE_appdir_app_template_has_userpref(const char *app_template)
{
  /* Test if app template provides a `userpref.blend`.
   * If not, we will share user preferences with the rest of Blender. */
  if (app_template[0] == '\0') {
    return false;
  }

  char app_template_path[FILE_MAX];
  if (!BKE_appdir_app_template_id_search(
          app_template, app_template_path, sizeof(app_template_path))) {
    return false;
  }

  char userpref_path[FILE_MAX];
  BLI_path_join(userpref_path, sizeof(userpref_path), app_template_path, BLENDER_USERPREF_FILE);
  return BLI_exists(userpref_path);
}

void BKE_appdir_app_templates(ListBase *templates)
{
  BLI_listbase_clear(templates);

  for (int i = 0; i < ARRAY_SIZE(app_template_directory_id); i++) {
    char subdir[FILE_MAX];
    if (!BKE_appdir_folder_id_ex(app_template_directory_id[i],
                                 app_template_directory_search[i],
                                 subdir,
                                 sizeof(subdir))) {
      continue;
    }

    struct direntry *dirs;
    const uint dir_num = BLI_filelist_dir_contents(subdir, &dirs);
    for (int f = 0; f < dir_num; f++) {
      if (!FILENAME_IS_CURRPAR(dirs[f].relname) && S_ISDIR(dirs[f].type)) {
        char *template = BLI_strdup(dirs[f].relname);
        BLI_addtail(templates, BLI_genericNodeN(template));
      }
    }

    BLI_filelist_free(dirs, dir_num);
  }
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Temporary Directories
 * \{ */

/**
 * Gets the temp directory when blender first runs.
 * If the default path is not found, use try $TEMP
 *
 * Also make sure the temp dir has a trailing slash
 *
 * \param tempdir: The full path to the temporary temp directory.
 * \param tempdir_len: The size of the \a tempdir buffer.
 * \param userdir: Directory specified in user preferences (may be NULL).
 * note that by default this is an empty string, only use when non-empty.
 */
static void where_is_temp(char *tempdir, const size_t tempdir_len, const char *userdir)
{

  tempdir[0] = '\0';

  if (userdir && BLI_is_dir(userdir)) {
    BLI_strncpy(tempdir, userdir, tempdir_len);
  }

  if (tempdir[0] == '\0') {
    const char *env_vars[] = {
#ifdef WIN32
        "TEMP",
#else
        /* Non standard (could be removed). */
        "TMP",
        /* Posix standard. */
        "TMPDIR",
#endif
    };
    for (int i = 0; i < ARRAY_SIZE(env_vars); i++) {
      const char *tmp = BLI_getenv(env_vars[i]);
      if (tmp && (tmp[0] != '\0') && BLI_is_dir(tmp)) {
        BLI_strncpy(tempdir, tmp, tempdir_len);
        break;
      }
    }
  }

  if (tempdir[0] == '\0') {
    BLI_strncpy(tempdir, "/tmp/", tempdir_len);
  }
  else {
    /* add a trailing slash if needed */
    BLI_path_slash_ensure(tempdir);
  }
}

static void tempdir_session_create(char *tempdir_session,
                                   const size_t tempdir_session_len,
                                   const char *tempdir)
{
  tempdir_session[0] = '\0';

  const int tempdir_len = strlen(tempdir);
  /* 'XXXXXX' is kind of tag to be replaced by `mktemp-family` by an UUID. */
  const char *session_name = "blender_XXXXXX";
  const int session_name_len = strlen(session_name);

  /* +1 as a slash is added,
   * #_mktemp_s also requires the last null character is included. */
  const int tempdir_session_len_required = tempdir_len + session_name_len + 1;

  if (tempdir_session_len_required <= tempdir_session_len) {
    /* No need to use path joining utility as we know the last character of #tempdir is a slash. */
    BLI_string_join(tempdir_session, tempdir_session_len, tempdir, session_name);
#ifdef WIN32
    const bool needs_create = (_mktemp_s(tempdir_session, tempdir_session_len_required) == 0);
#else
    const bool needs_create = (mkdtemp(tempdir_session) == NULL);
#endif
    if (needs_create) {
      BLI_dir_create_recursive(tempdir_session);
    }
    if (BLI_is_dir(tempdir_session)) {
      BLI_path_slash_ensure(tempdir_session);
      /* Success. */
      return;
    }
  }

  CLOG_WARN(&LOG,
            "Could not generate a temp file name for '%s', falling back to '%s'",
            tempdir_session,
            tempdir);
  BLI_strncpy(tempdir_session, tempdir, tempdir_session_len);
}

void BKE_tempdir_init(const char *userdir)
{
  /* Sets #g_app.temp_dirname_base to \a userdir if specified and is a valid directory,
   * otherwise chooses a suitable OS-specific temporary directory.
   * Sets #g_app.temp_dirname_session to a #mkdtemp
   * generated sub-dir of #g_app.temp_dirname_base. */

  where_is_temp(g_app.temp_dirname_base, sizeof(g_app.temp_dirname_base), userdir);

  /* Clear existing temp dir, if needed. */
  BKE_tempdir_session_purge();
  /* Now that we have a valid temp dir, add system-generated unique sub-dir. */
  tempdir_session_create(
      g_app.temp_dirname_session, sizeof(g_app.temp_dirname_session), g_app.temp_dirname_base);
}

const char *BKE_tempdir_session(void)
{
  return g_app.temp_dirname_session[0] ? g_app.temp_dirname_session : BKE_tempdir_base();
}

const char *BKE_tempdir_base(void)
{
  return g_app.temp_dirname_base;
}

void BKE_tempdir_session_purge(void)
{
  if (g_app.temp_dirname_session[0] && BLI_is_dir(g_app.temp_dirname_session)) {
    BLI_delete(g_app.temp_dirname_session, true, true);
  }
}

/** \} */