Example #1
0
// allocates an extra byte and null terminates
unsigned char *dfs_readfile_buf(char *name, unsigned long *len, char *inbuf, unsigned long left)
{
    unsigned char	*buf;
    int				fd;
    struct stat		statbuf;

    if (!(fd = open(name, O_RDONLY)))
	return NULL;
    if (fstat(fd, &statbuf)) {
	dfs_out("READFILE FAIL: '%s' no exist\n", name);
	return NULL;
    }
    if (inbuf) {
	if ((statbuf.st_size + 1) <= left) 
	    buf = inbuf;
	else {
	    dfs_out("READFILE FAIL: not enough room (%d,%d)\n", statbuf.st_size, left) ;
	    return NULL;
	}
    } else {
	if (!(buf = malloc(statbuf.st_size + 1))) {
	    dfs_out("READFILE FAIL: malloc  %d\n", statbuf.st_size) ;
	    return NULL;
	}
    }
    read(fd, buf, statbuf.st_size);
    buf[statbuf.st_size] = 0;
    close(fd);
    dfs_out("Read %ld bytes from '%s'\n", statbuf.st_size, name);
    if (len) *len = statbuf.st_size;
    return buf;
}
Example #2
0
File: dfs.c Project: bwhite/dfs
static void freeNode(const char *path, DfsFile *f) {
    DfsFile 	*p = f->parent;

    dfs_out("mknode '%s'\n", path);
    tdelete(path, &pathRoot, file_compare);

    assert(!f->num_children);
    free(f->data);
    free(f->name);
    if (p) {
	int	i;

	for (i = 0; i < p->num_children; i++) {
	    if (p->child[i] == f) {
		assert(p->num_children);
		dfs_out("memcpy %x, %x, %d (i %d #%d)\n", (void *)&p->child[i], (void *)&p->child[i+1], 
		   sizeof(DfsFile *) * (p->num_children - (i+1)), i, p->num_children);
		memmove((void *)&p->child[i], (void *)&p->child[i+1], sizeof(DfsFile *) * (p->num_children - (i+1)));
		break;
	    }
	}
	assert(i < p->num_children);
	p->num_children--;
    }
    free(f);
}
Example #3
0
Message *dfs_msg_recv(void *socket, char **buf) {
    unsigned long	len;

    *buf = dfs_prim_msg_recv(socket, &len);
    dfs_end_time(message_comm_type);
    Message *reply = message__unpack(NULL, len, *buf);

    if (reply->has_encrypted_payload) {
	assert(registered_aes_key && (socket == registered_aes_sock));
	dfs_out("\t\tdecrypt %x/%x (%d)\n", *(unsigned long *)reply->encrypted_payload.data,
		*(unsigned long *)registered_aes_key, reply->encrypted_payload.len);
	char *dec = dfs_aes_decrypt(registered_aes_key, NULL, 
				    reply->encrypted_payload.data, 
				    reply->encrypted_payload.len);
	Message *nreply = message__unpack(NULL, reply->encrypted_payload.len, dec);
	if (!nreply) {
	    dfs_out("\nERROR: decryption of incoming failed\n");
	    return NULL;
	}
	message__free_unpacked(reply, NULL);
	free(*buf);

	reply = nreply;
	*buf = dec;
    }

    dfs_out("Received %s%s from %s, seq %d, %d bytes, res %d ('%s')\n", 
	    reply->has_encrypted_payload ? "enc " : "", dfs_msgname(reply->type), 
	    reply->name ? reply->name : "NO NAME!", reply->seq, len, reply->res, 
	    reply->res ? strerror(-reply->res):"");
    return reply;
}
Example #4
0
// Packs, sends, and frees initialized message. 
void dfs_msg_send(int type, void *sock, Message *msg) 
{
    static int		seq = 0;
    char 			*enc = "";

    if (!seq) {
	srandom(time(NULL));
	seq = (random() & 15) * 100 + 1;
    }

    assert(msg);
    msg->type = type;
    msg->seq = seq++;
    msg->name = process_name;
    assert(msg->name);

    if (!pid) pid = getpid();
    msg->pid = pid;

    unsigned long	len = message__get_packed_size(msg);
    char		*buf = malloc(len);
    message__pack(msg, buf);

    dfs_incr_stats(type, len);
    dfs_start_time(message_comm_type = type);

    if (registered_aes_key && (sock == registered_aes_sock)) {
	// Encrypted! Only need to/pid.
	Message	emsg = MESSAGE__INIT;

	enc = "enc ";
	emsg.pid = msg->pid;

	char *edata = dfs_aes_encrypt(registered_aes_key, NULL, buf, len);
	emsg.encrypted_payload.data = edata;
	emsg.encrypted_payload.len = len;
	emsg.has_encrypted_payload = 1;

	dfs_out("ENCRYPT %x/%x (%d)\n", *(unsigned long *)edata, 
		*(unsigned long *)registered_aes_key, len);

	unsigned long	elen = message__get_packed_size(&emsg);
	char			*ebuf = malloc(elen);
	message__pack(&emsg, ebuf);

	dfs_prim_msg_send(sock, ebuf, elen);
	free(ebuf);
	free(edata);
    } else {
	dfs_prim_msg_send(sock, buf, len);
    }
    free(buf);

    dfs_out("Sent %s%s, seq %d, %d bytes, res %d ('%s')\n", 
	    enc, dfs_msgname(type), msg->seq, 
	    len, msg->res, msg->res ? strerror(-msg->res) : "");
    dfs_db_sync();
}
Example #5
0
File: dfs.c Project: bwhite/dfs
void destroy_node(void *node) {
    DfsFile *f = ((DfsFile *) node);
    if (!f)
	return;
    dfs_out("Destroy Data\n");
    free(f->data);
    dfs_out("Destroy Name\n");
    free(f->name);
    dfs_out("Destroy Recipe\n");
    free(f->recipe);
}
Example #6
0
File: dfs.c Project: bwhite/dfs
int _dfs_chmod(const char *path, mode_t mode)
{
    /* Assumes treeMut is locked */
    DfsFile	*f;
    dfs_out("CHMOD: '%s' %o\n", path, mode);

    if (!(f = findFile((char *)path)))
	return -ENOENT;
    
    f->stat.st_mode = (f->stat.st_mode &  ~(S_IRWXU | S_IRWXG | S_IRWXO)) | mode;
    dfs_out("\tend mode: %o\n", f->stat.st_mode);

    return 0;
}
Example #7
0
File: dfs.c Project: bwhite/dfs
static int _dfs_open(const char *path, struct fuse_file_info *fi)
{
    DfsFile	*f;
    dfs_out("\n\tFUSE OPEN '%s'\n\n", path);

    if (!(f = findFile((char *)path))) {
	return -ENOENT;
    }

    long	flags = fi ? fi->flags : 0;

    dfs_out("\tOPEN : '%s', flags %o, len %d, reclen %d, recipe %x, data %x\n", path, flags, f->len, f->recipelen, f->recipe, f->data);

    if (f->stat.st_mode & S_IFDIR) {
	return -EISDIR;
    }

    if (0 && !(fi->flags & f->stat.st_mode)) {
	dfs_out("OPEN permissions problem: %o, %o\n", fi->flags, f->stat.st_mode);
        return -EACCES;
    }

    // Why? don't remember....
    fi->fh = (uint64_t)f->stat.st_mode;

    if (f->len && !f->data) {
	assert(f->recipe);

	f->data = malloc(f->len);
	assert(f->data);

	char		*data = f->data, *dataEnd = f->data + f->len;
	char		*sig = f->recipe, *sigEnd = f->recipe + f->recipelen;

	while ((data < dataEnd) && (sig < sigEnd)) {
	    Extent	*ex = get_extent(sig);
	    if (!ex) dfs_die("No get signature '%s'\n", sig);
	
	    memcpy(data, ex->data, ex->sz);
	    data += ex->sz;

	    sig += strlen(sig) + 1;
	    free(ex);
	}
	assert((data == dataEnd) && (sig == sigEnd));
    }
    return 0;
}
Example #8
0
void dfs_db_commit()
{
    if (db && dfs_use_transactions) {
	dfs_out("END TRANSACTION\n");
	dfs_db_do("END TRANSACTION");
    }
}
Example #9
0
void dfs_db_start()
{
    if (db && dfs_use_transactions) {
	dfs_out("BEGIN TRANSACTION\n");
	dfs_db_do("BEGIN TRANSACTION");
    }
}
Example #10
0
File: dfs.c Project: bwhite/dfs
int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
    pthread_mutex_lock(&treeMut);
    path = narrow_path(path);
    if (remove_create) {
	pthread_mutex_unlock(&treeMut);
	return -EACCES;
    }
    dfs_out("CREATE: '%s'\n", path);

    DfsFile	*f = findFile((char *)path);
    DfsFile	*dir;
    char	*dname, *fname;

    if (f) {
	pthread_mutex_unlock(&treeMut);
	return -EEXIST;
    }

    if (!(fname = strrchr(path, '/'))) {
	pthread_mutex_unlock(&treeMut);
	return -EINVAL;
    }

    dname = strdup(path);
    dname[fname - path] = 0;
    fname++;

    if (!(dir = findFile(dname))) {
	free(dname);
	pthread_mutex_unlock(&treeMut);
	return -EINVAL;
    }

    f = mkNode(path, fname, dir, DEF_FILE_MODE);

    dfs_out("CREATE OUT, now %d children in '%s'\n", dir->num_children, dname);

    free(dname);

    f->version++;

    logFileVersion(f);
    pthread_mutex_unlock(&treeMut);
    return 0;
}
Example #11
0
File: dfs.c Project: bwhite/dfs
int _dfs_mkdir(const char *path, mode_t mode)
{
    /* Assumes treeMut is locked */
    //S_IFDIR
    DfsFile	*dir;
    char	*dname = strdup(path), *fname;

    dfs_out("MKDIR: '%s', %o\n", path, mode);

    if (dname[strlen(dname) - 1] == '/') 
	dname[strlen(dname) - 1] = 0;

    if (!dname[0]) {
	free(dname);
	return -EEXIST;
    }

    if (dir = findFile((char *)dname)) {
	free(dname);
	return -EEXIST;
    }
	
    fname = strrchr(dname, '/');
    dfs_out("STRRCHR '%s', '%s'\n", dname, fname ? fname : "''");
    if (!fname) {
	free(dname);
	return -EINVAL;
    }

    *fname++ = 0;
    dfs_out("dname %x ('%s'), fname %x ('%s')\n", dname, dname, fname, fname);

    if (!(dir = findFile((char *)dname))) {
	free(dname);
	return -EINVAL;
    }
	
    dfs_out("MKDIR2: '%s', '%s'\n", dname, fname);

    mkNode(path, fname, dir, S_IFDIR | mode);

    free(dname);

    return 0;
}
Example #12
0
File: dfs.c Project: bwhite/dfs
static void *listener(void *arg) 
{
    int		fd = *(int *)arg;

    dfs_out("\n\tLISTEN PROC IN, sock %d!\n\n", fd);
    
    Msg *m;
    while (m = comm_read(1, fd)) {

	switch (m->type) {
	case DFS_MSG_PUSH_LOG:
	    /* Lock tree and replay log.  Assumes we get the whole log.  */
	    pthread_mutex_lock(&treeMut);
	    pthread_mutex_lock(&replyLogserverMut);
	    dfs_out("Push calling play log\n");
	    char *log;
	    size_t log_sz;
	    assert(tuple_unserialize_log(&log, &log_sz, m->data, m->len) == 0);
	    playLog(log, log_sz);
	    free(log);
	    pthread_mutex_unlock(&replyLogserverMut);
	    pthread_mutex_unlock(&treeMut);
	    break;

	case MSG_REPLY:
	    /* Put on reply queue.  Waits until main thread gets the item
	     and sets the replyQueue to NULL to prevent losing messages */
	    pthread_mutex_lock(&replyLogserverMut);
	    assert(replyQueue == NULL);
	    replyQueue = m;
	    pthread_cond_signal(&replyLogserverCond);
	    pthread_cond_wait(&replyLogserverCond, &replyLogserverMut);
	    pthread_mutex_unlock(&replyLogserverMut);
	    break;
	default:
	    dfs_die("BAD MSG TYPE %d\n", m->type);
	}
    }
    
    dfs_out("\n\tLISTEN PROC EXIT, sock %d!\n\n", fd);
    return NULL;
}
Example #13
0
unsigned char *dfs_append(char *name, unsigned char *buf, unsigned long len)
{
    int		fd;

    if (!(fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0644)))
	return NULL;
    write(fd, buf, len);
    close(fd);
    dfs_out("Appended %ld bytes to '%s'\n", len, name);
    return buf;
}
Example #14
0
unsigned char *dfs_writefile(char *name, unsigned char *buf, unsigned long len)
{
    int		fd;

    if (!(fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644)))
	return NULL;
    write(fd, buf, len);
    close(fd);
    dfs_out("Wrote %ld bytes to '%s'\n", len, name);
    return buf;
}
Example #15
0
File: dfs.c Project: bwhite/dfs
static int dfs_getattr(const char *path, struct stat *stbuf)
{
    path = narrow_path(path);
    DfsFile	*f = findFile((char *)path);
    
    dfs_out("GETATTR: '%s' (%ld)\n", path, f);
    if (!f) return -ENOENT;

    *stbuf = f->stat;
    return 0;
}
Example #16
0
File: dfs.c Project: bwhite/dfs
static int dfs_truncate(const char *path, off_t sz)
{
    DfsFile	*f;
    pthread_mutex_lock(&treeMut);
    dfs_out("\n\tFUSE TRUNCATE\n\n");
    path = narrow_path(path);
    if (remove_write) {
	pthread_mutex_unlock(&treeMut);
	return -EACCES;
    }
    if (!(f = findFile((char *)path))) {
	pthread_mutex_unlock(&treeMut);
	return -ENOENT;
    }

    dfs_out("TRUNCATE to %d, was %d\n", sz, f->len);

    if (sz < f->len) {
	f->len = f->stat.st_size = sz;
	f->dirty = 1;
    }
    pthread_mutex_unlock(&treeMut);
    return 0;
}
Example #17
0
File: dfs.c Project: bwhite/dfs
static int dfs_write(const char *path, const char *buf, size_t size, off_t offset,
                      struct fuse_file_info *fi)
{
    size_t len;
    pthread_mutex_lock(&treeMut);
    path = narrow_path(path);
    dfs_out("WRITE: '%s', sz %d, offset %d\n", path, size, offset);
    if (remove_write) {
	pthread_mutex_unlock(&treeMut);
	return -EACCES;
    }

    DfsFile	*f = findFile((char *)path);

    if (!f) {
	pthread_mutex_unlock(&treeMut);
	return -ENOENT;
    }

    if (f->stat.st_mode & S_IFDIR) {
	pthread_mutex_unlock(&treeMut);
	return -EISDIR;
    }
    // Handles issue where a collision can cause us to fail this check
    if(!(!f->recipelen || f->data)) {
	struct fuse_file_info fii;
	fii.flags = 0;
	_dfs_open(path, &fii);
    }
    //assert(!f->recipelen || f->data);

    if ((size + offset) > f->len) {
	f->data = (char *)realloc(f->data, size + offset);
	f->stat.st_size = f->len = size + offset;
    }
    memcpy(f->data + offset, buf, size);

    f->dirty = 1;

    f->stat.st_mtime = f->stat.st_atime = time(NULL);
    pthread_mutex_unlock(&treeMut);
    return size;
}
Example #18
0
File: dfs.c Project: bwhite/dfs
int _dfs_unlink(const char *path)
{
    /* Assumes treeMut is locked */
    path = narrow_path(path);
    DfsFile	*f;

    dfs_out("Unlink '%s'\n", path);
    if (!(f = findFile((char *)path)))
	return -ENOENT;

    if (f->stat.st_mode & S_IFDIR) 
	return -EISDIR;

    if (--f->stat.st_nlink)
	return 0;

    freeNode(path, f);

    return 0;
}
Example #19
0
File: dfs.c Project: bwhite/dfs
DfsFile *mkNode(const char *path, const char *name, DfsFile *parent, mode_t mode) {
    DfsFile *f = (DfsFile *)calloc(1, sizeof(DfsFile));

    dfs_out("mknode '%s'\n", path);
    
    f->stat.st_mtime = f->stat.st_atime = f->stat.st_ctime = time(NULL);
    f->stat.st_mode = mode;
    f->stat.st_nlink = 1;
    f->stat.st_uid = getuid();
    f->stat.st_gid = getgid();
    f->name = strdup(name);
    strncpy(f->path, path, sizeof(f->path)-1);
    f->parent = parent;

    if (parent) {
	parent->child = (DfsFile **)realloc(parent->child, (parent->num_children + 1) * sizeof(DfsFile *));
	parent->child[parent->num_children++] = f;
    }

    tsearch(f, &pathRoot, file_compare);
    return f;
}
Example #20
0
File: dfs.c Project: bwhite/dfs
static int dfs_read(const char *path, char *buf, size_t size, off_t offset,
                      struct fuse_file_info *fi)
{
    size_t len;
    pthread_mutex_lock(&treeMut);
    path = narrow_path(path);
    dfs_out("READ: '%s', sz %d, offset %d\n", path, size, offset);

    DfsFile	*f = findFile((char *)path);

    if (!f) {
	pthread_mutex_unlock(&treeMut);
	return -ENOENT;
    }

    if (size && !f->len)  {
	pthread_mutex_unlock(&treeMut);
	return 0;
    }

    if (f->stat.st_mode & S_IFDIR) {
	pthread_mutex_unlock(&treeMut);
	return -EISDIR;
    }

    len = f->len;
    if (offset < len) {
        if (offset + size > len)
            size = len - offset;
        memcpy(buf, f->data + offset, size);
    } else
        size = 0;

    f->stat.st_atime = time(NULL);
    pthread_mutex_unlock(&treeMut);
    return size;
}
Example #21
0
File: dfs.c Project: bwhite/dfs
// Puts sig into sigbuf.   '0' on success.
char *put_extent(char *buf, long sz)
{
    int			i;

    assert(buf && sz && (sz < (1024L * 1024L * 1024L)));

    char *s = cry_hash_bytes(buf, sz);

    if (poll_extent(s)) {
	printf("Server already has EXTENT\n");
	return s;
    }
    char *serialized;
    size_t serialized_sz;
    assert(tuple_serialize_sig_extent(s, &serialized, &serialized_sz, buf, sz) == 0);
    Msg	*reply = comm_send_and_reply(0, extentSock, DFS_MSG_PUT_EXTENT, serialized, serialized_sz, NULL);
    free(serialized);
    if (!reply) dfs_die("No get reply\n");

    dfs_out("extent '%s' CREATED\n", s);
    free(reply);

    return s;
}
Example #22
0
File: dfs.c Project: bwhite/dfs
static int dfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
                         off_t offset, struct fuse_file_info *fi)
{
    pthread_mutex_lock(&treeMut);
    path = narrow_path(path);
    DfsFile	*f = findFile((char *)path);
    int		i;

    dfs_out("READDIR: '%s'\n", path);

    if (!f) {
	pthread_mutex_unlock(&treeMut);
	return -ENOENT;
    }

    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);

    for (i = 0; i < f->num_children; i++) {
	filler(buf, f->child[i]->name, NULL, 0);
    }
    pthread_mutex_unlock(&treeMut);
    return 0;
}
Example #23
0
File: dfs.c Project: bwhite/dfs
// If file dirty, chunkify, sent extents to extentserver, and send
// recipe to recipes server. In all cases, toss local file from hash table.
static int dfs_flush(const char *path, struct fuse_file_info *fi)
{
    DfsFile	*f;
    pthread_mutex_lock(&treeMut);
    path = narrow_path(path);
    dfs_out("DFS_FLUSH '%s\n", path);

    if (!(f = findFile((char *)path))) {
	pthread_mutex_unlock(&treeMut);
	return -EINVAL;
    }

    assert(f);

    dfs_out("\tflush '%s': len %d, dirty %d, data %x\n", f->path, f->len, f->dirty, f->data);

    // If no data, nothing to flush...
    if (!f->data || !f->dirty) {
	free(f->data);
	f->data = NULL;
	pthread_mutex_unlock(&treeMut);
	return 0;
    }

    // Need to create extents. For now, just chop up into 4k blocks.
    char	*buf = f->data, *bufend = buf + f->len;
    char	*sig;
    Extent	*ex, *last;

    int		blocks = f->len / BLOCK_SIZE;
    if (f->len % BLOCK_SIZE) blocks++;

    f->recipelen = blocks * A_HASH_SIZE;
    free(f->recipe);
    f->recipe = malloc(f->recipelen);
    char	*curr = f->recipe;

    while (buf < bufend) {
	int		sz = MIN(bufend - buf, BLOCK_SIZE);
	char		*hash;

	hash = put_extent(buf, sz);
	assert(hash);
	dfs_out("PUT extent size %d, '%s'\n", sz, hash);

	strcpy(curr, hash);
	curr += A_HASH_SIZE;
	free(hash);

	buf += sz;
    }
    assert(curr == (f->recipe + f->recipelen));

    free(f->data);
    f->data = NULL;
    f->version++;
    f->dirty = 0;

    logFileVersion(f);
    pthread_mutex_unlock(&treeMut);
    return 0;
}
Example #24
0
File: dfs.c Project: bwhite/dfs
// called to reply records from logs returned from server
void playLog(char *buf, int len)
{
    /* Assumes treeMut and replyLogserverMut are locked */
    /* Find first ID of the new entry, go through whole log
       and truncate if that ID or greater is found, then append
       the new log.  It is assumed that the server has successfully
       managed any conflicts and any of that is taken care of. */
    // If we aren't forcing, check if this entry is in the log
    char *data = opLog.data;
    char *end = opLog.data + opLog.used;
    int first_version = ((LogHdr *)buf)->id;
    /* If we already have this version then quit.
       We only check the first record as if we asked
       for the log we have an empty log.  If we get
       pushed then we recieve one log entry and we just
       need to check if we have already committed it.
     */
    if (pending_change_id == first_version) {
      dfs_out("PlayLog: Server sent us what we are waiting to commit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
      return;
    }
    while (data != NULL && data < end) {
	int version = ((LogHdr *)data)->id;
	if (version == first_version) {
	    dfs_out("PlayLog: Already have element in log\n");
	    return;
	}
	data += ((LogHdr *)data)->len;
    }
    dfs_out("PlayLog: Adding new element\n");
    checkLogSpace(len);
    data = opLog.data;
    dfs_out("PlayLog: Adding new element[%p][%d][%d]\n", opLog.data, opLog.used, opLog.alloced);
    memcpy(data + opLog.used, buf, len);
    opLog.used += len;
    end = opLog.data + opLog.used;
    destroy_tree();
    root = mkNode("", "", NULL, DEF_DIR_MODE);
    int last_id = 1;
    while (data < end) {
	switch ( ((LogHdr *)data)->type ) {
	case LOG_FILE_VERSION:
	    {
		LogFileVersion	*fv = (LogFileVersion *)data;
		char		*recipes = (char *)(fv + 1);
		char		*path = recipes + fv->recipelen;
		DfsFile	*f;
		dfs_out("Push[%s] id[%d]\n", path, fv->hdr.id);
		f = findFile((char *)path);
		if (f == NULL) {
		    DfsFile *dir;
		    char *dname, *fname;
		    fname = strrchr(path, '/');
		    dname = strdup(path);
		    dname[fname - path] = 0;
		    fname++;
		    dir = findFile(dname);
		    f = mkNode(path, fname, dir, DEF_FILE_MODE);
		    free(dname);
		}
		// Update mtime
		f->stat.st_mtime = fv->mtime;
		// Update version
		f->version = fv->hdr.version;
		// Update recipelen
		f->recipelen = fv->recipelen;
		// Update mode
		f->stat.st_mode = fv->flags;
		// Update length
		f->stat.st_size = f->len = fv->flen;
		// Update recipe
		free(f->recipe);
		f->recipe = malloc(fv->recipelen);
		memcpy(f->recipe, recipes, fv->recipelen);
		dfs_out("Push[%s] id[%d] version[%d] rlength[%d] recipe[%s] len[%d] dirty[%d]\n", path, fv->hdr.id, f->version, f->recipelen, recipes, f->len, f->dirty);
	    }
	    break;
	case LOG_UNLINK:
	    {
		char *path = (char *)((LogOther *)data + 1);
		_dfs_unlink(path);
	    }
	    break;
	case LOG_MKDIR:
	    {
		char *path = (char *)((LogOther *)data + 1);
		long mode = ((LogOther *)data)->flags;
		_dfs_mkdir(path, mode);
	    }
	    break;
	case LOG_RMDIR:
	    {
		char *path = (char *)((LogOther *)data + 1);
		_dfs_rmdir(path);
	    }
	    break;
	case LOG_CHMOD:
	    {
		char *path = (char *)((LogOther *)data + 1);
		long mode = ((LogOther *)data)->flags;
		_dfs_chmod(path, mode);
	    }
	    break;
	default:
	    printf("BAD RECORD\n");
	    exit(1);
	}
	last_id = ((LogHdr *)data)->id;
	data += ((LogHdr *)data)->len;
    }
    dfs_out("Last ID[%d]\n", last_id);
    opLog.id = last_id;
}
Example #25
0
File: dfs.c Project: bwhite/dfs
static void * dfs_init(struct fuse_conn_info *conn)
{
    pthread_mutex_lock(&treeMut);
    dfs_out("INIT!\n");
    root = mkNode("", "", NULL, DEF_DIR_MODE);

    comm_register_msgtypes(sizeof(messages) / sizeof(messages[0]), messages);

    if (!(extentSock = comm_client_socket(xname, xport))) 
	dfs_die("NO setup client socket to extent server at %d on '%s'\n", xport, xname);

    if (sname && ((opLog.net_fd = comm_client_socket(sname, sport)) > 0)) {
	// Here is where we auth
        assert(chit);
        // Send the server an OHAI
	Msg *reply = comm_send_and_reply(0, opLog.net_fd, DFS_OHAI_SERVER, NULL);
	char *reply_data = malloc(reply->len + 1);
	memcpy(reply_data, reply->data, reply->len);
	reply_data[reply->len] = '\0';
	char server_nonce = reply_data[0];
	char *server_pk = reply_data + 1;
	printf("Server gave us a nonce [%d] and a pk[%s].  He liked our picture!\n", (int) server_nonce, server_pk);
	// TODO Verify PK
	// Make an AES session key
	cry_create_nonce(16, session_key);
	printf("Sym Key[%x%x%x%x]\n", *(session_key), *(session_key + 4), *(session_key + 8), *(session_key + 12));
	// Encrypt session key with PK
	char *session_key_encrypted;
	int session_key_encrypted_sz;
	cry_asym_encrypt(&session_key_encrypted, &session_key_encrypted_sz, session_key, 16, server_pk);
	cry_sym_init(session_key);
	int out_chit_len = strlen(chit) + 3;
	char *out_chit = malloc(out_chit_len);
	char client_nonce;
	cry_create_nonce(1, &client_nonce);
	printf("client nonce[%d]\n", (int)client_nonce);
	out_chit[0] = client_nonce;
	out_chit[1] = server_nonce;
	strcpy(out_chit + 2, chit);
	out_chit[out_chit_len - 1] = '\0';
	char *out_chit_encrypted;
	int out_chit_encrypted_sz;
	cry_sym_encrypt(&out_chit_encrypted, &out_chit_encrypted_sz, out_chit, out_chit_len);
	reply = comm_send_and_reply(0, opLog.net_fd, DFS_TAKE_CHIT_SERVER, session_key_encrypted,
				    session_key_encrypted_sz, out_chit_encrypted, out_chit_encrypted_sz, NULL);
	char *out_nonce;
	int out_nonce_sz;
	cry_sym_init(session_key);
	cry_sym_decrypt(&out_nonce, &out_nonce_sz, reply->data, reply->len);
	printf("Nonce check[%d][%d]\n", (int)client_nonce, (int)(*out_nonce));
	assert(*out_nonce == (char)(client_nonce + 1));
	free(reply);

	// create a new thread reading this socket
	pthread_t		tid;
	pthread_create(&tid, NULL, listener, &opLog.net_fd);

	// grab current FS from server
	reply = comm_send_and_reply_mutex(1, &replyLogserverMut, &replyLogserverCond, opLog.net_fd, DFS_MSG_GET_LOG, NULL);
	char *log;
	size_t log_sz;
	assert(tuple_unserialize_log(&log, &log_sz, reply->data, reply->len) == 0);
	if (reply) {
	    if (reply->len) {
		playLog(log, log_sz);
		opLog.served = log_sz;
	    }
	    free(reply);
	}
	free(log);
    } else {
      dfs_die("NO setup client socket to log server at %d on '%s'\n", sport, sname);
    }
    pthread_mutex_unlock(&treeMut);
}