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\
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)
ppastats_SOURCES = \
cache.h cache.c\
+ fcache.h fcache.c\
html.h html.c\
io.h io.c\
list.h list.c\
-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@
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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
#include <json/json.h>
#include "cache.h"
+#include "fcache.h"
#include "list.h"
#include "log.h"
#include "lp_ws.h"
{
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;