Blender V4.3
appdir.cc File Reference
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "BLI_fileops.h"
#include "BLI_fileops_types.h"
#include "BLI_listbase.h"
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_string_utils.hh"
#include "BLI_tempfile.h"
#include "BLI_utildefines.h"
#include "BLI_vector.hh"
#include "BKE_appdir.hh"
#include "BKE_blender_version.h"
#include "BLT_translation.hh"
#include "GHOST_Path-api.hh"
#include "MEM_guardedalloc.h"
#include "CLG_log.h"
#include <unistd.h>
#include <pwd.h>

Go to the source code of this file.

Macros

#define STR_OR_FALLBACK(a)   ((a) ? (a) : _str_null)
 

Functions

Internal Utilities
static char * blender_version_decimal (const int version)
 
Default Directories
const char * BKE_appdir_folder_default ()
 
const char * BKE_appdir_folder_root ()
 
const char * BKE_appdir_folder_default_or_root ()
 
const char * BKE_appdir_folder_home ()
 
bool BKE_appdir_folder_documents (char *dir)
 
bool BKE_appdir_folder_caches (char *path, const size_t path_maxncpy)
 
bool BKE_appdir_font_folder_default (char *dir, size_t dir_maxncpy)
 
Path Presets (Internal Helpers)
static bool test_path (char *targetpath, size_t targetpath_maxncpy, const bool check_is_dir, const char *path_base, const char *folder_name, const char *subfolder_name)
 
static bool test_env_path (char *path, const char *envvar, const bool check_is_dir)
 
static bool get_path_local_ex (char *targetpath, size_t targetpath_maxncpy, const char *folder_name, const char *subfolder_name, const int version, const bool check_is_dir)
 
static bool get_path_local (char *targetpath, size_t targetpath_maxncpy, const char *folder_name, const char *subfolder_name)
 
static bool get_path_environment_ex (char *targetpath, size_t targetpath_maxncpy, const char *subfolder_name, const char *envvar, const bool check_is_dir)
 
static bool get_path_environment (char *targetpath, size_t targetpath_maxncpy, const char *subfolder_name, const char *envvar)
 
static bool get_path_user_ex (char *targetpath, size_t targetpath_maxncpy, const char *folder_name, const char *subfolder_name, const int version, const bool check_is_dir)
 
static bool get_path_user (char *targetpath, size_t targetpath_maxncpy, const char *folder_name, const char *subfolder_name)
 
static bool get_path_system_ex (char *targetpath, size_t targetpath_maxncpy, const char *folder_name, const char *subfolder_name, const int version, const bool check_is_dir)
 
static bool get_path_system (char *targetpath, size_t targetpath_maxncpy, const char *folder_name, const char *subfolder_name)
 
Path Presets API
bool BKE_appdir_folder_id_ex (const int folder_id, const char *subfolder, char *path, size_t path_maxncpy)
 
std::optional< std::string > BKE_appdir_folder_id (const int folder_id, const char *subfolder)
 
std::optional< std::string > BKE_appdir_folder_id_user_notest (const int folder_id, const char *subfolder)
 
std::optional< std::string > BKE_appdir_folder_id_create (const int folder_id, const char *subfolder)
 
std::optional< std::string > BKE_appdir_resource_path_id_with_version (const int folder_id, const bool check_is_dir, const int version)
 
std::optional< std::string > BKE_appdir_resource_path_id (const int folder_id, const bool check_is_dir)
 
Program Path Queries

Access locations of Blender & Python.

static void where_am_i (char *program_filepath, const size_t program_filepath_maxncpy, const char *program_name)
 
void BKE_appdir_program_path_init (const char *argv0)
 
const char * BKE_appdir_program_path ()
 
const char * BKE_appdir_program_dir ()
 
bool BKE_appdir_program_python_search (char *program_filepath, const size_t program_filepath_maxncpy, const int version_major, const int version_minor)
 
Application Templates
static blender::Vector< std::string > appdir_app_template_directories ()
 
bool BKE_appdir_app_template_any ()
 
bool BKE_appdir_app_template_id_search (const char *app_template, char *path, size_t path_maxncpy)
 
bool BKE_appdir_app_template_has_userpref (const char *app_template)
 
void BKE_appdir_app_templates (ListBase *templates)
 
Temporary Directories
static void where_is_temp (char *tempdir, const size_t tempdir_maxncpy, const char *userdir)
 
static void tempdir_session_create (char *tempdir_session, const size_t tempdir_session_maxncpy, const char *tempdir)
 
void BKE_tempdir_init (const char *userdir)
 
const char * BKE_tempdir_session ()
 
const char * BKE_tempdir_base ()
 
void BKE_tempdir_session_purge ()
 

Variables

static const char _str_null [] = "(null)"
 
Local Variables
static CLG_LogRef LOG = {"bke.appdir"}
 
struct { 
 
   char   program_filepath [FILE_MAX
 
   char   program_dirname [FILE_MAX
 
   char   temp_dirname_base [FILE_MAX
 
   char   temp_dirname_session [FILE_MAX
 
g_app 
 

Initialization

#define ASSERT_IS_INIT()   BLI_assert(is_appdir_init)
 
static bool is_appdir_init = false
 
void BKE_appdir_init ()
 
void BKE_appdir_exit ()
 

Detailed Description

Access to application level directories.

Definition in file appdir.cc.

Macro Definition Documentation

◆ ASSERT_IS_INIT

#define ASSERT_IS_INIT ( )    BLI_assert(is_appdir_init)

Definition at line 88 of file appdir.cc.

Referenced by BKE_appdir_program_python_search(), test_env_path(), and test_path().

◆ STR_OR_FALLBACK

#define STR_OR_FALLBACK ( a)    ((a) ? (a) : _str_null)

Definition at line 60 of file appdir.cc.

Referenced by get_path_local_ex(), get_path_system_ex(), and get_path_user_ex().

Function Documentation

◆ appdir_app_template_directories()

static blender::Vector< std::string > appdir_app_template_directories ( )
static

◆ BKE_appdir_app_template_any()

bool BKE_appdir_app_template_any ( )

Return true if templates exist

Definition at line 1066 of file appdir.cc.

References appdir_app_template_directories(), and blender::Vector< T, InlineBufferCapacity, Allocator >::is_empty().

Referenced by wm_file_read_post().

◆ BKE_appdir_app_template_has_userpref()

bool BKE_appdir_app_template_has_userpref ( const char * app_template)

◆ BKE_appdir_app_template_id_search()

bool BKE_appdir_app_template_id_search ( const char * app_template,
char * path,
size_t path_maxncpy )

◆ BKE_appdir_app_templates()

◆ BKE_appdir_exit()

◆ BKE_appdir_folder_caches()

bool BKE_appdir_folder_caches ( char * path,
size_t path_maxncpy )

Get the user's cache directory, i.e.

  • Linux: $HOME/.cache/blender/
  • Windows: USERPROFILE%\AppData\Local\Blender Foundation\Blender\
  • MacOS: /Library/Caches/Blender
Returns
True if the path is valid. It doesn't create or checks format if the blender folder exists. It does check if the parent of the path exists.

Definition at line 226 of file appdir.cc.

◆ BKE_appdir_folder_default()

const char * BKE_appdir_folder_default ( )

Get the folder that's the "natural" starting point for browsing files on an OS.

  • Unix: $HOME
  • Windows: userprofile%/Documents
Note
On Windows Users/{MyUserName}/Documents is used as it's the default location to save documents.

Definition at line 137 of file appdir.cc.

References BKE_appdir_folder_documents(), BKE_appdir_folder_home(), and FILE_MAXDIR.

Referenced by BKE_appdir_folder_default_or_root(), blo_update_defaults_screen(), and fileselect_initialize_params_common().

◆ BKE_appdir_folder_default_or_root()

const char * BKE_appdir_folder_default_or_root ( )

Definition at line 163 of file appdir.cc.

References BKE_appdir_folder_default(), and BKE_appdir_folder_root().

Referenced by file_browse_invoke(), and file_expand_directory().

◆ BKE_appdir_folder_documents()

bool BKE_appdir_folder_documents ( char * dir)

Get the user's document directory, i.e.

  • Linux: $HOME/Documents
  • Windows: userprofile%/Documents

If this can't be found using OS queries (via Ghost), try manually finding it.

Returns
True if the path is valid and points to an existing directory.

Definition at line 196 of file appdir.cc.

References BKE_appdir_folder_home(), BLI_is_dir(), BLI_path_join, BLI_strncpy(), FILE_MAXDIR, GHOST_getUserSpecialDir(), GHOST_kUserSpecialDirDocuments, and N_.

Referenced by BKE_appdir_folder_default(), and BKE_preferences_asset_library_default_add().

◆ BKE_appdir_folder_home()

const char * BKE_appdir_folder_home ( )

Get the user's home directory, i.e.

  • Unix: $HOME
  • Windows: userprofile%

Definition at line 172 of file appdir.cc.

References BLI_expand_tilde(), and BLI_getenv().

Referenced by BKE_appdir_folder_default(), BKE_appdir_folder_documents(), and file_expand_directory().

◆ BKE_appdir_folder_id()

◆ BKE_appdir_folder_id_create()

◆ BKE_appdir_folder_id_ex()

bool BKE_appdir_folder_id_ex ( int folder_id,
const char * subfolder,
char * path,
size_t path_maxncpy )

Get a folder out of the folder_id presets for paths.

Parameters
subfolderThe name of a directory to check for, this may contain path separators but must resolve to a directory, checked with BLI_is_dir.
Returns
The path if found, NULL string if not.

Definition at line 591 of file appdir.cc.

References BLENDER_DATAFILES, BLENDER_SYSTEM_DATAFILES, BLENDER_SYSTEM_EXTENSIONS, BLENDER_SYSTEM_PYTHON, BLENDER_SYSTEM_SCRIPTS, BLENDER_USER_CONFIG, BLENDER_USER_DATAFILES, BLENDER_USER_EXTENSIONS, BLENDER_USER_SCRIPTS, BLI_assert_unreachable, get_path_environment(), get_path_local(), get_path_system(), and get_path_user().

Referenced by appdir_app_template_directories(), and BKE_appdir_folder_id().

◆ BKE_appdir_folder_id_user_notest()

std::optional< std::string > BKE_appdir_folder_id_user_notest ( int folder_id,
const char * subfolder )

◆ BKE_appdir_folder_root()

const char * BKE_appdir_folder_root ( )

Definition at line 152 of file appdir.cc.

References BLI_windows_get_default_root_dir().

Referenced by BKE_appdir_folder_default_or_root().

◆ BKE_appdir_font_folder_default()

bool BKE_appdir_font_folder_default ( char * dir,
size_t dir_maxncpy )

Gets a good default directory for fonts.

Definition at line 250 of file appdir.cc.

References BLI_exists(), BLI_expand_tilde(), BLI_strncpy(), BLI_strncpy_wchar_as_utf8(), FILE_MAXDIR, and STRNCPY.

Referenced by BKE_blendfile_userdef_from_defaults(), and file_browse_invoke().

◆ BKE_appdir_init()

void BKE_appdir_init ( )

Sanity check to ensure correct API use in debug mode.

Run this once the first level of arguments has been passed so we can be sure --env-system-datafiles, and other --env-* arguments has been passed.

Without this any callers to this module that run early on, will miss out on changes from parsing arguments.

Definition at line 93 of file appdir.cc.

References BLI_assert, and is_appdir_init.

Referenced by blender::bke::tests::Context< TestData >::Context(), main(), blender::bke::image::partial_update::ImagePartialUpdateTest::SetUp(), BlendfileLoadingBaseTest::SetUpTestCase(), blender::bke::tests::TEST(), and blender::bke::tests::TEST().

◆ BKE_appdir_program_dir()

const char * BKE_appdir_program_dir ( )

Path to directory of executable

Definition at line 948 of file appdir.cc.

References BLI_assert, and g_app.

Referenced by Freestyle::Config::Path::Path().

◆ BKE_appdir_program_path()

const char * BKE_appdir_program_path ( )

Path to executable

Definition at line 940 of file appdir.cc.

References BLI_assert, and g_app.

Referenced by bpy_app_binary_path_get(), and BPY_python_start().

◆ BKE_appdir_program_path_init()

void BKE_appdir_program_path_init ( const char * argv0)

Initialize path to program executable.

Definition at line 917 of file appdir.cc.

References BLI_path_canonicalize_native(), BLI_path_split_dir_part(), g_app, STRNCPY, and where_am_i().

Referenced by bpy_app_binary_path_set(), and main().

◆ BKE_appdir_program_python_search()

bool BKE_appdir_program_python_search ( char * program_filepath,
size_t program_filepath_maxncpy,
int version_major,
int version_minor )

◆ BKE_appdir_resource_path_id()

std::optional< std::string > BKE_appdir_resource_path_id ( const int folder_id,
const bool check_is_dir )

Definition at line 817 of file appdir.cc.

References BKE_appdir_resource_path_id_with_version(), and BLENDER_VERSION.

◆ BKE_appdir_resource_path_id_with_version()

std::optional< std::string > BKE_appdir_resource_path_id_with_version ( int folder_id,
bool check_is_dir,
int version )

Returns the path of the top-level version-specific local, user or system directory. If check_is_dir, then the result will be NULL if the directory doesn't exist.

Definition at line 789 of file appdir.cc.

References BLENDER_RESOURCE_PATH_LOCAL, BLENDER_RESOURCE_PATH_SYSTEM, BLENDER_RESOURCE_PATH_USER, BLI_assert_msg, FILE_MAX, get_path_local_ex(), get_path_system_ex(), and get_path_user_ex().

Referenced by BKE_appdir_resource_path_id(), and bpy_resource_path().

◆ BKE_tempdir_base()

◆ BKE_tempdir_init()

◆ BKE_tempdir_session()

const char * BKE_tempdir_session ( )

Path to temporary directory (with trailing slash)

Definition at line 1206 of file appdir.cc.

References BKE_tempdir_base(), and g_app.

◆ BKE_tempdir_session_purge()

◆ blender_version_decimal()

static char * blender_version_decimal ( const int version)
static
Returns
a formatted representation of the specified version number. Non-re-entrant!

Definition at line 123 of file appdir.cc.

References BLI_assert, and SNPRINTF.

Referenced by get_path_local_ex(), get_path_system_ex(), and get_path_user_ex().

◆ get_path_environment()

static bool get_path_environment ( char * targetpath,
size_t targetpath_maxncpy,
const char * subfolder_name,
const char * envvar )
static

Definition at line 456 of file appdir.cc.

References get_path_environment_ex().

Referenced by appdir_app_template_directories(), and BKE_appdir_folder_id_ex().

◆ get_path_environment_ex()

static bool get_path_environment_ex ( char * targetpath,
size_t targetpath_maxncpy,
const char * subfolder_name,
const char * envvar,
const bool check_is_dir )
static

Returns the path of a folder from environment variables.

Parameters
targetpathString to return path.
subfolder_nameoptional name of sub-folder within folder.
envvarname of environment variable to check folder_name.
check_is_dirWhen false, return true even if the path doesn't exist.
Returns
true if it was able to construct such a path and the path exists.

Definition at line 441 of file appdir.cc.

References FILE_MAX, test_env_path(), and test_path().

Referenced by BKE_appdir_folder_id_user_notest(), and get_path_environment().

◆ get_path_local()

static bool get_path_local ( char * targetpath,
size_t targetpath_maxncpy,
const char * folder_name,
const char * subfolder_name )
static

Definition at line 421 of file appdir.cc.

References BLENDER_VERSION, and get_path_local_ex().

Referenced by BKE_appdir_folder_id_ex().

◆ get_path_local_ex()

static bool get_path_local_ex ( char * targetpath,
size_t targetpath_maxncpy,
const char * folder_name,
const char * subfolder_name,
const int version,
const bool check_is_dir )
static

Constructs in targetpath the name of a directory relative to a version-specific sub-directory in the parent directory of the Blender executable.

Parameters
targetpathString to return path.
folder_nameOptional folder name within version-specific directory.
subfolder_nameOptional sub-folder name within folder_name.
versionTo construct name of version-specific directory within #g_app.program_dirname.
check_is_dirWhen false, return true even if the path doesn't exist.
Returns
true if such a directory exists.

Definition at line 377 of file appdir.cc.

References blender_version_decimal(), BLI_path_join, BLI_path_join_array(), BLI_path_normalize_native(), CLOG_INFO, FILE_MAX, g_app, LOG, STR_OR_FALLBACK, and test_path().

Referenced by BKE_appdir_resource_path_id_with_version(), get_path_local(), and get_path_user_ex().

◆ get_path_system()

static bool get_path_system ( char * targetpath,
size_t targetpath_maxncpy,
const char * folder_name,
const char * subfolder_name )
static

Definition at line 574 of file appdir.cc.

References BLENDER_VERSION, and get_path_system_ex().

Referenced by BKE_appdir_folder_id_ex().

◆ get_path_system_ex()

static bool get_path_system_ex ( char * targetpath,
size_t targetpath_maxncpy,
const char * folder_name,
const char * subfolder_name,
const int version,
const bool check_is_dir )
static

Returns the path of a folder within the Blender installation directory.

Parameters
targetpathString to return path.
folder_namedefault name of folder within installation area.
subfolder_nameoptional name of sub-folder within folder.
versionBlender version, used to construct a sub-directory name.
check_is_dirWhen false, return true even if the path doesn't exist.
Returns
true if it was able to construct such a path.

Definition at line 538 of file appdir.cc.

References blender_version_decimal(), CLOG_INFO, FILE_MAX, GHOST_getSystemDir(), LOG, STR_OR_FALLBACK, STRNCPY, test_env_path(), and test_path().

Referenced by BKE_appdir_resource_path_id_with_version(), and get_path_system().

◆ get_path_user()

static bool get_path_user ( char * targetpath,
size_t targetpath_maxncpy,
const char * folder_name,
const char * subfolder_name )
static

Definition at line 517 of file appdir.cc.

References BLENDER_VERSION, and get_path_user_ex().

Referenced by BKE_appdir_folder_id_ex().

◆ get_path_user_ex()

static bool get_path_user_ex ( char * targetpath,
size_t targetpath_maxncpy,
const char * folder_name,
const char * subfolder_name,
const int version,
const bool check_is_dir )
static

Returns the path of a folder within the user-files area.

Parameters
targetpathString to return path.
folder_namedefault name of folder within user area.
subfolder_nameoptional name of sub-folder within folder.
versionBlender version, used to construct a sub-directory name.
check_is_dirWhen false, return true even if the path doesn't exist.
Returns
true if it was able to construct such a path.

Definition at line 476 of file appdir.cc.

References blender_version_decimal(), CLOG_INFO, FILE_MAX, get_path_local_ex(), GHOST_getUserDir(), LOG, STR_OR_FALLBACK, STRNCPY, test_env_path(), and test_path().

Referenced by BKE_appdir_folder_id_user_notest(), BKE_appdir_resource_path_id_with_version(), and get_path_user().

◆ tempdir_session_create()

static void tempdir_session_create ( char * tempdir_session,
const size_t tempdir_session_maxncpy,
const char * tempdir )
static

◆ test_env_path()

static bool test_env_path ( char * path,
const char * envvar,
const bool check_is_dir )
static

Puts the value of the specified environment variable into path if it exists.

Parameters
check_is_dirWhen true, checks if it points at a directory.
Returns
true when the value of the environment variable is stored at the address path points to.

Definition at line 336 of file appdir.cc.

References ASSERT_IS_INIT, BLI_getenv(), BLI_is_dir(), BLI_strncpy(), CLOG_INFO, FILE_MAX, and LOG.

Referenced by get_path_environment_ex(), get_path_system_ex(), and get_path_user_ex().

◆ test_path()

static bool test_path ( char * targetpath,
size_t targetpath_maxncpy,
const bool check_is_dir,
const char * path_base,
const char * folder_name,
const char * subfolder_name )
static

Concatenates paths into targetpath, returning true if result points to a directory.

Parameters
path_basePath base, never nullptr.
folder_nameFirst sub-directory (optional).
subfolder_nameSecond sub-directory (optional).
check_is_dirWhen 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.
If it's useful we could take an arbitrary number of paths. For now usage is limited and we don't need this.

Definition at line 296 of file appdir.cc.

References ASSERT_IS_INIT, BLI_assert, BLI_is_dir(), BLI_path_join_array(), CLOG_INFO, and LOG.

Referenced by get_path_environment_ex(), get_path_local_ex(), get_path_system_ex(), and get_path_user_ex().

◆ where_am_i()

static void where_am_i ( char * program_filepath,
const size_t program_filepath_maxncpy,
const char * program_name )
static

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 program_filepath.

Parameters
program_filepathThe full path and full name of the executable (must be FILE_MAX minimum)
nameThe name of the executable (usually argv[0]) to be checked

Definition at line 844 of file appdir.cc.

References BLI_exists(), BLI_path_abs_from_cwd(), BLI_path_normalize_native(), BLI_path_program_search(), BLI_path_slash_rfind(), BLI_strncpy(), CLOG_ERROR, CLOG_INFO, conv_utf_16_to_8(), free(), LOG, MEM_freeN(), MEM_mallocN, program_filepath, and STREQ.

Referenced by BKE_appdir_program_path_init().

◆ where_is_temp()

static void where_is_temp ( char * tempdir,
const size_t tempdir_maxncpy,
const char * userdir )
static

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

Parameters
tempdirThe full path to the temporary temp directory.
tempdir_maxncpyThe size of the tempdir buffer.
userdirDirectory specified in user preferences (may be nullptr). note that by default this is an empty string, only use when non-empty.

Definition at line 1142 of file appdir.cc.

References BLI_temp_directory_path_copy_if_valid(), and BLI_temp_directory_path_get().

Referenced by BKE_tempdir_init().

Variable Documentation

◆ _str_null

const char _str_null[] = "(null)"
static

Definition at line 59 of file appdir.cc.

◆ [struct]

◆ is_appdir_init

bool is_appdir_init = false
static

Definition at line 87 of file appdir.cc.

Referenced by BKE_appdir_exit(), and BKE_appdir_init().

◆ LOG

◆ program_dirname

char program_dirname[FILE_MAX]

Full path to directory in which executable is located.

Definition at line 73 of file appdir.cc.

◆ program_filepath

char program_filepath[FILE_MAX]

Full path to program executable.

Definition at line 71 of file appdir.cc.

Referenced by BKE_appdir_program_python_search(), BLI_path_program_search(), and where_am_i().

◆ temp_dirname_base

char temp_dirname_base[FILE_MAX]

Persistent temporary directory (defined by the preferences or OS).

Definition at line 75 of file appdir.cc.

◆ temp_dirname_session

char temp_dirname_session[FILE_MAX]

Volatile temporary directory (owned by Blender, removed on exit).

Definition at line 77 of file appdir.cc.