提交 82320a9c 编写于 作者: J jp9000

(API Change) Make os_get_config_path safer to use

Changed:
  char *os_get_config_path(const char *name);

To:
  int os_get_config_path(char *dst, size_t size, const char *name);

Also added:
  char *os_get_config_path_ptr(const char *name);

I don't like this function returning an allocation by default.
Similarly to what was done with the wide character conversion functions,
this function now operates on an array argument, and if you really want
to just get a pointer for convenience, you use the *_ptr version of the
function that clearly indicates that it's returning an allocation.
上级 2fec0f82
...@@ -68,7 +68,20 @@ uint64_t os_gettime_ns(void) ...@@ -68,7 +68,20 @@ uint64_t os_gettime_ns(void)
} }
/* gets the location ~/Library/Application Support/[name] */ /* gets the location ~/Library/Application Support/[name] */
char *os_get_config_path(const char *name) int os_get_config_path(char *dst, size_t size, const char *name)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSUserDomainMask, YES);
if([paths count] == 0)
bcrash("Could not get home directory (platform-cocoa)");
NSString *application_support = paths[0];
const char *base_path = [application_support UTF8String];
return snprintf(dst, size, "%s/%s", base_path, name);
}
char *os_get_config_path_ptr(const char *name)
{ {
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSUserDomainMask, YES); NSApplicationSupportDirectory, NSUserDomainMask, YES);
......
...@@ -154,7 +154,16 @@ uint64_t os_gettime_ns(void) ...@@ -154,7 +154,16 @@ uint64_t os_gettime_ns(void)
} }
/* should return $HOME/.[name] */ /* should return $HOME/.[name] */
char *os_get_config_path(const char *name) int os_get_config_path(char *dst, size_t size, const char *name)
{
char *path_ptr = getenv("HOME");
if (path_ptr == NULL)
bcrash("Could not get $HOME\n");
return snprintf(dst, size, "%s/.%s", path_ptr, name);
}
char *os_get_config_path_ptr(const char *name)
{ {
char *path_ptr = getenv("HOME"); char *path_ptr = getenv("HOME");
if (path_ptr == NULL) if (path_ptr == NULL)
......
...@@ -191,7 +191,25 @@ uint64_t os_gettime_ns(void) ...@@ -191,7 +191,25 @@ uint64_t os_gettime_ns(void)
} }
/* returns %appdata%\[name] on windows */ /* returns %appdata%\[name] on windows */
char *os_get_config_path(const char *name) int os_get_config_path(char *dst, size_t size, const char *name)
{
wchar_t path_utf16[MAX_PATH];
SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
path_utf16);
if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) {
if (strcat_s(dst, size, "\\") == 0) {
if (strcat_s(dst, size, name) == 0) {
return (int)strlen(dst);
}
}
}
return -1;
}
char *os_get_config_path_ptr(const char *name)
{ {
char *ptr; char *ptr;
wchar_t path_utf16[MAX_PATH]; wchar_t path_utf16[MAX_PATH];
......
...@@ -90,7 +90,8 @@ EXPORT void os_sleep_ms(uint32_t duration); ...@@ -90,7 +90,8 @@ EXPORT void os_sleep_ms(uint32_t duration);
EXPORT uint64_t os_gettime_ns(void); EXPORT uint64_t os_gettime_ns(void);
EXPORT char *os_get_config_path(const char *name); EXPORT int os_get_config_path(char *dst, size_t size, const char *name);
EXPORT char *os_get_config_path_ptr(const char *name);
EXPORT bool os_file_exists(const char *path); EXPORT bool os_file_exists(const char *path);
......
...@@ -127,17 +127,20 @@ static bool do_mkdir(const char *path) ...@@ -127,17 +127,20 @@ static bool do_mkdir(const char *path)
static bool MakeUserDirs() static bool MakeUserDirs()
{ {
BPtr<char> path; char path[512];
path = os_get_config_path("obs-studio"); if (os_get_config_path(path, sizeof(path), "obs-studio") <= 0)
return false;
if (!do_mkdir(path)) if (!do_mkdir(path))
return false; return false;
path = os_get_config_path("obs-studio/basic"); if (os_get_config_path(path, sizeof(path), "obs-studio/basic") <= 0)
return false;
if (!do_mkdir(path)) if (!do_mkdir(path))
return false; return false;
path = os_get_config_path("obs-studio/logs"); if (os_get_config_path(path, sizeof(path), "obs-studio/logs") <= 0)
return false;
if (!do_mkdir(path)) if (!do_mkdir(path))
return false; return false;
...@@ -146,7 +149,13 @@ static bool MakeUserDirs() ...@@ -146,7 +149,13 @@ static bool MakeUserDirs()
bool OBSApp::InitGlobalConfig() bool OBSApp::InitGlobalConfig()
{ {
BPtr<char> path(os_get_config_path("obs-studio/global.ini")); char path[512];
int len = os_get_config_path(path, sizeof(path),
"obs-studio/global.ini");
if (len <= 0) {
return false;
}
int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS); int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
if (errorcode != CONFIG_SUCCESS) { if (errorcode != CONFIG_SUCCESS) {
...@@ -408,7 +417,7 @@ static uint64_t convert_log_name(const char *name) ...@@ -408,7 +417,7 @@ static uint64_t convert_log_name(const char *name)
static void delete_oldest_log(void) static void delete_oldest_log(void)
{ {
BPtr<char> logDir(os_get_config_path("obs-studio/logs")); BPtr<char> logDir(os_get_config_path_ptr("obs-studio/logs"));
string oldestLog; string oldestLog;
uint64_t oldest_ts = (uint64_t)-1; uint64_t oldest_ts = (uint64_t)-1;
struct os_dirent *entry; struct os_dirent *entry;
...@@ -449,7 +458,7 @@ static void delete_oldest_log(void) ...@@ -449,7 +458,7 @@ static void delete_oldest_log(void)
static void get_last_log(void) static void get_last_log(void)
{ {
BPtr<char> logDir(os_get_config_path("obs-studio/logs")); BPtr<char> logDir(os_get_config_path_ptr("obs-studio/logs"));
struct os_dirent *entry; struct os_dirent *entry;
os_dir_t *dir = os_opendir(logDir); os_dir_t *dir = os_opendir(logDir);
uint64_t highest_ts = 0; uint64_t highest_ts = 0;
...@@ -522,7 +531,7 @@ static void create_log_file(fstream &logFile) ...@@ -522,7 +531,7 @@ static void create_log_file(fstream &logFile)
currentLogFile = GenerateTimeDateFilename("txt"); currentLogFile = GenerateTimeDateFilename("txt");
dst << "obs-studio/logs/" << currentLogFile.c_str(); dst << "obs-studio/logs/" << currentLogFile.c_str();
BPtr<char> path(os_get_config_path(dst.str().c_str())); BPtr<char> path(os_get_config_path_ptr(dst.str().c_str()));
logFile.open(path, logFile.open(path,
ios_base::in | ios_base::out | ios_base::trunc); ios_base::in | ios_base::out | ios_base::trunc);
......
...@@ -62,10 +62,11 @@ Q_DECLARE_METATYPE(obs_order_movement); ...@@ -62,10 +62,11 @@ Q_DECLARE_METATYPE(obs_order_movement);
static void AddExtraModulePaths() static void AddExtraModulePaths()
{ {
BPtr<char> base_module_dir = char base_module_dir[512];
os_get_config_path("obs-studio/plugins/%module%"); int ret = os_get_config_path(base_module_dir, sizeof(base_module_dir),
"obs-studio/plugins/%module%");
if (!base_module_dir) if (ret <= 0)
return; return;
string path = (char*)base_module_dir; string path = (char*)base_module_dir;
...@@ -319,8 +320,10 @@ void OBSBasic::SaveService() ...@@ -319,8 +320,10 @@ void OBSBasic::SaveService()
if (!service) if (!service)
return; return;
BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH)); char serviceJsonPath[512];
if (!serviceJsonPath) int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath),
SERVICE_PATH);
if (ret <= 0)
return; return;
obs_data_t *data = obs_data_create(); obs_data_t *data = obs_data_create();
...@@ -341,8 +344,10 @@ bool OBSBasic::LoadService() ...@@ -341,8 +344,10 @@ bool OBSBasic::LoadService()
{ {
const char *type; const char *type;
BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH)); char serviceJsonPath[512];
if (!serviceJsonPath) int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath),
SERVICE_PATH);
if (ret <= 0)
return false; return false;
BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath); BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath);
...@@ -498,7 +503,13 @@ bool OBSBasic::InitBasicConfigDefaults() ...@@ -498,7 +503,13 @@ bool OBSBasic::InitBasicConfigDefaults()
bool OBSBasic::InitBasicConfig() bool OBSBasic::InitBasicConfig()
{ {
BPtr<char> configPath(os_get_config_path("obs-studio/basic/basic.ini")); char configPath[512];
int ret = os_get_config_path(configPath, sizeof(configPath),
"obs-studio/basic/basic.ini");
if (ret <= 0) {
OBSErrorBox(nullptr, "Failed to get base.ini path");
return false;
}
int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS); int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
if (code != CONFIG_SUCCESS) { if (code != CONFIG_SUCCESS) {
...@@ -549,7 +560,11 @@ void OBSBasic::InitPrimitives() ...@@ -549,7 +560,11 @@ void OBSBasic::InitPrimitives()
void OBSBasic::OBSInit() void OBSBasic::OBSInit()
{ {
BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json")); char savePath[512];
int ret = os_get_config_path(savePath, sizeof(savePath),
"obs-studio/basic/scenes.json");
if (ret <= 0)
throw "Failed to get scenes.json file path";
/* make sure it's fully displayed before doing any initialization */ /* make sure it's fully displayed before doing any initialization */
show(); show();
...@@ -562,7 +577,7 @@ void OBSBasic::OBSInit() ...@@ -562,7 +577,7 @@ void OBSBasic::OBSInit()
if (!ResetAudio()) if (!ResetAudio())
throw "Failed to initialize audio"; throw "Failed to initialize audio";
int ret = ResetVideo(); ret = ResetVideo();
switch (ret) { switch (ret) {
case OBS_VIDEO_MODULE_NOT_FOUND: case OBS_VIDEO_MODULE_NOT_FOUND:
...@@ -646,7 +661,12 @@ OBSBasic::~OBSBasic() ...@@ -646,7 +661,12 @@ OBSBasic::~OBSBasic()
void OBSBasic::SaveProject() void OBSBasic::SaveProject()
{ {
BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json")); char savePath[512];
int ret = os_get_config_path(savePath, sizeof(savePath),
"obs-studio/basic/scenes.json");
if (ret <= 0)
return;
SaveService(); SaveService();
Save(savePath); Save(savePath);
} }
...@@ -1966,7 +1986,9 @@ void OBSBasic::on_actionMoveToBottom_triggered() ...@@ -1966,7 +1986,9 @@ void OBSBasic::on_actionMoveToBottom_triggered()
static BPtr<char> ReadLogFile(const char *log) static BPtr<char> ReadLogFile(const char *log)
{ {
BPtr<char> logDir(os_get_config_path("obs-studio/logs")); char logDir[512];
if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
return nullptr;
string path = (char*)logDir; string path = (char*)logDir;
path += "/"; path += "/";
...@@ -2033,7 +2055,10 @@ void OBSBasic::UploadLog(const char *file) ...@@ -2033,7 +2055,10 @@ void OBSBasic::UploadLog(const char *file)
void OBSBasic::on_actionShowLogs_triggered() void OBSBasic::on_actionShowLogs_triggered()
{ {
BPtr<char> logDir(os_get_config_path("obs-studio/logs")); char logDir[512];
if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
return;
QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir)); QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir));
QDesktopServices::openUrl(url); QDesktopServices::openUrl(url);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册