BLI_fileops: move home directory access to BLI_fileops

This was located in BKE_appdir which is higher level
(used for accessing Blender's paths), where as the home directory
may be accessed from lower level path code.
This commit is contained in:
Campbell Barton
2024-10-08 12:23:40 +11:00
parent ad88aedbac
commit 82ab7ceba6
5 changed files with 35 additions and 36 deletions

View File

@@ -42,12 +42,6 @@ void BKE_appdir_exit();
const char *BKE_appdir_folder_default() ATTR_WARN_UNUSED_RESULT;
const char *BKE_appdir_folder_root() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL;
const char *BKE_appdir_folder_default_or_root() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL;
/**
* Get the user's home directory, i.e.
* - Unix: `$HOME`
* - Windows: `%userprofile%`
*/
const char *BKE_appdir_folder_home();
/**
* Get the user's document directory, i.e.
* - Linux: `$HOME/Documents`

View File

@@ -51,9 +51,6 @@
# endif
/* #mkdtemp on OSX (and probably all *BSD?), not worth making specific check for this OS. */
# include <unistd.h>
# include <pwd.h> /* For `passwd` access. */
#endif /* !WIN32 */
static const char _str_null[] = "(null)";
@@ -137,7 +134,7 @@ static char *blender_version_decimal(const int version)
const char *BKE_appdir_folder_default()
{
#ifndef WIN32
return BKE_appdir_folder_home();
return BLI_dir_home();
#else /* Windows */
static char documentfolder[FILE_MAXDIR];
@@ -169,30 +166,6 @@ const char *BKE_appdir_folder_default_or_root()
return path;
}
const char *BKE_appdir_folder_home()
{
const char *home_dir = nullptr;
#ifdef WIN32
home_dir = BLI_getenv("userprofile");
#else
# if defined(__APPLE__)
home_dir = BLI_expand_tilde("~/");
# endif
if (home_dir == nullptr) {
home_dir = BLI_getenv("HOME");
if (home_dir == nullptr) {
if (const passwd *pwuser = getpwuid(getuid())) {
home_dir = pwuser->pw_dir;
}
}
}
#endif
return home_dir;
}
bool BKE_appdir_folder_documents(char *dir)
{
dir[0] = '\0';
@@ -207,7 +180,7 @@ bool BKE_appdir_folder_documents(char *dir)
/* 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();
const char *home_path = BLI_dir_home();
if (!home_path || !BLI_is_dir(home_path)) {
return false;
}

View File

@@ -227,6 +227,14 @@ double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(
* \note can return NULL when the size is not big enough
*/
char *BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
/**
* Get the user's home directory, i.e.
* - Unix: `$HOME`
* - Windows: `%userprofile%`
*/
const char *BLI_dir_home(void);
eFileAttributes BLI_file_attributes(const char *path);
/**
* Changes the current working directory to the provided path.

View File

@@ -107,6 +107,30 @@ char *BLI_current_working_dir(char *dir, const size_t maxncpy)
}
#endif /* !defined (__APPLE__) */
const char *BLI_dir_home()
{
const char *home_dir = nullptr;
#ifdef WIN32
home_dir = BLI_getenv("userprofile");
#else
# if defined(__APPLE__)
home_dir = BLI_expand_tilde("~/");
# endif
if (home_dir == nullptr) {
home_dir = BLI_getenv("HOME");
if (home_dir == nullptr) {
if (const passwd *pwuser = getpwuid(getuid())) {
home_dir = pwuser->pw_dir;
}
}
}
#endif
return home_dir;
}
double BLI_dir_free_space(const char *dir)
{
#ifdef WIN32

View File

@@ -2773,7 +2773,7 @@ static void file_expand_directory(const Main *bmain, FileSelectParams *params)
/* While path handling expansion typically doesn't support home directory expansion
* in Blender, this is a convenience to be able to type in a single character.
* Even though this is a UNIX convention, it's harmless to expand on WIN32 as well. */
if (const char *home_dir = BKE_appdir_folder_home()) {
if (const char *home_dir = BLI_dir_home()) {
char tmpstr[sizeof(params->dir) - 1];
STRNCPY(tmpstr, params->dir + 1);
BLI_path_join(params->dir, sizeof(params->dir), home_dir, tmpstr);