From: RĂ©mi Denis-Courmont Date: Mon, 24 Aug 2009 18:25:12 +0000 (+0300) Subject: Add a directory type parameter to config_GetHomeDir X-Git-Tag: 1.1.0-ff~3963 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=08d89300a6a09b812a406ecdf12c598fdf581d5d;p=vlc Add a directory type parameter to config_GetHomeDir This will avoid adding plenty of config_GetFoobarDir exports later. Also make config_GetHomeDir return a heap-allocated string. --- diff --git a/include/vlc_configuration.h b/include/vlc_configuration.h index 1c2c48fe1c..e5df206260 100644 --- a/include/vlc_configuration.h +++ b/include/vlc_configuration.h @@ -217,9 +217,14 @@ VLC_EXPORT( module_config_t *, config_FindConfig,( vlc_object_t *, const char * VLC_EXPORT(const char *, config_GetDataDir, ( void ) LIBVLC_USED); VLC_EXPORT(const char *, config_GetConfDir, ( void ) LIBVLC_USED); -VLC_EXPORT(const char *, config_GetHomeDir, ( void ) LIBVLC_USED); VLC_EXPORT(char *, config_GetUserConfDir, ( void ) LIBVLC_USED); VLC_EXPORT(char *, config_GetUserDataDir, ( void ) LIBVLC_USED); + +typedef enum vlc_userdir { + VLC_HOME_DIR, +} vlc_userdir_t; + +VLC_EXPORT(char *, config_GetUserDir, ( vlc_userdir_t ) LIBVLC_USED); VLC_EXPORT(char *, config_GetCacheDir, ( void ) LIBVLC_USED); VLC_EXPORT( void, __config_AddIntf, ( vlc_object_t *, const char * ) ); diff --git a/modules/access_filter/dump.c b/modules/access_filter/dump.c index 33a30422ad..fcfb8cabe7 100644 --- a/modules/access_filter/dump.c +++ b/modules/access_filter/dump.c @@ -271,12 +271,15 @@ static void Trigger (access_t *access) // and there is an off-by-one in the following sprintf(). return; - const char *home = config_GetHomeDir(); + char *dir = config_GetUserDir( VLC_HOME_DIR ); + if( dir == NULL ) + return; /* Hmm what about the extension?? */ - char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")]; - sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home, + char filename[strlen (dir) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")]; + sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", dir, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); + free( dir ); msg_Info (access, "dumping media to \"%s\"...", filename); diff --git a/modules/control/http/util.c b/modules/control/http/util.c index bb78bb7f4a..7cd4af7de3 100644 --- a/modules/control/http/util.c +++ b/modules/control/http/util.c @@ -951,9 +951,10 @@ char *RealPath( const char *psz_src ) if( psz_dir[0] == '~' ) { - char *dir; - asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 ); + char *home = config_GetUserDir( VLC_HOME_DIR ), *dir; + asprintf( &dir, "%s%s", home, psz_dir + 1 ); free( psz_dir ); + free( home ); psz_dir = dir; } diff --git a/modules/demux/mp4/libmp4.c b/modules/demux/mp4/libmp4.c index c4e9db15d8..8df00de92b 100644 --- a/modules/demux/mp4/libmp4.c +++ b/modules/demux/mp4/libmp4.c @@ -1243,12 +1243,12 @@ static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box ) if( p_box->i_type == FOURCC_drms ) { - p_box->data.p_sample_soun->p_drms = - drms_alloc( config_GetHomeDir() ); - - if( p_box->data.p_sample_soun->p_drms == NULL ) + char *home = config_GetUserDir( VLC_HOME_DIR ); + if( home != NULL ) { - msg_Err( p_stream, "drms_alloc() failed" ); + p_box->data.p_sample_soun->p_drms = drms_alloc( home ); + if( p_box->data.p_sample_soun->p_drms == NULL ) + msg_Err( p_stream, "drms_alloc() failed" ); } } @@ -1344,12 +1344,12 @@ int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box ) if( p_box->i_type == FOURCC_drmi ) { - p_box->data.p_sample_vide->p_drms = - drms_alloc( config_GetHomeDir() ); - - if( p_box->data.p_sample_vide->p_drms == NULL ) + char *home = config_GetUserDir( VLC_HOME_DIR ); + if( home != NULL ) { - msg_Err( p_stream, "drms_alloc() failed" ); + p_box->data.p_sample_vide->p_drms = drms_alloc( home ); + if( p_box->data.p_sample_vide->p_drms == NULL ) + msg_Err( p_stream, "drms_alloc() failed" ); } } diff --git a/modules/gui/ncurses.c b/modules/gui/ncurses.c index 04934d2fff..67afea8899 100644 --- a/modules/gui/ncurses.c +++ b/modules/gui/ncurses.c @@ -310,7 +310,7 @@ static int Open( vlc_object_t *p_this ) p_sys->psz_current_dir = psz_tmp; else { - p_sys->psz_current_dir = strdup( config_GetHomeDir() ); + p_sys->psz_current_dir = config_GetUserDir( VLC_HOME_DIR ); free( psz_tmp ); } diff --git a/modules/gui/qt4/components/preferences_widgets.cpp b/modules/gui/qt4/components/preferences_widgets.cpp index 2b6c99248f..a96e4379fe 100644 --- a/modules/gui/qt4/components/preferences_widgets.cpp +++ b/modules/gui/qt4/components/preferences_widgets.cpp @@ -327,7 +327,7 @@ FileConfigControl::FileConfigControl( vlc_object_t *_p_this, void FileConfigControl::updateField() { QString file = QFileDialog::getOpenFileName( NULL, - qtr( "Select File" ), qfu( config_GetHomeDir() ) ); + qtr( "Select File" ), QVLCUserDir( VLC_HOME_DIR ) ); if( file.isNull() ) return; text->setText( toNativeSeparators( file ) ); } @@ -361,7 +361,7 @@ void DirectoryConfigControl::updateField() QString dir = QFileDialog::getExistingDirectory( NULL, qtr( "Select Directory" ), text->text().isEmpty() ? - qfu( config_GetHomeDir() ) : text->text(), + QVLCUserDir( VLC_HOME_DIR ) : text->text(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks ); if( dir.isNull() ) return; diff --git a/modules/gui/qt4/dialogs/help.cpp b/modules/gui/qt4/dialogs/help.cpp index 13d5531eea..edcba5253c 100644 --- a/modules/gui/qt4/dialogs/help.cpp +++ b/modules/gui/qt4/dialogs/help.cpp @@ -271,7 +271,7 @@ void UpdateDialog::UpdateOrDownload() { QString dest_dir = QFileDialog::getExistingDirectory( this, qtr( "Select a directory..." ), - qfu( config_GetHomeDir() ) ); + QVLCUserDir( VLC_DOWNLOAD_DIR ) ); if( !dest_dir.isEmpty() ) { diff --git a/modules/gui/qt4/dialogs/messages.cpp b/modules/gui/qt4/dialogs/messages.cpp index a2371df3c6..f491b6e747 100644 --- a/modules/gui/qt4/dialogs/messages.cpp +++ b/modules/gui/qt4/dialogs/messages.cpp @@ -245,7 +245,7 @@ bool MessagesDialog::save() { QString saveLogFileName = QFileDialog::getSaveFileName( this, qtr( "Save log file as..." ), - qfu( config_GetHomeDir() ), + QVLCUserDir( VLC_HOME_DIR ), qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") ); if( !saveLogFileName.isNull() ) diff --git a/modules/gui/qt4/dialogs/vlm.cpp b/modules/gui/qt4/dialogs/vlm.cpp index 31b2a5be78..8a706dc2f6 100644 --- a/modules/gui/qt4/dialogs/vlm.cpp +++ b/modules/gui/qt4/dialogs/vlm.cpp @@ -267,7 +267,7 @@ bool VLMDialog::exportVLMConf() { QString saveVLMConfFileName = QFileDialog::getSaveFileName( this, qtr( "Save VLM configuration as..." ), - qfu( config_GetHomeDir() ), + QVLCUserDir( VLC_HOME_DIR ), qtr( "VLM conf (*.vlm);;All (*)" ) ); if( !saveVLMConfFileName.isEmpty() ) @@ -339,7 +339,7 @@ bool VLMDialog::importVLMConf() QString openVLMConfFileName = toNativeSeparators( QFileDialog::getOpenFileName( this, qtr( "Open VLM configuration..." ), - qfu( config_GetHomeDir() ), + QVLCUserDir( VLC_HOME_DIR ), qtr( "VLM conf (*.vlm);;All (*)" ) ) ); if( !openVLMConfFileName.isEmpty() ) diff --git a/modules/gui/qt4/qt4.cpp b/modules/gui/qt4/qt4.cpp index a6b44c3394..e1128ac55c 100644 --- a/modules/gui/qt4/qt4.cpp +++ b/modules/gui/qt4/qt4.cpp @@ -453,7 +453,7 @@ static void *Thread( void *obj ) /* Retrieve last known path used in file browsing */ p_intf->p_sys->filepath = - getSettings()->value( "filedialog-path", config_GetHomeDir() ).toString(); + getSettings()->value( "filedialog-path", QVLCUserDir( VLC_HOME_DIR ) ).toString(); /* Loads and tries to apply the preferred QStyle */ QString s_style = getSettings()->value( "MainWindow/QtStyle", "" ).toString(); diff --git a/modules/gui/qt4/qt4.hpp b/modules/gui/qt4/qt4.hpp index 6bde271569..c93754a4a1 100644 --- a/modules/gui/qt4/qt4.hpp +++ b/modules/gui/qt4/qt4.hpp @@ -120,5 +120,14 @@ struct intf_sys_t #define getSettings() p_intf->p_sys->mainSettings +static inline QString QVLCUserDir( vlc_userdir_t type ) +{ + char *dir = config_GetUserDir( type ); + if( !dir ) + abort(); + QString res = qfu( dir ); + free( dir ); + return res; +} #endif diff --git a/modules/misc/logger.c b/modules/misc/logger.c index 9a8a9d020e..db842d13ac 100644 --- a/modules/misc/logger.c +++ b/modules/misc/logger.c @@ -234,10 +234,13 @@ static int Open( vlc_object_t *p_this ) if( !psz_file ) { #ifdef __APPLE__ - if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(), + char *home = config_GetUserDir(VLC_HOME_DIR); + if( home == NULL + || asprintf( &psz_file, "%s/"LOG_DIR"/%s", home, (p_sys->msg.i_mode == MODE_HTML) ? LOG_FILE_HTML : LOG_FILE_TEXT ) == -1 ) psz_file = NULL; + free(home); #else switch( p_sys->msg.i_mode ) { diff --git a/modules/misc/lua/libs/misc.c b/modules/misc/lua/libs/misc.c index 7aab9ec9d2..1bbc1b0aad 100644 --- a/modules/misc/lua/libs/misc.c +++ b/modules/misc/lua/libs/misc.c @@ -126,7 +126,9 @@ static int vlclua_userdatadir( lua_State *L ) static int vlclua_homedir( lua_State *L ) { - lua_pushstring( L, config_GetHomeDir() ); + char *home = config_GetUserDir( VLC_HOME_DIR ); + lua_pushstring( L, home ); + free( home ); return 1; } diff --git a/modules/stream_filter/record.c b/modules/stream_filter/record.c index 51fd62370e..dee348ac01 100644 --- a/modules/stream_filter/record.c +++ b/modules/stream_filter/record.c @@ -175,12 +175,9 @@ static int Start( stream_t *s, const char *psz_extension ) psz_extension = "dat"; /* Retreive path */ - char *psz_path = var_CreateGetString( s, "input-record-path" ); - if( !psz_path || *psz_path == '\0' ) - { - free( psz_path ); - psz_path = strdup( config_GetHomeDir() ); - } + char *psz_path = var_CreateGetNonEmptyString( s, "input-record-path" ); + if( !psz_path ) + psz_path = config_GetUserDir( VLC_HOME_DIR ); if( !psz_path ) return VLC_ENOMEM; diff --git a/modules/video_filter/scene.c b/modules/video_filter/scene.c index dfd3047f13..6a0d6581bd 100644 --- a/modules/video_filter/scene.c +++ b/modules/video_filter/scene.c @@ -193,11 +193,7 @@ static int Create( vlc_object_t *p_this ) p_sys->psz_prefix = var_CreateGetString( p_this, CFG_PREFIX "prefix" ); p_sys->psz_path = var_GetNonEmptyString( p_this, CFG_PREFIX "path" ); if( p_sys->psz_path == NULL ) - { - const char *psz_homedir = config_GetHomeDir(); - if( psz_homedir ) - p_sys->psz_path = strdup( psz_homedir ); - } + p_sys->psz_path = config_GetUserDir( VLC_HOME_DIR ); p_filter->pf_video_filter = Filter; diff --git a/src/config/dirs.c b/src/config/dirs.c index c8cfc822d1..a6314baa90 100644 --- a/src/config/dirs.c +++ b/src/config/dirs.c @@ -78,6 +78,7 @@ const char *config_GetDataDir( void ) #endif } +#if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS) static const char *GetDir( bool b_appdata, bool b_common_appdata ) { /* FIXME: a full memory page here - quite a waste... */ @@ -146,6 +147,7 @@ static const char *GetDir( bool b_appdata, bool b_common_appdata ) #endif return homedir; } +#endif /** * Determines the system configuration directory. @@ -171,12 +173,53 @@ const char *config_GetConfDir( void ) #endif } -/** - * Get the user's home directory - */ -const char *config_GetHomeDir( void ) +static char *config_GetHomeDir (void) +{ +#ifndef WIN32 + /* 1/ Try $HOME */ + const char *home = getenv ("HOME"); +#if defined(HAVE_GETPWUID_R) + /* 2/ Try /etc/passwd */ + char buf[sysconf (_SC_GETPW_R_SIZE_MAX)]; + if (home == NULL) + { + struct passwd pw, *res; + + if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res) + home = pw.pw_dir; + } +#endif + /* 3/ Desperately try $TMP */ + if (home == NULL) + home = getenv( "TMP" ); + /* 4/ Beyond hope, hard-code /tmp */ + if (home == NULL) + home = "/tmp"; + + return FromLocaleDup (home); + +#else /* WIN32 */ + wchar_t wdir[MAX_PATH]; + +# if defined (UNDER_CE) + /*There are some errors in cegcc headers*/ +#undef SHGetSpecialFolderPath + BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL); + if (SHGetSpecialFolderPath (NULL, wdir, CSIDL_APPDATA, 1)) +# else + if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, + NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK) +# endif + return FromWide (wdir); + return NULL; +#endif +} + +char *config_GetUserDir (vlc_userdir_t type) { - return GetDir (false, false); + char *home = config_GetHomeDir (); + (void)type; + return home; } static char *config_GetFooDir (const char *xdg_name, const char *xdg_default) @@ -194,8 +237,7 @@ static char *config_GetFooDir (const char *xdg_name, const char *xdg_default) /* XDG Base Directory Specification - Version 0.6 */ snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name); - const char *psz_home = getenv (var); - psz_home = psz_home ? FromLocale (psz_home) : NULL; + char *psz_home = FromLocale (getenv (var)); if( psz_home ) { if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 ) @@ -204,10 +246,11 @@ static char *config_GetFooDir (const char *xdg_name, const char *xdg_default) return psz_dir; } - /* Try HOME, then fallback to non-XDG dirs */ - psz_home = config_GetHomeDir(); - if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 ) + psz_home = config_GetUserDir (VLC_HOME_DIR); + if( psz_home == NULL + || asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 ) psz_dir = NULL; + free (psz_home); #endif return psz_dir; } diff --git a/src/config/file.c b/src/config/file.c index b3153ea7e1..e1162bdec2 100644 --- a/src/config/file.c +++ b/src/config/file.c @@ -88,9 +88,12 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj ) { /* This is the fallback for pre XDG Base Directory * Specification configs */ + char *home = config_GetUserDir(VLC_HOME_DIR); char *psz_old; - if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE, - config_GetHomeDir() ) != -1 ) + + if( home != NULL + && asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE, + home ) != -1 ) { p_stream = utf8_fopen( psz_old, "rt" ); if( p_stream ) @@ -101,7 +104,7 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj ) "VLC will now use %s.", psz_old, psz_filename ); char *psz_readme; if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README", - config_GetHomeDir() ) != -1 ) + home ) != -1 ) { FILE *p_readme = utf8_fopen( psz_readme, "wt" ); if( p_readme ) @@ -120,6 +123,7 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj ) } free( psz_old ); } + free( home ); } #endif free( psz_filename ); diff --git a/src/input/es_out.c b/src/input/es_out.c index 9ca862851b..4f97a92d64 100644 --- a/src/input/es_out.c +++ b/src/input/es_out.c @@ -464,12 +464,9 @@ static int EsOutSetRecord( es_out_t *out, bool b_record ) if( b_record ) { - char *psz_path = var_CreateGetString( p_input, "input-record-path" ); - if( !psz_path || *psz_path == '\0' ) - { - free( psz_path ); - psz_path = strdup( config_GetHomeDir() ); - } + char *psz_path = var_CreateGetNonEmptyString( p_input, "input-record-path" ); + if( !psz_path ) + psz_path = config_GetUserDir(VLC_HOME_DIR); char *psz_sout = NULL; // TODO conf diff --git a/src/libvlccore.sym b/src/libvlccore.sym index 5d40e4af7c..4f567a9525 100644 --- a/src/libvlccore.sym +++ b/src/libvlccore.sym @@ -54,7 +54,7 @@ config_GetCacheDir config_GetConfDir config_GetDataDir __config_GetFloat -config_GetHomeDir +config_GetUserDir __config_GetInt __config_GetPsz __config_GetType diff --git a/src/video_output/snapshot.c b/src/video_output/snapshot.c index f6beb19a79..1866cfd7ae 100644 --- a/src/video_output/snapshot.c +++ b/src/video_output/snapshot.c @@ -131,67 +131,7 @@ void vout_snapshot_Set(vout_snapshot_t *snap, /* */ char *vout_snapshot_GetDirectory(void) { - char *psz_path = NULL; -#if defined(__APPLE__) || defined(SYS_BEOS) - - if (asprintf(&psz_path, "%s/Desktop", - config_GetHomeDir()) == -1) - psz_path = NULL; - -#elif defined(WIN32) && !defined(UNDER_CE) - - /* Get the My Pictures folder path */ - char *p_mypicturesdir = NULL; - typedef HRESULT (WINAPI *SHGETFOLDERPATH)(HWND, int, HANDLE, DWORD, - LPWSTR); - #ifndef CSIDL_FLAG_CREATE - # define CSIDL_FLAG_CREATE 0x8000 - #endif - #ifndef CSIDL_MYPICTURES - # define CSIDL_MYPICTURES 0x27 - #endif - #ifndef SHGFP_TYPE_CURRENT - # define SHGFP_TYPE_CURRENT 0 - #endif - - HINSTANCE shfolder_dll; - SHGETFOLDERPATH SHGetFolderPath ; - - /* load the shfolder dll to retrieve SHGetFolderPath */ - if ((shfolder_dll = LoadLibrary(_T("SHFolder.dll"))) != NULL) - { - wchar_t wdir[PATH_MAX]; - SHGetFolderPath = (void *)GetProcAddress(shfolder_dll, - _T("SHGetFolderPathW")); - if ((SHGetFolderPath != NULL) - && SUCCEEDED (SHGetFolderPath (NULL, - CSIDL_MYPICTURES | CSIDL_FLAG_CREATE, - NULL, SHGFP_TYPE_CURRENT, - wdir))) - p_mypicturesdir = FromWide (wdir); - - FreeLibrary(shfolder_dll); - } - - if (p_mypicturesdir == NULL) - psz_path = strdup(config_GetHomeDir()); - else - psz_path = p_mypicturesdir; - -#else - - /* XXX: This saves in the data directory. Shouldn't we try saving - * to psz_homedir/Desktop or something nicer ? */ - char *psz_datadir = config_GetUserDataDir(); - if (psz_datadir) - { - if (asprintf(&psz_path, "%s", psz_datadir) == -1) - psz_path = NULL; - free(psz_datadir); - } - -#endif - return psz_path; + return config_GetUserDir(VLC_HOME_DIR); } /* */ int vout_snapshot_SaveImage(char **name, int *sequential,