Exemple #1
0
/* find a list.  loads from disk if not in memory. */
static struct List *list_find(int p, enum ListWhich l)
{
	struct List *lst;
	FILE *fp;
	char listmember[100];
	int count=0;

	lst = list_find1(p, l);
	if (lst) {
		return lst;
	}

	/* create the base list */
	list_add(p, l, NULL);

	if (ListArray[l].rights == P_PERSONAL) {
		list_find1(p, l);
	}

	fp = fopen_p("%s/%s", "r", LISTS_DIR, ListArray[l].name);
	if (!fp) {
		return NULL;
	}
	while (!feof(fp)) {
		if (fgets(listmember, sizeof(listmember), fp) != NULL) {
			if (list_add(p, l, listmember) == 0) {
				count++;
			}
		}
	}
	fclose(fp);

	/* we've added some, retry */
	if (count) {
		return list_find1(p, l);
	}

	return NULL;
}
Exemple #2
0
/* TODO: use .part extentions and continue even when using GRITS_ONCE */
gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local,
		GritsCacheType mode, GritsChunkCallback callback, gpointer user_data)
{
	g_debug("GritsHttp: fetch - %s mode=%d", local, mode);
	gchar *path = _get_cache_path(http, local);

	/* Unlink the file if we're refreshing it */
	if (mode == GRITS_REFRESH)
		g_remove(path);

	/* Do the cache if necessasairy */
	if (!(mode == GRITS_ONCE && g_file_test(path, G_FILE_TEST_EXISTS)) &&
			mode != GRITS_LOCAL) {
		g_debug("GritsHttp: fetch - Caching file %s", local);

		/* Open the file for writting */
		gchar *part = path;
		if (!g_file_test(path, G_FILE_TEST_EXISTS))
			part = g_strdup_printf("%s.part", path);
		FILE *fp = fopen_p(part, "ab");
		if (!fp) {
			g_warning("GritsHttp: fetch - error opening %s", path);
			return NULL;
		}
		fseek(fp, 0, SEEK_END); // "a" is broken on Windows, twice

		/* Make temp data */
		struct _CacheInfo info = {
			.fp        = fp,
			.path      = path,
			.callback  = callback,
			.user_data = user_data,
		};

		/* Download the file */
		SoupMessage *message = soup_message_new("GET", uri);
		if (message == NULL)
			g_error("message is null, cannot parse uri");
		g_signal_connect(message, "got-chunk", G_CALLBACK(_chunk_cb), &info);
		//if (ftell(fp) > 0)
			soup_message_headers_set_range(message->request_headers, ftell(fp), -1);
		if (mode == GRITS_REFRESH)
			soup_message_headers_replace(message->request_headers,
					"Cache-Control", "max-age=0");
		soup_session_send_message(http->soup, message);

		/* Close file */
		fclose(fp);
		if (path != part) {
			if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
				g_rename(part, path);
			g_free(part);
		}

		/* Finished */
		guint status = message->status_code;
		g_object_unref(message);
		if (status == SOUP_STATUS_CANCELLED) {
			return NULL;
		} else if (status == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE) {
			/* Range unsatisfiable, file already complete */
		} else if (!SOUP_STATUS_IS_SUCCESSFUL(status)) {
			g_warning("GritsHttp: done_cb - error copying file, status=%d\n"
					"\tsrc=%s\n"
					"\tdst=%s",
					status, uri, path);
			return NULL;
		}
	}