Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/intern/appdir.c
| Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | |||||
| static char btempdir_session[FILE_MAX] = ""; /* volatile temporary directory */ | static char btempdir_session[FILE_MAX] = ""; /* volatile temporary directory */ | ||||
| /* This is now only used to really get the user's default document folder */ | /* This is now only used to really get the user's default document folder */ | ||||
| /* On Windows I chose the 'Users/<MyUserName>/Documents' since it's used | /* On Windows I chose the 'Users/<MyUserName>/Documents' since it's used | ||||
| * as default location to save documents */ | * as default location to save documents */ | ||||
| const char *BKE_appdir_folder_default(void) | const char *BKE_appdir_folder_default(void) | ||||
| { | { | ||||
| #ifndef WIN32 | #ifndef WIN32 | ||||
| const char * const xdg_documents_dir = getenv("XDG_DOCUMENTS_DIR"); | const char * const xdg_documents_dir = BLI_getenv("XDG_DOCUMENTS_DIR"); | ||||
| if (xdg_documents_dir) | if (xdg_documents_dir) | ||||
| return xdg_documents_dir; | return xdg_documents_dir; | ||||
| return getenv("HOME"); | return BLI_getenv("HOME"); | ||||
| #else /* Windows */ | #else /* Windows */ | ||||
| static char documentfolder[MAXPATHLEN]; | static char documentfolder[MAXPATHLEN]; | ||||
| HRESULT hResult; | HRESULT hResult; | ||||
| /* Check for %HOME% env var */ | /* Check for %HOME% env var */ | ||||
| if (uput_getenv("HOME", documentfolder, MAXPATHLEN)) { | if (uput_getenv("HOME", documentfolder, MAXPATHLEN)) { | ||||
| if (BLI_is_dir(documentfolder)) return documentfolder; | if (BLI_is_dir(documentfolder)) return documentfolder; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| /** | /** | ||||
| * Puts the value of the specified environment variable into *path if it exists | * Puts the value of the specified environment variable into *path if it exists | ||||
| * and points at a directory. Returns true if this was done. | * and points at a directory. Returns true if this was done. | ||||
| */ | */ | ||||
| static bool test_env_path(char *path, const char *envvar) | static bool test_env_path(char *path, const char *envvar) | ||||
| { | { | ||||
| const char *env = envvar ? getenv(envvar) : NULL; | const char *env = envvar ? BLI_getenv(envvar) : NULL; | ||||
| if (!env) return false; | if (!env) return false; | ||||
| if (BLI_is_dir(env)) { | if (BLI_is_dir(env)) { | ||||
| BLI_strncpy(path, env, FILE_MAX); | BLI_strncpy(path, env, FILE_MAX); | ||||
| #ifdef PATH_DEBUG | #ifdef PATH_DEBUG | ||||
| printf("\t%s env %s found: %s\n", __func__, envvar, env); | printf("\t%s env %s found: %s\n", __func__, envvar, env); | ||||
| #endif | #endif | ||||
| return true; | return true; | ||||
| ▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | static bool is_portable_install(void) | ||||
| /* detect portable install by the existence of config folder */ | /* detect portable install by the existence of config folder */ | ||||
| const int ver = BLENDER_VERSION; | const int ver = BLENDER_VERSION; | ||||
| char path[FILE_MAX]; | char path[FILE_MAX]; | ||||
| return get_path_local(path, sizeof(path), "config", NULL, ver); | return get_path_local(path, sizeof(path), "config", NULL, ver); | ||||
| } | } | ||||
| /** | /** | ||||
| * Returns the path of a folder from environment variables | |||||
| * | |||||
| * | |||||
| * \param targetpath String to return path | |||||
| * \param subfolder_name optional name of subfolder within folder | |||||
| * \param envvar name of environment variable to check | |||||
| * folder_name \param ver Blender version, used to construct a subdirectory | |||||
| * name \return true if it was able to construct such a path. | |||||
| */ | |||||
| static bool get_path_environment(char *targetpath, | |||||
| size_t targetpath_len, | |||||
| const char *subfolder_name, | |||||
| const char *envvar, | |||||
| const int ver) | |||||
| { | |||||
| char user_path[FILE_MAX]; | |||||
| if (test_env_path(user_path, envvar)) { | |||||
| if (subfolder_name) { | |||||
| return test_path(targetpath, | |||||
| targetpath_len, | |||||
| user_path, | |||||
| NULL, | |||||
| subfolder_name); | |||||
| } | |||||
| else { | |||||
| BLI_strncpy(targetpath, user_path, FILE_MAX); | |||||
| return true; | |||||
| } | |||||
| } | |||||
| return false; | |||||
| } | |||||
| /** | |||||
| * Returns the path of a folder within the user-files area. | * Returns the path of a folder within the user-files area. | ||||
| * | * | ||||
| * | * | ||||
| * \param targetpath String to return path | * \param targetpath String to return path | ||||
| * \param folder_name default name of folder within user area | * \param folder_name default name of folder within user area | ||||
| * \param subfolder_name optional name of subfolder within folder | * \param subfolder_name optional name of subfolder within folder | ||||
| * \param envvar name of environment variable which, if defined, overrides folder_name | |||||
| * \param ver Blender version, used to construct a subdirectory name | * \param ver Blender version, used to construct a subdirectory name | ||||
| * \return true if it was able to construct such a path. | * \return true if it was able to construct such a path. | ||||
| */ | */ | ||||
| static bool get_path_user( | static bool get_path_user( | ||||
| char *targetpath, size_t targetpath_len, const char *folder_name, const char *subfolder_name, | char *targetpath, size_t targetpath_len, const char *folder_name, const char *subfolder_name, | ||||
| const char *envvar, const int ver) | const int ver) | ||||
| { | { | ||||
| char user_path[FILE_MAX]; | char user_path[FILE_MAX]; | ||||
| const char *user_base_path; | const char *user_base_path; | ||||
| /* for portable install, user path is always local */ | /* for portable install, user path is always local */ | ||||
| if (is_portable_install()) { | if (is_portable_install()) { | ||||
| return get_path_local(targetpath, targetpath_len, folder_name, subfolder_name, ver); | return get_path_local(targetpath, targetpath_len, folder_name, subfolder_name, ver); | ||||
| } | } | ||||
| user_path[0] = '\0'; | user_path[0] = '\0'; | ||||
| if (test_env_path(user_path, envvar)) { | |||||
| if (subfolder_name) { | |||||
| return test_path(targetpath, targetpath_len, user_path, NULL, subfolder_name); | |||||
| } | |||||
| else { | |||||
| BLI_strncpy(targetpath, user_path, FILE_MAX); | |||||
| return true; | |||||
| } | |||||
| } | |||||
| user_base_path = (const char *)GHOST_getUserDir(ver, blender_version_decimal(ver)); | user_base_path = (const char *)GHOST_getUserDir(ver, blender_version_decimal(ver)); | ||||
| if (user_base_path) | if (user_base_path) | ||||
| BLI_strncpy(user_path, user_base_path, FILE_MAX); | BLI_strncpy(user_path, user_base_path, FILE_MAX); | ||||
| if (!user_path[0]) | if (!user_path[0]) | ||||
| return false; | return false; | ||||
| #ifdef PATH_DEBUG | #ifdef PATH_DEBUG | ||||
| Show All 9 Lines | |||||
| } | } | ||||
| /** | /** | ||||
| * Returns the path of a folder within the Blender installation directory. | * Returns the path of a folder within the Blender installation directory. | ||||
| * | * | ||||
| * \param targetpath String to return path | * \param targetpath String to return path | ||||
| * \param folder_name default name of folder within installation area | * \param folder_name default name of folder within installation area | ||||
| * \param subfolder_name optional name of subfolder within folder | * \param subfolder_name optional name of subfolder within folder | ||||
| * \param envvar name of environment variable which, if defined, overrides folder_name | |||||
| * \param ver Blender version, used to construct a subdirectory name | * \param ver Blender version, used to construct a subdirectory name | ||||
| * \return true if it was able to construct such a path. | * \return true if it was able to construct such a path. | ||||
| */ | */ | ||||
| static bool get_path_system( | static bool get_path_system( | ||||
| char *targetpath, size_t targetpath_len, const char *folder_name, const char *subfolder_name, | char *targetpath, size_t targetpath_len, const char *folder_name, const char *subfolder_name, | ||||
| const char *envvar, const int ver) | const int ver) | ||||
| { | { | ||||
| char system_path[FILE_MAX]; | char system_path[FILE_MAX]; | ||||
| const char *system_base_path; | const char *system_base_path; | ||||
| char relfolder[FILE_MAX]; | char relfolder[FILE_MAX]; | ||||
| if (folder_name) { | if (folder_name) { | ||||
| if (subfolder_name) { | if (subfolder_name) { | ||||
| BLI_join_dirfile(relfolder, sizeof(relfolder), folder_name, subfolder_name); | BLI_join_dirfile(relfolder, sizeof(relfolder), folder_name, subfolder_name); | ||||
| } | } | ||||
| else { | else { | ||||
| BLI_strncpy(relfolder, folder_name, sizeof(relfolder)); | BLI_strncpy(relfolder, folder_name, sizeof(relfolder)); | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| relfolder[0] = '\0'; | relfolder[0] = '\0'; | ||||
| } | } | ||||
| system_path[0] = '\0'; | system_path[0] = '\0'; | ||||
| if (test_env_path(system_path, envvar)) { | |||||
| if (subfolder_name) { | |||||
| return test_path(targetpath, targetpath_len, system_path, NULL, subfolder_name); | |||||
| } | |||||
| else { | |||||
| BLI_strncpy(targetpath, system_path, FILE_MAX); | |||||
| return true; | |||||
| } | |||||
| } | |||||
| system_base_path = (const char *)GHOST_getSystemDir(ver, blender_version_decimal(ver)); | system_base_path = (const char *)GHOST_getSystemDir(ver, blender_version_decimal(ver)); | ||||
| if (system_base_path) | if (system_base_path) | ||||
| BLI_strncpy(system_path, system_base_path, FILE_MAX); | BLI_strncpy(system_path, system_base_path, FILE_MAX); | ||||
| if (!system_path[0]) | if (!system_path[0]) | ||||
| return false; | return false; | ||||
| #ifdef PATH_DEBUG | #ifdef PATH_DEBUG | ||||
| Show All 20 Lines | |||||
| const char *BKE_appdir_folder_id_ex( | const char *BKE_appdir_folder_id_ex( | ||||
| const int folder_id, const char *subfolder, | const int folder_id, const char *subfolder, | ||||
| char *path, size_t path_len) | char *path, size_t path_len) | ||||
| { | { | ||||
| const int ver = BLENDER_VERSION; | const int ver = BLENDER_VERSION; | ||||
| switch (folder_id) { | switch (folder_id) { | ||||
| case BLENDER_DATAFILES: /* general case */ | case BLENDER_DATAFILES: /* general case */ | ||||
| if (get_path_user(path, path_len, "datafiles", subfolder, "BLENDER_USER_DATAFILES", ver)) break; | if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES", ver)) break; | ||||
| if (get_path_user(path, path_len, "datafiles", subfolder, ver)) break; | |||||
| if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_DATAFILES", ver)) break; | |||||
| if (get_path_local(path, path_len, "datafiles", subfolder, ver)) break; | if (get_path_local(path, path_len, "datafiles", subfolder, ver)) break; | ||||
| if (get_path_system(path, path_len, "datafiles", subfolder, "BLENDER_SYSTEM_DATAFILES", ver)) break; | if (get_path_system(path, path_len, "datafiles", subfolder, ver)) break; | ||||
| return NULL; | return NULL; | ||||
| case BLENDER_USER_DATAFILES: | case BLENDER_USER_DATAFILES: | ||||
| if (get_path_user(path, path_len, "datafiles", subfolder, "BLENDER_USER_DATAFILES", ver)) break; | if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES", ver)) break; | ||||
| if (get_path_user(path, path_len, "datafiles", subfolder, ver)) break; | |||||
| return NULL; | return NULL; | ||||
| case BLENDER_SYSTEM_DATAFILES: | case BLENDER_SYSTEM_DATAFILES: | ||||
| if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_DATAFILES", ver)) break; | |||||
| if (get_path_system(path, path_len, "datafiles", subfolder, ver)) break; | |||||
| if (get_path_local(path, path_len, "datafiles", subfolder, ver)) break; | if (get_path_local(path, path_len, "datafiles", subfolder, ver)) break; | ||||
| if (get_path_system(path, path_len, "datafiles", subfolder, "BLENDER_SYSTEM_DATAFILES", ver)) break; | |||||
| return NULL; | return NULL; | ||||
| case BLENDER_USER_AUTOSAVE: | case BLENDER_USER_AUTOSAVE: | ||||
| if (get_path_user(path, path_len, "autosave", subfolder, "BLENDER_USER_DATAFILES", ver)) break; | if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES", ver)) break; | ||||
| if (get_path_user(path, path_len, "autosave", subfolder, ver)) break; | |||||
| return NULL; | return NULL; | ||||
| case BLENDER_USER_CONFIG: | case BLENDER_USER_CONFIG: | ||||
| if (get_path_user(path, path_len, "config", subfolder, "BLENDER_USER_CONFIG", ver)) break; | if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_CONFIG", ver)) break; | ||||
| if (get_path_user(path, path_len, "config", subfolder, ver)) break; | |||||
| return NULL; | return NULL; | ||||
| case BLENDER_USER_SCRIPTS: | case BLENDER_USER_SCRIPTS: | ||||
| if (get_path_user(path, path_len, "scripts", subfolder, "BLENDER_USER_SCRIPTS", ver)) break; | if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_SCRIPTS", ver)) break; | ||||
| if (get_path_user(path, path_len, "scripts", subfolder, ver)) break; | |||||
| return NULL; | return NULL; | ||||
| case BLENDER_SYSTEM_SCRIPTS: | case BLENDER_SYSTEM_SCRIPTS: | ||||
| if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_SCRIPTS", ver)) break; | |||||
| if (get_path_system(path, path_len, "scripts", subfolder, ver)) break; | |||||
| if (get_path_local(path, path_len, "scripts", subfolder, ver)) break; | if (get_path_local(path, path_len, "scripts", subfolder, ver)) break; | ||||
| if (get_path_system(path, path_len, "scripts", subfolder, "BLENDER_SYSTEM_SCRIPTS", ver)) break; | |||||
| return NULL; | return NULL; | ||||
| case BLENDER_SYSTEM_PYTHON: | case BLENDER_SYSTEM_PYTHON: | ||||
| if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_PYTHON", ver)) break; | |||||
| if (get_path_system(path, path_len, "python", subfolder, ver)) break; | |||||
| if (get_path_local(path, path_len, "python", subfolder, ver)) break; | if (get_path_local(path, path_len, "python", subfolder, ver)) break; | ||||
| if (get_path_system(path, path_len, "python", subfolder, "BLENDER_SYSTEM_PYTHON", ver)) break; | |||||
| return NULL; | return NULL; | ||||
| default: | default: | ||||
| BLI_assert(0); | BLI_assert(0); | ||||
| break; | break; | ||||
| } | } | ||||
| return path; | return path; | ||||
| Show All 11 Lines | |||||
| */ | */ | ||||
| const char *BKE_appdir_folder_id_user_notest(const int folder_id, const char *subfolder) | const char *BKE_appdir_folder_id_user_notest(const int folder_id, const char *subfolder) | ||||
| { | { | ||||
| const int ver = BLENDER_VERSION; | const int ver = BLENDER_VERSION; | ||||
| static char path[FILE_MAX] = ""; | static char path[FILE_MAX] = ""; | ||||
| switch (folder_id) { | switch (folder_id) { | ||||
| case BLENDER_USER_DATAFILES: | case BLENDER_USER_DATAFILES: | ||||
| get_path_user(path, sizeof(path), "datafiles", subfolder, "BLENDER_USER_DATAFILES", ver); | if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_DATAFILES", ver)) break; | ||||
| get_path_user(path, sizeof(path), "datafiles", subfolder, ver); | |||||
| break; | break; | ||||
| case BLENDER_USER_CONFIG: | case BLENDER_USER_CONFIG: | ||||
| get_path_user(path, sizeof(path), "config", subfolder, "BLENDER_USER_CONFIG", ver); | if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_CONFIG", ver)) break; | ||||
| get_path_user(path, sizeof(path), "config", subfolder, ver); | |||||
| break; | break; | ||||
| case BLENDER_USER_AUTOSAVE: | case BLENDER_USER_AUTOSAVE: | ||||
| get_path_user(path, sizeof(path), "autosave", subfolder, "BLENDER_USER_AUTOSAVE", ver); | if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_AUTOSAVE", ver)) break; | ||||
| get_path_user(path, sizeof(path), "autosave", subfolder, ver); | |||||
| break; | break; | ||||
| case BLENDER_USER_SCRIPTS: | case BLENDER_USER_SCRIPTS: | ||||
| get_path_user(path, sizeof(path), "scripts", subfolder, "BLENDER_USER_SCRIPTS", ver); | if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_SCRIPTS", ver)) break; | ||||
| get_path_user(path, sizeof(path), "scripts", subfolder, ver); | |||||
| break; | break; | ||||
| default: | default: | ||||
| BLI_assert(0); | BLI_assert(0); | ||||
| break; | break; | ||||
| } | } | ||||
| if ('\0' == path[0]) { | if ('\0' == path[0]) { | ||||
| return NULL; | return NULL; | ||||
| Show All 27 Lines | |||||
| * If do_check, then the result will be NULL if the directory doesn't exist. | * If do_check, then the result will be NULL if the directory doesn't exist. | ||||
| */ | */ | ||||
| const char *BKE_appdir_folder_id_version(const int folder_id, const int ver, const bool do_check) | const char *BKE_appdir_folder_id_version(const int folder_id, const int ver, const bool do_check) | ||||
| { | { | ||||
| static char path[FILE_MAX] = ""; | static char path[FILE_MAX] = ""; | ||||
| bool ok; | bool ok; | ||||
| switch (folder_id) { | switch (folder_id) { | ||||
| case BLENDER_RESOURCE_PATH_USER: | case BLENDER_RESOURCE_PATH_USER: | ||||
| ok = get_path_user(path, sizeof(path), NULL, NULL, NULL, ver); | ok = get_path_user(path, sizeof(path), NULL, NULL, ver); | ||||
| break; | break; | ||||
| case BLENDER_RESOURCE_PATH_LOCAL: | case BLENDER_RESOURCE_PATH_LOCAL: | ||||
| ok = get_path_local(path, sizeof(path), NULL, NULL, ver); | ok = get_path_local(path, sizeof(path), NULL, NULL, ver); | ||||
| break; | break; | ||||
| case BLENDER_RESOURCE_PATH_SYSTEM: | case BLENDER_RESOURCE_PATH_SYSTEM: | ||||
| ok = get_path_system(path, sizeof(path), NULL, NULL, NULL, ver); | ok = get_path_system(path, sizeof(path), NULL, NULL, ver); | ||||
| break; | break; | ||||
| default: | default: | ||||
| path[0] = '\0'; /* in case do_check is false */ | path[0] = '\0'; /* in case do_check is false */ | ||||
| ok = false; | ok = false; | ||||
| BLI_assert(!"incorrect ID"); | BLI_assert(!"incorrect ID"); | ||||
| break; | break; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 239 Lines • ▼ Show 20 Lines | static void where_is_temp(char *fullname, char *basename, const size_t maxlen, char *userdir) | ||||
| if (userdir && BLI_is_dir(userdir)) { | if (userdir && BLI_is_dir(userdir)) { | ||||
| BLI_strncpy(fullname, userdir, maxlen); | BLI_strncpy(fullname, userdir, maxlen); | ||||
| } | } | ||||
| #ifdef WIN32 | #ifdef WIN32 | ||||
| if (fullname[0] == '\0') { | if (fullname[0] == '\0') { | ||||
| const char *tmp = getenv("TEMP"); /* Windows */ | const char *tmp = BLI_getenv("TEMP"); /* Windows */ | ||||
| if (tmp && BLI_is_dir(tmp)) { | if (tmp && BLI_is_dir(tmp)) { | ||||
| BLI_strncpy(fullname, tmp, maxlen); | BLI_strncpy(fullname, tmp, maxlen); | ||||
| } | } | ||||
| } | } | ||||
| #else | #else | ||||
| /* Other OS's - Try TMP and TMPDIR */ | /* Other OS's - Try TMP and TMPDIR */ | ||||
| if (fullname[0] == '\0') { | if (fullname[0] == '\0') { | ||||
| const char *tmp = getenv("TMP"); | const char *tmp = BLI_getenv("TMP"); | ||||
| if (tmp && BLI_is_dir(tmp)) { | if (tmp && BLI_is_dir(tmp)) { | ||||
| BLI_strncpy(fullname, tmp, maxlen); | BLI_strncpy(fullname, tmp, maxlen); | ||||
| } | } | ||||
| } | } | ||||
| if (fullname[0] == '\0') { | if (fullname[0] == '\0') { | ||||
| const char *tmp = getenv("TMPDIR"); | const char *tmp = BLI_getenv("TMPDIR"); | ||||
| if (tmp && BLI_is_dir(tmp)) { | if (tmp && BLI_is_dir(tmp)) { | ||||
| BLI_strncpy(fullname, tmp, maxlen); | BLI_strncpy(fullname, tmp, maxlen); | ||||
| } | } | ||||
| } | } | ||||
| #endif | #endif | ||||
| if (fullname[0] == '\0') { | if (fullname[0] == '\0') { | ||||
| BLI_strncpy(fullname, "/tmp/", maxlen); | BLI_strncpy(fullname, "/tmp/", maxlen); | ||||
| ▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines | |||||