[libbdplus-devel] [Git][videolan/libbdplus][master] 2 commits: configfile: move BDPLUS_DIR define to configfile.h

Petri Hintukainen (@hpi) gitlab at videolan.org
Fri Oct 8 14:26:12 UTC 2021



Petri Hintukainen pushed to branch master at VideoLAN / libbdplus


Commits:
499defc0 by John Doe at 2021-10-07T23:20:00+02:00
configfile: move BDPLUS_DIR define to configfile.h

- - - - -
f424133f by John Doe at 2021-10-07T23:20:33+02:00
Scan bdplus home/system config+cache dirs for cached convtabs

If no cached convtab filename was passed via the BDPLUS_CONVTAB env var, find
and eventually use a cached convtab located (in this order) in the home or
system bdplus config and cache dirs, if the cache file contains the discs
media key in it's file name.

- - - - -


5 changed files:

- src/file/configfile.c
- src/file/configfile.h
- src/libbdplus/bdplus.c
- src/libbdplus/internal.c
- src/libbdplus/internal.h


Changes:

=====================================
src/file/configfile.c
=====================================
@@ -34,9 +34,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#define BDPLUS_DIR "bdplus"
-
-
 #define MIN_FILE_SIZE 1
 #define MAX_FILE_SIZE 0xffffff
 


=====================================
src/file/configfile.h
=====================================
@@ -25,6 +25,8 @@
 #include <stdint.h>
 #include <stdio.h>
 
+#define BDPLUS_DIR "bdplus"
+
 BD_PRIVATE char *       file_get_cache_dir(void) BD_ATTR_MALLOC;
 BD_PRIVATE char *       file_get_config_dir(const char *file) BD_ATTR_MALLOC;
 


=====================================
src/libbdplus/bdplus.c
=====================================
@@ -224,6 +224,7 @@ void bdplus_set_mk(bdplus_t *plus, const uint8_t *mk)
 
 int32_t bdplus_start(bdplus_t *plus)
 {
+    char *cachefile = NULL;
     int32_t result = 0;
 
     if (!plus) return -1;
@@ -240,7 +241,11 @@ int32_t bdplus_start(bdplus_t *plus)
 
     plus->started = 1;
 
-    const char *cachefile = getenv("BDPLUS_CONVTAB");
+    cachefile = str_dup(getenv("BDPLUS_CONVTAB"));
+
+    if (!cachefile)
+        cachefile = bdplus_disc_findcachefile(plus);
+
     if (cachefile && !plus->cache_tab) {
         FILE *fp = fopen(cachefile, "rb");
         if (fp) {
@@ -254,12 +259,13 @@ int32_t bdplus_start(bdplus_t *plus)
         }
     }
 
+    X_FREE(cachefile);
+
     bd_mutex_unlock(&plus->mutex);
 
     return result;
 }
 
-
 void bdplus_free(bdplus_t *plus)
 {
     BD_DEBUG(DBG_BDPLUS, "[bdplus] releasing %p..\n", plus);


=====================================
src/libbdplus/internal.c
=====================================
@@ -28,11 +28,13 @@
 #include "bdsvm/segment.h"
 
 #include "file/configfile.h"
+#include "file/dirs.h"
 #include "file/file.h"
 #include "util/logging.h"
 #include "util/macro.h"
 #include "util/strutl.h"
 
+#include <ctype.h>
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
@@ -66,6 +68,103 @@ int crypto_init()
   return crypto_init_check;
 }
 
+static char *_cache_scanpath(const char *cachepath, const char *mk_str)
+{
+    char             *result = NULL;
+    char             *fullpath = NULL;
+    BD_DIR_H         *dir;
+    BD_DIRENT         ent, entlower;
+    long unsigned int i;
+    size_t            len;
+
+    if(!cachepath)
+        return NULL;
+
+    /* open and scan cachepath for mk_str[.bin] */
+    BD_DEBUG(DBG_BDPLUS | DBG_CRIT, "[bdplus] Scanning %s for cached conversion table...\n", cachepath);
+    fullpath = str_printf("%s%s%s", cachepath, DIR_SEP, "convtab");
+    if (fullpath) {
+        dir = dir_open_default()(fullpath);
+
+        if (dir) {
+            while (!result && !dir_read(dir, &ent)) {
+                len = strlen(ent.d_name);
+                /* skip if filename is shorter than 32 chars (ie. 32 hexdigits plus .bin) */
+                if (len < 36)
+                    continue;
+
+                /* create lower-case copy of the filename for comparison */
+                for (i = 0; i < len; i++) {
+                    entlower.d_name[i] = tolower(ent.d_name[i]);
+                }
+
+                /* check if (lowercase) filename contains the MK and ends with .bin, create
+                   result with original-case filename if it matches */
+                if (!memcmp(entlower.d_name, mk_str, 32) && !memcmp(entlower.d_name + len - 4, ".bin", 4))
+                    result = str_printf("%s%s%s", fullpath, DIR_SEP, ent.d_name);
+            }
+            dir_close(dir);
+        }
+        X_FREE(fullpath);
+    }
+
+    return result;
+}
+
+char *bdplus_disc_findcachefile(bdplus_t *plus)
+{
+    char       *cache_home = file_get_cache_dir();
+    char       *config_home = file_get_config_home();
+    const char *sysbase = NULL;
+    char       *syspath = NULL;
+    char       *home_convtab = NULL;
+    char       *result = NULL;
+    char        mk_str[33];
+
+    str_print_hex(mk_str, plus->mediaKey, 16);
+
+    /* Scan home config convtab dir (ie. bdplus/convtab/ */
+    if (config_home) {
+        home_convtab = str_printf("%s%s%s", config_home, DIR_SEP, BDPLUS_DIR);
+        if (home_convtab)
+            result = _cache_scanpath(home_convtab, mk_str);
+
+        X_FREE(home_convtab);
+    }
+
+    /* Scan home cache dir */
+    if (!result && cache_home)
+        result = _cache_scanpath(cache_home, mk_str);
+
+    /* Scan system config dirs (no convtab in cache and home conf dirs) */
+    if (!result) {
+        sysbase = file_get_config_system(NULL);
+        while (sysbase) {
+            syspath = str_printf("%s%s%s", sysbase, DIR_SEP, BDPLUS_DIR);
+            if (syspath)
+                result = _cache_scanpath(syspath, mk_str);
+
+            X_FREE(syspath);
+
+            /* stop iterator if cached convtab was found */
+            if (result)
+                break;
+
+            sysbase = file_get_config_system(sysbase);
+        }
+    }
+
+    if (result)
+        BD_DEBUG(DBG_BDPLUS | DBG_CRIT, "[bdplus] Found cached conversion table at %s\n", result);
+    else
+        BD_DEBUG(DBG_BDPLUS | DBG_CRIT, "[bdplus] No cached conversion table found\n");
+
+    X_FREE(config_home);
+    X_FREE(cache_home);
+
+    return result;
+}
+
 char *bdplus_disc_cache_file(bdplus_t *plus, const char *file)
 {
     char *base = file_get_cache_dir();


=====================================
src/libbdplus/internal.h
=====================================
@@ -43,6 +43,7 @@ BD_PRIVATE void                    bdplus_setConvTable( bdplus_t *plus, struct c
 BD_PRIVATE struct conv_table_s    *bdplus_getConvTable ( bdplus_t *plus );
 BD_PRIVATE struct bdplus_config_s *bdplus_getConfig    ( bdplus_t *plus );
 BD_PRIVATE char                   *bdplus_disc_cache_file( bdplus_t *plus, const char *file );
+BD_PRIVATE char                   *bdplus_disc_findcachefile( bdplus_t *plus );
 
 BD_PRIVATE void     bdplus_getSlot           ( bdplus_t *plus, uint32_t slot, struct slot_s *dst );
 BD_PRIVATE void     bdplus_getAttachStatus   ( bdplus_t *plus, uint8_t *dst );



View it on GitLab: https://code.videolan.org/videolan/libbdplus/-/compare/6889d280befd3b091538bb876b1eafe0134a83ea...f424133fb0e0be4cead599efb4d460f5eab7f3e1

-- 
View it on GitLab: https://code.videolan.org/videolan/libbdplus/-/compare/6889d280befd3b091538bb876b1eafe0134a83ea...f424133fb0e0be4cead599efb4d460f5eab7f3e1
You're receiving this email because of your account on code.videolan.org.




More information about the libbdplus-devel mailing list