Beispiel #1
0
Datei: post.c Projekt: gerow/hnfs
/*
 * the calling thread should have a lock on the collection before
 * calling this function
 */
int hnfs_post_fetch_content(hnfs_post_t *post)
{
  if ((time(NULL) - post->content_update_time) < HNFS_TIME_BETWEEN_UPDATES) {
    fprintf(stderr, "Content fresh. Skipping update.\n");
    return 0;
  }

  /* if content is allocated free it */
  if (post->content) {
    free(post->content);
  }

  curl_saver_t saver = {
    .data = NULL,
    .size = 0
  };
  CURLcode res = fetch_url(post->url, &saver);
  if (res != CURLE_OK) {
    fprintf(stderr,
            "Failed to fetch url %s: %s\n",
            post->url,
            curl_easy_strerror(res));
    return res;
  }
  post->content = saver.data;
  post->content_update_time = time(NULL);

  return 0;
}
Beispiel #2
0
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	pam_url_opts opts;
	int ret=0;
	int len = 0;
	char* addextra = "&PAM_SM_SESSION=close";
	char* tmp = NULL;

	if ( PAM_SUCCESS != pam_get_item(pamh, PAM_USER, &opts.user) )
	{
		ret++;
		debug(pamh, "Could not get user item from pam.");
	}

	if( PAM_SUCCESS != parse_opts(&opts, argc, argv, PAM_SM_SESSION) )
	{
		ret++;
		debug(pamh, "Could not parse module options.");
	}

	len = strlen(opts.extra_field) + strlen(addextra) + 1;
	opts.extra_field = realloc(opts.extra_field, len);
	if (opts.extra_field == NULL)
		goto done;

	tmp = calloc(1, strlen(opts.extra_field) + 1);
	if (tmp == NULL)
		goto done;

	snprintf(tmp, strlen(opts.extra_field) + 1, "%s", opts.extra_field );
	snprintf(opts.extra_field, len, "%s%s", addextra, tmp);
	free(tmp);

	if( PAM_SUCCESS != fetch_url(pamh, opts) )
	{
		ret++;
		debug(pamh, "Could not fetch URL.");
	}

	if( PAM_SUCCESS != check_rc(opts) )
	{
		ret++;
		debug(pamh, "Wrong Return Code.");
	}

done:
	cleanup(&opts);

	if( 0 == ret )
	{
		return PAM_SUCCESS;
	}
	else
	{
		debug(pamh, "Session not releasing. Failing.");
		return PAM_SESSION_ERR;
	}
}
Beispiel #3
0
void cache_update(string url, string& url_data, int size)
{
	while(cache_size < (size + running_size))
	{
		cout<<"\n shudnt be here ... \n";
		replacement_algo(replacement_policy);
	}
	if(cache_size >= (size + running_size))
	{
		running_size += size;
		fetch_url(url, url_data);
		Page_Info pi;
		pi.page_data = *url_data;
		pi.page_entry = access_count;
		pi.page_size = size;
		cache.insert(pair<string, Page_Info>(url, pi));	
	}
}
Beispiel #4
0
Datei: post.c Projekt: gerow/hnfs
int
hnfs_post_update(hnfs_post_collection_t *collection)
{
  int ret = 0;
  const static int num_tokens = 512;
  time_t cur_time = time(NULL);
  char *json = NULL;
  jsmn_parser p;
  /* now get jsmn to parse it into a list of tokens */
  /* 512 tokens should be enough for anybody... right? */
  jsmntok_t tokens[num_tokens];
  int posts_index;

  /* lock our collection mutex */
  pthread_mutex_lock(&collection->mutex);
  /* check to see if our data is old */
  if (cur_time - collection->update_time < HNFS_TIME_BETWEEN_UPDATES) {
    fprintf(stderr, "Data fresh. Skipping update.");
    goto cleanup_lock;
  }

  (void) fetch_url;
  curl_saver_t saver;
  fetch_url(front_page_api_path, &saver);
  json = saver.data;
  //load_file("/Users/gerow/proj/hnfs/test.json", &json);
  //assert(strlen(json) == 12113);

  jsmn_init(&p);
  //assert(strlen(json) == 12113);
  int jsmn_res = jsmn_parse(&p, json, tokens, 512);
  //assert(strlen(json) == 12113);
  if (jsmn_res != JSMN_SUCCESS) {
    fprintf(stderr, "jsmn had trouble parsing the data, dumping:\n");
    fprintf(stderr, "%s\n", json);
    exit(1);
  }

  aldn_context_t context;
  context.json = json;
  context.tokens = tokens;
  context.num_tokens = num_tokens;

  posts_index = aldn_key_value(&context, 0, "items");
  if (posts_index < 0) {
    fprintf(stderr, "Failed to find index of posts inside object\n");
    exit(1);
  }

  assert(tokens[posts_index].type == JSMN_ARRAY);
  /* only the first 30 entires are what we're looking for */
  for (int i = 0; i < 30; i++) {
    /* free this entry's content pointer if it is set */
    if (collection->posts[i].content) {
      free(collection->posts[i].content);
      collection->posts[i].content = NULL;
      collection->posts[i].content_update_time = 0;
    }
    int cur_post = aldn_ith_value(&context, posts_index, i);
    printf("object type is %d\n", tokens[cur_post].type);
    assert(tokens[cur_post].type == JSMN_OBJECT);
    int title_index = aldn_key_value(&context, cur_post, "title");
    assert(title_index >= 0);
    aldn_extract_string(&context,
                        title_index,
                        collection->posts[i].title,
                        HNFS_POST_STRING_SIZE);
    int url_index = aldn_key_value(&context, cur_post, "url");
    assert(url_index >= 0);
    aldn_extract_string(&context,
                        url_index,
                        collection->posts[i].url,
                        HNFS_POST_STRING_SIZE);
  }

  collection->update_time = cur_time;
/*cleanup_saver:*/
  free(json);
cleanup_lock:
  pthread_mutex_unlock(&collection->mutex);

  return ret;
}