Blame entry.c

Packit 4511e4
#include "cache.h"
Packit 4511e4
#include "blob.h"
Packit 4511e4
#include "dir.h"
Packit 4511e4
#include "streaming.h"
Packit 4511e4
#include "submodule.h"
Packit 4511e4
#include "progress.h"
Packit 4511e4
#include "fsmonitor.h"
Packit 4511e4
Packit 4511e4
static void create_directories(const char *path, int path_len,
Packit 4511e4
			       const struct checkout *state)
Packit 4511e4
{
Packit 4511e4
	char *buf = xmallocz(path_len);
Packit 4511e4
	int len = 0;
Packit 4511e4
Packit 4511e4
	while (len < path_len) {
Packit 4511e4
		do {
Packit 4511e4
			buf[len] = path[len];
Packit 4511e4
			len++;
Packit 4511e4
		} while (len < path_len && path[len] != '/');
Packit 4511e4
		if (len >= path_len)
Packit 4511e4
			break;
Packit 4511e4
		buf[len] = 0;
Packit 4511e4
Packit 4511e4
		/*
Packit 4511e4
		 * For 'checkout-index --prefix=<dir>', <dir> is
Packit 4511e4
		 * allowed to be a symlink to an existing directory,
Packit 4511e4
		 * and we set 'state->base_dir_len' below, such that
Packit 4511e4
		 * we test the path components of the prefix with the
Packit 4511e4
		 * stat() function instead of the lstat() function.
Packit 4511e4
		 */
Packit 4511e4
		if (has_dirs_only_path(buf, len, state->base_dir_len))
Packit 4511e4
			continue; /* ok, it is already a directory. */
Packit 4511e4
Packit 4511e4
		/*
Packit 4511e4
		 * If this mkdir() would fail, it could be that there
Packit 4511e4
		 * is already a symlink or something else exists
Packit 4511e4
		 * there, therefore we then try to unlink it and try
Packit 4511e4
		 * one more time to create the directory.
Packit 4511e4
		 */
Packit 4511e4
		if (mkdir(buf, 0777)) {
Packit 4511e4
			if (errno == EEXIST && state->force &&
Packit 4511e4
			    !unlink_or_warn(buf) && !mkdir(buf, 0777))
Packit 4511e4
				continue;
Packit 4511e4
			die_errno("cannot create directory at '%s'", buf);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
	free(buf);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void remove_subtree(struct strbuf *path)
Packit 4511e4
{
Packit 4511e4
	DIR *dir = opendir(path->buf);
Packit 4511e4
	struct dirent *de;
Packit 4511e4
	int origlen = path->len;
Packit 4511e4
Packit 4511e4
	if (!dir)
Packit 4511e4
		die_errno("cannot opendir '%s'", path->buf);
Packit 4511e4
	while ((de = readdir(dir)) != NULL) {
Packit 4511e4
		struct stat st;
Packit 4511e4
Packit 4511e4
		if (is_dot_or_dotdot(de->d_name))
Packit 4511e4
			continue;
Packit 4511e4
Packit 4511e4
		strbuf_addch(path, '/');
Packit 4511e4
		strbuf_addstr(path, de->d_name);
Packit 4511e4
		if (lstat(path->buf, &st))
Packit 4511e4
			die_errno("cannot lstat '%s'", path->buf);
Packit 4511e4
		if (S_ISDIR(st.st_mode))
Packit 4511e4
			remove_subtree(path);
Packit 4511e4
		else if (unlink(path->buf))
Packit 4511e4
			die_errno("cannot unlink '%s'", path->buf);
Packit 4511e4
		strbuf_setlen(path, origlen);
Packit 4511e4
	}
Packit 4511e4
	closedir(dir);
Packit 4511e4
	if (rmdir(path->buf))
Packit 4511e4
		die_errno("cannot rmdir '%s'", path->buf);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int create_file(const char *path, unsigned int mode)
Packit 4511e4
{
Packit 4511e4
	mode = (mode & 0100) ? 0777 : 0666;
Packit 4511e4
	return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
Packit 4511e4
{
Packit 4511e4
	enum object_type type;
Packit 4511e4
	void *blob_data = read_object_file(&ce->oid, &type, size);
Packit 4511e4
Packit 4511e4
	if (blob_data) {
Packit 4511e4
		if (type == OBJ_BLOB)
Packit 4511e4
			return blob_data;
Packit 4511e4
		free(blob_data);
Packit 4511e4
	}
Packit 4511e4
	return NULL;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
Packit 4511e4
{
Packit 4511e4
	int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
Packit 4511e4
	if (to_tempfile) {
Packit 4511e4
		xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
Packit 4511e4
			  symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
Packit 4511e4
		return mkstemp(path);
Packit 4511e4
	} else {
Packit 4511e4
		return create_file(path, !symlink ? ce->ce_mode : 0666);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int fstat_output(int fd, const struct checkout *state, struct stat *st)
Packit 4511e4
{
Packit 4511e4
	/* use fstat() only when path == ce->name */
Packit 4511e4
	if (fstat_is_reliable() &&
Packit 4511e4
	    state->refresh_cache && !state->base_dir_len) {
Packit 4511e4
		fstat(fd, st);
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int streaming_write_entry(const struct cache_entry *ce, char *path,
Packit 4511e4
				 struct stream_filter *filter,
Packit 4511e4
				 const struct checkout *state, int to_tempfile,
Packit 4511e4
				 int *fstat_done, struct stat *statbuf)
Packit 4511e4
{
Packit 4511e4
	int result = 0;
Packit 4511e4
	int fd;
Packit 4511e4
Packit 4511e4
	fd = open_output_fd(path, ce, to_tempfile);
Packit 4511e4
	if (fd < 0)
Packit 4511e4
		return -1;
Packit 4511e4
Packit 4511e4
	result |= stream_blob_to_fd(fd, &ce->oid, filter, 1);
Packit 4511e4
	*fstat_done = fstat_output(fd, state, statbuf);
Packit 4511e4
	result |= close(fd);
Packit 4511e4
Packit 4511e4
	if (result)
Packit 4511e4
		unlink(path);
Packit 4511e4
	return result;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void enable_delayed_checkout(struct checkout *state)
Packit 4511e4
{
Packit 4511e4
	if (!state->delayed_checkout) {
Packit 4511e4
		state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout));
Packit 4511e4
		state->delayed_checkout->state = CE_CAN_DELAY;
Packit 4511e4
		string_list_init(&state->delayed_checkout->filters, 0);
Packit 4511e4
		string_list_init(&state->delayed_checkout->paths, 0);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int remove_available_paths(struct string_list_item *item, void *cb_data)
Packit 4511e4
{
Packit 4511e4
	struct string_list *available_paths = cb_data;
Packit 4511e4
	struct string_list_item *available;
Packit 4511e4
Packit 4511e4
	available = string_list_lookup(available_paths, item->string);
Packit 4511e4
	if (available)
Packit 4511e4
		available->util = (void *)item->string;
Packit 4511e4
	return !available;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int finish_delayed_checkout(struct checkout *state)
Packit 4511e4
{
Packit 4511e4
	int errs = 0;
Packit 4511e4
	unsigned delayed_object_count;
Packit 4511e4
	off_t filtered_bytes = 0;
Packit 4511e4
	struct string_list_item *filter, *path;
Packit 4511e4
	struct progress *progress;
Packit 4511e4
	struct delayed_checkout *dco = state->delayed_checkout;
Packit 4511e4
Packit 4511e4
	if (!state->delayed_checkout)
Packit 4511e4
		return errs;
Packit 4511e4
Packit 4511e4
	dco->state = CE_RETRY;
Packit 4511e4
	delayed_object_count = dco->paths.nr;
Packit 4511e4
	progress = start_delayed_progress(_("Filtering content"), delayed_object_count);
Packit 4511e4
	while (dco->filters.nr > 0) {
Packit 4511e4
		for_each_string_list_item(filter, &dco->filters) {
Packit 4511e4
			struct string_list available_paths = STRING_LIST_INIT_NODUP;
Packit 4511e4
			display_progress(progress, delayed_object_count - dco->paths.nr);
Packit 4511e4
Packit 4511e4
			if (!async_query_available_blobs(filter->string, &available_paths)) {
Packit 4511e4
				/* Filter reported an error */
Packit 4511e4
				errs = 1;
Packit 4511e4
				filter->string = "";
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (available_paths.nr <= 0) {
Packit 4511e4
				/*
Packit 4511e4
				 * Filter responded with no entries. That means
Packit 4511e4
				 * the filter is done and we can remove the
Packit 4511e4
				 * filter from the list (see
Packit 4511e4
				 * "string_list_remove_empty_items" call below).
Packit 4511e4
				 */
Packit 4511e4
				filter->string = "";
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
Packit 4511e4
			/*
Packit 4511e4
			 * In dco->paths we store a list of all delayed paths.
Packit 4511e4
			 * The filter just send us a list of available paths.
Packit 4511e4
			 * Remove them from the list.
Packit 4511e4
			 */
Packit 4511e4
			filter_string_list(&dco->paths, 0,
Packit 4511e4
				&remove_available_paths, &available_paths);
Packit 4511e4
Packit 4511e4
			for_each_string_list_item(path, &available_paths) {
Packit 4511e4
				struct cache_entry* ce;
Packit 4511e4
Packit 4511e4
				if (!path->util) {
Packit 4511e4
					error("external filter '%s' signaled that '%s' "
Packit 4511e4
					      "is now available although it has not been "
Packit 4511e4
					      "delayed earlier",
Packit 4511e4
					      filter->string, path->string);
Packit 4511e4
					errs |= 1;
Packit 4511e4
Packit 4511e4
					/*
Packit 4511e4
					 * Do not ask the filter for available blobs,
Packit 4511e4
					 * again, as the filter is likely buggy.
Packit 4511e4
					 */
Packit 4511e4
					filter->string = "";
Packit 4511e4
					continue;
Packit 4511e4
				}
Packit 4511e4
				ce = index_file_exists(state->istate, path->string,
Packit 4511e4
						       strlen(path->string), 0);
Packit 4511e4
				if (ce) {
Packit 4511e4
					errs |= checkout_entry(ce, state, NULL);
Packit 4511e4
					filtered_bytes += ce->ce_stat_data.sd_size;
Packit 4511e4
					display_throughput(progress, filtered_bytes);
Packit 4511e4
				} else
Packit 4511e4
					errs = 1;
Packit 4511e4
			}
Packit 4511e4
		}
Packit 4511e4
		string_list_remove_empty_items(&dco->filters, 0);
Packit 4511e4
	}
Packit 4511e4
	stop_progress(&progress);
Packit 4511e4
	string_list_clear(&dco->filters, 0);
Packit 4511e4
Packit 4511e4
	/* At this point we should not have any delayed paths anymore. */
Packit 4511e4
	errs |= dco->paths.nr;
Packit 4511e4
	for_each_string_list_item(path, &dco->paths) {
Packit 4511e4
		error("'%s' was not filtered properly", path->string);
Packit 4511e4
	}
Packit 4511e4
	string_list_clear(&dco->paths, 0);
Packit 4511e4
Packit 4511e4
	free(dco);
Packit 4511e4
	state->delayed_checkout = NULL;
Packit 4511e4
Packit 4511e4
	return errs;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int write_entry(struct cache_entry *ce,
Packit 4511e4
		       char *path, const struct checkout *state, int to_tempfile)
Packit 4511e4
{
Packit 4511e4
	unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
Packit 4511e4
	struct delayed_checkout *dco = state->delayed_checkout;
Packit 4511e4
	int fd, ret, fstat_done = 0;
Packit 4511e4
	char *new_blob;
Packit 4511e4
	struct strbuf buf = STRBUF_INIT;
Packit 4511e4
	unsigned long size;
Packit 4511e4
	ssize_t wrote;
Packit 4511e4
	size_t newsize = 0;
Packit 4511e4
	struct stat st;
Packit 4511e4
	const struct submodule *sub;
Packit 4511e4
Packit 4511e4
	if (ce_mode_s_ifmt == S_IFREG) {
Packit 4511e4
		struct stream_filter *filter = get_stream_filter(ce->name,
Packit 4511e4
								 &ce->oid);
Packit 4511e4
		if (filter &&
Packit 4511e4
		    !streaming_write_entry(ce, path, filter,
Packit 4511e4
					   state, to_tempfile,
Packit 4511e4
					   &fstat_done, &st))
Packit 4511e4
			goto finish;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	switch (ce_mode_s_ifmt) {
Packit 4511e4
	case S_IFLNK:
Packit 4511e4
		new_blob = read_blob_entry(ce, &size);
Packit 4511e4
		if (!new_blob)
Packit 4511e4
			return error("unable to read sha1 file of %s (%s)",
Packit 4511e4
				     path, oid_to_hex(&ce->oid));
Packit 4511e4
Packit 4511e4
		/*
Packit 4511e4
		 * We can't make a real symlink; write out a regular file entry
Packit 4511e4
		 * with the symlink destination as its contents.
Packit 4511e4
		 */
Packit 4511e4
		if (!has_symlinks || to_tempfile)
Packit 4511e4
			goto write_file_entry;
Packit 4511e4
Packit 4511e4
		ret = symlink(new_blob, path);
Packit 4511e4
		free(new_blob);
Packit 4511e4
		if (ret)
Packit 4511e4
			return error_errno("unable to create symlink %s", path);
Packit 4511e4
		break;
Packit 4511e4
Packit 4511e4
	case S_IFREG:
Packit 4511e4
		/*
Packit 4511e4
		 * We do not send the blob in case of a retry, so do not
Packit 4511e4
		 * bother reading it at all.
Packit 4511e4
		 */
Packit 4511e4
		if (dco && dco->state == CE_RETRY) {
Packit 4511e4
			new_blob = NULL;
Packit 4511e4
			size = 0;
Packit 4511e4
		} else {
Packit 4511e4
			new_blob = read_blob_entry(ce, &size);
Packit 4511e4
			if (!new_blob)
Packit 4511e4
				return error("unable to read sha1 file of %s (%s)",
Packit 4511e4
					     path, oid_to_hex(&ce->oid));
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		/*
Packit 4511e4
		 * Convert from git internal format to working tree format
Packit 4511e4
		 */
Packit 4511e4
		if (dco && dco->state != CE_NO_DELAY) {
Packit 4511e4
			ret = async_convert_to_working_tree(ce->name, new_blob,
Packit 4511e4
							    size, &buf, dco);
Packit 4511e4
			if (ret && string_list_has_string(&dco->paths, ce->name)) {
Packit 4511e4
				free(new_blob);
Packit 4511e4
				goto delayed;
Packit 4511e4
			}
Packit 4511e4
		} else
Packit 4511e4
			ret = convert_to_working_tree(ce->name, new_blob, size, &buf;;
Packit 4511e4
Packit 4511e4
		if (ret) {
Packit 4511e4
			free(new_blob);
Packit 4511e4
			new_blob = strbuf_detach(&buf, &newsize);
Packit 4511e4
			size = newsize;
Packit 4511e4
		}
Packit 4511e4
		/*
Packit 4511e4
		 * No "else" here as errors from convert are OK at this
Packit 4511e4
		 * point. If the error would have been fatal (e.g.
Packit 4511e4
		 * filter is required), then we would have died already.
Packit 4511e4
		 */
Packit 4511e4
Packit 4511e4
	write_file_entry:
Packit 4511e4
		fd = open_output_fd(path, ce, to_tempfile);
Packit 4511e4
		if (fd < 0) {
Packit 4511e4
			free(new_blob);
Packit 4511e4
			return error_errno("unable to create file %s", path);
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		wrote = write_in_full(fd, new_blob, size);
Packit 4511e4
		if (!to_tempfile)
Packit 4511e4
			fstat_done = fstat_output(fd, state, &st);
Packit 4511e4
		close(fd);
Packit 4511e4
		free(new_blob);
Packit 4511e4
		if (wrote < 0)
Packit 4511e4
			return error("unable to write file %s", path);
Packit 4511e4
		break;
Packit 4511e4
Packit 4511e4
	case S_IFGITLINK:
Packit 4511e4
		if (to_tempfile)
Packit 4511e4
			return error("cannot create temporary submodule %s", path);
Packit 4511e4
		if (mkdir(path, 0777) < 0)
Packit 4511e4
			return error("cannot create submodule directory %s", path);
Packit 4511e4
		sub = submodule_from_ce(ce);
Packit 4511e4
		if (sub)
Packit 4511e4
			return submodule_move_head(ce->name,
Packit 4511e4
				NULL, oid_to_hex(&ce->oid),
Packit 4511e4
				state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
Packit 4511e4
		break;
Packit 4511e4
Packit 4511e4
	default:
Packit 4511e4
		return error("unknown file mode for %s in index", path);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
finish:
Packit 4511e4
	if (state->refresh_cache) {
Packit 4511e4
		assert(state->istate);
Packit 4511e4
		if (!fstat_done)
Packit 4511e4
			if (lstat(ce->name, &st) < 0)
Packit 4511e4
				return error_errno("unable to stat just-written file %s",
Packit 4511e4
						   ce->name);
Packit 4511e4
		fill_stat_cache_info(ce, &st);
Packit 4511e4
		ce->ce_flags |= CE_UPDATE_IN_BASE;
Packit 4511e4
		mark_fsmonitor_invalid(state->istate, ce);
Packit 4511e4
		state->istate->cache_changed |= CE_ENTRY_CHANGED;
Packit 4511e4
	}
Packit 4511e4
delayed:
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * This is like 'lstat()', except it refuses to follow symlinks
Packit 4511e4
 * in the path, after skipping "skiplen".
Packit 4511e4
 */
Packit 4511e4
static int check_path(const char *path, int len, struct stat *st, int skiplen)
Packit 4511e4
{
Packit 4511e4
	const char *slash = path + len;
Packit 4511e4
Packit 4511e4
	while (path < slash && *slash != '/')
Packit 4511e4
		slash--;
Packit 4511e4
	if (!has_dirs_only_path(path, slash - path, skiplen)) {
Packit 4511e4
		errno = ENOENT;
Packit 4511e4
		return -1;
Packit 4511e4
	}
Packit 4511e4
	return lstat(path, st);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Write the contents from ce out to the working tree.
Packit 4511e4
 *
Packit 4511e4
 * When topath[] is not NULL, instead of writing to the working tree
Packit 4511e4
 * file named by ce, a temporary file is created by this function and
Packit 4511e4
 * its name is returned in topath[], which must be able to hold at
Packit 4511e4
 * least TEMPORARY_FILENAME_LENGTH bytes long.
Packit 4511e4
 */
Packit 4511e4
int checkout_entry(struct cache_entry *ce,
Packit 4511e4
		   const struct checkout *state, char *topath)
Packit 4511e4
{
Packit 4511e4
	static struct strbuf path = STRBUF_INIT;
Packit 4511e4
	struct stat st;
Packit 4511e4
Packit 4511e4
	if (topath)
Packit 4511e4
		return write_entry(ce, topath, state, 1);
Packit 4511e4
Packit 4511e4
	strbuf_reset(&path);
Packit 4511e4
	strbuf_add(&path, state->base_dir, state->base_dir_len);
Packit 4511e4
	strbuf_add(&path, ce->name, ce_namelen(ce));
Packit 4511e4
Packit 4511e4
	if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
Packit 4511e4
		const struct submodule *sub;
Packit 4511e4
		unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
Packit 4511e4
		/*
Packit 4511e4
		 * Needs to be checked before !changed returns early,
Packit 4511e4
		 * as the possibly empty directory was not changed
Packit 4511e4
		 */
Packit 4511e4
		sub = submodule_from_ce(ce);
Packit 4511e4
		if (sub) {
Packit 4511e4
			int err;
Packit 4511e4
			if (!is_submodule_populated_gently(ce->name, &err)) {
Packit 4511e4
				struct stat sb;
Packit 4511e4
				if (lstat(ce->name, &sb))
Packit 4511e4
					die(_("could not stat file '%s'"), ce->name);
Packit 4511e4
				if (!(st.st_mode & S_IFDIR))
Packit 4511e4
					unlink_or_warn(ce->name);
Packit 4511e4
Packit 4511e4
				return submodule_move_head(ce->name,
Packit 4511e4
					NULL, oid_to_hex(&ce->oid), 0);
Packit 4511e4
			} else
Packit 4511e4
				return submodule_move_head(ce->name,
Packit 4511e4
					"HEAD", oid_to_hex(&ce->oid),
Packit 4511e4
					state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		if (!changed)
Packit 4511e4
			return 0;
Packit 4511e4
		if (!state->force) {
Packit 4511e4
			if (!state->quiet)
Packit 4511e4
				fprintf(stderr,
Packit 4511e4
					"%s already exists, no checkout\n",
Packit 4511e4
					path.buf);
Packit 4511e4
			return -1;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		/*
Packit 4511e4
		 * We unlink the old file, to get the new one with the
Packit 4511e4
		 * right permissions (including umask, which is nasty
Packit 4511e4
		 * to emulate by hand - much easier to let the system
Packit 4511e4
		 * just do the right thing)
Packit 4511e4
		 */
Packit 4511e4
		if (S_ISDIR(st.st_mode)) {
Packit 4511e4
			/* If it is a gitlink, leave it alone! */
Packit 4511e4
			if (S_ISGITLINK(ce->ce_mode))
Packit 4511e4
				return 0;
Packit 4511e4
			if (!state->force)
Packit 4511e4
				return error("%s is a directory", path.buf);
Packit 4511e4
			remove_subtree(&path);
Packit 4511e4
		} else if (unlink(path.buf))
Packit 4511e4
			return error_errno("unable to unlink old '%s'", path.buf);
Packit 4511e4
	} else if (state->not_new)
Packit 4511e4
		return 0;
Packit 4511e4
Packit 4511e4
	create_directories(path.buf, path.len, state);
Packit 4511e4
	return write_entry(ce, path.buf, state, 0);
Packit 4511e4
}