Exemple #1
0
EVENTD_EXPORT
gint8
evhelpers_config_key_file_get_int(GKeyFile *config_file, const gchar *group, const gchar *key, Int *value)
{
    GError *error = NULL;

    value->value = g_key_file_get_int64(config_file, group, key, &error);
    value->set = ( error == NULL );

    return _evhelpers_config_key_file_error(&error, group, key);
}
Exemple #2
0
static gboolean
lr_fastestmirrorcache_lookup(LrFastestMirrorCache *cache,
                             gchar *url,
                             gint64 *ts,
                             double *connecttime)
{
    if (!cache || !cache->keyfile || !url)
        return FALSE;

    GKeyFile *keyfile = cache->keyfile;

    if (!g_key_file_has_group(keyfile, url))
        return FALSE;

    gint64 l_ts;
    double l_connecttime;
    GError *tmp_err = NULL;

    // Get timestamp
    l_ts = g_key_file_get_int64(keyfile, url, CACHE_KEY_TS, &tmp_err);
    if (tmp_err) {
        g_error_free(tmp_err);
        return FALSE;
    }

    // Get connect time
    l_connecttime = (double) g_key_file_get_double(keyfile,
                                                   url,
                                                   CACHE_KEY_CONNECTTIME,
                                                   &tmp_err);
    if (tmp_err) {
        g_error_free(tmp_err);
        return FALSE;
    }

    *ts = l_ts;
    *connecttime = l_connecttime;

    return TRUE;
}
Exemple #3
0
static gboolean
lr_fastestmirrorcache_load(LrFastestMirrorCache **cache,
                           gchar *path,
                           LrFastestMirrorCb cb,
                           void *cbdata,
                           GError **err)
{
    assert(cache);
    assert(!err || *err == NULL);

    if (!path) {
        // No cache file specified
        *cache = NULL;
        return TRUE;
    }

    cb(cbdata, LR_FMSTAGE_CACHELOADING, path);

    GKeyFile *keyfile = g_key_file_new();

    *cache = lr_malloc0(sizeof(LrFastestMirrorCache));
    (*cache)->path = g_strdup(path);
    (*cache)->keyfile = keyfile;

    if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
        // Cache file doesn't exist
        cb(cbdata, LR_FMSTAGE_CACHELOADINGSTATUS,
           "Cache doesn't exist");
    } else {
        // Cache exists, try to load it

        gboolean something_wrong = FALSE;
        GError *tmp_err = NULL;
        gboolean ret = g_key_file_load_from_file(keyfile,
                                                 path,
                                                 G_KEY_FILE_NONE,
                                                 &tmp_err);
        if (!ret) {
            // Cannot parse cache file
            char *msg = g_strdup_printf("Cannot parse fastestmirror "
                                        "cache %s: %s", path, tmp_err->message);
            g_debug("%s: %s", __func__, msg);
            cb(cbdata, LR_FMSTAGE_CACHELOADINGSTATUS, msg);
            something_wrong = TRUE;
            g_free(msg);
            g_error_free(tmp_err);
        } else {
            // File parsed successfully
            if (!g_key_file_has_group(keyfile, CACHE_GROUP_METADATA)) {
                // Not a fastestmirror cache
                g_debug("%s: File %s is not a fastestmirror cache file",
                          __func__, path);
                cb(cbdata, LR_FMSTAGE_CACHELOADINGSTATUS,
                   "File is not a fastestmirror cache");
                something_wrong = TRUE;
            } else {
                int version = (int) g_key_file_get_integer(keyfile,
                                                           CACHE_GROUP_METADATA,
                                                           CACHE_KEY_VERSION,
                                                           NULL);
                if (version != CACHE_VERSION) {
                    g_debug("%s: Old cache version %d vs %d",
                            __func__, version, CACHE_VERSION);
                    cb(cbdata, LR_FMSTAGE_CACHELOADINGSTATUS,
                       "Old version of cache format");
                    something_wrong = TRUE;
                }
            }
        }

        if (something_wrong) {
            // Reinit keyfile
            g_key_file_free(keyfile);
            keyfile = g_key_file_new();
            (*cache)->keyfile = keyfile;
        } else {
            gsize len;
            gchar **array = g_key_file_get_groups(keyfile, &len);
            g_debug("%s: Loaded: %"G_GSIZE_FORMAT" records", __func__, len);

            // Remove really outdated records
            gint64 current_time = g_get_real_time() / 1000000;
            char **group, *groupname;
            for (group=array; groupname=*group, groupname; group++) {
                if (g_str_has_prefix(groupname, ":_"))
                    continue;

                gint64 ts = g_key_file_get_int64(keyfile,
                                                 groupname,
                                                 CACHE_KEY_TS,
                                                 NULL);
                if (ts < (current_time - CACHE_RECORD_MAX_AGE)) {
                    // Record is too old, remove it
                    g_debug("%s: Removing too old record from cache: %s "
                            "(ts: %"G_GINT64_FORMAT")",
                            __func__, groupname, ts);
                    g_key_file_remove_group(keyfile, groupname, NULL);
                }
            }
            g_strfreev(array);

            cb(cbdata, LR_FMSTAGE_CACHELOADINGSTATUS, NULL);
        }
    }

    // Set version of cache format
    g_key_file_set_integer(keyfile,
                           CACHE_GROUP_METADATA,
                           CACHE_KEY_VERSION,
                           CACHE_VERSION);

    return TRUE;
}
Exemple #4
0
static void
cleaner_task_handler (GTask *task, gpointer source_object,
			gpointer task_data, GCancellable *cancellable)
{
	HevFileboxCleaner *self = HEV_FILEBOX_CLEANER (source_object);
    HevFileboxCleanerPrivate *priv = HEV_FILEBOX_CLEANER_GET_PRIVATE (self);
	gchar *fp_path = NULL, *fm_path = NULL;
	GFile *file = NULL;
	GFileEnumerator *file_enumerator = NULL;
	GFileInfo *file_info = NULL;

    g_debug ("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__);

	fp_path = g_key_file_get_string (priv->config, "Module", "FilePoolPath", NULL);
	fm_path = g_key_file_get_string (priv->config, "Module", "FileMetaPath", NULL);

	/* enumerate file pool */
	file = g_file_new_for_path (fp_path);
	file_enumerator = g_file_enumerate_children (file,
				G_FILE_ATTRIBUTE_STANDARD_NAME","G_FILE_ATTRIBUTE_STANDARD_TYPE,
				G_FILE_QUERY_INFO_NONE, NULL, NULL);
	while (file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL)) {
		if (G_FILE_TYPE_REGULAR == g_file_info_get_file_type (file_info)) {
			gchar *meta_path = NULL;
			GKeyFile *meta = NULL;

			meta_path = g_build_filename (fm_path, g_file_info_get_name (file_info), NULL);
			meta = g_key_file_new ();
			if (g_key_file_load_from_file (meta, meta_path, G_KEY_FILE_NONE, NULL)) {
				gint64 exp = 0;
				GDateTime *now_date = NULL, *exp_date = NULL;

				exp = g_key_file_get_int64 (meta, "Meta", "ExpDate", NULL);
				now_date = g_date_time_new_now_utc ();
				exp_date = g_date_time_new_from_unix_utc (exp);

				/* compare date */
				if (1 == g_date_time_compare (now_date, exp_date)) {
					gchar *file_path = NULL;

					file_path = g_build_filename (fp_path,
								g_file_info_get_name (file_info), NULL);
					/* delete file and it's meta */
					g_unlink ((const gchar *) file_path);
					g_unlink ((const gchar *) meta_path);
					g_free (file_path);
				}

				g_date_time_unref (now_date);
				g_date_time_unref (exp_date);
			}
			g_key_file_unref (meta);
			g_free (meta_path);
		}
		g_object_unref (file_info);
	}
	g_object_unref (file_enumerator);
	g_object_unref (file);

	g_free (fp_path);
	g_free (fm_path);

	g_task_return_boolean (task, TRUE);
}