Example #1
0
static int unresolve_one(const char *path)
{
	int namelen = strlen(path);
	int pos;
	int ret = 0;
	struct cache_entry *ce_2 = NULL, *ce_3 = NULL;

	/* See if there is such entry in the index. */
	pos = cache_name_pos(path, namelen);
	if (pos < 0) {
		/* If there isn't, either it is unmerged, or
		 * resolved as "removed" by mistake.  We do not
		 * want to do anything in the former case.
		 */
		pos = -pos-1;
		if (pos < active_nr) {
			struct cache_entry *ce = active_cache[pos];
			if (ce_namelen(ce) == namelen &&
			    !memcmp(ce->name, path, namelen)) {
				fprintf(stderr,
					"%s: skipping still unmerged path.\n",
					path);
				goto free_return;
			}
		}
	}

	/* Grab blobs from given path from HEAD and MERGE_HEAD,
	 * stuff HEAD version in stage #2,
	 * stuff MERGE_HEAD version in stage #3.
	 */
	ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
	ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);

	if (!ce_2 || !ce_3) {
		ret = -1;
		goto free_return;
	}
	if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
	    ce_2->ce_mode == ce_3->ce_mode) {
		fprintf(stderr, "%s: identical in both, skipping.\n",
			path);
		goto free_return;
	}

	remove_file_from_cache(path);
	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
		error("%s: cannot add our version to the index.", path);
		ret = -1;
		goto free_return;
	}
	if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
		return 0;
	error("%s: cannot add their version to the index.", path);
	ret = -1;
 free_return:
	free(ce_2);
	free(ce_3);
	return ret;
}
Example #2
0
void stage_updated_gitmodules(void)
{
	struct strbuf buf = STRBUF_INIT;
	struct stat st;
	int pos;
	struct cache_entry *ce;
	int namelen = strlen(".gitmodules");

	pos = cache_name_pos(".gitmodules", namelen);
	if (pos < 0) {
		warning(_("could not find .gitmodules in index"));
		return;
	}
	ce = active_cache[pos];
	ce->ce_flags = namelen;
	if (strbuf_read_file(&buf, ".gitmodules", 0) < 0)
		die(_("reading updated .gitmodules failed"));
	if (lstat(".gitmodules", &st) < 0)
		die_errno(_("unable to stat updated .gitmodules"));
	fill_stat_cache_info(ce, &st);
	ce->ce_mode = ce_mode_from_stat(ce, st.st_mode);
	if (remove_cache_entry_at(pos) < 0)
		die(_("unable to remove .gitmodules from index"));
	if (write_sha1_file(buf.buf, buf.len, blob_type, ce->sha1))
		die(_("adding updated .gitmodules failed"));
	if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
		die(_("staging updated .gitmodules failed"));
}
Example #3
0
static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
			 const char *path, int stage)
{
	int len, option;
	struct cache_entry *ce;

	if (!verify_path(path, mode))
		return error("Invalid path '%s'", path);

	len = strlen(path);
	ce = make_empty_cache_entry(&the_index, len);

	oidcpy(&ce->oid, oid);
	memcpy(ce->name, path, len);
	ce->ce_flags = create_ce_flags(stage);
	ce->ce_namelen = len;
	ce->ce_mode = create_ce_mode(mode);
	if (assume_unchanged)
		ce->ce_flags |= CE_VALID;
	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
	if (add_cache_entry(ce, option))
		return error("%s: cannot add to the index - missing --add option?",
			     path);
	report("add '%s'", path);
	return 0;
}
Example #4
0
static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
			 const char *path, int stage)
{
	int size, len, option;
	struct cache_entry *ce;

	if (!verify_path(path))
		return error("Invalid path '%s'", path);

	len = strlen(path);
	size = cache_entry_size(len);
	ce = xcalloc(1, size);

	hashcpy(ce->sha1, sha1);
	memcpy(ce->name, path, len);
	ce->ce_flags = create_ce_flags(stage);
	ce->ce_namelen = len;
	ce->ce_mode = create_ce_mode(mode);
	if (assume_unchanged)
		ce->ce_flags |= CE_VALID;
	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
	if (add_cache_entry(ce, option))
		return error("%s: cannot add to the index - missing --add option?",
			     path);
	report("add '%s'", path);
	return 0;
}
Example #5
0
static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
{
	int option, size;
	struct cache_entry *ce;

	/* Was the old index entry already up-to-date? */
	if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
		return 0;

	size = cache_entry_size(len);
	ce = xcalloc(1, size);
	memcpy(ce->name, path, len);
	ce->ce_flags = create_ce_flags(0);
	ce->ce_namelen = len;
	fill_stat_cache_info(ce, st);
	ce->ce_mode = ce_mode_from_stat(old, st->st_mode);

	if (index_path(ce->sha1, path, st,
		       info_only ? 0 : HASH_WRITE_OBJECT)) {
		free(ce);
		return -1;
	}
	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
	if (add_cache_entry(ce, option))
		return error("%s: cannot add to the index - missing --add option?", path);
	return 0;
}
Example #6
0
File: reset.c Project: 80520997/git
static void update_index_from_diff(struct diff_queue_struct *q,
		struct diff_options *opt, void *data)
{
	int i;
	int intent_to_add = *(int *)data;

	for (i = 0; i < q->nr; i++) {
		struct diff_filespec *one = q->queue[i]->one;
		int is_missing = !(one->mode && !is_null_sha1(one->sha1));
		struct cache_entry *ce;

		if (is_missing && !intent_to_add) {
			remove_file_from_cache(one->path);
			continue;
		}

		ce = make_cache_entry(one->mode, one->sha1, one->path,
				      0, 0);
		if (!ce)
			die(_("make_cache_entry failed for path '%s'"),
			    one->path);
		if (is_missing) {
			ce->ce_flags |= CE_INTENT_TO_ADD;
			set_object_name_for_intent_to_add_entry(ce);
		}
		add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
	}
}
Example #7
0
static void update_index_from_diff(struct diff_queue_struct *q,
		struct diff_options *opt, void *data)
{
	int i;
	int *discard_flag = data;

	/* do_diff_cache() mangled the index */
	discard_cache();
	*discard_flag = 1;
	read_cache();

	for (i = 0; i < q->nr; i++) {
		struct diff_filespec *one = q->queue[i]->one;
		if (one->mode) {
			struct cache_entry *ce;
			ce = make_cache_entry(one->mode, one->sha1, one->path,
				0, 0);
			if (!ce)
				die("make_cache_entry failed for path '%s'",
				    one->path);
			add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
				ADD_CACHE_OK_TO_REPLACE);
		} else
			remove_file_from_cache(one->path);
	}
}
Example #8
0
static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
		const char *path, int stage, int refresh, int options)
{
	struct cache_entry *ce;
	ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
	if (!ce)
		return error("addinfo_cache failed for path '%s'", path);
	return add_cache_entry(ce, options);
}
Example #9
0
File: proxy.c Project: TC1211/proxy
/******************************************************

int client: called if entry is not in cache

******************************************************/
int client(char *addr, char *message, int sock_proxy, char *URL) {
	struct sockaddr_in server_addr;
	char response[MAX_MSG_LENGTH];
	memset(response, 0, MAX_MSG_LENGTH);

	int sock;
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("Create socket error:");
		return 1;
	}

	printf("Socket created (client)\n");
	server_addr.sin_addr.s_addr = inet_addr(addr);
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(80); //convention for Internet
	
	if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
		perror("Connect error");
		return 1;
	}

	printf("Connected to server %s\n", addr);
	//printf("////////////////////////\n%s**********************\n", message);
	

	if (send(sock, message, MAX_MSG_LENGTH, 0) < 0) {
		perror("Send error");
		return 1;
	}

	int readlen;
	char *packet = (char *)malloc(MAX_MSG_LENGTH*1024);
	memset(packet, 0, MAX_MSG_LENGTH*1024);
	while ((readlen = read(sock, response, sizeof(response)))!= 0){
		//printf("%s", response);
		strcat(packet, response);
        	write(sock_proxy, response, readlen);
	}
	
	//printf("%s\n", packet);
	if(strstr(packet, "200 OK") != NULL){ //must be 200 response
		while((cache_size+sizeof(packet)) > max_cache_size){
			cache_t *temp = cache;
			cache = cache->next;
			cache_size -= temp->length;
			//free(temp);
		}
		printf("\n\n\nmax: %i current: %i\n\n\n", max_cache_size, cache_size);
		add_cache_entry(&cache, packet, URL);
		cache_size += (int)strlen(packet);
	}
	//free(packet);
	return 0;

}
Example #10
0
static void update_index_from_diff(struct diff_queue_struct *q,
		struct diff_options *opt, void *data)
{
	int i;

	for (i = 0; i < q->nr; i++) {
		struct diff_filespec *one = q->queue[i]->one;
		if (one->mode && !is_null_sha1(one->sha1)) {
			struct cache_entry *ce;
			ce = make_cache_entry(one->mode, one->sha1, one->path,
				0, 0);
			if (!ce)
				die(_("make_cache_entry failed for path '%s'"),
				    one->path);
			add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
				ADD_CACHE_OK_TO_REPLACE);
		} else
			remove_file_from_cache(one->path);
	}
}
Example #11
0
static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
{
	int len;
	unsigned int size;
	struct cache_entry *ce;

	if (S_ISDIR(mode))
		return READ_TREE_RECURSIVE;

	len = strlen(pathname);
	size = cache_entry_size(baselen + len);
	ce = xcalloc(1, size);

	ce->ce_mode = create_ce_mode(mode);
	ce->ce_flags = create_ce_flags(baselen + len, stage);
	memcpy(ce->name, base, baselen);
	memcpy(ce->name + baselen, pathname, len+1);
	hashcpy(ce->sha1, sha1);
	return add_cache_entry(ce, opt);
}
Example #12
0
static void
cogl_gst_yv12_glsl_setup_pipeline (CoglGstVideoSink *sink,
                                   CoglPipeline *pipeline)
{
  CoglGstVideoSinkPrivate *priv = sink->priv;
  static SnippetCache snippet_cache;
  SnippetCacheEntry *entry;

  entry = get_cache_entry (sink, &snippet_cache);

  if (entry == NULL)
    {
      char *source;

      source =
        g_strdup_printf ("vec4\n"
                         "cogl_gst_sample_video%i (vec2 UV)\n"
                         "{\n"
                         "  float y = 1.1640625 * "
                         "(texture2D (cogl_sampler%i, UV).a - 0.0625);\n"
                         "  float u = texture2D (cogl_sampler%i, UV).a - 0.5;\n"
                         "  float v = texture2D (cogl_sampler%i, UV).a - 0.5;\n"
                         "  vec4 color;\n"
                         "  color.r = y + 1.59765625 * v;\n"
                         "  color.g = y - 0.390625 * u - 0.8125 * v;\n"
                         "  color.b = y + 2.015625 * u;\n"
                         "  color.a = 1.0;\n"
                         "  return color;\n"
                         "}\n",
                         priv->custom_start,
                         priv->custom_start,
                         priv->custom_start + 1,
                         priv->custom_start + 2);

      entry = add_cache_entry (sink, &snippet_cache, source);
      g_free (source);
    }

  setup_pipeline_from_cache_entry (sink, pipeline, entry, 3);
}
Example #13
0
static int add_file_to_cache(char *path)
{
	int size, namelen;
	struct cache_entry *ce;
	struct stat st;
	int fd;

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		if (errno == ENOENT)
			return remove_file_from_cache(path);
		return -1;
	}
	if (fstat(fd, &st) < 0) {
		close(fd);
		return -1;
	}
	namelen = strlen(path);
	size = cache_entry_size(namelen);
	ce = malloc(size);
	memset(ce, 0, size);
	memcpy(ce->name, path, namelen);
	ce->ctime.sec = st.st_ctime;
	ce->ctime.nsec = st.st_ctim.tv_nsec;
	ce->mtime.sec = st.st_mtime;
	ce->mtime.nsec = st.st_mtim.tv_nsec;
	ce->st_dev = st.st_dev;
	ce->st_ino = st.st_ino;
	ce->st_mode = st.st_mode;
	ce->st_uid = st.st_uid;
	ce->st_gid = st.st_gid;
	ce->st_size = st.st_size;
	ce->namelen = namelen;

	if (index_fd(path, namelen, ce, fd, &st) < 0)
		return -1;

	return add_cache_entry(ce);
}
Example #14
0
static int update_some(const struct object_id *oid, struct strbuf *base,
		const char *pathname, unsigned mode, int stage, void *context)
{
	int len;
	struct cache_entry *ce;
	int pos;

	if (S_ISDIR(mode))
		return READ_TREE_RECURSIVE;

	len = base->len + strlen(pathname);
	ce = make_empty_cache_entry(&the_index, len);
	oidcpy(&ce->oid, oid);
	memcpy(ce->name, base->buf, base->len);
	memcpy(ce->name + base->len, pathname, len - base->len);
	ce->ce_flags = create_ce_flags(0) | CE_UPDATE;
	ce->ce_namelen = len;
	ce->ce_mode = create_ce_mode(mode);

	/*
	 * If the entry is the same as the current index, we can leave the old
	 * entry in place. Whether it is UPTODATE or not, checkout_entry will
	 * do the right thing.
	 */
	pos = cache_name_pos(ce->name, ce->ce_namelen);
	if (pos >= 0) {
		struct cache_entry *old = active_cache[pos];
		if (ce->ce_mode == old->ce_mode &&
		    oideq(&ce->oid, &old->oid)) {
			old->ce_flags |= CE_UPDATE;
			discard_cache_entry(ce);
			return 0;
		}
	}

	add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
	return 0;
}
Example #15
0
/*******************************************************************
*   Adds information from received ARP Reply to ARP Cache and returns newly added ARP Cache entry.
*******************************************************************/
struct arp_cache_entry* got_Reply(struct packet_state * ps, struct sr_arphdr * arp)
{
	add_cache_entry(ps, arp->ar_sip, arp->ar_sha); /*Add IP and MAC address from reply to cache */
	return search_cache(ps, arp->ar_sip); /*Return the newly added entry. */	
}
Example #16
0
/* Implements message handlers for the front-end server.
 * 	Responds to messages:
 * 			echo
 * 			heartbeat
 * 			client
 * 			query
 * 			invalidate */
int read_msg(char *src, char *Instr, char *Arg1, char *Arg2)
{
	char instr[CONTENT_MAX];
	char arg1[CONTENT_MAX];
	char arg2[CONTENT_MAX];

	strcpy(instr,Instr);
	strcpy(arg1,Arg1);
	strcpy(arg2,Arg2);

	/* If we've received a heartbeat from our neighbor, reset the beat counter */
	if(heartbeat(instr))
	{
		pthread_mutex_lock(&heart);
		beat_count = 0;
		pthread_mutex_unlock(&heart);
		char h_msg[CONTENT_MAX];
		sprintf(h_msg,"%s,%d,NULL",hea,server_id);
		message_host(neighbor_addr,h_msg);
		return 0;
	}

	/* Register all clients with both front-end servers */
	if(client(instr))
	{
		// update client cache
		printf("client update: %s,%s,%s\n",instr,arg1,arg2);
		struct cache_entry *client;
		unsigned int c_id;

		inet_pton(AF_INET,arg1,&c_id);
		if( (client = found(c_id,clients))==NULL)
		{
			client = new_cache_entry(c_id,arg1);
			client->server_id = atoi(arg2);
			add_cache_entry(SORTED,client,clients);	
		}
		print_cache(clients);

	}

	/* When we receive a query from a client, first check to see if we
 * have the result cached locally. If not, store the request and
 * query the database. The request will be serviced once the database
 * has responded */
	if(query(instr))
	{
		
		printf("Servicing client [%s] request for [%s:%s]\n",
			src,arg1,arg2);
		// first check and see if we have the query in our local cache
		struct cache_entry *result;
		int q_id = create_cache_id(arg1,arg2,NULL);
		//printf("new q_id: %d\n",q_id);
		result = found(q_id,cache);

		// if we don't have it, request entry from database
		if(result == NULL)
		{
			printf("Entry not found. Checking Database [%s]\n",database_addr);
			char request[CONTENT_MAX];
			sprintf(request,"%s,%s,%s",que,arg1,arg2);
			message_host(database_addr,request);
		}
		else
		{
			// we can respond right away!
			printf("FOUND! Returning result to client...\n");
			char msg[CONTENT_MAX];
			sprintf(msg,"%s,%s,%s",arg1,arg2,result->value);
			message_host(src,msg);
			return 0;
		}
		// add client to service queue - will be handled when we get db response
		//printf("add client to service queue.\n");
		add_cache_entry(SORTED,new_cache_entry(q_id,src),pending_queries);	
		return 0;
	}

	/* If the database is pushing updates, the front-end server will replace
 * outdated entries when told to */
	if(invalidate(instr))
	{
		printf("Replacing old entry for [%s:%s]\n",arg1,arg2);
		int q_id = create_cache_id(instr,arg1,arg2);
		invalidate_cache(q_id,cache);
		//printf("Invalidate entry [%d] => \n",q_id);
		//print_cache(cache);
		char message[CONTENT_MAX];
		sprintf(message,"%s,%s,%s",que,arg1,arg2);
		message_host(src,message);
		return 0;
	}
	
	/* Used to register new clients when they boot up */
	if(echo(instr))
	{
		// update client cache
		printf("register new client: %s,%s\n",instr,arg1);
		struct cache_entry *client;
		unsigned int c_id;

		inet_pton(AF_INET,arg1,&c_id);
		if( (client = found(c_id,clients))==NULL)
		{
			client = new_cache_entry(c_id,arg1);
			client->server_id = server_id;
			add_cache_entry(SORTED,client,clients);	
		}
		print_cache(clients);

		// notify other Frontend server
		printf("Notifying neighbor: %s\n",neighbor_addr);
		char c_msg[CONTENT_MAX];
		sprintf(c_msg,"%s,%s,%d",cli,src,server_id);
		message_host(neighbor_addr,c_msg);

		return 0;
	}
	
	// query response
	int q_id = create_cache_id(instr,arg1,arg2);
	struct cache_entry *f;
	if( (f=found(q_id,cache))==NULL)
		add_cache_entry(SORTED,new_cache_entry(q_id,arg2),cache);	
	else if( (f!=NULL) && strcmp(f->value,arg2) != 0)
	{
		// if we have an update, replace the old entry
		printf("Replacing old entry for [%s:%s]\n",arg1,arg2);
		delete_entry(f,cache);
		add_cache_entry(SORTED,new_cache_entry(q_id,arg2),cache);	
	}

	//print_cache(cache);
	// if we have a client waiting for this query, respond
	struct cache_entry *pending_client;
	//print_cache(pending_queries);
	while( (pending_client = found(q_id,pending_queries)) != NULL)
	{
		printf("Database returned entry [%s:%s]. Responding to client [%s]\n",
			arg1,arg2,pending_client->value);
		//printf("next client->%s\n",pending_client->value);
		char msg[CONTENT_MAX];
		sprintf(msg,"%s,%s,%s",instr,arg1,arg2);
		message_host(pending_client->value,msg);
		delete_entry(pending_client,pending_queries);	
	}

	return 0;
}
Example #17
0
File: blame.c Project: basilgor/git
/*
 * Prepare a dummy commit that represents the work tree (or staged) item.
 * Note that annotating work tree item never works in the reverse.
 */
static struct commit *fake_working_tree_commit(struct diff_options *opt,
					       const char *path,
					       const char *contents_from)
{
	struct commit *commit;
	struct blame_origin *origin;
	struct commit_list **parent_tail, *parent;
	struct object_id head_oid;
	struct strbuf buf = STRBUF_INIT;
	const char *ident;
	time_t now;
	int size, len;
	struct cache_entry *ce;
	unsigned mode;
	struct strbuf msg = STRBUF_INIT;

	read_cache();
	time(&now);
	commit = alloc_commit_node();
	commit->object.parsed = 1;
	commit->date = now;
	parent_tail = &commit->parents;

	if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL))
		die("no such ref: HEAD");

	parent_tail = append_parent(parent_tail, &head_oid);
	append_merge_parents(parent_tail);
	verify_working_tree_path(commit, path);

	origin = make_origin(commit, path);

	ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
	strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
	for (parent = commit->parents; parent; parent = parent->next)
		strbuf_addf(&msg, "parent %s\n",
			    oid_to_hex(&parent->item->object.oid));
	strbuf_addf(&msg,
		    "author %s\n"
		    "committer %s\n\n"
		    "Version of %s from %s\n",
		    ident, ident, path,
		    (!contents_from ? path :
		     (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
	set_commit_buffer_from_strbuf(commit, &msg);

	if (!contents_from || strcmp("-", contents_from)) {
		struct stat st;
		const char *read_from;
		char *buf_ptr;
		unsigned long buf_len;

		if (contents_from) {
			if (stat(contents_from, &st) < 0)
				die_errno("Cannot stat '%s'", contents_from);
			read_from = contents_from;
		}
		else {
			if (lstat(path, &st) < 0)
				die_errno("Cannot lstat '%s'", path);
			read_from = path;
		}
		mode = canon_mode(st.st_mode);

		switch (st.st_mode & S_IFMT) {
		case S_IFREG:
			if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
			    textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
				strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
			else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
				die_errno("cannot open or read '%s'", read_from);
			break;
		case S_IFLNK:
			if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
				die_errno("cannot readlink '%s'", read_from);
			break;
		default:
			die("unsupported file type %s", read_from);
		}
	}
	else {
		/* Reading from stdin */
		mode = 0;
		if (strbuf_read(&buf, 0, 0) < 0)
			die_errno("failed to read from stdin");
	}
	convert_to_git(path, buf.buf, buf.len, &buf, 0);
	origin->file.ptr = buf.buf;
	origin->file.size = buf.len;
	pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash);

	/*
	 * Read the current index, replace the path entry with
	 * origin->blob_sha1 without mucking with its mode or type
	 * bits; we are not going to write this index out -- we just
	 * want to run "diff-index --cached".
	 */
	discard_cache();
	read_cache();

	len = strlen(path);
	if (!mode) {
		int pos = cache_name_pos(path, len);
		if (0 <= pos)
			mode = active_cache[pos]->ce_mode;
		else
			/* Let's not bother reading from HEAD tree */
			mode = S_IFREG | 0644;
	}
	size = cache_entry_size(len);
	ce = xcalloc(1, size);
	oidcpy(&ce->oid, &origin->blob_oid);
	memcpy(ce->name, path, len);
	ce->ce_flags = create_ce_flags(0);
	ce->ce_namelen = len;
	ce->ce_mode = create_ce_mode(mode);
	add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);

	cache_tree_invalidate_path(&the_index, path);

	return commit;
}
Example #18
0
void add_to_cache(dlinklist* arp_cache, const uint8_t *const packet)
{
	struct sr_arphdr *arp = (struct sr_arphdr *) (packet + sizeof(struct sr_ethernet_hdr));
	add_cache_entry(arp_cache, arp->ar_sip, arp->ar_sha);
}
int mydisk_read(int start_address, int nbytes, void *buffer) {
    //TODO:
    //startBlockIdx and endBlockIdx indicate start and end block
    //of this writing operation
    //pls carefully calculate them
    int startBlockIdx = 0;
    int endBlockIdx = 0;
    int i = 0, j = 0;
    int iEndAddress = start_address + nbytes -1;
    int iCurrPosition = 0;
    int iNumberOfBlocks;
    char *cCache = (char *)malloc(BLOCK_SIZE);
    int bSeek = 0;

    startBlockIdx = start_address / BLOCK_SIZE;
    endBlockIdx = (start_address + nbytes - 1) / BLOCK_SIZE;

    iNumberOfBlocks = endBlockIdx - startBlockIdx + 1;

    // Validating that the parameters are good for the operation.
    if (start_address < 0) {
        return (1);
    } else if (nbytes < 0) {
        return (1);
    } else if ((start_address + nbytes) >= (diskEntity.max_blocks*BLOCK_SIZE) ) {
        return (1);
    }

    size_t bufferOffset = 0;

    for (i = startBlockIdx; i < endBlockIdx + 1; i++) {
        int blockInCacheFlag = 0; //0 - no, 1 - yes

        if (1 == CACHE_SWITCH) blockInCacheFlag = query_cache(i);

        // The time required to move between sector for the first block.
        if ((diskEntity.disk_type == 0) && !blockInCacheFlag && !bSeek) {
            Disk_Latency += HDD_SEEK;
            bSeek = 1;
        }
        
        if (0 == blockInCacheFlag) {
            //if cache is missed or disabled
            //TODO:read data from disk

            // Calculation for the disk latency for each block.
            if (diskEntity.disk_type == 0) {
                Disk_Latency += HDD_READ_LATENCY;
            } else if (diskEntity.disk_type == 1) {
                Disk_Latency += SSD_READ_LATENCY;
            }

            for (j = 0 ; j < BLOCK_SIZE; j++) {
                iCurrPosition = i*BLOCK_SIZE+j;
                if ((iCurrPosition >= start_address) && (iCurrPosition <= iEndAddress)) {
                    fseek(diskEntity.pBlockStore, iCurrPosition, SEEK_SET);
                    fread(buffer+bufferOffset, 1, 1, diskEntity.pBlockStore);
                    // Add the content in "cCache" to have a full block of information.
                    bufferOffset++;
                }
            }

            if (1 == CACHE_SWITCH) {
                // Add the latency for the caching.
                if (diskEntity.disk_type == 0) {
                    Disk_Latency += CACHE_WRITE_LATENCY;
                } else if (diskEntity.disk_type == 1) {
                    Disk_Latency += CACHE_WRITE_LATENCY;
                }

                // Reading the data on the disk to have the full block in the cache.
                for (j = 0 ; j < BLOCK_SIZE; j++) {
                    iCurrPosition = i*BLOCK_SIZE+j;
                    if ((iCurrPosition >= start_address) && (iCurrPosition <= iEndAddress)) {
                        // Add the content in "cCache" to have a full block of information.
                        fseek (diskEntity.pBlockStore, iCurrPosition, SEEK_SET);
                        fread(cCache+j, 1, 1, diskEntity.pBlockStore);
                    } else {
                        // Will read and store the part of the block which is not in the passed buffer
                        // to have a complete block in the cache.
                        fseek (diskEntity.pBlockStore, iCurrPosition, SEEK_SET);
                        fread(cCache+j, 1, 1, diskEntity.pBlockStore);
                    }
                }

                //TODO: if cache is enabled, cache this block
                add_cache_entry(i, cCache);
                cCache = (char *)malloc(BLOCK_SIZE);
            }
        } else {
            Disk_Latency += CACHE_READ_LATENCY;
            //if this block is cached, 
            //TODO:read this block from cache through read_cache(int)
            char *cCachedBlock = read_cache(i);
            for (j = 0 ; j < BLOCK_SIZE ; j++) {
                iCurrPosition = i*BLOCK_SIZE+j;
                if ((iCurrPosition >= start_address) && (iCurrPosition <= iEndAddress)) {
                    memset(buffer+bufferOffset, cCachedBlock[j], 1);
                    bufferOffset++;
                }
            }
        }
        //TODO:copy the data into the buffer[
        //take care about the offsets
    }

    return 0;
}
int mydisk_write(int start_address, int nbytes, void *buffer) {
    //TODO:
    //startBlockIdx and endBlockIdx indicate start and end block
    //of this writing operation
    //pls carefully calculate them
    int startBlockIdx = 0;
    int endBlockIdx = 0;
    int i = 0, iBufferTarget = 0, j = 0;
    int iEndAddress = start_address + nbytes -1;
    int iCurrPosition = 0;
    int iNumberOfBlocks;
    char *cCache = (char *)malloc(BLOCK_SIZE);
    int bSeek = 0;
    
    //printf("*** mydisk_write(%i, %i, %s), buffersize(%i)\n",start_address, nbytes, buffer, strlen(buffer));

    startBlockIdx = start_address / BLOCK_SIZE;
    endBlockIdx = (start_address + nbytes - 1) / BLOCK_SIZE;

    iNumberOfBlocks = endBlockIdx - startBlockIdx + 1;
    
    // Validating that the parameters are good for the operation.
    if (start_address < 0) {
        return (1);
    } else if (nbytes < 0) {
        return (1);
    } else if ((start_address + nbytes) >= (diskEntity.max_blocks*BLOCK_SIZE) ) {
        return (1);
    }

    for (i = startBlockIdx; i < endBlockIdx + 1; i++) {
        //TODO:write block by block
        //be careful for some extreme case
        //e.g. start_address and start_address are not start or end of a block
        
        // The code under is for when the cache is enabled.
        int blockInCacheFlag = 0; //0 - no, 1 - yes

        //check if the current block is cached
        if (1 == CACHE_SWITCH) blockInCacheFlag = query_cache(i);

        // The time required to move between sector for the first block.
        if ((diskEntity.disk_type == 0) && !blockInCacheFlag && !bSeek) {
            //printf("----- (Seek sector): 1S\n");
            Disk_Latency += HDD_SEEK;
            bSeek = 1;
        }

        if (0 == blockInCacheFlag) {
            
            // Calculation for the disk latency for each block.
            if (diskEntity.disk_type == 0) {
                Disk_Latency += HDD_WRITE_LATENCY;
            } else if (diskEntity.disk_type == 1) {
                Disk_Latency += SSD_WRITE_LATENCY;
            }
            
            //TODO:write data into the disk
            for (j = 0 ; j < BLOCK_SIZE; j++) {
                iCurrPosition = i*BLOCK_SIZE+j;
                if ((iCurrPosition >= start_address) && (iCurrPosition <= iEndAddress)) {
                    fseek (diskEntity.pBlockStore, iCurrPosition, SEEK_SET);
                    fwrite(buffer+iBufferTarget, 1, 1, diskEntity.pBlockStore);
                    iBufferTarget++;
                }
            }

            //TODO:if cache is enabled, cache current block
            if (1 == CACHE_SWITCH) {
                // Add the latency for the caching.
                if (diskEntity.disk_type == 0) {
                    Disk_Latency += HDD_SEEK + HDD_READ_LATENCY + CACHE_WRITE_LATENCY;
                } else if (diskEntity.disk_type == 1) {
                    Disk_Latency += SSD_READ_LATENCY + CACHE_WRITE_LATENCY;
                }

                // Reading the data on the disk to have the full block in the cache.
                for (j = 0 ; j < BLOCK_SIZE; j++) {
                    iCurrPosition = i*BLOCK_SIZE+j;
                    if ((iCurrPosition >= start_address) && (iCurrPosition <= iEndAddress)) {
                        // Add the content in "cCache" to have a full block of information.
                        fseek (diskEntity.pBlockStore, iCurrPosition, SEEK_SET);
                        fread(cCache+j, 1, 1, diskEntity.pBlockStore);
                    } else {
                        // Will read and store the part of the block which is not in the passed buffer
                        // to have a complete block in the cache.
                        fseek (diskEntity.pBlockStore, iCurrPosition, SEEK_SET);
                        fread(cCache+j, 1, 1, diskEntity.pBlockStore);
                    }
                }

                add_cache_entry(i, cCache);
                // Reset the memory.
                cCache = (char *)malloc(BLOCK_SIZE);
            }
        }//if yes, write the new content into the cache,
            //and make the dirty flag of cached blk to be 1
        else {
            //TODO: if current block is cached,
            //then modify the data in cache and mark it as dirty
            size_t blkOffset = 0;
            size_t contentSize = 0;
            char *block_data = (char *) malloc(BLOCK_SIZE);

            // Setting the offset from the start of the block.
            if (start_address > (i * BLOCK_SIZE)) {
                blkOffset = start_address - (i * BLOCK_SIZE);
            } else {
                blkOffset = 0;
            }
            // Setting the number of bits to update in the cache.
            if ((start_address + nbytes) < ((i + 1) * BLOCK_SIZE)) {
                contentSize = BLOCK_SIZE - blkOffset - (((i + 1) * BLOCK_SIZE) - (start_address + nbytes));
            } else {
                contentSize = BLOCK_SIZE - blkOffset;
            }

            // Determining "data_block" to pass it in the function.
            for (j = 0; j < BLOCK_SIZE; j++) {
                iCurrPosition = i * BLOCK_SIZE + j;
                if ((iCurrPosition >= start_address) && (iCurrPosition <= iEndAddress)) {
                    fwrite(buffer + iBufferTarget, 1, 1, diskEntity.pBlockStore);
                    block_data[j] = *((char*) buffer + iBufferTarget);
                    iBufferTarget++;
                }
            }

            write_cache(i, block_data, blkOffset, contentSize);

            Disk_Latency += CACHE_WRITE_LATENCY;
        }
    }

    return 0;
}
Example #21
0
int read_msg(char *src, char *Instr, char *Arg1, char *Arg2)
{
	//printf("Received: FROM => %s\tCMD => %s,%s,%s\n",src,Instr,Arg1,Arg2);
	char instr[CONTENT_MAX];
	char arg1[CONTENT_MAX];
	char arg2[CONTENT_MAX];

	strcpy(instr,Instr);
	strcpy(arg1,Arg1);
	strcpy(arg2,Arg2);


	if(echo(instr))
	{
		message_host(src,"echo,NULL,NULL");
	}

	/* Database will immediately notify its registered clients if local data
 * has been updated, only when the database is configured to PUSH updates 
 * with the command line flag -p */
	else if(incr(instr))
	{
		if(PUSH_UPDATES)
		{
			// send invalidation msg to all known clients
			struct cache_entry *client;
			int index=0;
			/* Iterates over all saved clients */
			for(index=0;index<clients_cache->length;index++)
			{
				client = get_index(index,clients_cache);
				printf("Pushing updates to client [%s]\n",client->value);
				char msg[CONTENT_MAX];
				sprintf(msg,"%s,%s,%s",inv,arg1,arg2);
				message_host(client->value,msg);
			}
		}
	
		/* Routine for updating local data */
		if(rome(arg1))
		{
			if(gold(arg2))
			{
				r.gold++;
			}
			if(silver(arg2))
			{
				r.silver++;
			}	
			if(bronze(arg2))
			{
				r.bronze++;
			}
			if(tennis(arg2))
			{
				score_tennis.rome_score++;	
			}
			if(soccer(arg2))
			{
				score_soccer.rome_score++;
			}
			if(frisbee(arg2))
			{
				score_frisbee.rome_score++;
			}
		}
		if(gaul(arg1))
		{
			if(gold(arg2))
			{		
				g.gold++;
			}
			if(silver(arg2))
			{
				g.silver++;
			}
			if(bronze(arg2))
			{
				g.bronze++;
			}
			if(tennis(arg2))
			{
				score_tennis.gaul_score++;	
			}
			if(soccer(arg2))
			{
				score_soccer.gaul_score++;
			}
			if(frisbee(arg2))
			{
				score_frisbee.gaul_score++;
			}
		}
		/* Display updates on the terminal screen */
		printf("Update received -------------------------\n"
			"Gaul >>	Gold %d, Silver %d, Bronze %d\n"
			"Rome >>  Gold %d, Silver %d, Bronze %d\n"
			"--------------------------------------------\n"
			"Tennis:  Gaul %d	|	Rome %d\n"
			"Frisbee:	Gaul %d	|	Rome	%d\n"
			"Soccer:  Gaul %d	|	Rome	%d\n",
			r.gold, r.silver, r.bronze,
			g.gold, g.silver, g.bronze,
			score_tennis.gaul_score, score_tennis.rome_score,
			score_frisbee.gaul_score, score_frisbee.rome_score,
			score_soccer.gaul_score, score_soccer.rome_score);
	}

	/* Service client query */
	else if(query(instr))
	{
		// when a front-end server queries for the first time, add it to the
		// cache of known hosts
		printf("Responding to query [%s:%s] from client [%s]\n",
			arg1,arg2,src);
		while(clients_cache==NULL);	// wait for client cache to come online
		struct cache_entry *connected_client;
		unsigned int client_id;

		inet_pton(AF_INET,src,&client_id);
		connected_client = new_cache_entry(client_id,src);
		if(!found(client_id,clients_cache))	
			add_cache_entry(SORTED,connected_client,clients_cache);

		/* Build query response */
		
		char response[100];
		if(rome(arg1))
		{
			if(gold(arg2))
			{
				sprintf(response,"rome,gold,%d",r.gold);
			}
			if(silver(arg2))
			{
				sprintf(response,"rome,silver,%d",r.silver);
			}	
			if(bronze(arg2))
			{
				sprintf(response,"rome,bronze,%d",r.bronze);
			}
			if(tennis(arg2))
			{
				sprintf(response,"rome,tennis,%d",score_tennis.rome_score);
			}
			if(soccer(arg2))
			{
				sprintf(response,"rome,soccer,%d",score_soccer.rome_score);
			}
			if(frisbee(arg2))
			{
				sprintf(response,"rome,frisbee,%d",score_frisbee.rome_score);
			}
		}
		if(gaul(arg1))
		{
			if(gold(arg2))
			{		
				sprintf(response,"gaul,gold,%d",r.gold);
			}
			if(silver(arg2))
			{
				sprintf(response,"gaul,silver,%d",r.silver);
			}
			if(bronze(arg2))
			{
				sprintf(response,"gaul,bronze,%d",r.bronze);
			}
			if(tennis(arg2))
			{
				sprintf(response,"gaul,tennis,%d",score_tennis.gaul_score);
			}
			if(soccer(arg2))
			{
				sprintf(response,"gaul,soccer,%d",score_soccer.gaul_score);
			}
			if(frisbee(arg2))
			{
				sprintf(response,"gaul,frisbee,%d",score_soccer.gaul_score);
			}
		}	
		printf("Responding to %s\n",src);
		message_host(src,response);
	}
	return 0;
}