Blender  V3.3
Macros | Variables
appdir.c File Reference
#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"
#include "BKE_blender_version.h"
#include "BLT_translation.h"
#include "GHOST_Path-api.h"
#include "MEM_guardedalloc.h"
#include "CLG_log.h"
#include <unistd.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 (void)
 
const char * BKE_appdir_folder_root (void)
 
const char * BKE_appdir_folder_default_or_root (void)
 
const char * BKE_appdir_folder_home (void)
 
bool BKE_appdir_folder_documents (char *dir)
 
bool BKE_appdir_folder_caches (char *r_path, const size_t path_len)
 
bool BKE_appdir_font_folder_default (char *dir)
 
Path Presets (Internal Helpers)
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)
 
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_len, 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_len, const char *folder_name, const char *subfolder_name)
 
bool BKE_appdir_app_is_portable_install (void)
 
static bool get_path_environment_ex (char *targetpath, size_t targetpath_len, const char *subfolder_name, const char *envvar, const bool check_is_dir)
 
static bool get_path_environment (char *targetpath, size_t targetpath_len, const char *subfolder_name, const char *envvar)
 
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)
 
static bool get_path_user (char *targetpath, size_t targetpath_len, const char *folder_name, const char *subfolder_name)
 
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)
 
static bool get_path_system (char *targetpath, size_t targetpath_len, 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_len)
 
const char * BKE_appdir_folder_id (const int folder_id, const char *subfolder)
 
const char * BKE_appdir_folder_id_user_notest (const int folder_id, const char *subfolder)
 
const char * BKE_appdir_folder_id_create (const int folder_id, const char *subfolder)
 
const char * BKE_appdir_folder_id_version (const int folder_id, const int version, const bool check_is_dir)
 
Program Path Queries

Access locations of Blender & Python.

static void where_am_i (char *fullname, const size_t maxlen, const char *name)
 
void BKE_appdir_program_path_init (const char *argv0)
 
const char * BKE_appdir_program_path (void)
 
const char * BKE_appdir_program_dir (void)
 
bool BKE_appdir_program_python_search (char *fullpath, const size_t fullpath_len, const int version_major, const int version_minor)
 
Temporary Directories
static void where_is_temp (char *tempdir, const size_t tempdir_len, const char *userdir)
 
static void tempdir_session_create (char *tempdir_session, const size_t tempdir_session_len, const char *tempdir)
 
void BKE_tempdir_init (const char *userdir)
 
const char * BKE_tempdir_session (void)
 
const char * BKE_tempdir_base (void)
 
void BKE_tempdir_session_purge (void)
 

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)
 
void BKE_appdir_exit (void)
 

Application Templates

static const char * app_template_directory_search [2]
 
static const int app_template_directory_id [2]
 
bool BKE_appdir_app_template_any (void)
 
bool BKE_appdir_app_template_id_search (const char *app_template, char *path, size_t path_len)
 
bool BKE_appdir_app_template_has_userpref (const char *app_template)
 
void BKE_appdir_app_templates (ListBase *templates)
 

Detailed Description

Access to application level directories.

Definition in file appdir.c.

Macro Definition Documentation

◆ ASSERT_IS_INIT

#define ASSERT_IS_INIT ( )    BLI_assert(is_appdir_init)

Definition at line 83 of file appdir.c.

◆ STR_OR_FALLBACK

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

Definition at line 53 of file appdir.c.

Function Documentation

◆ BKE_appdir_app_is_portable_install()

bool BKE_appdir_app_is_portable_install ( void  )

Check if this is an install with user files kept together with the Blender executable and its installation files.

Definition at line 404 of file appdir.c.

References FILE_MAX, get_path_local(), and NULL.

Referenced by BKE_studiolight_init(), and get_path_user_ex().

◆ BKE_appdir_app_template_any()

bool BKE_appdir_app_template_any ( void  )

Return true if templates exist

Definition at line 966 of file appdir.c.

References app_template_directory_id, app_template_directory_search, ARRAY_SIZE, BKE_appdir_folder_id_ex(), and FILE_MAX.

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_len 
)

◆ BKE_appdir_app_templates()

void BKE_appdir_app_templates ( ListBase templates)

◆ BKE_appdir_exit()

void BKE_appdir_exit ( void  )

◆ BKE_appdir_folder_caches()

bool BKE_appdir_folder_caches ( char *  r_path,
size_t  path_len 
)

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 203 of file appdir.c.

References BKE_tempdir_base(), BLI_is_dir(), BLI_path_join(), GHOST_getUserSpecialDir(), GHOST_kUserSpecialDirCaches, NULL, and SEP_STR.

Referenced by blender::ed::asset::index::AssetLibraryIndex::init_indices_base_path().

◆ BKE_appdir_folder_default()

const char* BKE_appdir_folder_default ( void  )

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 127 of file appdir.c.

References BKE_appdir_folder_documents(), BLI_getenv(), MAXPATHLEN, and NULL.

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 ( void  )

Definition at line 153 of file appdir.c.

References BKE_appdir_folder_default(), BKE_appdir_folder_root(), and NULL.

Referenced by 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 173 of file appdir.c.

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

Referenced by BKE_appdir_folder_default(), and BKE_preferences_asset_library_default_add().

◆ BKE_appdir_folder_home()

const char* BKE_appdir_folder_home ( void  )

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

  • Unix: $HOME
  • Windows: userprofile%

Definition at line 162 of file appdir.c.

References BLI_expand_tilde(), and BLI_getenv().

Referenced by BKE_appdir_folder_documents().

◆ BKE_appdir_folder_id()

const char* BKE_appdir_folder_id ( const int  folder_id,
const char *  subfolder 
)

◆ BKE_appdir_folder_id_create()

const char* BKE_appdir_folder_id_create ( int  folder_id,
const char *  subfolder 
)

◆ BKE_appdir_folder_id_ex()

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

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 568 of file appdir.c.

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

Referenced by BKE_appdir_app_template_any(), BKE_appdir_app_template_id_search(), BKE_appdir_app_templates(), and BKE_appdir_folder_id().

◆ BKE_appdir_folder_id_user_notest()

const char* BKE_appdir_folder_id_user_notest ( int  folder_id,
const char *  subfolder 
)

Returns the path to a folder in the user area without checking that it actually exists first.

Definition at line 681 of file appdir.c.

References BLENDER_USER_AUTOSAVE, BLENDER_USER_CONFIG, BLENDER_USER_DATAFILES, BLENDER_USER_SCRIPTS, BLENDER_VERSION, BLI_assert_unreachable, FILE_MAX, get_path_environment_ex(), get_path_user_ex(), and NULL.

Referenced by BKE_appdir_folder_id_create(), and bpy_user_resource().

◆ BKE_appdir_folder_id_version()

const char* BKE_appdir_folder_id_version ( int  folder_id,
int  version,
bool  check_is_dir 
)

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 752 of file appdir.c.

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(), get_path_user_ex(), and NULL.

Referenced by bpy_resource_path().

◆ BKE_appdir_folder_root()

const char* BKE_appdir_folder_root ( void  )

Definition at line 142 of file appdir.c.

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)

Gets a good default directory for fonts.

Definition at line 227 of file appdir.c.

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

Referenced by BKE_blendfile_userdef_from_defaults().

◆ BKE_appdir_init()

void BKE_appdir_init ( void  )

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 88 of file appdir.c.

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(), and blender::bke::tests::TEST().

◆ BKE_appdir_program_dir()

const char* BKE_appdir_program_dir ( void  )

Path to directory of executable

Definition at line 873 of file appdir.c.

References BLI_assert, and g_app.

◆ BKE_appdir_program_path()

const char* BKE_appdir_program_path ( void  )

Path to executable

Definition at line 867 of file appdir.c.

References BLI_assert, and g_app.

Referenced by BPY_python_start(), and make_app_info().

◆ BKE_appdir_program_path_init()

void BKE_appdir_program_path_init ( const char *  argv0)

Initialize path to program executable.

Definition at line 861 of file appdir.c.

References BLI_split_dir_part(), g_app, and where_am_i().

Referenced by main().

◆ BKE_appdir_program_python_search()

bool BKE_appdir_program_python_search ( char *  fullpath,
size_t  fullpath_len,
int  version_major,
int  version_minor 
)

◆ BKE_tempdir_base()

const char* BKE_tempdir_base ( void  )

◆ BKE_tempdir_init()

void BKE_tempdir_init ( const char *  userdir)

◆ BKE_tempdir_session()

const char* BKE_tempdir_session ( void  )

Path to temporary directory (with trailing slash)

Definition at line 1149 of file appdir.c.

References BKE_tempdir_base(), and g_app.

◆ BKE_tempdir_session_purge()

void BKE_tempdir_session_purge ( void  )

◆ 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 113 of file appdir.c.

References BLI_assert, and BLI_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_len,
const char *  subfolder_name,
const char *  envvar 
)
static

Definition at line 434 of file appdir.c.

References get_path_environment_ex().

Referenced by BKE_appdir_folder_id_ex().

◆ get_path_environment_ex()

static bool get_path_environment_ex ( char *  targetpath,
size_t  targetpath_len,
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 420 of file appdir.c.

References FILE_MAX, NULL, 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_len,
const char *  folder_name,
const char *  subfolder_name 
)
static

Definition at line 393 of file appdir.c.

References BLENDER_VERSION, and get_path_local_ex().

Referenced by BKE_appdir_app_is_portable_install(), and BKE_appdir_folder_id_ex().

◆ get_path_local_ex()

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 
)
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 352 of file appdir.c.

References blender_version_decimal(), BLI_path_join(), BLI_path_normalize(), BLI_snprintf(), CLOG_INFO, FILE_MAX, g_app, LOG, NULL, STR_OR_FALLBACK, and test_path().

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

◆ get_path_system()

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

Definition at line 551 of file appdir.c.

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_len,
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 511 of file appdir.c.

References blender_version_decimal(), BLI_path_join(), BLI_strncpy(), CLOG_INFO, FILE_MAX, GHOST_getSystemDir(), LOG, NULL, STR_OR_FALLBACK, and test_path().

Referenced by BKE_appdir_folder_id_version(), and get_path_system().

◆ get_path_user()

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

Definition at line 490 of file appdir.c.

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_len,
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 453 of file appdir.c.

References BKE_appdir_app_is_portable_install(), blender_version_decimal(), BLI_strncpy(), CLOG_INFO, FILE_MAX, get_path_local_ex(), GHOST_getUserDir(), LOG, STR_OR_FALLBACK, and test_path().

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

◆ tempdir_session_create()

static void tempdir_session_create ( char *  tempdir_session,
const size_t  tempdir_session_len,
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 311 of file appdir.c.

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

Referenced by get_path_environment_ex().

◆ test_path()

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 
)
static

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

Parameters
path_basePath base, never NULL.
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 273 of file appdir.c.

References ASSERT_IS_INIT, BLI_assert, BLI_is_dir(), BLI_path_join(), CLOG_INFO, LOG, and NULL.

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 *  fullname,
const size_t  maxlen,
const char *  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 fullname.

Parameters
fullnameThe 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 797 of file appdir.c.

References BLI_exists(), BLI_path_abs_from_cwd(), BLI_path_normalize(), 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, NULL, and STREQ.

Referenced by BKE_appdir_program_path_init().

◆ where_is_temp()

static void where_is_temp ( char *  tempdir,
const size_t  tempdir_len,
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_lenThe size of the tempdir buffer.
userdirDirectory specified in user preferences (may be NULL). note that by default this is an empty string, only use when non-empty.

Definition at line 1055 of file appdir.c.

References ARRAY_SIZE, BLI_getenv(), BLI_is_dir(), BLI_path_slash_ensure(), and BLI_strncpy().

Referenced by BKE_tempdir_init().

Variable Documentation

◆ _str_null

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

Definition at line 52 of file appdir.c.

◆ app_template_directory_id

const int app_template_directory_id[2]
static
Initial value:
= {
}
@ BLENDER_SYSTEM_SCRIPTS
Definition: BKE_appdir.h:164
@ BLENDER_USER_SCRIPTS
Definition: BKE_appdir.h:159

Definition at line 959 of file appdir.c.

Referenced by BKE_appdir_app_template_any(), BKE_appdir_app_template_id_search(), and BKE_appdir_app_templates().

◆ app_template_directory_search

const char* app_template_directory_search[2]
static
Initial value:
= {
"startup" SEP_STR "bl_app_templates_user",
"startup" SEP_STR "bl_app_templates_system",
}
#define SEP_STR
Definition: unit.c:33

Keep in sync with bpy.utils.app_template_paths()

Definition at line 954 of file appdir.c.

Referenced by BKE_appdir_app_template_any(), BKE_appdir_app_template_id_search(), and BKE_appdir_app_templates().

◆ 

struct { ... } g_app

◆ is_appdir_init

bool is_appdir_init = false
static

Definition at line 82 of file appdir.c.

Referenced by BKE_appdir_exit(), and BKE_appdir_init().

◆ LOG

CLG_LogRef LOG = {"bke.appdir"}
static

◆ program_dirname

char program_dirname[FILE_MAX]

Full path to directory in which executable is located.

Definition at line 66 of file appdir.c.

◆ program_filepath

char program_filepath[FILE_MAX]

Full path to program executable.

Definition at line 64 of file appdir.c.

◆ temp_dirname_base

char temp_dirname_base[FILE_MAX]

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

Definition at line 68 of file appdir.c.

◆ temp_dirname_session

char temp_dirname_session[FILE_MAX]

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

Definition at line 70 of file appdir.c.