]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
settings: use gzip for the mux/service settings
authorJaroslav Kysela <perex@perex.cz>
Thu, 11 Feb 2016 13:46:54 +0000 (14:46 +0100)
committerJaroslav Kysela <perex@perex.cz>
Thu, 11 Feb 2016 13:48:03 +0000 (14:48 +0100)
src/epgdb.c
src/htsmsg_binary.c
src/settings.c
src/tvheadend.h
src/zlib.c

index c405d95a1768707ac5a8e01920ef589d2fa49507..513184c6289e2c630d47d1f5c9387f207727f433 100644 (file)
@@ -345,22 +345,7 @@ static void epg_save_tsk_callback ( void *p, int dearmed )
   if (fd >= 0) {
 #if ENABLE_ZLIB
     if (config.epg_compress) {
-      r = tvh_write(fd, "\xff\xffGZIP00\x00\x00\x00\x00", 12);
-      if (!r)
-        r = tvh_gzip_deflate_fd(fd, sb->sb_data, sb->sb_ptr, &size, 3) < 0;
-      if (!r && size > UINT_MAX)
-        r = 1;
-      if (!r) {
-        r = lseek(fd, 8, SEEK_SET) != (off_t)8;
-        if (!r) {
-          uint8_t data2[4];
-          data2[0] = (sb->sb_ptr >> 24) & 0xff;
-          data2[1] = (sb->sb_ptr >> 16) & 0xff;
-          data2[2] = (sb->sb_ptr >> 8) & 0xff;
-          data2[3] = (sb->sb_ptr & 0xff);
-          r = tvh_write(fd, data2, 4);
-        }
-      }
+      r = tvh_gzip_deflate_fd_header(fd, sb->sb_data, sb->sb_ptr, 3) < 0;
    } else
 #endif
       r = tvh_write(fd, sb->sb_data, sb->sb_ptr);
index 998b21222c7a77994c29a9a4dbd97e10d35cbe48..074da77afe8b2f7edbcf8a31a574fbcc132a4396 100644 (file)
@@ -104,6 +104,10 @@ htsmsg_binary_des0(htsmsg_t *msg, const uint8_t *buf, size_t len)
       }
       break;
 
+    case HMF_BOOL:
+      f->hmf_bool = datalen == 1 ? buf[0] : 0;
+      break;
+
     default:
       free(n);
       free(f);
@@ -173,6 +177,11 @@ htsmsg_binary_count(htsmsg_t *msg)
        u64 = u64 >> 8;
       }
       break;
+
+    case HMF_BOOL:
+      if (f->hmf_bool) len++;
+      break;
+
     }
   }
   return len;
@@ -216,6 +225,11 @@ htsmsg_binary_write(htsmsg_t *msg, uint8_t *ptr)
        u64 = u64 >> 8;
       }
       break;
+
+    case HMF_BOOL:
+      l = f->hmf_bool ? 1 : 0;
+      break;
+
     default:
       abort();
     }
@@ -252,6 +266,11 @@ htsmsg_binary_write(htsmsg_t *msg, uint8_t *ptr)
        u64 = u64 >> 8;
       }
       break;
+
+    case HMF_BOOL:
+      if (f->hmf_bool)
+        ptr[0] = 1;
+      break;
     }
     ptr += l;
   }
index e3e508d0305830a8dd65cd5d9ba1acb6040a3288..790896073ef5ecd873e9b91a033711b8a0243ef8 100644 (file)
@@ -28,6 +28,7 @@
 #include <dirent.h>
 
 #include "htsmsg.h"
+#include "htsmsg_binary.h"
 #include "htsmsg_json.h"
 #include "settings.h"
 #include "tvheadend.h"
@@ -133,7 +134,9 @@ hts_settings_save(htsmsg_t *record, const char *pathfmt, ...)
   va_list ap;
   htsbuf_queue_t hq;
   htsbuf_data_t *hd;
-  int ok, r;
+  int ok, r, pack;
+  void *msgdata;
+  size_t msglen;
 
   if(settingspath == NULL)
     return;
@@ -157,18 +160,37 @@ hts_settings_save(htsmsg_t *record, const char *pathfmt, ...)
   }
 
   /* Store data */
+#if ENABLE_ZLIB
+  pack = strstr(path, "/muxes/") != NULL && /* ugly, redesign API */
+         strstr(path, "/networks/") != NULL &&
+         strstr(path, "/input/") != NULL;
+#else
+  pack = 0;
+#endif
   ok = 1;
-  htsbuf_queue_init(&hq, 0);
-  htsmsg_json_serialize(record, &hq, 1);
-  TAILQ_FOREACH(hd, &hq.hq_q, hd_link)
-    if(tvh_write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len)) {
-      tvhlog(LOG_ALERT, "settings", "Failed to write file \"%s\" - %s",
-             tmppath, strerror(errno));
-      ok = 0;
-      break;
+
+  if (!pack) {
+    htsbuf_queue_init(&hq, 0);
+    htsmsg_json_serialize(record, &hq, 1);
+    TAILQ_FOREACH(hd, &hq.hq_q, hd_link)
+      if(tvh_write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len)) {
+        tvhlog(LOG_ALERT, "settings", "Failed to write file \"%s\" - %s",
+                tmppath, strerror(errno));
+        ok = 0;
+        break;
+      }
+    htsbuf_queue_flush(&hq);
+  } else {
+#if ENABLE_ZLIB
+    r = htsmsg_binary_serialize(record, &msgdata, &msglen, 0x10000);
+    if (!r) {
+      r = tvh_gzip_deflate_fd_header(fd, msgdata, msglen, 3);
+      if (r)
+        ok = 0;
     }
+#endif
+  }
   close(fd);
-  htsbuf_queue_flush(&hq);
 
   /* Move */
   if(ok) {
@@ -194,8 +216,10 @@ hts_settings_load_one(const char *filename)
 {
   ssize_t n, size;
   char *mem;
+  uint8_t *unpacked;
   fb_file *fp;
   htsmsg_t *r = NULL;
+  uint32_t orig;
 
   /* Open */
   if (!(fp = fb_open(filename, 1, 0))) return NULL;
@@ -207,8 +231,22 @@ hts_settings_load_one(const char *filename)
   if (n >= 0) mem[n] = 0;
 
   /* Decode */
-  if(n == size)
-    r = htsmsg_json_deserialize(mem);
+  if(n == size) {
+    if (size > 12 && memcmp(mem, "\xff\xffGZIP00", 8) == 0) {
+#if ENABLE_ZLIB
+      orig = (mem[8] << 24) | (mem[9] << 16) | (mem[10] << 8) | mem[11];
+      if (orig > 0) {
+        unpacked = tvh_gzip_inflate((uint8_t *)mem + 12, size - 12, orig);
+        if (unpacked) {
+          r = htsmsg_binary_deserialize(unpacked, orig, NULL);
+          free(unpacked);
+        }
+      }
+#endif
+    } else {
+      r = htsmsg_json_deserialize(mem);
+    }
+  }
 
   /* Close */
   fb_close(fp);
index b1ae554ab67a4584d3dbc49e4e28ccc52865d96b..1f158bdfbe88f4532183c35acc63bc8441348eff 100644 (file)
@@ -794,6 +794,7 @@ char *regexp_escape ( const char *str );
 uint8_t *tvh_gzip_inflate ( const uint8_t *data, size_t size, size_t orig );
 uint8_t *tvh_gzip_deflate ( const uint8_t *data, size_t orig, size_t *size );
 int      tvh_gzip_deflate_fd ( int fd, const uint8_t *data, size_t orig, size_t *size, int speed );
+int      tvh_gzip_deflate_fd_header ( int fd, const uint8_t *data, size_t orig, int speed );
 #endif
 
 /* URL decoding */
index 48b93866f609abb52d92569b5fa867b7f76db73a..1f5a3a0d732338c47d0c1f8241a3dc9d02209e17 100644 (file)
@@ -152,3 +152,25 @@ int tvh_gzip_deflate_fd ( int fd, const uint8_t *data, size_t orig, size_t *size
 
   return r;
 }
+
+int tvh_gzip_deflate_fd_header ( int fd, const uint8_t *data, size_t orig, int speed )
+{
+  uint8_t data2[4];
+  size_t size;
+  int r;
+
+  r = tvh_write(fd, "\xff\xffGZIP00\x00\x00\x00\x00", 12);
+  if (r)
+    return 1;
+  r = tvh_gzip_deflate_fd(fd, data, orig, &size, 3) < 0;
+  if (r || size > UINT_MAX)
+    return 1;
+  r = lseek(fd, 8, SEEK_SET) != (off_t)8;
+  if (r)
+    return 1;
+  data2[0] = (orig >> 24) & 0xff;
+  data2[1] = (orig >> 16) & 0xff;
+  data2[2] = (orig >> 8) & 0xff;
+  data2[3] = (orig & 0xff);
+  return tvh_write(fd, data2, 4);
+}