[libdvdcss-devel] [PATCH 38/47] libdvdcss: Factor out setting cache directory from dvdcss_open()

Diego Biurrun <[email protected]>
Newsgroups gmane.comp.video.videolan.libdvdcss,gmane.comp.video.videolan.libdvdcss.devel
Message-ID <[email protected]>
---
 src/libdvdcss.c | 130 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 67 insertions(+), 63 deletions(-)

diff --git a/src/libdvdcss.c b/src/libdvdcss.c
index c74ef7b..d886ad9 100644
--- a/src/libdvdcss.c
+++ b/src/libdvdcss.c
@@ -182,67 +182,11 @@ static int set_access_method( dvdcss_t dvdcss )
     return 0;
 }
 
-/**
- * \brief Open a DVD device or directory and return a dvdcss instance.
- *
- * \param psz_target a string containing the target name, for instance
- *        "/dev/hdc" or "E:"
- * \return a handle to a dvdcss instance or NULL on error.
- *
- * Initialize the \e libdvdcss library, open the requested DVD device or
- * directory, and return a handle to be used for all subsequent \e libdvdcss
- * calls. \e libdvdcss checks whether ioctls can be performed on the disc,
- * and when possible, the disc key is retrieved.
- */
-LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target )
+static char *set_cache_directory( dvdcss_t dvdcss )
 {
     char psz_buffer[PATH_MAX];
-    int i_ret;
-
-    const char *psz_cache = getenv( "DVDCSS_CACHE" );
-#ifdef DVDCSS_RAW_OPEN
-    const char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" );
-#endif
-
-    dvdcss_t dvdcss;
-
-    /*
-     *  Allocate the library structure
-     */
-    dvdcss = malloc( sizeof( struct dvdcss_s ) );
-    if( dvdcss == NULL )
-    {
-        return NULL;
-    }
+    char *psz_cache = getenv( "DVDCSS_CACHE" );
 
-    /*
-     *  Initialize structure with default values
-     */
-#ifdef DVDCSS_RAW_OPEN
-    dvdcss->i_raw_fd = -1;
-#endif
-    dvdcss->p_titles = NULL;
-    dvdcss->psz_device = strdup( psz_target );
-    dvdcss->psz_error = "no error";
-    dvdcss->i_method = DVDCSS_METHOD_KEY;
-    dvdcss->psz_cachefile[0] = '\0';
-
-    /*
-     *  Find verbosity from DVDCSS_VERBOSE environment variable
-     */
-    set_verbosity( dvdcss );
-
-    /*
-     *  Find method from DVDCSS_METHOD environment variable
-     */
-    if( set_access_method( dvdcss ) < 0 )
-    {
-        goto error;
-    }
-
-    /*
-     *  If DVDCSS_CACHE was not set, try to guess a default value
-     */
     if( psz_cache == NULL || psz_cache[0] == '\0' )
     {
 #if defined(_WIN32_IE) && _WIN32_IE >= 0x500
@@ -307,23 +251,83 @@ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target )
 #endif
     }
 
-    /*
-     *  Find cache dir from the DVDCSS_CACHE environment variable
-     */
+    /* sanity check psz_cache value */
     if( psz_cache != NULL )
     {
         if( psz_cache[0] == '\0' || !strcmp( psz_cache, "off" ) )
         {
-            psz_cache = NULL;
+            return 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;
+            return NULL;
         }
     }
+    return psz_cache;
+}
+
+/**
+ * \brief Open a DVD device or directory and return a dvdcss instance.
+ *
+ * \param psz_target a string containing the target name, for instance
+ *        "/dev/hdc" or "E:"
+ * \return a handle to a dvdcss instance or NULL on error.
+ *
+ * Initialize the \e libdvdcss library, open the requested DVD device or
+ * directory, and return a handle to be used for all subsequent \e libdvdcss
+ * calls. \e libdvdcss checks whether ioctls can be performed on the disc,
+ * and when possible, the disc key is retrieved.
+ */
+LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target )
+{
+    int i_ret;
+
+    const char *psz_cache;
+#ifdef DVDCSS_RAW_OPEN
+    const char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" );
+#endif
+
+    dvdcss_t dvdcss;
+
+    /*
+     *  Allocate the library structure
+     */
+    dvdcss = malloc( sizeof( struct dvdcss_s ) );
+    if( dvdcss == NULL )
+    {
+        return NULL;
+    }
+
+    /*
+     *  Initialize structure with default values
+     */
+#ifdef DVDCSS_RAW_OPEN
+    dvdcss->i_raw_fd = -1;
+#endif
+    dvdcss->p_titles = NULL;
+    dvdcss->psz_device = strdup( psz_target );
+    dvdcss->psz_error = "no error";
+    dvdcss->i_method = DVDCSS_METHOD_KEY;
+    dvdcss->psz_cachefile[0] = '\0';
+
+    /*
+     *  Find verbosity from DVDCSS_VERBOSE environment variable
+     */
+    set_verbosity( dvdcss );
+
+    /*
+     *  Find method from DVDCSS_METHOD environment variable
+     */
+    if( set_access_method( dvdcss ) < 0 )
+    {
+        goto error;
+    }
+
+    /* Set CSS key cache directory. */
+    psz_cache = set_cache_directory( dvdcss );
 
     /*
      *  Open device
-- 
1.9.1

_______________________________________________
libdvdcss-devel mailing list
[email protected]
https://mailman.videolan.org/listinfo/libdvdcss-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.