use file cache for distros
authorJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 9 May 2012 22:50:49 +0000 (22:50 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 9 May 2012 22:50:49 +0000 (22:50 +0000)
src/Makefile.am
src/Makefile.in
src/fcache.c [new file with mode: 0644]
src/fcache.h [new file with mode: 0644]
src/lp_ws.c

index be2d881..5add147 100644 (file)
@@ -10,6 +10,7 @@ DEFS = -DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" -DLOCALEDIR=\"$(localedir)\" @DEFS@
 bin_PROGRAMS = ppastats
 ppastats_SOURCES = \
        cache.h cache.c\
+       fcache.h fcache.c\
        html.h html.c\
        io.h io.c\
        list.h list.c\
index 0fbe8d1..20ce8c4 100644 (file)
@@ -53,10 +53,10 @@ CONFIG_CLEAN_FILES =
 CONFIG_CLEAN_VPATH_FILES =
 am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)"
 PROGRAMS = $(bin_PROGRAMS)
-am_ppastats_OBJECTS = cache.$(OBJEXT) html.$(OBJEXT) io.$(OBJEXT) \
-       list.$(OBJEXT) log.$(OBJEXT) lp.$(OBJEXT) lp_json.$(OBJEXT) \
-       lp_ws.$(OBJEXT) main.$(OBJEXT) ppastats.$(OBJEXT) \
-       str.$(OBJEXT)
+am_ppastats_OBJECTS = cache.$(OBJEXT) fcache.$(OBJEXT) html.$(OBJEXT) \
+       io.$(OBJEXT) list.$(OBJEXT) log.$(OBJEXT) lp.$(OBJEXT) \
+       lp_json.$(OBJEXT) lp_ws.$(OBJEXT) main.$(OBJEXT) \
+       ppastats.$(OBJEXT) str.$(OBJEXT)
 ppastats_OBJECTS = $(am_ppastats_OBJECTS)
 ppastats_LDADD = $(LDADD)
 DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
@@ -229,6 +229,7 @@ AM_CPPFLAGS = -Wall -std=gnu99 -Werror\
 
 ppastats_SOURCES = \
        cache.h cache.c\
+       fcache.h fcache.c\
        html.h html.c\
        io.h io.c\
        list.h list.c\
@@ -323,6 +324,7 @@ distclean-compile:
        -rm -f *.tab.c
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cache.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fcache.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/html.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/io.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list.Po@am__quote@
diff --git a/src/fcache.c b/src/fcache.c
new file mode 100644 (file)
index 0000000..89c66d4
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2011-2012 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <libintl.h>
+#define _(String) gettext(String)
+
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "fcache.h"
+#include "io.h"
+#include "log.h"
+
+static const char *cache_dir;
+
+static const char *get_cache_dir()
+{
+       char *home;
+
+       if (!cache_dir) {
+               home = getenv("HOME");
+
+               if (home) {
+                       cache_dir = malloc(strlen(home)
+                                          + 1 + strlen(".ppastats/cache")
+                                          + 1);
+                       sprintf((char *)cache_dir,
+                               "%s/%s", home, ".ppastats/cache");
+
+                       mkdirs(cache_dir, 0777);
+               } else {
+                       log_warn(_("$HOME not defined"));
+               }
+       }
+
+       return cache_dir;
+}
+
+static char *key_to_path(const char *key)
+{
+       char *path;
+       const char *dir;
+
+       dir = get_cache_dir();
+       if (!dir)
+               return NULL;
+
+       path = malloc(strlen(dir) + 1 + strlen(key) + strlen("/data.json") + 1);
+       sprintf(path, "%s%s/data.json", dir, key);
+
+       return path;
+}
+
+char *fcache_get(const char *key)
+{
+       char *path, *content;
+
+       path = key_to_path(key);
+       if (!path)
+               return NULL;
+
+       content = file_get_content(path);
+       
+       if (content)
+               log_debug(_("file cache hit %s"), key);
+       else
+               log_debug(_("file cache miss %s %s"), key, path);
+       
+       free(path);
+       
+       return content;
+}
+       
+void fcache_put(const char *key, char *value)
+{
+       char *path, *dir, *tmp;
+       FILE *f;
+
+       path = key_to_path(key);
+       if (!path)
+               return ;
+
+       tmp = strdup(path);
+       dir = dirname(tmp);
+       mkdirs(dir, 0777);
+       free(tmp);
+
+       f = fopen(path, "w");
+
+       if (f) {
+               fputs(value, f);
+
+               fclose(f);
+       } else {
+               log_err(_("Failed to open %s"), path);
+       }
+
+       free(path);
+}
+
+void fcache_cleanup()
+{
+       free((char *)cache_dir);
+}
diff --git a/src/fcache.h b/src/fcache.h
new file mode 100644 (file)
index 0000000..0e73b8d
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2011-2012 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef _PPASTATS_FCACHE_H_
+#define _PPASTATS_FCACHE_H_
+
+#include <json/json.h>
+
+char *fcache_get(const char *k);
+void fcache_put(const char *k, char *v);
+
+void fcache_cleanup();
+
+#endif
index f52f935..023ba9b 100644 (file)
@@ -28,6 +28,7 @@
 #include <json/json.h>
 
 #include "cache.h"
+#include "fcache.h"
 #include "list.h"
 #include "log.h"
 #include "lp_ws.h"
@@ -235,12 +236,24 @@ const struct distro_arch_series *get_distro_arch_series(const char *url)
 {
        json_object *obj;
        const struct distro_arch_series *distro;
+       char *content;
 
        distro = cache_get(url);
        if (distro)
                return (struct distro_arch_series *)distro;
 
-       obj = get_json_object(url);
+       content = fcache_get(url + 7);
+       if (!content) {
+               content = fetch_url(url);
+               if (content)
+                       fcache_put(url + 7, content);
+               else
+                       return NULL;
+       }
+
+       obj = json_tokener_parse(content);
+
+       free(content);
 
        if (!obj)
                return NULL;