Example #1
0
/* handlers for ct_exists_file. */
void
ct_state_exists(struct ct_global_state *state, struct ct_trans *trans)
{
	/*
	 * State flow:
	 * exists packets -> either EXISTS or NEXISTS.
	 * when all exists done, S_DONE trans sent.
	 */
	switch (trans->tr_state) {
	case TR_S_COMPSHA_ED:
	case TR_S_UNCOMPSHA_ED:
		/* do exists, will return with either S_EXISTS or S_NEXISTS */
		ct_queue_write(state, trans);
		break;

	case TR_S_EXISTS:
	case TR_S_NEXISTS:
	case TR_S_DONE:
		/* done here, complete */
		ct_queue_complete(state, trans);
		break;
	default:
		CABORTX("state %d, not handled in %s()",
		    trans->tr_state, __func__);
	}
}
Example #2
0
int
ct_match_rb_is_empty(struct ct_match *match)
{
	if (match->cm_mode != CT_MATCH_RB)
		CABORTX("match mode %d is not rb", match->cm_mode);
	return (RB_EMPTY(match->cm_rb_head));
}
Example #3
0
int
ct_op_complete(struct ct_global_state *state)
{
	struct ct_op	*op;
	int		 ret, s_errno;;

	op = TAILQ_FIRST(&state->ct_operations);
	if (op == NULL)
		CABORTX("no operation in queue");

	if (op->op_complete != NULL && (ret = op->op_complete(state, op)) != 0)
	{
		s_errno = errno;
		state->ct_errno = ret;
		ct_clear_operation(state);
		errno = s_errno;

		return (1);
	}

	TAILQ_REMOVE(&state->ct_operations, op, op_link);
	e_free(&op);

	if (TAILQ_EMPTY(&state->ct_operations))
		return (1);

	/* set up for the next loop */
	ct_set_file_state(state, CT_S_STARTING);
	ct_wakeup_file(state->event_state);
	return (0);
}
Example #4
0
int
ct_extract_open_next(struct ct_extract_head *extract_head, struct ctfile_parse_state *ctx)
{
	struct ct_extract_stack *next;
	int			 ret, s_errno;

	if (!TAILQ_EMPTY(extract_head)) {
		next = TAILQ_FIRST(extract_head);
		CNDBG(CT_LOG_CTFILE,
		    "should start restoring [%s]", next->filename);

		/* Basedir not needed here because we are done with prevlvl */
		if ((ret = ctfile_parse_init(ctx, next->filename, NULL)) != 0) {
			s_errno = errno;
			/* chain is broken, clean it up */
			ct_extract_cleanup_queue(extract_head);
			errno = s_errno;
			return (ret);
		}

		TAILQ_REMOVE(extract_head, next, next);
		if (next->filename)
			e_free(&next->filename);
		if (next)
			e_free(&next);
	} else {
		CABORTX("open next with no next archive");
	}

	return (0);
}
Example #5
0
void
ct_match_unwind(struct ct_match *match)
{
	switch (match->cm_mode) {
	case CT_MATCH_EVERYTHING:
		break;
	case CT_MATCH_REGEX:
		if (match->cm_regex) {
			ct_regex_unwind(match->cm_regex);
			e_free(&match->cm_regex);
		}
		break;
	case CT_MATCH_RB:
		if (match->cm_rb_head) {
			ct_rb_unwind(match->cm_rb_head);
			e_free(&match->cm_rb_head);
		}
		break;
	case CT_MATCH_GLOB:
		if (match->cm_glob) {
			ct_glob_unwind(match->cm_glob);
			e_free(&match->cm_glob);
		}
		break;
	default:
		CABORTX("invalid match mode");
	}
	e_free(&match);
}
Example #6
0
const char *
ct_strerror(int ct_errno)
{
	if (ct_errno == CTE_ERRNO)
		return (strerror(errno));

	if (ct_errno < 0 || ct_errno >= CTE_MAX)
		CABORTX("ct_errno out of range %d", ct_errno);

	return (ct_errmsgs[ct_errno]);
}
Example #7
0
void
ct_nextop(void *vctx)
{
	struct ct_global_state	*state = vctx;
	struct ct_op		*op;

	op = TAILQ_FIRST(&state->ct_operations);
	if (op == NULL)
		CABORTX("no operation in queue");

	op->op_start(state, op);
}
Example #8
0
void
ct_match_insert_rb(struct ct_match *match, char *string)
{
	struct ct_match_node	*n;

	if (match->cm_mode != CT_MATCH_RB)
		CABORTX("match mode %d is not rb", match->cm_mode);
	n = e_calloc(1, sizeof(struct ct_match_node));
	n->cmn_string = e_strdup(string);
	if (RB_INSERT(ct_match_tree, match->cm_rb_head, n)) {
		/* pattern already exists free it */
		e_free(&n->cmn_string);
		e_free(&n);
	}
}
Example #9
0
int
ct_match_compile(struct ct_match **matchp, int mode, char **flist)
{
	struct ct_match	*match;
	int		 i, ret;

	match = e_calloc(1, sizeof(*match));
	match->cm_mode = mode;

	switch (mode) {
	case CT_MATCH_EVERYTHING:
		ret = 0;
		break;
	case CT_MATCH_REGEX:
		match->cm_regex = e_calloc(1, sizeof(regex_t));
		ret = ct_regex_comp(match->cm_regex, flist);
		break;
	case CT_MATCH_RB:
		match->cm_rb_head = e_calloc(1, sizeof(*match->cm_rb_head));
		ret = ct_rb_comp(match->cm_rb_head, flist);
		break;
	case CT_MATCH_GLOB:
		for (i = 0; flist[i] != NULL; i++)
			if (flist[i] == NULL)
				break;
		if (i == 0) {
			ret = 0;
			break;
		}
		i++; /* extra NULL */
		match->cm_glob = e_calloc(i, sizeof(char *));

		for (i = 0; flist[i] != NULL; i++) {
			if (flist[i] == NULL)
				break;
			match->cm_glob[i] = e_strdup(flist[i]);
		}
		ret = 0;
		break;
	default:
		CABORTX("invalid match mode");
	}

	*matchp = match;
	return (ret);
}
Example #10
0
int
ct_run_eventloop(struct ct_global_state *state)
{
	int		evret, ret = 0;

	if ((evret = ct_event_dispatch(state->event_state)) == -1) {
		ret = CTE_ERRNO; /* libevent error */
	} else if (evret != 0) {
		/*
		 * That i can see only happens if you call libevent with
		 * no events. so this would be a programming error. - oga.
		 */
		CABORTX("ct_event_dispatch returned non zero, non errno %d",
		    evret);
	} else if (state->ct_errno != 0) {
		ret = state->ct_errno;
	}

	return (ret);
}
Example #11
0
int
ct_match(struct ct_match *match, char *candidate)
{
	switch (match->cm_mode) {
	case CT_MATCH_EVERYTHING:
		return (0);
		break;
	case CT_MATCH_REGEX:
		return (ct_regex_match(match->cm_regex, candidate));
		break;
	case CT_MATCH_RB:
		return (ct_rb_match(match->cm_rb_head, candidate));
		break;
	case CT_MATCH_GLOB:
		return (ct_glob_match(match->cm_glob, candidate));
		break;
	default:
		CABORTX("invalid match mode");
	}
	/* NOTREACHED */
}
Example #12
0
/*
 * returns boolean 1/0 whether or not the ctfile in question is the full tag
 * with date/time or not.
 */
int
ctfile_is_fullname(const char *ctfile)
{
	char			*pattern = "^[[:digit:]]{8}-[[:digit:]]{6}-";
	char			error[1024];
	regex_t			re;
	int			match = 0, rv;
	if ((rv = regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB)) != 0) {
		regerror(rv, &re, error, sizeof(error) - 1);
		/*
		 * We use exude, so memory errors are already fatal. The
		 * only way we will fail here is OOM or programming error.
		 */
		CABORTX("%s: regcomp failed: %s", __func__, error);
	}
	if (regexec(&re, ctfile, 0, NULL, 0) == 0)
		match = 1;

	regfree(&re);
	return match;
}
Example #13
0
int
ctdb_insert_sha(struct ctdb_state *state, uint8_t *sha_k, uint8_t *sha_v,
    uint8_t *iv, int32_t genid)
{
	char			shatk[SHA_DIGEST_STRING_LENGTH];
	char			shatv[SHA_DIGEST_STRING_LENGTH];
	int			rv, rc;
	sqlite3_stmt		*stmt;

	rv = 0;

	if (state == NULL || state->ctdb_db == NULL)
		return rv;

	stmt = state->ctdb_stmt_insert;

	if (state->ctdb_in_transaction == 0) {
		if (ctdb_begin_transaction(state) != 0)
			return (rv);
		state->ctdb_trans_commit_rem = OPS_PER_TRANSACTION;
	}

	if (clog_mask_is_set(CT_LOG_DB)) {
		ct_sha1_encode(sha_k, shatk);
		if (sha_v == NULL)
			shatv[0] = '\0';
		else
			ct_sha1_encode(sha_v, shatv);
		CNDBG(CT_LOG_DB, "inserting for bin %s, %s", shatk, shatv);
	}
	if (sqlite3_bind_blob(stmt, 1, sha_k, SHA_DIGEST_LENGTH,
	    SQLITE_STATIC)) {
		CNDBG(CT_LOG_DB, "could not bind sha_k");
		return rv;
	}
	if (state->ctdb_crypt) {
		if (sha_v == NULL || iv == NULL)
			CABORTX("crypt mode, but no sha_v/iv");
		if (sqlite3_bind_blob(stmt, 2, sha_v,
		    SHA_DIGEST_LENGTH, SQLITE_STATIC)) {
			CNDBG(CT_LOG_DB, "could not bind sha_v");
			sqlite3_reset(stmt);
			return rv;
		}

		if (sqlite3_bind_blob(stmt, 3, iv,
		    CT_IV_LEN, SQLITE_STATIC)) {
			CNDBG(CT_LOG_DB, "could not bind iv ");
			sqlite3_reset(stmt);
			return rv;
		}
		if (sqlite3_bind_int(stmt, 4, genid) != 0) {
			CNDBG(CT_LOG_DB, "could not bind genid");
			sqlite3_reset(stmt);
			return rv;
		}
	} else {
		if (sqlite3_bind_int(stmt, 2, genid) != 0) {
			CNDBG(CT_LOG_DB, "could not bind genid");
			sqlite3_reset(stmt);
			return rv;
		}

	}

	rc = sqlite3_step(stmt);
	if (rc == SQLITE_DONE) {
		CNDBG(CT_LOG_DB, "insert completed");
		rv = 1;
	} else if (rc != SQLITE_CONSTRAINT) {
		CNDBG(CT_LOG_DB, "insert failed %d %d [%s]", rc,
		    sqlite3_extended_errcode(state->ctdb_db),
		    sqlite3_errmsg(state->ctdb_db));
	} else  {
		CNDBG(CT_LOG_DB, "sha already exists");
	}

	sqlite3_reset(stmt);

	/* inserts are more 'costly' than reads */
	state->ctdb_trans_commit_rem -= 4;
	if (state->ctdb_trans_commit_rem <= 0)
		ctdb_end_transaction(state);

	return rv;
}
Example #14
0
/*
 * Extract an individual file that has been passed into the op by op_priv.
 */
void
ct_extract_file(struct ct_global_state *state, struct ct_op *op)
{
	struct ct_extract_file_args	*cefa = op->op_args;
	struct ct_file_extract_priv	*ex_priv = op->op_priv;
	const char			*localfile = cefa->cefa_filename;
	struct ct_trans			*trans;
	int				 ret;
	char				 shat[SHA_DIGEST_STRING_LENGTH];

	if (state->ct_dying != 0)
		goto dying;

	CNDBG(CT_LOG_TRANS, "entry");
	switch (ct_get_file_state(state)) {
	case CT_S_STARTING:
		CNDBG(CT_LOG_TRANS, "starting");
		ex_priv = e_calloc(1, sizeof(*ex_priv));
		/* open file and seek to beginning of file */
		if ((ret = ctfile_parse_init_at(&ex_priv->xdr_ctx,
		    cefa->cefa_ctfile, NULL, cefa->cefa_ctfile_off)) != 0) {
			/* XXX add pathname */
			ct_fatal(state, "Can't open ctfile", ret);
			e_free(&ex_priv);
			goto dying;
		}
		 /* XXX we should handle this better */
		if (state->ct_max_block_size <
		    ex_priv->xdr_ctx.xs_gh.cmg_chunk_size)
			CABORTX("block size negotiated with server %d is "
			    "smaller than file max block size %d",
			    state->ct_max_block_size,
			    ex_priv->xdr_ctx.xs_gh.cmg_chunk_size);
		if ((ret = ct_file_extract_init(&state->extract_state,
		    NULL, 0, 0, 0, NULL, NULL)) != 0) {
			ct_fatal(state, "Can not initialise extract state",
			    ret);
			e_free(&ex_priv);
			goto dying;
		}
		op->op_priv = ex_priv;
		break;
	case CT_S_FINISHED:
		return;
	default:
		break;
	}

	ct_set_file_state(state, CT_S_RUNNING);
	while (1) {
		if ((trans = ct_trans_alloc(state)) == NULL) {
			CNDBG(CT_LOG_TRANS, "ran out of transactions, waiting");
			ct_set_file_state(state, CT_S_WAITING_TRANS);
			return;
		}
		trans->tr_statemachine = ct_state_extract;

		if (ex_priv->done) {
			CNDBG(CT_LOG_CTFILE, "Hit end of ctfile");
			ctfile_parse_close(&ex_priv->xdr_ctx);
			e_free(&ex_priv);
			op->op_priv = NULL;
			trans->tr_state = TR_S_DONE;
			trans->tr_complete = ct_extract_complete_done;
			trans->tr_cleanup = ct_extract_cleanup_done;
			ct_queue_first(state, trans);
			CNDBG(CT_LOG_TRANS, "extract finished");
			ct_set_file_state(state, CT_S_FINISHED);
			return;
		}

		/* unless start of file this is right */
		trans->tr_fl_node = ex_priv->fl_ex_node;

		switch ((ret = ctfile_parse(&ex_priv->xdr_ctx))) {
		case XS_RET_FILE:
			CNDBG(CT_LOG_CTFILE, "opening file");
			if (ex_priv->xdr_ctx.xs_hdr.cmh_nr_shas == -1)
				CABORTX("can't extract file with -1 shas");

			trans = ct_trans_realloc_local(state, trans);
			trans->tr_fl_node = ex_priv->fl_ex_node =
			    ct_alloc_fnode();

			/* Make it local directory, it won't be set up right. */
			ex_priv->xdr_ctx.xs_hdr.cmh_parent_dir = -1;
			/*
			 * Allfiles doesn't matter, only processing one file.
			 * We have a full path to extract to so always strip
			 * slash.
			 */
			ct_populate_fnode(state->extract_state,
			    &ex_priv->xdr_ctx, trans->tr_fl_node,
			    &trans->tr_state, 0, 1);
			if (trans->tr_state == TR_S_EX_SPECIAL) {
				trans->tr_complete =
				    ct_extract_complete_special;
			} else {
				trans->tr_complete =
				    ct_extract_complete_file_start;
			}
			trans->tr_cleanup = ct_extract_cleanup_fnode;

			e_free(&trans->tr_fl_node->fn_fullname);
			trans->tr_fl_node->fn_fullname = e_strdup(localfile);
			e_free(&trans->tr_fl_node->fn_name);
			trans->tr_fl_node->fn_name = e_strdup(localfile);
			/* Set name pointer to something else passed in */

			CNDBG(CT_LOG_CTFILE, "file %s numshas %" PRId64,
			    trans->tr_fl_node->fn_fullname,
			    ex_priv->xdr_ctx.xs_hdr.cmh_nr_shas);
			/*
			 * special files we give our refcount up
			 * regular files we need a new one since we need to
			 * keep ours.
			 */
			if (trans->tr_state != TR_S_EX_SPECIAL) {
				ct_ref_fnode(trans->tr_fl_node);
			} else {
				ex_priv->fl_ex_node = NULL;
			}
			break;
		case XS_RET_SHA:
			CNDBG(CT_LOG_SHA, "sha!");
			if (ex_priv->xdr_ctx.xs_gh.cmg_flags & CT_MD_CRYPTO) {
				/*
				 * yes csha and sha are reversed, we want
				 * to download csha, but putting it in sha
				 * simplifies the code
				 */
				bcopy(ex_priv->xdr_ctx.xs_sha, trans->tr_csha,
				    sizeof(trans->tr_csha));
				bcopy(ex_priv->xdr_ctx.xs_csha, trans->tr_sha,
				    sizeof(trans->tr_sha));
				bcopy(ex_priv->xdr_ctx.xs_iv, trans->tr_iv,
				    sizeof(trans->tr_iv));
			} else {
				bcopy(ex_priv->xdr_ctx.xs_sha, trans->tr_sha,
				    sizeof(trans->tr_sha));
			}
			if (clog_mask_is_set(CT_LOG_SHA)) {
				ct_sha1_encode(trans->tr_sha, shat);
				CNDBG(CT_LOG_SHA, "extracting sha %s", shat);
			}
			trans->tr_state = TR_S_EX_SHA;
			trans->tr_complete = ct_extract_complete_file_read;
			trans->tr_cleanup = ct_extract_cleanup_fnode;
			trans->tr_dataslot = 0;
			ct_ref_fnode(trans->tr_fl_node);
			break;
		case XS_RET_FILE_END:
			trans = ct_trans_realloc_local(state, trans);
			trans->tr_fl_node = ex_priv->fl_ex_node; /* reload */

			CNDBG(CT_LOG_CTFILE, "file end!");
			bcopy(ex_priv->xdr_ctx.xs_trl.cmt_sha, trans->tr_sha,
			    sizeof(trans->tr_sha));
			trans->tr_state = TR_S_EX_FILE_END;
			trans->tr_complete = ct_extract_complete_file_end;
			trans->tr_cleanup = ct_extract_cleanup_fnode;
			trans->tr_fl_node->fn_size =
			    ex_priv->xdr_ctx.xs_trl.cmt_orig_size;
			/* Done now, don't parse further. */
			ex_priv->done = 1;
			/*
			 * no reference here since we give our reference to the
			 * last transaction on that file.
			 */
			ex_priv->fl_ex_node = NULL;

			break;
		case XS_RET_FAIL:
			ct_fatal(state, "Failed to parse ctfile",
			    ex_priv->xdr_ctx.xs_errno);
			goto dying;
			break;
		default:
			CABORTX("%s: invalid state %d", __func__, ret);
		}
		ct_queue_first(state, trans);
	}
	return;

dying:
	if (ex_priv) {
		ctfile_parse_close(&ex_priv->xdr_ctx);
		if (ex_priv->fl_ex_node != NULL) {
			ct_free_fnode(ex_priv->fl_ex_node);
		}
		e_free(&ex_priv);
		/* will be cleaned up by trans if ex_priv already gone */
		if (state->extract_state)
			ct_file_extract_cleanup(state->extract_state);	
	}
	return;
}
Example #15
0
void
ct_extract(struct ct_global_state *state, struct ct_op *op)
{
	struct ct_extract_args	*cea = op->op_args;
	const char		*ctfile = cea->cea_local_ctfile;
	char			**filelist = cea->cea_filelist;
	int			 match_mode = cea->cea_matchmode;
	struct ct_extract_priv	*ex_priv = op->op_priv;
	int			ret;
	struct ct_trans		*trans;
	char			shat[SHA_DIGEST_STRING_LENGTH];

	/* if we were woken up due to fatal, just clean up local state */
	if (state->ct_dying != 0)
		goto dying;

	CNDBG(CT_LOG_TRANS, "entry");
	switch (ct_get_file_state(state)) {
	case CT_S_STARTING:
		if (ex_priv == NULL) {
			ex_priv = e_calloc(1, sizeof(*ex_priv));
			TAILQ_INIT(&ex_priv->extract_head);

			if ((ret = ct_match_compile(&ex_priv->inc_match,
			    match_mode, filelist)) != 0) {
				ct_fatal(state,
				    "failed to compile include pattern", ret);
				goto dying;
			}
			if (cea->cea_excllist != NULL &&
			    (ret = ct_match_compile(&ex_priv->ex_match,
			    match_mode, cea->cea_excllist)) != 0) {
				ct_fatal(state,
				    "failed to compile exclude pattern", ret);
				goto dying;
			}
			op->op_priv = ex_priv;
			RB_INIT(&ex_priv->pending_tree);
		}
		
		if ((ret = ct_file_extract_init(&state->extract_state,
		    cea->cea_tdir, cea->cea_attr,  cea->cea_follow_symlinks,
		    ex_priv->allfiles, cea->cea_log_state,
		    cea->cea_log_chown_failed)) != 0) {
			ct_fatal(state, "Can not initialize extract state",
			    ret);
			goto dying;
		}

		if (ct_extract_calculate_total(state, cea, ex_priv->inc_match,
		    ex_priv->ex_match) != 0) {
			CWARNX("failed to calculate stats");
			goto dying;
		}

		if ((ret = ct_extract_setup(&ex_priv->extract_head,
		    &ex_priv->xdr_ctx, ctfile, cea->cea_ctfile_basedir,
		    &ex_priv->allfiles)) != 0) {
			ct_fatal(state, "can't setup extract queue", ret);
			goto dying;
		}
		state->ct_print_ctfile_info(state->ct_print_state,
		    ex_priv->xdr_ctx.xs_filename, &ex_priv->xdr_ctx.xs_gh);
		/* XXX we should handle this better */
		if (state->ct_max_block_size <
		    ex_priv->xdr_ctx.xs_gh.cmg_chunk_size)
			CABORTX("block size negotiated with server %d is "
			    "smaller than file max block size %d",
			    state->ct_max_block_size,
			    ex_priv->xdr_ctx.xs_gh.cmg_chunk_size);
		/* create rb tree head, prepare to start inserting */
		if (ex_priv->allfiles) {
			ex_priv->fillrb = 1;
		}
		break;
	case CT_S_FINISHED:
		return;
	default:
		break;
	}

	ct_set_file_state(state, CT_S_RUNNING);
	while (1) {
		trans = ct_trans_alloc(state);
		if (trans == NULL) {
			/* system busy, return */
			CNDBG(CT_LOG_TRANS, "ran out of transactions, waiting");
			ct_set_file_state(state, CT_S_WAITING_TRANS);
			return;
		}
		trans->tr_statemachine = ct_state_extract;

		switch ((ret = ctfile_parse(&ex_priv->xdr_ctx))) {
		case XS_RET_FILE:
			if (ex_priv->fillrb == 0 &&
			    ex_priv->xdr_ctx.xs_hdr.cmh_nr_shas == -1) {
				if (ex_priv->allfiles == 0)
					CINFO("file %s has negative shas "
					    "and backup is not allfiles",
					    ex_priv->xdr_ctx.xs_hdr.cmh_filename);
				ex_priv->doextract = 0;
				goto skip; /* skip ze file for now */
			}

			trans = ct_trans_realloc_local(state, trans);
			trans->tr_fl_node = ex_priv->fl_ex_node = 
			    ct_alloc_fnode();

			ct_populate_fnode(state->extract_state,
			    &ex_priv->xdr_ctx, trans->tr_fl_node,
			    &trans->tr_state, ex_priv->allfiles,
			    cea->cea_strip_slash);
			if (trans->tr_state == TR_S_EX_SPECIAL) {
				trans->tr_complete =
				    ct_extract_complete_special;
			} else {
				trans->tr_complete =
				    ct_extract_complete_file_start;
			}
			trans->tr_cleanup = ct_extract_cleanup_fnode;

			if (ex_priv->haverb) {
				struct ct_pending_file *cpf;
				if ((cpf = ct_extract_find_entry(
				    &ex_priv->pending_tree,
				    trans->tr_fl_node->fn_fullname)) != NULL) {
					struct fnode *hardlink;
					/* copy permissions over */
					trans->tr_fl_node->fn_uid =
					    cpf->cpf_uid;
					trans->tr_fl_node->fn_gid =
					    cpf->cpf_gid;
					trans->tr_fl_node->fn_mode =
					    cpf->cpf_mode;
					trans->tr_fl_node->fn_mtime =
					    cpf->cpf_mtime;
					trans->tr_fl_node->fn_atime =
					    cpf->cpf_atime;

					/* copy list of pending links over */
					while ((hardlink =
					    TAILQ_FIRST(&cpf->cpf_links))) {
						TAILQ_REMOVE(&cpf->cpf_links,
						    hardlink, fn_list);
						TAILQ_INSERT_TAIL(
						    &trans->tr_fl_node->fn_hardlinks,
						    hardlink, fn_list);
					}
					
					ex_priv->doextract = 1;
					ct_extract_free_entry(
					    &ex_priv->pending_tree, cpf);
				} else {
					ex_priv->doextract = 0;
				}
				
			} else {
				ex_priv->doextract =
				    !ct_match(ex_priv->inc_match,
				    trans->tr_fl_node->fn_fullname);
				if (ex_priv->doextract &&
				    ex_priv->ex_match != NULL &&
				    !ct_match(ex_priv->ex_match,
				    trans->tr_fl_node->fn_fullname)) {
					ex_priv->doextract = 0;
				}
			}
			if (ex_priv->doextract &&
			    trans->tr_fl_node->fn_hardlink) {
				struct ct_pending_file	*file;
				if ((file = ct_extract_find_entry(
				    &ex_priv->pending_tree,
				    trans->tr_fl_node->fn_hlname)) != NULL) {
					CNDBG(CT_LOG_FILE,
					    "adding pending link for %s to %s",
					    file->cpf_name,
					    trans->tr_fl_node->fn_fullname);
					/* our reference to node passed */
					ct_pending_file_add_link(file,
					    trans->tr_fl_node);
					ex_priv->doextract = 0;
					goto skip;
				}
			}

			/*
			 * If we're on the first ctfile in an allfiles backup
			 * put the matches with -1 on the rb tree so we'll
			 * remember to extract it from older files.
			 */
			if (ex_priv->doextract == 1 && ex_priv->fillrb &&
			    ex_priv->xdr_ctx.xs_hdr.cmh_nr_shas == -1) {
				ct_extract_insert_entry(&ex_priv->pending_tree,
				    trans->tr_fl_node);

				ex_priv->doextract = 0;
				/* XXX reconsider the freeing */
			}
			if (ex_priv->doextract == 0) {
				ct_free_fnode(trans->tr_fl_node);
skip:
				ex_priv->fl_ex_node = NULL;
				ct_trans_free(state, trans);
				continue;
			}

			CNDBG(CT_LOG_CTFILE,
			    "file %s numshas %" PRId64,
			    trans->tr_fl_node->fn_fullname,
			    ex_priv->xdr_ctx.xs_hdr.cmh_nr_shas);

			/*
			 * special files we give our refcount up
			 * regular files we need a new one since we need to
			 * keep ours.
			 */
			if (trans->tr_state != TR_S_EX_SPECIAL) {
				ct_ref_fnode(trans->tr_fl_node);
			} else {
				ex_priv->fl_ex_node = NULL;
			}
			ct_queue_first(state, trans);
			break;
		case XS_RET_SHA:
			if (ex_priv->doextract == 0 ||
			    ex_priv->fl_ex_node->fn_skip_file != 0) {
				if (ctfile_parse_seek(&ex_priv->xdr_ctx)) {
					ct_fatal(state, "Can't seek past shas",
					    ex_priv->xdr_ctx.xs_errno);
					goto dying;
				}
				ct_trans_free(state, trans);
				continue;
			}

			/* use saved fnode */
			trans->tr_fl_node = ex_priv->fl_ex_node;

			if (memcmp(zerosha, ex_priv->xdr_ctx.xs_sha,
				SHA_DIGEST_LENGTH) == 0) {
				CWARNX("\"%s\" truncated during backup",
				    trans->tr_fl_node->fn_fullname);
				if (ctfile_parse_seek(&ex_priv->xdr_ctx)) {
					ct_fatal(state, "Can't seek past "
					    "truncation shas",
					    ex_priv->xdr_ctx.xs_errno);
					goto dying;
				}
				ct_trans_free(state, trans);
				continue;
			}

			if (ex_priv->xdr_ctx.xs_gh.cmg_flags & CT_MD_CRYPTO) {
				/*
				 * yes csha and sha are reversed, we want
				 * to download csha, but putting it in sha
				 * simplifies the code
				 */
				bcopy(ex_priv->xdr_ctx.xs_sha, trans->tr_csha,
				    sizeof(trans->tr_csha));
				bcopy(ex_priv->xdr_ctx.xs_csha, trans->tr_sha,
				    sizeof(trans->tr_sha));
				bcopy(ex_priv->xdr_ctx.xs_iv, trans->tr_iv,
				    sizeof(trans->tr_iv));
			} else {
				bcopy(ex_priv->xdr_ctx.xs_sha, trans->tr_sha,
				    sizeof(trans->tr_sha));
			}
			if (clog_mask_is_set(CT_LOG_SHA)) {
				ct_sha1_encode(trans->tr_sha, shat);
				CNDBG(CT_LOG_SHA, "extracting sha %s", shat);
			}
			trans->tr_state = TR_S_EX_SHA;
			trans->tr_complete = ct_extract_complete_file_read;
			trans->tr_dataslot = 0;
			ct_ref_fnode(trans->tr_fl_node);
			trans->tr_cleanup = ct_extract_cleanup_fnode;
			ct_queue_first(state, trans);
			break;
		case XS_RET_FILE_END:
			trans = ct_trans_realloc_local(state, trans);


			if (ex_priv->doextract == 0 ||
			    ex_priv->fl_ex_node->fn_skip_file != 0) {
				/* release our reference done with file */
				if (ex_priv->fl_ex_node) {
					ct_free_fnode(ex_priv->fl_ex_node);
					ex_priv->fl_ex_node = NULL;
				}
				ct_trans_free(state, trans);
				continue;
			}

			/* use saved fnode from state */
			trans->tr_fl_node = ex_priv->fl_ex_node;
			bcopy(ex_priv->xdr_ctx.xs_trl.cmt_sha, trans->tr_sha,
			    sizeof(trans->tr_sha));
			trans->tr_state = TR_S_EX_FILE_END;
			trans->tr_complete = ct_extract_complete_file_end;
			trans->tr_cleanup = ct_extract_cleanup_fnode;
			trans->tr_fl_node->fn_size =
			    ex_priv->xdr_ctx.xs_trl.cmt_orig_size;
			/*
			 * no reference here since we give our reference to the
			 * last transaction on that file. We are done with it.
			 */
			ex_priv->fl_ex_node = NULL;
			ct_queue_first(state, trans);
			break;
		case XS_RET_EOF:
			CNDBG(CT_LOG_CTFILE, "Hit end of ctfile");
			ctfile_parse_close(&ex_priv->xdr_ctx);
			/* if rb tree and rb is empty, goto end state */
			if ((ex_priv->haverb || ex_priv->fillrb) &&
			    ct_extract_rb_empty(&ex_priv->pending_tree)) {
				/*
				 * Cleanup extract queue, in case we had files
				 * left.
				 */
				ct_extract_cleanup_queue(
				    &ex_priv->extract_head);
				goto we_re_done_here;
			}


			if (!TAILQ_EMPTY(&ex_priv->extract_head)) {
				/*
				 * if allfiles and this was the first pass.
				 * free the current match lists
				 * switch to rb tree mode
				 */
				if (ex_priv->fillrb) {
					ct_match_unwind(ex_priv->inc_match);
					if (ex_priv->ex_match)
						ct_match_unwind(
						    ex_priv->ex_match);
					ex_priv->ex_match = NULL;
					ex_priv->inc_match = NULL;
					ex_priv->haverb = 1;
					ex_priv->fillrb = 0;
				}
				ct_trans_free(state, trans);
				/* reinits ex_priv->xdr_ctx */
				if ((ret =
				    ct_extract_open_next(&ex_priv->extract_head,
				    &ex_priv->xdr_ctx)) != 0) {
					ct_fatal(state,
					    "Can't open next ctfile", ret);
					goto dying;
				}
				state->ct_print_ctfile_info(
				    state->ct_print_state,
				    ex_priv->xdr_ctx.xs_filename,
				    &ex_priv->xdr_ctx.xs_gh);

				/* poke file into action */
				ct_wakeup_file(state->event_state);
			} else {
				/*
				 * If rb tree and it is still has entries,
				 * bitch about it
				 */
				/* XXX print out missing files */
				if ((ex_priv->haverb || ex_priv->fillrb) &&
				    ct_extract_rb_empty(
				    &ex_priv->pending_tree)) {
					CWARNX("out of ctfiles but some "
					    "files are not found");
				}

we_re_done_here:
				if (ex_priv->inc_match)
					ct_match_unwind(ex_priv->inc_match);
				if (ex_priv->ex_match)
					ct_match_unwind(
					    ex_priv->ex_match);
				ct_extract_pending_cleanup(
				    &ex_priv->pending_tree);
				e_free(&ex_priv);
				op->op_priv = NULL;
				trans->tr_state = TR_S_DONE;
				trans->tr_complete = ct_extract_complete_done;
				trans->tr_cleanup = ct_extract_cleanup_done;
				/*
				 * Technically this should be a local
				 * transaction. However, since we are done
				 * it doesn't really matter either way.
				 */
				ct_queue_first(state, trans);
				CNDBG(CT_LOG_TRANS, "extract finished");
				ct_set_file_state(state, CT_S_FINISHED);
			}
			return;
			break;
		case XS_RET_FAIL:
			ct_fatal(state, "Failed to parse ctfile",
			    ex_priv->xdr_ctx.xs_errno);
			goto dying;
			break;
		}
	}

	return;

dying:
	/* only if we hadn't sent the final transaction yet */
	if (ex_priv != NULL) {
		ct_extract_cleanup_queue(&ex_priv->extract_head);
		if (ex_priv->inc_match)
			ct_match_unwind(ex_priv->inc_match);
		if (ex_priv->ex_match)
			ct_match_unwind(ex_priv->ex_match);
		if (!ct_extract_rb_empty(&ex_priv->pending_tree)) {
			ct_extract_pending_cleanup(&ex_priv->pending_tree);
		}
		if (ex_priv->fl_ex_node != NULL) {
			ct_free_fnode(ex_priv->fl_ex_node);
		}
		/* XXX what about ex_priv->xdr_ctx ? */
		e_free(&ex_priv);
		op->op_priv = NULL;
		/* if ex_priv is gone then the trans will clean this up */
		if (state->extract_state)
			ct_file_extract_cleanup(state->extract_state);	
	}
	return;
}
Example #16
0
/*
 * Main guts of  ctd_build_version_tree. Factored out to avoid deep nesting.
 * Insert or update an entry in the tree with the information received from
 * the ctfile.
 */
static int
ct_vertree_add(struct ct_vertree_dnode_cache *dnode_cache,
               struct ct_vertree_entry *head, struct ctfile_parse_state *parse_state,
               struct ct_vertree_ctfile *ctfile, off_t fileoffset, int allfiles)
{
    struct ctfile_header		*hdr = &parse_state->xs_hdr;
    struct ctfile_header		*hdrlnk= &parse_state->xs_lnkhdr;
    struct dnode 			*dnode;
    struct ct_vertree_dnode		*fb_dnode;
    struct ct_vertree_entry		*parent = NULL, sentry, *entry;
    struct ct_vertree_ver		*lastver, *ver;
    struct ct_vertree_file		*file;
    struct ct_vertree_spec		*spec;
    struct ct_vertree_link		*linkver;
    size_t				 sz;
    bool				 root_dnode = false;

    entry = NULL;

    /* First find parent directory if any */
    if (hdr->cmh_parent_dir != -1 && hdr->cmh_parent_dir != -2) {
        if ((dnode = ctfile_parse_finddir(parse_state,
                                          hdr->cmh_parent_dir)) == NULL) {
            CNDBG(CT_LOG_VERTREE, "can't find dir %" PRId64,
                  hdr->cmh_parent_dir);
            return (CTE_CTFILE_CORRUPT);
        }
        fb_dnode = (struct ct_vertree_dnode *)dnode;
        if (fb_dnode == dnode_cache->root_dnode) {
            // If we have the root dnode, store in head.
            parent = head;
        } else {
            parent = fb_dnode->cvd_dir;
        }

    } else {
        parent = head;
    }

    if (parent == head && strcmp(hdr->cmh_filename, CT_PATHSEP_STR) == 0) {
        root_dnode = true;
    }
    /*
     * Have parent node, search children to see if we already exist.
     * Else make a new one and insert.
     */
    sentry.cve_name = hdr->cmh_filename;
    if ((entry = RB_FIND(ct_vertree_entries, &parent->cve_children,
                         &sentry)) == NULL) {
        /* new name, insert node */
        entry = e_calloc(1, sizeof(*entry));

        TAILQ_INIT(&entry->cve_versions);
        RB_INIT(&entry->cve_children);
        entry->cve_parent = parent;
        entry->cve_name = e_strdup(sentry.cve_name);

        /* don't insert root dnodes, just do dnode dance */
        if (root_dnode) {
            goto rootdir;
        }

        if (RB_INSERT(ct_vertree_entries, &parent->cve_children,
                      entry) != NULL) {
            CNDBG(CT_LOG_VERTREE, "entry %s already exists",
                  sentry.cve_name);
            e_free(&sentry.cve_name);
            goto err;
        }
    }

    /*
     * then check version tags -> head/tail if mtime and type match, we're
     * good else prepare version entry.
     */
    if (allfiles) {
        lastver = TAILQ_FIRST(&entry->cve_versions);
    } else {
        lastver = TAILQ_LAST(&entry->cve_versions, ct_vertree_vers);
    }
    /* Don't check atime, it doesn't matter */
    if (lastver != NULL && lastver->cvv_type == hdr->cmh_type &&
            lastver->cvv_mtime == hdr->cmh_mtime &&
            lastver->cvv_uid == hdr->cmh_uid &&
            lastver->cvv_gid == hdr->cmh_gid &&
            lastver->cvv_mode == hdr->cmh_mode) {
        ver = lastver;
    } else { /* something changed. make a new one */
        if (C_ISDIR(hdr->cmh_type)) {
            sz = sizeof(struct ct_vertree_dir);
        } else if (C_ISBLK(hdr->cmh_type) ||
                   C_ISCHR(hdr->cmh_type)) {
            sz = sizeof(struct ct_vertree_spec);
        } else  if (C_ISLINK(hdr->cmh_type)) {
            sz = sizeof(struct ct_vertree_link);
        } else if (C_ISREG(hdr->cmh_type)) {
            sz = sizeof(struct ct_vertree_file);
        } else {
            CNDBG(CT_LOG_VERTREE, "invalid type %d", hdr->cmh_type);
            goto err;
        }
        ver = e_calloc(1, sz);
        ver->cvv_type = hdr->cmh_type;
        ver->cvv_mtime = hdr->cmh_mtime;
        ver->cvv_atime = hdr->cmh_atime;
        ver->cvv_uid = hdr->cmh_uid;
        ver->cvv_gid = hdr->cmh_gid;
        ver->cvv_mode = hdr->cmh_mode;
        /* dir handled below */
        if (C_ISBLK(hdr->cmh_type) || C_ISCHR(hdr->cmh_type))  {
            spec = (struct ct_vertree_spec *)ver;
            spec->cvs_rdev = hdr->cmh_rdev;
        } else if (C_ISLINK(hdr->cmh_type)) {
            /* hardlink/symlink */
            linkver = (struct ct_vertree_link *)ver;
            linkver->cvl_linkname = e_strdup(hdrlnk->cmh_filename);
            linkver->cvl_hardlink = !C_ISLINK(hdrlnk->cmh_type);
        } else if (C_ISREG(hdr->cmh_type)) {
            file = (struct ct_vertree_file *)ver;
            file->cvf_nr_shas = -1;
        }
        if (allfiles) {
            TAILQ_INSERT_HEAD(&entry->cve_versions, ver, cvv_link);
        } else {
            TAILQ_INSERT_TAIL(&entry->cve_versions, ver, cvv_link);
        }
    }


    /*
     * Each ctfile only has each directory referenced once, so put it
     * in the cache regardless of whether it was known of before, that
     * will be a previous run and the cache will have been wiped since
     * then.
     */
    if (C_ISDIR(hdr->cmh_type)) {
rootdir:
        fb_dnode = e_calloc(1, sizeof(*fb_dnode));
        fb_dnode->cvd_dnode.d_name = e_strdup(entry->cve_name);
        /*
         * in the root_dnode case this will be a bad pointer but it
         * will never be derefed.
         */
        fb_dnode->cvd_dir = entry;
        if ((dnode = ctfile_parse_insertdir(parse_state,
                                            &fb_dnode->cvd_dnode)) != NULL)
            CABORTX("duplicate dentry");
        TAILQ_INSERT_TAIL(&dnode_cache->cache, fb_dnode, cvd_link);
        if (root_dnode) {
            dnode_cache->root_dnode = fb_dnode;
        }
    } else if (C_ISREG(hdr->cmh_type)) {
        /*
         * Allfiles ctfiles may have shas == -1, so in some cases we
         * may wish to update an existing file when we find the actual
         * shas. It is an error to have a file node with -1 for shas
         * after all metadata have been parsed. it means one was
         * missing.
         */
        file = (struct ct_vertree_file *)ver;

        /*
         * bugs in previous editions with incremental selection and
         * off_t on linux mean that there are ctfiles in the wild which
         * provide a list of shas in a later level when the file is
         * defined in an earlier level file, also. For example for the
         * same filename and date we have level 0: 3 shas, level 1: -1
         * shas (i.e. in a previous level), level 2: 3 shas (same as
         * level * 0). In that case we just assume that if we already
         * have sha data for a file * then it is correct and we skip
         * previous versions.
         */
        if (file->cvf_nr_shas != -1) {
            goto out;
        }

        /*
         * previous linux off_t bugs with files over 2gb mean that there
         * are sign extended ctfiles in the wild, so we count those as
         * zero length for purposes of the version tree.
         */
        if (hdr->cmh_nr_shas < -1) {
            hdr->cmh_nr_shas = 0;
        }
        if (hdr->cmh_nr_shas != -1)  {
            file->cvf_nr_shas = hdr->cmh_nr_shas;
            file->cvf_sha_offs = fileoffset;
            file->cvf_file = ctfile;
            if (ctfile_parse_seek(parse_state)) {
                CNDBG(CT_LOG_VERTREE,
                      "failed to skip shas in %s",
                      ctfile->cvc_path);
                goto err;
            }
        }
        if (ctfile_parse(parse_state) != XS_RET_FILE_END) {
            CNDBG(CT_LOG_VERTREE, "no file trailer found");
            goto err;
        }

        file->cvf_file_size = parse_state->xs_trl.cmt_orig_size;
    }

out:
    /*
     * If we're an explicit "/" entry then we don't want to be added to
     * the tree. all our children will be added to the root entry.
     */
    if (root_dnode) {
        e_free(&entry);
    }
    return (0);

err:
    if (entry != NULL)
        e_free(&entry);
    return (CTE_CTFILE_CORRUPT);
}
Example #17
0
int
ctfile_nextop_archive(struct ct_global_state *state, char *basis, void *args)
{
	struct ct_archive_args	*caa = args;
	struct ct_ctfileop_args	*cca;
	char			*ctfile;
	char	 		 buf[TIMEDATA_LEN], *fullname, *cachename;
	time_t	 		 now;

	CNDBG(CT_LOG_CTFILE, "setting basisname %s", basis ? basis : "<none>");
	caa->caa_basis = basis;

	/*
	 * We now have the basis found for us, cook and prepare the tag
	 * we wish to create then add the operation.
	 */
	if ((ctfile = ctfile_cook_name(caa->caa_tag)) == NULL) {
		CWARNX("%s: %s", caa->caa_tag,
		    ct_strerror(CTE_INVALID_CTFILE_NAME));
		return (CTE_INVALID_CTFILE_NAME);
	}

	if (ctfile_is_fullname(ctfile) != 0) {
		CWARNX("%s", ct_strerror(CTE_ARCHIVE_FULLNAME));
		e_free(&ctfile);
		return (CTE_ARCHIVE_FULLNAME);
	}

	now = time(NULL);
	if (strftime(buf, TIMEDATA_LEN, "%Y%m%d-%H%M%S",
	    localtime(&now)) == 0)
		CABORTX("can't format time");
	e_asprintf(&fullname, "%s-%s", buf, ctfile);
	CNDBG(CT_LOG_CTFILE, "backup file is %s", fullname);

	/* check it isn't already in the cache */
	cachename = ctfile_get_cachename(fullname,
	    state->ct_config->ct_ctfile_cachedir);
	if (ctfile_in_cache(fullname, state->ct_config->ct_ctfile_cachedir)) {
		CWARNX("%s: %s", fullname,
		    ct_strerror(CTE_BACKUP_ALREADY_EXISTS));
		e_free(&ctfile);
		e_free(&fullname);
		e_free(&cachename);
		return (CTE_BACKUP_ALREADY_EXISTS);
	}

	e_free(&ctfile);
	e_free(&fullname);

	caa->caa_local_ctfile = cachename;
	ct_add_operation(state, ct_archive, NULL, caa);
	/*
	 * set up an additional operation to upload the newly created
	 * ctfile after the archive is completed.
	 */
	cca = e_calloc(1, sizeof(*cca));
	cca->cca_localname = cachename;
	cca->cca_cleartext = 0;
	cca->cca_ctfile = 1;
	ct_add_operation(state, ctfile_archive, ctfile_nextop_archive_cleanup,
	    cca);
	return (0);
}