int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
{
	const unsigned char *buf = buff;

	ENSURE_BUF_OK(file);

	if (file->do_not_buffer)
		return file->write(file, (void *)buff, len);

	for (;;) {
		size_t space_left = file->buf_size - file->buf_pos;

		/* cache if it's small */
		if (space_left > len) {
			add_to_cache(file, buf, len);
			return 0;
		}

		add_to_cache(file, buf, space_left);
		if (flush_buffer(file) < 0)
			return -1;

		len -= space_left;
		buf += space_left;
	}
}
Exemple #2
0
int git_filebuf_write(git_filebuf *file, void *buff, size_t len)
{
	int error;
	unsigned char *buf = buff;

	for (;;) {
		size_t space_left = file->buf_size - file->buf_pos;

		/* cache if it's small */
		if (space_left > len) {
			add_to_cache(file, buf, len);
			return GIT_SUCCESS;
		}

		/* flush the cache if it doesn't fit */
		if (file->buf_pos > 0) {
			add_to_cache(file, buf, space_left);

			if ((error = flush_buffer(file)) < GIT_SUCCESS)
				return error;

			len -= space_left;
			buf += space_left;
		}

		/* write too-large chunks immediately */
		if (len > file->buf_size) {
			error = gitfo_write(file->fd, buf, len);
			if (file->digest)
				git_hash_update(file->digest, buf, len);
		}
	}
}
Exemple #3
0
int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
{
    int error;
    const unsigned char *buf = buff;

    for (;;) {
        size_t space_left = file->buf_size - file->buf_pos;

        /* cache if it's small */
        if (space_left > len) {
            add_to_cache(file, buf, len);
            return GIT_SUCCESS;
        }

        /* flush the cache if it doesn't fit */
        if (file->buf_pos > 0) {
            add_to_cache(file, buf, space_left);

            if ((error = flush_buffer(file)) < GIT_SUCCESS)
                return git__rethrow(error, "Failed to write to buffer");

            len -= space_left;
            buf += space_left;
        }

        /* write too-large chunks immediately */
        if (len > file->buf_size) {
            error = file->write(file, buf, len);
            if (error < GIT_SUCCESS)
                return git__rethrow(error, "Failed to write to buffer");
        }
    }
}
Exemple #4
0
int test_cache (void)
{
    int error = 0;

    /* save the current values */
    long long saved_size = cache_size_mb;
    long long saved_free = cache_free_mb;
    cache_entry * saved_head = cache_head;

    cache_size_mb = 10;
    cache_free_mb = 10;
    cache_head = NULL;

    touch (F1); 
    if (ok_to_cache (F1, 3*MEGABYTE)!=1) { error= 1; goto out; }
    LogprintfCache();
    sleep (1);

    touch (F2);
    add_to_cache (F2, 3*MEGABYTE);
    LogprintfCache();
    sleep (1);

    touch (F3);
    if (ok_to_cache (F3, 11*MEGABYTE)!=0) { error = 2; goto out; }
    if (ok_to_cache (F3, 7*MEGABYTE)!=1) { error = 3; goto out; }
    LogprintfCache();

    touch (F4);
    if (ok_to_cache (F4, 4*MEGABYTE)!=1) { error = 4; goto out; }
    touch (F5);
    if (ok_to_cache (F5, 6*MEGABYTE)!=1) { error = 5; goto out; }
    LogprintfCache();

    touch (F3);
    add_to_cache (F3, 3*MEGABYTE);
    touch (F2);
    add_to_cache (F2, 5*MEGABYTE);
    LogprintfCache();

    touch (F1);
    if (ok_to_cache (F1, 1*MEGABYTE)!=1) { error = 6; goto out; }
    LogprintfCache();
    
out:
    cache_size_mb = saved_size;
    cache_free_mb = saved_free;
    cache_head = saved_head;
    system (RM_CMD);
    return error;
}
Exemple #5
0
void
probe_scsi_device(long bus, long id, int unsure)
{
    UInt32 devType;
    
    if (DoInquiry(id, bus, &devType)) {
    	if (devType == kScsiDevTypeDirect
    		|| devType == kScsiDevTypeOptical) {
    	    add_to_cache(bus, id, 0, unsure);
    	} else if (devType == kScsiDevTypeCDROM
		|| devType == kScsiDevTypeWorm) {
    	    add_to_cache(bus, id, 1, unsure);
    	}
    }
}
Exemple #6
0
void aggr_source_discovery::discovered_source(int ifindex,
					      const inet6_addr &group,
					      const inet6_addr &src) {
	/* if it wasn't in the cache or was old*/
	if (add_to_cache(m_cache, ifindex, group, src) >= 0)
		source_discovery_origin::discovered_source(ifindex, group, src);
}
char *file_cache_mutable_file_data(file_cache *cache, const char *file){
	printf("\n File to be changed is %s",file);
	file_cache *search_val = search_cache(cache,file);
	
	// If file exist in cache and already dirty , flush contents to storage before writing new content.
	if(search_val && search_val->pin && search_val->dirty){
	       write_to_file(search_val);
	}	
	
	// write to cache when entry exist in cache and is pinned and is not dirty already 
	else if(search_val && search_val->pin && !search_val->dirty){
	        printf("\n File %s exist in cache.\n",file);
			printf("\n Copy file_content to buffer \n");
		    strcpy(search_val->buf,file_content);
	}
	// File doesnt exist in cache. Add it to cache and update buffer of file_cache object.
	else {
	       #ifdef DEBUG
			  printf("\n File not found in cache. Adding it to cache. \n");
			  fflush(stdout);
			#endif  /* DEBUG */
			search_val = add_to_cache(file);
			strcpy(search_val->buf,file_content);
	}
	search_val->dirty = 1;		// make dirty bit to 1 to indicate that buffer has some data which is not yet written to disk.
	return search_val->buf;
}
SDL_Surface *WadImageCache::get_image(WadImageDescriptor& desc, int width, int height, SDL_Surface *image)
{
	SDL_Surface *surface = retrieve_image(desc, width, height);
	if (surface)
		return surface;

	if (image)
	{
		surface = resize_image(image, width, height);
	}
	else
	{
		image = image_from_desc(desc);
		if (image)
		{
			surface = resize_image(image, width, height);
			SDL_FreeSurface(image);
		}
	}

	if (surface)
	{
		add_to_cache(cache_key_t(desc, width, height), surface);
	}
	return surface;
}
Exemple #9
0
void sectorcache_add(__u32 ioblkID, char *content, int len, int updatehits,
		__u32 newleader_ioblkID, int *bcachefoundout)
{
	char key[25], newleaderkey[25];

	if (updatehits && (freplayflag || preplayflag))	/* Write request */
		sprintf(newleaderkey, "%u", newleader_ioblkID);
	else
		strcpy(newleaderkey, "");

	/* Content in sector-cache should be BLKSIZE long, but for sake
	 * of simulation by traces, we may just use the human representation of
	 * MD5 hashes as content themselves.
	 */
	sprintf(key, "%u", ioblkID);
	add_to_cache(key, content, len, updatehits, newleaderkey, bcachefoundout); 
#if defined(SIMREPLAY_DEBUG_SS_DONE)
	if (strstr(key, "1050479") || strstr(key, "1095667"))
		printf("In %s, added to cache for key = %s\n", 
				__FUNCTION__, key);
#endif

#if defined(SIMREPLAY_DEBUG_SS_DONE)
	if (strcmp(key, "558350")==0)
    printf("Buffer add: key=%s, buf=%s\n", key, (char*)content);
#endif
}
Exemple #10
0
void WadImageCache::cache_image(WadImageDescriptor& desc, int width, int height, SDL_Surface *image)
{
	std::string name = retrieve_name(desc, width, height, true);
	if (!name.empty())
	{
		autosave_cache();
		return;
	}
	
	SDL_Surface *resized_image = NULL;
	if (image)
	{
		resized_image = resize_image(image, width, height);
	}
	else
	{
		image = image_from_desc(desc);
		if (image)
		{
			resized_image = resize_image(image, width, height);
			SDL_FreeSurface(image);
		}
	}
	if (!resized_image)
		return;
	
	name = add_to_cache(cache_key_t(desc, width, height), resized_image);
	SDL_FreeSurface(resized_image);
}
Exemple #11
0
/* Returns 1 if there is space in the cache for the image,
 * purging the cache in LRU fashion, if necessary.
 * The function assumes the image is not in the cache already */
static int ok_to_cache (const char * cached_path, const long long file_size_bytes)
{
    long long file_size_mb = file_size_bytes/MEGABYTE;

    if (file_size_mb > cache_size_mb) return 0;

    while (file_size_mb > cache_free_mb) {
        time_t oldest_mtime = time (NULL) + 1;
        off_t  oldest_size = 0;
        cache_entry * oldest_entry = NULL;
        struct stat mystat;
        cache_entry * e;
        for ( e = cache_head; e; e=e->next) {
            if (stat (e->path, &mystat)<0) {
                logprintfl (EUCAERROR, "error: ok_to_cache() can't stat %s\n", cached_path);
                return 0;
            }
            if (mystat.st_mtime<oldest_mtime) {
                oldest_mtime = mystat.st_mtime;
                oldest_size = e->size_mb; /* (mystat.st_size doesn't include digest) */
                oldest_entry = e;
            } else {
                if (mystat.st_mtime==oldest_mtime) {
                    /* smaller ones get purged first */
                    if (oldest_size > e->size_mb) {
                        oldest_size = e->size_mb;
                        oldest_entry = e;
                    }
                }
            }
        }

        if ( oldest_entry ) { // remove it
            logprintfl (EUCAINFO, "purging from cache image %s\n", oldest_entry->path);
            if ( oldest_entry->next ) {
                oldest_entry->next->prev = oldest_entry->prev;
            }
            if ( oldest_entry->prev ) {
                oldest_entry->prev->next = oldest_entry->next;
            } else {
                cache_head = oldest_entry->next;
            }
            if ( unlink (oldest_entry->path) != 0 ) { // should allow open descriptors to complete
                logprintfl (EUCAERROR, "error: failed to unlink file %s (%s)\n", oldest_entry->path, strerror (errno));
            }
            char digest_path [BUFSIZE];
            snprintf (digest_path, BUFSIZE, "%s-digest", oldest_entry->path);
            unlink (digest_path);
            cache_free_mb += oldest_entry->size_mb;
            free (oldest_entry);
        } else {
            logprintfl (EUCAERROR, "error: cannot find oldest entry in cache\n");
            return 0;
        }
    }
    add_to_cache (cached_path, file_size_bytes);
    return 1;
}
Exemple #12
0
void cache_set(cache_t cache, key_type key, val_type val, uint32_t val_size){
    link_t * init_link = querry_hash(cache,key);
    //if the item is already in the list, then delete it
    del_link(cache,init_link);
    //if the policy tells the cache not to add the item, do not add it
    if(!should_add_evict_deletions(cache,val_size)){
        return;
    }
    add_to_cache(cache,key,val,val_size);
}
/** Computes the function Pre_crit(a) (critical signals optimization) where critical_signals_index is the set of critical
    signals for a (the first element of the array is the number of critical signals)
    prev_a is the last antichain of the input computed
 	Uses a cache memory to prevent to recompute already computed omega                                                    **/
antichain*
pre_crit(antichain* a, antichain* prev_a, int *critical_signals_index, alphabet_info *alphabet) {
	int nb_critical_signals = critical_signals_index[0];

    // If the set of critical signal or the antichain is empty, the fixpoint is reached
    if(nb_critical_signals == 0 || a->incomparable_elements == NULL) {
		 return clone_antichain(prev_a, (void*)clone_tuple);
    }

    antichain *pre, *cur_antichain;
    tuple *cur_omega;
    GList *antichains_list = NULL;
    GList *curlink;
    int i;
    LABEL_BIT_REPRES **sigma = alphabet->sigma_input;

	for(i=1; i<=nb_critical_signals; i++) {
		cur_antichain = new_antichain();
		curlink = a->incomparable_elements;
		while(curlink != NULL) {
			//Check whether we have already computed omega for curlink->data and sigma i
			cur_omega = (tuple*)get_from_cache(cache_tuples, curlink->data, critical_signals_index[i]);
			if(cur_omega == NULL) { //cache miss
				cur_omega = tuple_omega(curlink->data, critical_signals_index[i], sigma);
				add_to_cache(cache_tuples, clone_tuple(curlink->data), critical_signals_index[i], (void*)cur_omega); //add the computed tuple to cache
			}
			if(cur_omega != NOT_DEFINED) {
				//Add to antichain
				add_element_to_antichain(cur_antichain, cur_omega, (void*)compare_tuples); // no need to free as all omega computed are stored in cache
			}
			curlink = curlink->next;
		}
		antichains_list = scan_add_or_remove_and_sort_and_free(antichains_list, cur_antichain, (void*)compare_antichains, (void*)compare_tuples, (void*)compare_antichains_size, (void*)free_antichain);
	}

	if(antichains_list != NULL) {
		antichain* prev_pre;
		pre = clone_antichain(antichains_list->data, (void*)clone_tuple);
		curlink = antichains_list->next;
		while(curlink != NULL) {
			prev_pre = pre;
			pre = compute_antichains_intersection(prev_pre, curlink->data, (void*)compare_tuples, (void*)compute_tuples_intersection, (void*)clone_tuple, (void*)free_tuple_full);
			free_antichain(curlink->data);
			free_antichain_full(prev_pre, (void*)free_tuple_full);
			curlink = curlink->next;
		}
		g_list_free(antichains_list);
	}
	else { // impossible (?)
		printf("antichains_list empty!!!");
	}

	return pre;
}
Exemple #14
0
bool resolver::make_request(const char* name, size_t namelen, rr_type type, bool in_cache)
{
	msg msg;
	msg.h = (header*) msg.data;

	fill_header(msg.h, QUERY_STANDARD);

	size_t size;
	if (!fill_question(name, namelen, type, msg.data + sizeof(header), size)) {
		return false;
	}

	size += sizeof(header);

	if (!in_cache) {
		if (!add_to_cache(name, namelen, type)) {
			return false;
		}
	}

	int sd;
	if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("socket");
		return false;
	}

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = 0x0102a8c0;
	addr.sin_port = htons(DNS_SERVER_PORT);

	memset(&addr.sin_zero, 0, sizeof(addr.sin_zero));

	ssize_t ret;
	if ((ret = sendto(sd, msg.data, size, 0, (const struct sockaddr*) &addr, sizeof(struct sockaddr))) != (ssize_t) size) {
		perror("sendto");
		close(sd);
		return false;
	}

	socklen_t addrlen = sizeof(struct sockaddr);
	if ((ret = recvfrom(sd, msg.data, sizeof(msg.data), 0, (struct sockaddr*) &addr, &addrlen)) < 0) {
		perror("sendto");
		close(sd);
		return false;
	}

	close(sd);

	msg.end = msg.data + ret;

	return parse_message(msg);
}
Exemple #15
0
void make_cache_step(long int address, struct cache_model *cache, struct passed_args *args) {
/*
    A main function to process the lines of the trace file in a sequential way
*/
    age_cache(cache);
    if (is_in_cache(address, cache, args)) {
        cache_hits += 1;
        return;
    }
    cache_misses += 1;
    add_to_cache(address, cache, args);
}
Exemple #16
0
static void add_remove_files(struct path_list *list)
{
	int i;
	for (i = 0; i < list->nr; i++) {
		struct stat st;
		struct path_list_item *p = &(list->items[i]);

		if (!lstat(p->path, &st))
			add_to_cache(p->path, &st, 0);
		else
			remove_file_from_cache(p->path);
	}
}
Exemple #17
0
static void add_remove_files(struct string_list *list)
{
	int i;
	for (i = 0; i < list->nr; i++) {
		struct stat st;
		struct string_list_item *p = &(list->items[i]);

		if (!lstat(p->string, &st)) {
			if (add_to_cache(p->string, &st, 0))
				die("updating files failed");
		} else
			remove_file_from_cache(p->string);
	}
}
/** Computes the function Pre_O(a)
 	prev_a is the last antichain of P_O computed
 	Uses a cache memory to prevent to recompute already computed antichains **/
static antichain*
pre_O(antichain* a, antichain* prev_a, alphabet_info *alphabet) {
	// If the antichain is empty, the fix point is reached
    if(a->incomparable_elements == NULL) {
		return clone_antichain(a, (void*)clone_tuple);
    }

    antichain *pre, *cur_antichain;
    tuple *cur_omega;
    GList *curlink = a->incomparable_elements;
    int j, sigma_size = alphabet->sigma_output_size;
    char first_iteration = TRUE;
    LABEL_BIT_REPRES **sigma = alphabet->sigma_output;

	while(curlink != NULL) {
		//Check whether we have already computed the antichain of predecessors for curlink->data
		cur_antichain = (antichain*)get_from_cache(cache_antichains, curlink->data, -1);
		if(cur_antichain == NULL) { //cache miss -> compute cur_antichain
			cur_antichain = new_antichain();
			for(j=0; j<sigma_size; j++) {
				cur_omega = tuple_omega((tuple*)(curlink->data), j, sigma);

				if(cur_omega != NOT_DEFINED) {
					//Add to cur_antichain
					add_element_to_antichain_and_free(cur_antichain, cur_omega, (void*)compare_tuples, (void*)free_tuple_full);
				}
			}
			add_to_cache(cache_antichains, clone_tuple(curlink->data), -1, (void*)cur_antichain); //add the computed antichain to cache
		}

		if(first_iteration == TRUE) {
			//Clone the antichain because cur_antichain (which is stored in cache) must be a constant pointer (constant = never modified)
			pre = clone_antichain(cur_antichain, (void*)clone_tuple);
			first_iteration = FALSE;
		}
		else {
			pre = compute_antichains_union(pre, clone_antichain(cur_antichain, (void*)clone_tuple), (void*)compare_tuples, (void*)free_tuple_full);
		}
		curlink = curlink->next;
	}

    antichain *inters = compute_antichains_intersection(pre, prev_a, (void*)compare_tuples, (void*)compute_tuples_intersection, (void*)clone_tuple, (void*)free_tuple_full);
    free_antichain_full(pre, (void*)free_tuple_full);

    return inters;
}
Exemple #19
0
static void add_remove_files(struct string_list *list)
{
	int i;
	for (i = 0; i < list->nr; i++) {
		struct stat st;
		struct string_list_item *p = &(list->items[i]);

		/* p->util is skip-worktree */
		if (p->util)
			continue;

		if (!lstat(p->string, &st)) {
			if (add_to_cache(p->string, &st, 0))
				die(_("updating files failed"));
		} else
			remove_file_from_cache(p->string);
	}
}
const char *file_cache_file_data(file_cache *cache, const char *file){
	
	file_cache *search_val = search_cache(cache,file); // Check if file exist in cache.
		if(search_val && search_val->pin){
			#ifdef DEBUG
				printf("\n File exist in cache \n");
				fflush(stdout);
			#endif  /* DEBUG */
			return search_val->buf;         // If exist read from cache.
		}
		else {
			#ifdef DEBUG
				printf("\n File not found in cache. Adding it to cache. \n");
				fflush(stdout);
			#endif  /* DEBUG */
			search_val = add_to_cache(file); // File not found in cache . Add file to cache and read.
			return search_val->buf;
		}
	
}
Exemple #21
0
int __collatz(long long N)
{
	int t=0;
	long long n = N;
	
	if ((t = lookup_cache(N)) > 0)
		goto done;

	while (!(n & 0x1)) {
		n >>= 1;
		++t;
	}
	
	if (n != 1)
		t = __collatz(3*n+1);
	++t;
	
add_to_cache_return:
	add_to_cache(N, t);
done:	
	return t;
}
void file_cache_pin_files(file_cache *cache,const char **files,int num_files) {
	int i;
	printf("\n Starting file_cache_pin_files \n");
	
	for(i=0;i<num_files;i++) {  // Search if the file to 
        file_cache* search_val = search_cache(cache,*(files+i));
		if(search_val && search_val->pin){ 
			printf("\n File %s already exist in cache \n",files[i]); //mutex
			search_val->pin++;
		}
	    else {
			#ifdef DEBUG
			   printf("\n File %s not found in cache. Adding it to cache. \n",files[i]);
			   fflush(stdout);
			#endif  /* DEBUG */
			search_val = add_to_cache(files[i]);
			if(search_val){
    			printf("\n File %s successfully added to cache \n",files[i]);
    		}
		}
		cache = head;
	}
}
Exemple #23
0
long long scan_cache (void)
{
    long long total_size = 0;

    // remove old LL
    disk_item * d = cache_head;
    while ( d != NULL ) {
        disk_item * prev_d = d;
        d = d->next;
        prev_d->next = NULL;
        prev_d->prev = NULL;
        free_disk_item (prev_d);
    }
    cache_head = NULL;

    logprintfl (EUCAINFO, "scanning the cache directory (%s)\n", cache_path);

    if (strlen(cache_path) == 0) {
        logprintfl (EUCAINFO, "no cache directory yet\n");
        return total_size;
    }

    struct stat mystat;
    if (stat (cache_path, &mystat) < 0) {
        logprintfl (EUCAWARN, "warning: could not stat %s\n", cache_path);
        return -1L;
    }
    total_size += mystat.st_size;

    DIR * cache_dir;
    if ((cache_dir=opendir(cache_path))==NULL) {
        logprintfl (EUCAFATAL, "errror: could not open cache directory %s\n", cache_path);
        return -1L;
    }

    // iterate over all directories in cache directory
    struct dirent * cache_dir_entry;
    while ((cache_dir_entry=readdir(cache_dir))!=NULL) {
        char * image_name = cache_dir_entry->d_name;
        char image_path [EUCA_MAX_PATH];
        long long image_size = 0;
        long long content_size = 0;
        int image_files = 0;


        if (!strcmp(".", image_name) ||
            !strcmp("..", image_name))
            continue;

        DIR * image_dir;
        snprintf (image_path, EUCA_MAX_PATH, "%s/%s", cache_path, image_name);
        if ((image_dir=opendir(image_path))==NULL) {
            logprintfl (EUCAWARN, "warning: unopeneable directory %s\n", image_path);
            continue;
        }

        if (stat (image_path, &mystat) < 0) {
            logprintfl (EUCAWARN, "warning: could not stat %s\n", image_path);
            closedir(image_dir);
            continue;
        }
        image_size += mystat.st_size;

        // add up sizes of all files in a directory
        struct dirent * image_dir_entry;
        boolean found_content = FALSE;
        boolean found_summary = FALSE;
        while ((image_dir_entry=readdir(image_dir))!=NULL) {
            char name [EUCA_MAX_PATH];
            strncpy (name, image_dir_entry->d_name, EUCA_MAX_PATH);

            if (!strcmp(".", name) ||
                !strcmp("..", name))
                continue;

            image_files++;

            char filepath [EUCA_MAX_PATH];
            snprintf (filepath, EUCA_MAX_PATH, "%s/%s", image_path, name);
            if (stat (filepath, &mystat) < 0 ) {
                logprintfl (EUCAERROR, "error: could not stat file %s\n", filepath);
                break;
            }
            if (mystat.st_size < 1) {
                logprintfl (EUCAERROR, "error: empty file among cached images in '%s'\n", filepath);
                break;
            }

            if (!strcmp ("content", name)) {
                content_size = mystat.st_size;
                found_content = TRUE;
            }
            if (!strcmp ("summary", name))
                found_summary = TRUE;

            image_size += mystat.st_size;
        }
        closedir(image_dir);

        // report on what was found in each cache directory
        if (image_files > 0) { // ignore empty directories
            if (image_size>0) {
                logprintfl (EUCAINFO, "- cached image '%s': size=%dMB, files=%d\n", image_name, image_size/MEGABYTE, image_files);
                total_size += image_size;

                //              if (!found_content || !found_summary)
                //                  logprintfl (EUCAINFO, "(warning: incomplete cache entry for image '%s')\n", image_name); // TODO: do this if cache entry is not locked

                // allocate a disk item object
                disk_item * di = alloc_disk_item (image_name, content_size, image_size, TRUE);
                add_to_cache (di);
            } else {
                logprintfl (EUCAWARN, "warning: empty cached image directory %s\n", image_path);
            }
        }
    }
    closedir (cache_dir);

    print_cache ();

    return total_size;
}
Exemple #24
0
int reserve_cache_slot (disk_item * di)
{
    // TODO: check if there is enough disk space?

    if (cache_limit==EUCA_SIZE_UNLIMITED) { // cache is always big enough, we never purge
        cache_used += di->total_size;
        goto create;
    }

    if (di->total_size > cache_limit) // cache isn't big enough
        return ERROR;

    while (di->total_size > (cache_limit-cache_used) ) {
        time_t oldest_mtime = time (NULL) + 1;
        off_t  oldest_size = 0;
        disk_item * oldest_entry = NULL;
        struct stat mystat;
        disk_item * e;

        // run through cache and find the LRU entry
        for ( e = cache_head; e; e=e->next) {
            if (stat (e->path, &mystat)<0) {
                logprintfl (EUCAERROR, "error: ok_to_cache() can't stat %s\n", e->path);
                return ERROR;
            }
            if (mystat.st_mtime<oldest_mtime) {
                oldest_mtime = mystat.st_mtime;
                oldest_size = e->total_size; // (mystat.st_size doesn't include digest)
                oldest_entry = e;
            } else {
                if (mystat.st_mtime==oldest_mtime) { // with same age, smaller ones get purged first
                    if (oldest_size > e->total_size) {
                        oldest_size = e->total_size;
                        oldest_entry = e;
                    }
                }
            }
        }

        if ( oldest_entry ) { // remove it
            logprintfl (EUCAINFO, "purging from cache entry %s\n", oldest_entry->base);
            if ( oldest_entry->next ) {
                oldest_entry->next->prev = oldest_entry->prev;
            }
            if ( oldest_entry->prev ) {
                oldest_entry->prev->next = oldest_entry->next;
            } else {
                cache_head = oldest_entry->next;
            }
            delete_disk_item (oldest_entry);
            cache_used -= oldest_entry->total_size;
            free_disk_item (oldest_entry);
        } else {
            logprintfl (EUCAERROR, "error: cannot find oldest entry in cache\n");
            return 1;
        }
    }

 create:

    if (ensure_path_exists (di->base, 0700)) {
        logprintfl (EUCAERROR, "error: failed to create cache entry '%d'\n", di->base);
        return ERROR;
    }

    add_to_cache (di);

    return OK;
}
Exemple #25
0
static void deserialize_value(lua_State *L, luaL_Buffer* buffer, char** frame, int* index, int* gindex, size_t* len, uint16_t* key) {
    if (!lua_checkstack(L, 2)) {
        luaL_error(L, "cannot deserialize: stack won't grow");
    }
    uint16_t size = 0x0000;
    read_object(L, buffer, frame, index, 1, len);
    uint8_t type = (*frame)[*index];
    *index = *index + 1;
    *gindex = *gindex + 1;
    switch (type) {
    case BIN_NIL: {
        // printf("\r\n\t(nil[1])[%p]", *frame);
        lua_pushnil(L);
        break;
    }

    case BIN_BOOLEAN: {
        read_object(L, buffer, frame, index, 1, len);
        // printf("\r\n\t(boolean[2][%p]", *frame);
        lua_pushboolean(L, (*frame)[*index]);
        *index = *index + 1;
        *gindex = *gindex + 1;
        break;
    }

    case BIN_DOUBLE: {
        double number = 0;
        size = sizeof(number);
        read_object(L, buffer, frame, index, size, len);
        // printf("\r\n\t(number[%d])[%p]", size, *frame);
        char* b = (char*) &number;
        int i;
        for (i = 0; i < size; i++) {
            b[i] = (*frame)[*index + i];
        }
        if (sendian.double_) ntoh(&number, sizeof(number), sendian.double_);
        lua_pushnumber(L, number);
        *index = *index + size;
        *gindex = *gindex + size;
        break;
    }

    case BIN_INTEGER: {
        int32_t number = 0;
        size = sizeof(number);
        read_object(L, buffer, frame, index, size, len);
        // printf("\r\n\t(number[%d])[%p]", size, *frame);
        char* b = (char*) &number;
        int i;
        for (i = 0; i < size; i++) {
            b[i] = (*frame)[*index + i];
        }
        if (sendian.int32_) ntoh(&number, sizeof(number), sendian.int32_);
        lua_pushinteger(L, number);
        *index = *index + size;
        *gindex = *gindex + size;
        break;
    }

    case BIN_REF: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] =(*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        // printf("\r\n\t(ref[%d])[%p]", size, *frame);
        get_from_cache(L, size);
        break;
    }

    case BIN_STRING: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] = (*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        read_object(L, buffer, frame, index, size, len);
        // printf("\r\n\t(string[%d])[%p]", size, *frame);
        lua_pushlstring(L, &((*frame)[*index]), size);
        *index = *index + size;
        *gindex = *gindex + size;
        add_to_cache(L, key);
        break;
    }

    case BIN_FUNCTION: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] = (*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        // printf("\r\n\t(function[%d])[%p]", size, *frame);
        read_object(L, buffer, frame, index, size, len);
        Dfunction dfunction;
        dfunction.counter = 0;
        dfunction.size = size;
        dfunction.buffer = &((*frame)[*index]);
        lua_load(L, f_reader, &dfunction, "function");
        *index = *index + size;
        *gindex = *gindex + size;
        add_to_cache(L, key);
        break;
    }

    case BIN_TABLE: {
        read_object(L, buffer, frame, index, 2, len);
        ((char*) &size)[0] = (*frame)[*index];
        ((char*) &size)[1] = (*frame)[*index + 1];
        if (sendian.int16_) ntoh(&size, sizeof(size), sendian.int16_);
        *index = *index + 2;
        *gindex = *gindex + 2;
        lua_newtable(L);
        // printf("\r\n\t{table[%d][%p]=", size, *frame);
        add_to_cache(L, key);
        int top = lua_gettop(L);
        while (size > 0) {
            // printf("\r\n\t|key[%d][%p]", *index, *frame);
            deserialize_value(L, buffer, frame, index, gindex, len, key);
            // printf("\r\n\t+value[%d][%p]", *index, *frame);
            deserialize_value(L, buffer, frame, index, gindex, len, key);
            lua_rawset(L, top);
            size--;
        }
        // printf("\r\n\t)");
        // printf("\r\n\t}");
        break;
    }

    default:
        luaL_error(L, "cannot deserialize: unsupported type [%d] at %d", type, *gindex);
    }
}
Exemple #26
0
static long long init_cache (const char * cache_path)
{
    long long total_size = 0;
    
    logprintfl (EUCAINFO, "checking the integrity of the cache directory (%s)\n", cache_path);
    
    if (cache_path==NULL) {
        logprintfl (EUCAINFO, "no cache directory yet\n");
        return total_size;
    }

    struct stat mystat;
    if (stat (cache_path, &mystat) < 0) {
        logprintfl (EUCAFATAL, "error: could not stat %s\n", cache_path);
        return -1;
    }
    total_size += mystat.st_size;
   
    DIR * cache_dir;
    if ((cache_dir=opendir(cache_path))==NULL) {
        logprintfl (EUCAFATAL, "errror: could not open cache directory %s\n", cache_path);
        return -1;
    }

    struct dirent * cache_dir_entry;
    while ((cache_dir_entry=readdir(cache_dir))!=NULL) {
        char * image_name = cache_dir_entry->d_name;
        char image_path [BUFSIZE];
        int image_size = 0;
        int image_files = 0;

        if (!strcmp(".", image_name) || 
            !strcmp("..", image_name))
            continue;
        
        DIR * image_dir;
        snprintf (image_path, BUFSIZE, "%s/%s", cache_path, image_name);
        if ((image_dir=opendir(image_path))==NULL) {
            logprintfl (EUCAWARN, "warning: unopeneable directory %s\n", image_path);
            continue;
        }

        if (stat (image_path, &mystat) < 0) {
            logprintfl (EUCAWARN, "warning: could not stat %s\n", image_path);
	    closedir(image_dir);
            continue;
        }
        image_size += mystat.st_size;
        
        /* make sure that image directory contains only two files: one
         * named X and another X-digest, also add up their sizes */
        char X        [BUFSIZE] = "";
        char X_digest [BUFSIZE] = "";
        struct dirent * image_dir_entry;
        while ((image_dir_entry=readdir(image_dir))!=NULL) {
            char name [BUFSIZE];
            strncpy (name, image_dir_entry->d_name, BUFSIZE);
            
            if (!strcmp(".", name) ||
                !strcmp("..", name))
                continue;

            image_files++;
            
            char filepath [BUFSIZE];
            snprintf (filepath, BUFSIZE, "%s/%s", image_path, name);
            if (stat (filepath, &mystat) < 0 ) {
                logprintfl (EUCAERROR, "error: could not stat file %s\n", filepath);
                break;
            }
            if (mystat.st_size < 1) {
                logprintfl (EUCAERROR, "error: empty file among cached images in %s\n", filepath);
                break;
            }
            image_size += mystat.st_size;
            
            char * suffix;
            if ((suffix=strstr (name, "-digest"))==NULL) {
                if (strlen (X)) 
                    break; /* already saw X => fail */
                strncpy (X, name, BUFSIZE);
            } else {
                if (strlen (X_digest))
                    break; /* already saw X-digest => fail */
                * suffix = '\0';
                strncpy (X_digest, name, BUFSIZE);
            }
        }
	closedir(image_dir);

        if (image_files > 0) { /* ignore empty directories */
            if (image_files != 2 || strncmp (X, X_digest, BUFSIZE) != 0 ) {
                logprintfl (EUCAERROR, "error: inconsistent state of cached image %s, deleting it\n", image_name);
                if (vrun ("rm -rf %s", image_path)) {            
                    logprintfl (EUCAWARN, "warning: failed to remove %s\n", image_path);
                }
            } else {
                char filepath [BUFSIZE];
                snprintf (filepath, BUFSIZE, "%s/%s", image_path, X);
                if (image_size>0) {
                    logprintfl (EUCAINFO, "- cached image %s directory, size=%d\n", image_name, image_size);
                    total_size += image_size;
                    add_to_cache (filepath, image_size);
                } else {
                    logprintfl (EUCAWARN, "warning: empty cached image directory %s\n", image_path);
                }
            }
        }
    }
    closedir (cache_dir);

    return total_size;
}
/** Critical input signals optimization: Computes a minimal critical set of inputs signals for antichain
    For each element f of antichain, for each symbol s_O of the output alphabet, find a critical input signal,
    i.e. a signal s_I s.t. succ(succ(f, s_O), s_I) does not belong to antichain (+ privilege already added signals to try to minimize the set)
    Stops when the critical signals for one one-step-loosing tuple is found															**/
int*
compute_critical_set(antichain *a, alphabet_info *alphabet) {
//  Note: local cache memories optimization disabled since it seems less efficient
//	GHashTable *cache_lvl1 = g_hash_table_new_full(hash_key, compare_keys, (GDestroyNotify)free_hash_table_key, NULL); //cache memory used to avoid redondant computation (for the successors of t)
//	GHashTable *cache_lvl2 = g_hash_table_new_full(hash_key, compare_keys, (GDestroyNotify)free_hash_table_key, NULL); //cache memory used to avoid redondant computation (for the successors of the successors of t)

	char found, inC, is_one_step_loosing, is_in_antichain;
	char *info_lvl1, *info_lvl2;
	char *true_value = malloc(sizeof(char));
	true_value[0] = TRUE;
	char *false_value = malloc(sizeof(char));
	false_value[0] = FALSE;

	int i, input_size = alphabet->sigma_input_size;
	tuple *t, *succ_t, *succ_succ_t;
	antichain *min_succ;

	int* c = (int*)malloc((input_size+1)*sizeof(int)); //array of critical signals index in the input alphabet
	char* c_bool = (char*)malloc((input_size)*sizeof(char)); //array of booleans representing the critical set on a boolean format
	int* curC = (int*)malloc((input_size+1)*sizeof(int)); //array of signals found to be critical till here and for t (will be added to c only if t is one step loosing)
	char* curC_bool = (char*)malloc((input_size)*sizeof(char)); //c_bool but on a boolean format

	// Initialize c and c_bool
	c[0] = 0;
	for(i=0; i<input_size; i++) {
		c[i+1] = -1;
		c_bool[i] = FALSE;
	}

	GList *succ_t_link, *cur_link = a->incomparable_elements;
	while(cur_link != NULL) { //for each element t of the antichain
		t = cur_link->data;

		//Copy c and c_bool in curC and curC_bool
		curC[0] = c[0];
		for(i=0; i<input_size; i++) {
			curC[i+1] = c[i+1];
			curC_bool[i] = c_bool[i];
		}

		is_one_step_loosing = TRUE;
		min_succ = (antichain*)get_from_cache(cache_min_succ, t, -1);
		if(min_succ == NULL) { //cache miss
			min_succ = minimal_tuple_succ(t, alphabet); //compute the antichain of minimal successors of t (no need of analyzing non minimal successors)
			add_to_cache(cache_min_succ, clone_tuple(t), -1, (void*)min_succ); //add the computed antichain to cache
		}
		succ_t_link = min_succ->incomparable_elements;
		while(succ_t_link != NULL) { //for all succ of t s.t. succ is minimal
			found = FALSE; //will be set to TRUE if a critical s_I is found for t
			inC = FALSE; //will be set to True if the critical signal s_I found has already been for another t or min_succ
			succ_t = clone_tuple(succ_t_link->data);

			//Seek on cache if succ_t has already been processed, i.e. if we have already checked if there exists a s_I s.t. succ(succ_t, s_I) does not belong to the antichain
			//info_lvl1 = get_from_cache(cache_lvl1, succ_t, -1); //attempt to retrieve data from cache_lvl1
			//if(info_lvl1 == NULL) { //cache miss
				for(i=1; i<=curC[0]; i++) { //for s_I in c (privilege already added signals)
					succ_succ_t = (tuple*)get_from_cache(cache_succ, succ_t, curC[i]);
					if(succ_succ_t == NULL) { // cache miss
						succ_succ_t = tuple_succ(succ_t, curC[i], alphabet);
						add_to_cache(cache_succ, clone_tuple(succ_t), curC[i], (void*)succ_succ_t); //add the computed succ to cache
					}

					//Seek on cache if succ_succ_t has already been processed
					//info_lvl2 = get_from_cache(cache_lvl2, succ_succ_t, -1); //attempt to retrieve data from cache_lvl2
					//if(info_lvl2 == NULL) { //cache miss
						is_in_antichain = contains_element(a, succ_succ_t, (void*)compare_tuples);
						/*if(is_in_antichain == TRUE) {
							add_to_cache(cache_lvl2, clone_tuple(succ_succ_t), -1, true_value); //add the computed result to cache_lvl2
						}
						else {
							add_to_cache(cache_lvl2, clone_tuple(succ_succ_t), -1, false_value); //add the computed result to cache_lvl2
						}
					}
					else {
						//free_tuple_full(succ_succ_t);
						is_in_antichain = info_lvl2[0];
					}*/

					if(is_in_antichain == FALSE) {
						inC = TRUE;
						break;
					}
				}

				if(inC == FALSE) { //if not found yet
					for(i=0; i<input_size; i++) { //for s_I in input alphabet
						if(curC_bool[i] == FALSE) { //if s_I does not belong to curC (otherwise it has already been tested)
							succ_succ_t = (tuple*)get_from_cache(cache_succ, succ_t, i);
							if(succ_succ_t == NULL) { // cache miss
								succ_succ_t = tuple_succ(succ_t, i, alphabet);
								add_to_cache(cache_succ, clone_tuple(succ_t), i, (void*)succ_succ_t); //add the computed succ to cache
							}

							//Seek on cache if succ_succ_t has already been processed
							//info_lvl2 = get_from_cache(cache_lvl2, succ_succ_t, -1); //attempt to retrieve data from cache_lvl2
							//if(info_lvl2 == NULL) { //cache miss
								is_in_antichain = contains_element(a, succ_succ_t, (void*)compare_tuples);
								/*if(is_in_antichain == TRUE) {
									add_to_cache(cache_lvl2, clone_tuple(succ_succ_t), -1, true_value); //add the computed result to cache_lvl2
								}
								else {
									add_to_cache(cache_lvl2, clone_tuple(succ_succ_t), -1, false_value); //add the computed result to cache_lvl2
								}
							}
							else {
								//free_tuple_full(succ_succ_t);
								is_in_antichain = info_lvl2[0];
							}*/

							if(is_in_antichain == FALSE) {
								found = TRUE; //there exists a s_I s.t. succ(succ(f, s_O), s_I) does not belong to antichain
								curC[0] = curC[0]+1; //increment the number of critical signals
								curC[curC[0]] = i; //add s_I to curC
								curC_bool[i] = TRUE; //set the boolean of s_I to True
								break;
							}
						}
					}
				}

				if(found == FALSE && inC == FALSE) { //there exists a s_O s.t. for all s_I succ(succ(t, s_O), s,I) belongs to antichain -> t is not one step loosing
					is_one_step_loosing = FALSE;
				}
				/*if(is_one_step_loosing == TRUE) {
					add_to_cache(cache_lvl1, clone_tuple(succ_t), -1, true_value); //add the computed result to cache_lvl1
				}
				else {
					add_to_cache(cache_lvl1, clone_tuple(succ_t), -1, false_value); //add the computed result to cache_lvl1
				}
			}
			else { //cache hit
				free_tuple_full(succ_t);
				is_one_step_loosing = info_lvl1[0];
			}*/

			if(is_one_step_loosing == FALSE) {
				break; //not one step loosing -> skip to the next t
			}

			succ_t_link = succ_t_link->next;
		}

		//free_antichain_full(min_succ, (void*)free_tuple_full); // Not freed here because min_succ is added in cache (will be freed at the end on the fix point computation)

		// If f is one step loosing, add signals in curC to c
		if(is_one_step_loosing == TRUE) {
			c[0] = curC[0];
			for(i=0; i<input_size; i++) {
				c[i+1] = curC[i+1];
				c_bool[i] = curC_bool[i];
			}
			break; //break here because we found critical signals for one one-step-loosing tuple (enough)
		}

		cur_link = cur_link->next;
	}
	free(c_bool);
	free(curC);
	free(curC_bool);
	free(true_value);
	free(false_value);

//	g_hash_table_destroy(cache_lvl1);
//	g_hash_table_destroy(cache_lvl2);

	return c;
}
Exemple #28
0
bool resolver::parse_message(msg& msg)
{
	if ((size_t) (msg.end - msg.data) <= sizeof(header)) {
		return false;
	}

	if (!parse_header(msg)) {
		return false;
	}

	if (msg.h->ancount == 0) {
		return false;
	}

	if (!find_first_record(msg)) {
		return false;
	}

	dns_entry entry;
	entry.name = msg.name;
	entry.namelen = msg.namelen;

	red_black_tree<dns_entry>::iterator it;
	if (!_M_cache.find(entry, it)) {
		// Name not found in the cache.
		return false;
	}

	dns_entry* e = it.data;

	msg.narecords = 0;
	msg.cnamelen = 0;

	for (unsigned short i = msg.h->ancount; (msg.ptr < msg.end) && (i > 0); i--) {
		rr rr;
		if (!find_next_record(msg, rr)) {
			return false;
		}

		if (!process_record(msg, rr)) {
			return false;
		}
	}

	time_t now = time(NULL);

	if (msg.narecords == 1) {
		// If not the original request...
		if (e->cnamelen > 0) {
			// The domain name of the original request is in e->cname.
			red_black_tree<dns_entry>::iterator itcname;
			if (!find(e->cname, e->cnamelen, itcname)) {
				_M_cache.erase(it);
				return false;
			}

			_M_cache.erase(it);

			e = itcname.data;
		}

		struct address* addr = msg.arecords;

		e->address.addr = addr->addr;
		e->address.expire = now + addr->expire;
		e->address.preference = addr->preference;

		e->addresses = &e->address;
		e->naddresses = 1;

		e->status = STATUS_VALID;

		return true;
	} else if (msg.narecords > 1) {
		// If not the original request...
		if (e->cnamelen > 0) {
			// The domain name of the original request is in e->cname.
			red_black_tree<dns_entry>::iterator itcname;
			if (!find(e->cname, e->cnamelen, itcname)) {
				_M_cache.erase(it);
				return false;
			}

			_M_cache.erase(it);

			e = itcname.data;
		}

		if ((e->addresses = (struct address*) malloc(msg.narecords * sizeof(struct address))) == NULL) {
			_M_cache.erase(it);
			return false;
		}

		struct address* addr = msg.arecords;
		for (unsigned short i = 0; i < msg.narecords; i++, addr++) {
			e->addresses[i].addr = addr->addr;
			e->addresses[i].expire = now + addr->expire;
			e->addresses[i].preference = addr->preference;
		}

		e->naddresses = msg.narecords;

		e->status = STATUS_VALID;

		return true;
	} else if (msg.cnamelen > 0) {
		dns_entry* ecname;

		// If not the original request...
		if (e->cnamelen > 0) {
			// The domain name of the original request is in e->cname.
			char* cname = e->cname;
			unsigned short cnamelen = e->cnamelen;

			red_black_tree<dns_entry>::iterator itcname;
			if (!find(cname, cnamelen, itcname)) {
				_M_cache.erase(it);
				return false;
			}

			e->cnamelen = 0;

			_M_cache.erase(it);

			if (++itcname.data->recursion > MAX_RECURSION) {
				_M_cache.erase(itcname);
				free(cname);
				return false;
			}

			if (!add_to_cache(msg.cname, msg.cnamelen, (rr_type) msg.type, &itcname)) {
				_M_cache.erase(itcname);
				free(cname);
				return false;
			}

			ecname = itcname.data;
			ecname->cname = cname;
			ecname->cnamelen = cnamelen;
		} else {
			red_black_tree<dns_entry>::iterator itcname;
			if (!add_to_cache(msg.cname, msg.cnamelen, (rr_type) msg.type, &itcname)) {
				_M_cache.erase(it);
				return false;
			}

			ecname = itcname.data;
			if ((ecname->cname = (char*) malloc(msg.namelen)) == NULL) {
				_M_cache.erase(itcname);
				_M_cache.erase(it);
				return false;
			}

			memcpy(ecname->cname, msg.name, msg.namelen);
			ecname->cnamelen = msg.namelen;
		}

		return make_request(ecname->name, ecname->namelen, (rr_type) ecname->type, true);
	} else {
		return false;
	}
}
Exemple #29
0
void doit(int fd) 
{
    char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
    char hostname[MAXLINE], path[MAXLINE], port[MAXLINE];
    char host_header[MAXLINE], remaining_headers[MAXLINE];
    char request[MAXLINE], server_buf[MAXLINE];
    rio_t rio;
    rio_t server_rio;
    memset(buf, 0, MAXLINE);
    memset(method, 0, MAXLINE);
    memset(uri, 0, MAXLINE);
    memset(version, 0, MAXLINE);
    memset(hostname, 0, MAXLINE);
    memset(path, 0, MAXLINE);
    memset(port, 0, MAXLINE);
    memset(host_header, 0, MAXLINE);
    memset(remaining_headers, 0, MAXLINE);
    memset(request, 0, MAXLINE);
    memset(server_buf, 0, MAXLINE);
    
  	
    /* Read request line and headers */
    Rio_readinitb(&rio, fd);
    Rio_readlineb(&rio, buf, MAXLINE);
    sscanf(buf, "%s %s %s", method, uri, version);
    if (strcasecmp(method, "GET")) { 
       clienterror(fd, method, "501", "Not Implemented",
                "Tiny does not implement this method");
        return;
    }

    cache_node *cache_hit = check_for_hit(proxy_cache, uri);
    if (cache_hit != NULL){
    	if(rio_writen(fd, cache_hit->data, cache_hit->data_size) < 0){
   		    pthread_rwlock_unlock(&lock);
    		return;
    	}
    }
	pthread_rwlock_unlock(&lock);

    if (cache_hit == NULL) {
    	char *response_data = Malloc(sizeof(char));
    	unsigned int data_size = 0;
    	read_requesthdrs(&rio, host_header, remaining_headers);
    	/* Parse URI from GET request */
   		if (parse_uri(uri, hostname, path, port) < 0) {
   			return;
   		}
   		if (strncmp(host_header, "Host: ", strlen("Host: ")) != 0){
   			sprintf(host_header, "Host: %s\r\n", hostname);
   		}
   		// printf("%s %s %s\n", hostname, path, port);
   		compile_request(request, host_header, path, remaining_headers);
    	int port_num = atoi(port);

    	int server_fd = Open_clientfd_r(hostname, port_num);
    	if (rio_writen(server_fd, request, strlen(request)) < 0){
    		return;
    	}
    
    	Rio_readinitb(&server_rio, server_fd);
    	int len;
    	while ((len = rio_readnb(&server_rio, server_buf, MAXLINE)) > 0){
    		if (rio_writen(fd, server_buf, len) < 0){
    			return;
    		}
    		response_data = Realloc(response_data, data_size + len);
    		memcpy(response_data + data_size, server_buf, len);
    		data_size += len;
    	}

    	add_to_cache(proxy_cache, uri, response_data, data_size);
    
    	Close(server_fd);
    	Free(response_data);
    	return; 
    } 
}
Exemple #30
0
static void handle_packet(AvahiWideAreaLookupEngine *e, AvahiDnsPacket *p) {
    AvahiWideAreaLookup *l = NULL;
    int i, r;

    AvahiBrowserEvent final_event = AVAHI_BROWSER_ALL_FOR_NOW;
    
    assert(e);
    assert(p);

    /* Some superficial validity tests */
    if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
        avahi_log_warn(__FILE__": Ignoring invalid response for wide area datagram.");
        goto finish;
    }

    /* Look for the lookup that issued this query */
    if (!(l = find_lookup(e, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID))) || l->dead)
        goto finish;

    /* Check whether this a packet indicating a failure */
    if ((r = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & 15) != 0 ||
        avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0) {

        avahi_server_set_errno(e->server, r == 0 ? AVAHI_ERR_NOT_FOUND : map_dns_error(r));
        /* Tell the user about the failure */
        final_event = AVAHI_BROWSER_FAILURE;

        /* We go on here, since some of the records contained in the
           reply might be interesting in some way */
    }

    /* Skip over the question */
    for (i = (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); i > 0; i--) {
        AvahiKey *k;
        
        if (!(k = avahi_dns_packet_consume_key(p, NULL))) {
            avahi_log_warn(__FILE__": Wide area response packet too short or invalid while reading question key. (Maybe a UTF-8 problem?)");
            avahi_server_set_errno(e->server, AVAHI_ERR_INVALID_PACKET);
            final_event = AVAHI_BROWSER_FAILURE;
            goto finish;
        }

        avahi_key_unref(k);
    }

    /* Process responses */
    for (i = (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
             (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) +
             (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); i > 0; i--) {

        AvahiRecord *rr;

        if (!(rr = avahi_dns_packet_consume_record(p, NULL))) {
            avahi_log_warn(__FILE__": Wide area response packet too short or invalid while reading response record. (Maybe a UTF-8 problem?)");
            avahi_server_set_errno(e->server, AVAHI_ERR_INVALID_PACKET);
            final_event = AVAHI_BROWSER_FAILURE;
            goto finish;
        }

        add_to_cache(e, rr);
        avahi_record_unref(rr);
    }

finish:
    
    if (l && !l->dead) {
        if (l->callback)
            l->callback(e, final_event, AVAHI_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);

        lookup_stop(l);
    }
}