path, win32: redo user configfile path handling

Remove the ifdef hell from mp_find_user_config_file(). Move the win32
specific code (for MinGW and Cygwin) to path-win.c. The behavior should
be about the same, but I can't be sure due to lack of testing and
because the old path.c code was hard to follow. (I expect those who care
about windows will fix things, should issues pop up - sorry.)

One difference is that the new code will always force MPV_HOME. It looks
like the old code preferred the mpv config dir in the exe dir if it
exists.

Also, make sure MP_PATH_MAX has enough space, even if the equivalent
wchar_t string is not 0-terminated with PATH_MAX (because apparently the
winapi doesn't require this). (Actually, maybe we should just kill all
uses of PATH_MAX/MP_PATH_MAX.)
This commit is contained in:
wm4 2013-09-18 18:42:18 +02:00
parent 12372298a2
commit 1cb55cebf9
5 changed files with 102 additions and 72 deletions

View File

@ -54,6 +54,7 @@ SOURCES-$(MPG123) += audio/decode/ad_mpg123.c
SOURCES-$(NEED_GETTIMEOFDAY) += osdep/gettimeofday.c
SOURCES-$(NEED_GLOB) += osdep/glob-win.c
SOURCES-$(WIN32) += osdep/path-win.c
SOURCES-$(PRIORITY) += osdep/priority.c
SOURCES-$(PVR) += stream/stream_pvr.c

View File

@ -37,14 +37,7 @@
#include "mpvcore/path.h"
#include "talloc.h"
#include "osdep/io.h"
#if defined(__MINGW32__)
#include <windows.h>
#include <shlobj.h>
#elif defined(__CYGWIN__)
#include <windows.h>
#include <sys/cygwin.h>
#endif
#include "osdep/path.h"
#ifdef CONFIG_COCOA
#include "osdep/macosx_bundle.h"
@ -77,75 +70,27 @@ char *mp_find_config_file(const char *filename)
char *mp_find_user_config_file(const char *filename)
{
char *homedir = NULL, *buff = NULL;
char *homepath = NULL;
#ifdef __MINGW32__
char *config_dir = "mpv";
char buf[MAX_PATH];
char *homedir = getenv("MPV_HOME");
char *configdir = NULL;
char *result = NULL;
if (homepath == NULL) {
if (SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, NULL,
SHGFP_TYPE_CURRENT, buf) == S_OK) {
homepath = buf;
}
}
#else
char *config_dir = ".mpv";
if (homepath == NULL) {
homepath = getenv("HOME");
}
if (!homedir) {
#ifdef _WIN32
result = mp_get_win_config_path(filename);
#endif
#if defined(__MINGW32__) || defined(__CYGWIN__)
char *temp = NULL;
char exedir[260];
/* Hack to get fonts etc. loaded outside of Cygwin environment. */
int i, imax = 0;
int len = (int)GetModuleFileNameA(NULL, exedir, 260);
for (i = 0; i < len; i++) {
if (exedir[i] == '\\') {
exedir[i] = '/';
imax = i;
}
homedir = getenv("HOME");
configdir = ".mpv";
}
exedir[imax] = '\0';
if (filename) {
temp = mp_path_join(NULL, bstr0(exedir), bstr0(filename));
}
if (temp && mp_path_exists(temp) && !mp_path_isdir(temp)) {
homedir = exedir;
config_dir = "";
}
else
#endif
if ((homedir = getenv("MPV_HOME")) != NULL) {
config_dir = "";
} else if ((homedir = homepath) == NULL) {
#if defined(__MINGW32__) || defined(__CYGWIN__)
homedir = exedir;
#else
return NULL;
#endif
}
#if defined(__MINGW32__) || defined(__CYGWIN__)
talloc_free(temp);
#endif
if (filename) {
char * temp = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir));
buff = mp_path_join(NULL, bstr0(temp), bstr0(filename));
if (!result && homedir) {
char *temp = mp_path_join(NULL, bstr0(homedir), bstr0(configdir));
result = mp_path_join(NULL, bstr0(temp), bstr0(filename));
talloc_free(temp);
} else {
buff = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir));
}
mp_msg(MSGT_GLOBAL, MSGL_V, "get_path('%s') -> '%s'\n", filename, buff);
return buff;
mp_msg(MSGT_GLOBAL, MSGL_V, "mp_find_user_config_file('%s') -> '%s'\n",
filename ? filename : "(NULL)", result ? result : "(NULL)");
return result;
}
char *mp_find_global_config_file(const char *filename)

View File

@ -43,9 +43,11 @@ char *mp_to_utf8(void *talloc_ctx, const wchar_t *s);
// applies to unicode paths encoded with wchar_t (2 bytes on Windows). The UTF-8
// version could end up bigger in memory. In the worst case each wchar_t is
// encoded to 3 bytes in UTF-8, so in the worst case we have:
// wcslen(wpath) <= strlen(utf8path) * 3
// wcslen(wpath) * 3 <= strlen(utf8path)
// Thus we need MP_PATH_MAX as the UTF-8/char version of PATH_MAX.
#define MP_PATH_MAX (FILENAME_MAX * 3)
// Also make sure there's free space for the terminating \0.
// (For codepoints encoded as UTF-16 surrogate pairs, UTF-8 has the same length.)
#define MP_PATH_MAX (FILENAME_MAX * 3 + 1)
void mp_get_converted_argv(int *argc, char ***argv);

74
osdep/path-win.c Normal file
View File

@ -0,0 +1,74 @@
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <shlobj.h>
#include "osdep/path.h"
#include "osdep/io.h"
#include "mpvcore/path.h"
// Warning: do not use PATH_MAX. Cygwin messed it up.
static void get_exe_dir(wchar_t path[MAX_PATH + 1])
{
int len = (int)GetModuleFileNameW(NULL, path, MAX_PATH);
int imax = 0;
for (int i = 0; i < len; i++) {
if (path[i] == '\\') {
path[i] = '/';
imax = i;
}
}
path[imax] = '\0';
}
char *mp_get_win_config_path(const char *filename)
{
wchar_t w_appdir[MAX_PATH + 1] = {0};
wchar_t w_exedir[MAX_PATH + 1] = {0};
char *res = NULL;
void *tmp = talloc_new(NULL);
#ifndef __CYGWIN__
if (SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, NULL,
SHGFP_TYPE_CURRENT, w_appdir) != S_OK)
w_appdir[0] = '\0';
#endif
get_exe_dir(w_exedir);
if (filename && filename[0] && w_exedir[0]) {
char *dir = mp_to_utf8(tmp, w_exedir);
char *temp = mp_path_join(tmp, bstr0(dir), bstr0("mpv"));
res = mp_path_join(NULL, bstr0(temp), bstr0(filename));
if (!mp_path_exists(res) || mp_path_isdir(res)) {
talloc_free(res);
res = NULL;
}
}
if (!res && w_appdir[0]) {
char *dir = mp_to_utf8(tmp, w_appdir);
char *temp = mp_path_join(tmp, bstr0(dir), bstr0("mpv"));
res = mp_path_join(NULL, bstr0(temp), bstr0(filename));
}
talloc_free(tmp);
return res;
}

8
osdep/path.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef OSDEP_PATH_H
#define OSDEP_PATH_H
#ifdef _WIN32
char *mp_get_win_config_path(const char *filename);
#endif
#endif