[libdvdcss-devel] cache: use libxdg-basedir and save cache into XDG-compliant location.

Diego Elio Pettenò git at videolan.org
Tue Feb 26 14:57:15 CET 2013


libdvdcss | branch: xdgcache | Diego Elio Pettenò <flameeyes at flameeyes.eu> | Tue Feb 26 14:55:48 2013 +0100| [8af8d2d561721af6eb30a7e8d26a94063c18c5d1] | committer: Diego Elio Pettenò

cache: use libxdg-basedir and save cache into XDG-compliant location.

For most users, this means that ~/.dvdcss is replaced by
~/.cache/dvdcss, but now it follows XDG_CACHE_HOME instead of
DVDCSS_CACHE to know where to store it.

> http://git.videolan.org/gitweb.cgi/libdvdcss.git/?a=commit;h=8af8d2d561721af6eb30a7e8d26a94063c18c5d1
---

 Makefile.am     |    3 +-
 configure.ac    |    2 +
 src/libdvdcss.c |  126 +++++++++++++------------------------------------------
 3 files changed, 32 insertions(+), 99 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8ff1727..2902b10 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,7 +31,8 @@ libdvdcss_la_SOURCES = \
 	src/common.h
 
 libdvdcss_la_LDFLAGS = -version-info $(DVDCSS_LTVERSION) $(DVDCSS_LDFLAGS)
-libdvdcss_la_LIBADD =
+libdvdcss_la_CFLAGS = $(XDG_CFLAGS)
+libdvdcss_la_LIBADD = $(XDG_LIBS)
 
 if !SYS_MSVC
 libdvdcss_la_LDFLAGS += = -no-undefined
diff --git a/configure.ac b/configure.ac
index 5125197..a59428e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -196,6 +196,8 @@ AC_CHECK_HEADERS(sys/ioctl.h,[
                 Define if FreeBSD-like dvd_struct is defined.)
     fi
   fi
+
+  PKG_CHECK_MODULES([XDG], [libxdg-basedir])
 ])
 
 CC_CHECK_CFLAGS_APPEND([-Wall -Wsign-compare])
diff --git a/src/libdvdcss.c b/src/libdvdcss.c
index 961f072..e481f2e 100644
--- a/src/libdvdcss.c
+++ b/src/libdvdcss.c
@@ -132,6 +132,12 @@
 #define mkdir(a, b) _mkdir(a)
 #endif
 
+#if defined(_WIN32_IE) && _WIN32_IE >= 0x401
+# define DVDCSS_CACHE_WIN32
+#else
+# include <basedir.h>
+#endif
+
 /**
  * \brief Symbol for version checks.
  *
@@ -160,12 +166,12 @@ char * dvdcss_interface_2 = VERSION;
  */
 LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( char *psz_target )
 {
-    char psz_buffer[PATH_MAX];
+    char psz_cache[PATH_MAX];
+    char psz_home[PATH_MAX];
     int i_ret;
 
     char *psz_method = getenv( "DVDCSS_METHOD" );
     char *psz_verbose = getenv( "DVDCSS_VERBOSE" );
-    char *psz_cache = getenv( "DVDCSS_CACHE" );
 #ifdef DVDCSS_RAW_OPEN
     char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" );
 #endif
@@ -233,20 +239,15 @@ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( char *psz_target )
         }
     }
 
-    /*
-     *  If DVDCSS_CACHE was not set, try to guess a default value
-     */
-    if( psz_cache == NULL || psz_cache[0] == '\0' )
+#if DVDCSS_CACHE_WIN32
     {
-#if defined(_WIN32_IE) && _WIN32_IE >= 0x401
-        typedef HRESULT( WINAPI *SHGETFOLDERPATH )
+	 typedef HRESULT( WINAPI *SHGETFOLDERPATH )
                        ( HWND, int, HANDLE, DWORD, LPTSTR );
 
 #   define CSIDL_FLAG_CREATE 0x8000
 #   define CSIDL_APPDATA 0x1A
 #   define SHGFP_TYPE_CURRENT 0
 
-        char psz_home[MAX_PATH];
         HINSTANCE p_dll;
         SHGETFOLDERPATH p_getpath;
 
@@ -275,78 +276,27 @@ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( char *psz_target )
 
         /* Cache our keys in
          * C:\Documents and Settings\$USER\Application Data\dvdcss\ */
-        if( *psz_home )
-        {
-            snprintf( psz_buffer, PATH_MAX, "%s/dvdcss", psz_home );
-            psz_buffer[PATH_MAX-1] = '\0';
-            psz_cache = psz_buffer;
-        }
+    }
 #else
-        char *psz_home = NULL;
-#   ifdef HAVE_PWD_H
-        struct passwd *p_pwd;
-
-        /* Try looking in password file for home dir. */
-        p_pwd = getpwuid(getuid());
-        if( p_pwd )
-        {
-            psz_home = p_pwd->pw_dir;
-        }
-#   endif
-
-        if( psz_home == NULL )
-        {
-            psz_home = getenv( "HOME" );
-        }
-        if( psz_home == NULL )
-        {
-            psz_home = getenv( "USERPROFILE" );
-        }
-
-        /* Cache our keys in ${HOME}/.dvdcss/ */
-        if( psz_home )
-        {
-            int home_pos = 0;
-
-#ifdef __OS2__
-            if( *psz_home == '/' || *psz_home == '\\')
-            {
-                char *psz_unixroot = getenv("UNIXROOT");
-
-                if( psz_unixroot &&
-                    psz_unixroot[0] &&
-                    psz_unixroot[1] == ':'  &&
-                    psz_unixroot[2] == '\0')
-                {
-                    strcpy( psz_buffer, psz_unixroot );
-                    home_pos = 2;
-                }
-            }
-#endif
-            snprintf( psz_buffer + home_pos, PATH_MAX - home_pos,
-                      "%s/.dvdcss", psz_home );
-            psz_buffer[PATH_MAX-1] = '\0';
-            psz_cache = psz_buffer;
-        }
-#endif
+    {
+	static xdgHandle xdg;
+	xdgInitHandle(&xdg);
+	strncpy(psz_home, xdgCacheHome(&xdg), sizeof(psz_home)-1);
+	xdgWipeHandle(&xdg);
     }
+#endif
 
-    /*
-     *  Find cache dir from the DVDCSS_CACHE environment variable
-     */
-    if( psz_cache != NULL )
+    if( *psz_home )
     {
-        if( psz_cache[0] == '\0' || !strcmp( psz_cache, "off" ) )
-        {
-            psz_cache = NULL;
-        }
-        /* Check that we can add the ID directory and the block filename */
-        else if( strlen( psz_cache ) + 1 + 32 + 1 + (KEY_SIZE * 2) + 10 + 1
-                  > PATH_MAX )
-        {
-            print_error( dvdcss, "cache directory name is too long" );
-            psz_cache = NULL;
-        }
+	/* Check that we can add the ID directory and the block filename */
+	if( strlen( psz_home ) + strlen("/dvdcss") + 1 + 32 + 1 + (KEY_SIZE * 2) + 10 + 1
+	    > PATH_MAX )
+	{
+	    print_error( dvdcss, "cache directory name is too long" );
+	} else {
+	    snprintf( psz_cache, PATH_MAX, "%s/dvdcss", psz_home );
+	    psz_cache[PATH_MAX-1] = '\0';
+	}
     }
 
     /*
@@ -401,28 +351,8 @@ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( char *psz_target )
         }
     }
 
-    /* If the cache is enabled, write the cache directory tag */
-    if( psz_cache )
-    {
-        static const char psz_tag[] =
-            "Signature: 8a477f597d28d172789f06886806bc55\r\n"
-            "# This file is a cache directory tag created by libdvdcss.\r\n"
-            "# For information about cache directory tags, see:\r\n"
-            "#   http://www.brynosaurus.com/cachedir/\r\n";
-        char psz_tagfile[PATH_MAX + 1 + 12 + 1];
-        int i_fd;
-
-        sprintf( psz_tagfile, "%s/CACHEDIR.TAG", psz_cache );
-        i_fd = open( psz_tagfile, O_RDWR|O_CREAT, 0644 );
-        if( i_fd >= 0 )
-        {
-            write( i_fd, psz_tag, strlen(psz_tag) );
-            close( i_fd );
-        }
-    }
-
     /* If the cache is enabled, extract a unique disc ID */
-    if( psz_cache )
+    if( *psz_cache )
     {
         uint8_t p_sector[DVDCSS_BLOCK_SIZE];
         char psz_key[1 + KEY_SIZE * 2 + 1];



More information about the libdvdcss-devel mailing list