提交 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)
}
/* 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(
NSApplicationSupportDirectory, NSUserDomainMask, YES);
......
......@@ -154,7 +154,16 @@ uint64_t os_gettime_ns(void)
}
/* 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");
if (path_ptr == NULL)
......
......@@ -191,7 +191,25 @@ uint64_t os_gettime_ns(void)
}
/* 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;
wchar_t path_utf16[MAX_PATH];
......
......@@ -90,7 +90,8 @@ EXPORT void os_sleep_ms(uint32_t duration);
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);
......
......@@ -127,17 +127,20 @@ static bool do_mkdir(const char *path)
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))
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))
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))
return false;
......@@ -146,7 +149,13 @@ static bool MakeUserDirs()
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);
if (errorcode != CONFIG_SUCCESS) {
......@@ -408,7 +417,7 @@ static uint64_t convert_log_name(const char *name)
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;
uint64_t oldest_ts = (uint64_t)-1;
struct os_dirent *entry;
......@@ -449,7 +458,7 @@ static void delete_oldest_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;
os_dir_t *dir = os_opendir(logDir);
uint64_t highest_ts = 0;
......@@ -522,7 +531,7 @@ static void create_log_file(fstream &logFile)
currentLogFile = GenerateTimeDateFilename("txt");
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,
ios_base::in | ios_base::out | ios_base::trunc);
......
......@@ -62,10 +62,11 @@ Q_DECLARE_METATYPE(obs_order_movement);
static void AddExtraModulePaths()
{
BPtr<char> base_module_dir =
os_get_config_path("obs-studio/plugins/%module%");
char base_module_dir[512];
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;
string path = (char*)base_module_dir;
......@@ -319,8 +320,10 @@ void OBSBasic::SaveService()
if (!service)
return;
BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
if (!serviceJsonPath)
char serviceJsonPath[512];
int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath),
SERVICE_PATH);
if (ret <= 0)
return;
obs_data_t *data = obs_data_create();
......@@ -341,8 +344,10 @@ bool OBSBasic::LoadService()
{
const char *type;
BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
if (!serviceJsonPath)
char serviceJsonPath[512];
int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath),
SERVICE_PATH);
if (ret <= 0)
return false;
BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath);
......@@ -498,7 +503,13 @@ bool OBSBasic::InitBasicConfigDefaults()
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);
if (code != CONFIG_SUCCESS) {
......@@ -549,7 +560,11 @@ void OBSBasic::InitPrimitives()
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 */
show();
......@@ -562,7 +577,7 @@ void OBSBasic::OBSInit()
if (!ResetAudio())
throw "Failed to initialize audio";
int ret = ResetVideo();
ret = ResetVideo();
switch (ret) {
case OBS_VIDEO_MODULE_NOT_FOUND:
......@@ -646,7 +661,12 @@ OBSBasic::~OBSBasic()
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();
Save(savePath);
}
......@@ -1966,7 +1986,9 @@ void OBSBasic::on_actionMoveToBottom_triggered()
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;
path += "/";
......@@ -2033,7 +2055,10 @@ void OBSBasic::UploadLog(const char *file)
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));
QDesktopServices::openUrl(url);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册