entry.c 7.4 KB
Newer Older
1
#include "cache.h"
2
#include "blob.h"
3
#include "dir.h"
4
#include "streaming.h"
5

6 7
static void create_directories(const char *path, int path_len,
			       const struct checkout *state)
8
{
9
	char *buf = xmallocz(path_len);
10 11 12 13 14 15 16 17 18
	int len = 0;

	while (len < path_len) {
		do {
			buf[len] = path[len];
			len++;
		} while (len < path_len && path[len] != '/');
		if (len >= path_len)
			break;
19
		buf[len] = 0;
20

21 22 23 24 25 26 27
		/*
		 * For 'checkout-index --prefix=<dir>', <dir> is
		 * allowed to be a symlink to an existing directory,
		 * and we set 'state->base_dir_len' below, such that
		 * we test the path components of the prefix with the
		 * stat() function instead of the lstat() function.
		 */
28
		if (has_dirs_only_path(buf, len, state->base_dir_len))
29 30 31
			continue; /* ok, it is already a directory. */

		/*
32 33 34 35
		 * If this mkdir() would fail, it could be that there
		 * is already a symlink or something else exists
		 * there, therefore we then try to unlink it and try
		 * one more time to create the directory.
36
		 */
37
		if (mkdir(buf, 0777)) {
38
			if (errno == EEXIST && state->force &&
39
			    !unlink_or_warn(buf) && !mkdir(buf, 0777))
40
				continue;
41
			die_errno("cannot create directory at '%s'", buf);
42 43 44 45 46
		}
	}
	free(buf);
}

47
static void remove_subtree(struct strbuf *path)
48
{
49
	DIR *dir = opendir(path->buf);
50
	struct dirent *de;
51
	int origlen = path->len;
Junio C Hamano's avatar
Junio C Hamano committed
52

53
	if (!dir)
54
		die_errno("cannot opendir '%s'", path->buf);
55 56
	while ((de = readdir(dir)) != NULL) {
		struct stat st;
57

58
		if (is_dot_or_dotdot(de->d_name))
59
			continue;
60 61 62 63 64

		strbuf_addch(path, '/');
		strbuf_addstr(path, de->d_name);
		if (lstat(path->buf, &st))
			die_errno("cannot lstat '%s'", path->buf);
65
		if (S_ISDIR(st.st_mode))
66 67 68 69
			remove_subtree(path);
		else if (unlink(path->buf))
			die_errno("cannot unlink '%s'", path->buf);
		strbuf_setlen(path, origlen);
70 71
	}
	closedir(dir);
72 73
	if (rmdir(path->buf))
		die_errno("cannot rmdir '%s'", path->buf);
74 75
}

76
static int create_file(const char *path, unsigned int mode)
77 78
{
	mode = (mode & 0100) ? 0777 : 0666;
79
	return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
80 81
}

82
static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
83 84 85 86 87 88 89 90 91 92 93 94
{
	enum object_type type;
	void *new = read_sha1_file(ce->sha1, &type, size);

	if (new) {
		if (type == OBJ_BLOB)
			return new;
		free(new);
	}
	return NULL;
}

95
static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
96 97 98
{
	int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
	if (to_tempfile) {
99 100
		xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
			  symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		return mkstemp(path);
	} else {
		return create_file(path, !symlink ? ce->ce_mode : 0666);
	}
}

static int fstat_output(int fd, const struct checkout *state, struct stat *st)
{
	/* use fstat() only when path == ce->name */
	if (fstat_is_reliable() &&
	    state->refresh_cache && !state->base_dir_len) {
		fstat(fd, st);
		return 1;
	}
	return 0;
}

118
static int streaming_write_entry(const struct cache_entry *ce, char *path,
119
				 struct stream_filter *filter,
120 121 122
				 const struct checkout *state, int to_tempfile,
				 int *fstat_done, struct stat *statbuf)
{
123
	int result = 0;
124
	int fd;
125 126

	fd = open_output_fd(path, ce, to_tempfile);
127 128 129 130 131 132 133 134
	if (fd < 0)
		return -1;

	result |= stream_blob_to_fd(fd, ce->sha1, filter, 1);
	*fstat_done = fstat_output(fd, state, statbuf);
	result |= close(fd);

	if (result)
135 136 137 138
		unlink(path);
	return result;
}

139 140
static int write_entry(struct cache_entry *ce,
		       char *path, const struct checkout *state, int to_tempfile)
141
{
142
	unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
143
	int fd, ret, fstat_done = 0;
144 145 146 147
	char *new;
	struct strbuf buf = STRBUF_INIT;
	unsigned long size;
	size_t wrote, newsize = 0;
148
	struct stat st;
149

150
	if (ce_mode_s_ifmt == S_IFREG) {
John Keeping's avatar
John Keeping committed
151
		struct stream_filter *filter = get_stream_filter(ce->name, ce->sha1);
152 153 154 155 156 157
		if (filter &&
		    !streaming_write_entry(ce, path, filter,
					   state, to_tempfile,
					   &fstat_done, &st))
			goto finish;
	}
158

159
	switch (ce_mode_s_ifmt) {
160
	case S_IFREG:
161 162
	case S_IFLNK:
		new = read_blob_entry(ce, &size);
163
		if (!new)
164
			return error("unable to read sha1 file of %s (%s)",
165
				path, sha1_to_hex(ce->sha1));
166

167 168 169 170
		if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
			ret = symlink(new, path);
			free(new);
			if (ret)
171
				return error("unable to create symlink %s (%s)",
172 173 174 175
					     path, strerror(errno));
			break;
		}

176 177 178
		/*
		 * Convert from git internal format to working tree format
		 */
179 180
		if (ce_mode_s_ifmt == S_IFREG &&
		    convert_to_working_tree(ce->name, new, size, &buf)) {
181
			free(new);
182 183
			new = strbuf_detach(&buf, &newsize);
			size = newsize;
184 185
		}

186
		fd = open_output_fd(path, ce, to_tempfile);
187 188
		if (fd < 0) {
			free(new);
189
			return error("unable to create file %s (%s)",
190 191
				path, strerror(errno));
		}
Linus Torvalds's avatar
Linus Torvalds committed
192

193
		wrote = write_in_full(fd, new, size);
194 195
		if (!to_tempfile)
			fstat_done = fstat_output(fd, state, &st);
196 197 198
		close(fd);
		free(new);
		if (wrote != size)
199
			return error("unable to write file %s", path);
200
		break;
201
	case S_IFGITLINK:
202
		if (to_tempfile)
203
			return error("cannot create temporary submodule %s", path);
204
		if (mkdir(path, 0777) < 0)
205
			return error("cannot create submodule directory %s", path);
206
		break;
207
	default:
208
		return error("unknown file mode for %s in index", path);
209 210
	}

211
finish:
212
	if (state->refresh_cache) {
213
		assert(state->istate);
214 215
		if (!fstat_done)
			lstat(ce->name, &st);
216
		fill_stat_cache_info(ce, &st);
217
		ce->ce_flags |= CE_UPDATE_IN_BASE;
218
		state->istate->cache_changed |= CE_ENTRY_CHANGED;
219 220 221 222
	}
	return 0;
}

223 224
/*
 * This is like 'lstat()', except it refuses to follow symlinks
225
 * in the path, after skipping "skiplen".
226
 */
227
static int check_path(const char *path, int len, struct stat *st, int skiplen)
228
{
229 230 231 232 233
	const char *slash = path + len;

	while (path < slash && *slash != '/')
		slash--;
	if (!has_dirs_only_path(path, slash - path, skiplen)) {
234 235 236 237 238 239
		errno = ENOENT;
		return -1;
	}
	return lstat(path, st);
}

240 241 242 243 244 245 246 247
/*
 * Write the contents from ce out to the working tree.
 *
 * When topath[] is not NULL, instead of writing to the working tree
 * file named by ce, a temporary file is created by this function and
 * its name is returned in topath[], which must be able to hold at
 * least TEMPORARY_FILENAME_LENGTH bytes long.
 */
248 249
int checkout_entry(struct cache_entry *ce,
		   const struct checkout *state, char *topath)
250
{
251
	static struct strbuf path = STRBUF_INIT;
252
	struct stat st;
253

254 255 256
	if (topath)
		return write_entry(ce, topath, state, 1);

257 258 259
	strbuf_reset(&path);
	strbuf_add(&path, state->base_dir, state->base_dir_len);
	strbuf_add(&path, ce->name, ce_namelen(ce));
260

261
	if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
262
		unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
263 264 265 266
		if (!changed)
			return 0;
		if (!state->force) {
			if (!state->quiet)
267 268 269
				fprintf(stderr,
					"%s already exists, no checkout\n",
					path.buf);
270
			return -1;
271 272 273 274 275 276 277 278
		}

		/*
		 * We unlink the old file, to get the new one with the
		 * right permissions (including umask, which is nasty
		 * to emulate by hand - much easier to let the system
		 * just do the right thing)
		 */
279
		if (S_ISDIR(st.st_mode)) {
280
			/* If it is a gitlink, leave it alone! */
281
			if (S_ISGITLINK(ce->ce_mode))
282
				return 0;
283
			if (!state->force)
284
				return error("%s is a directory", path.buf);
285
			remove_subtree(&path);
286 287 288
		} else if (unlink(path.buf))
			return error("unable to unlink old '%s' (%s)",
				     path.buf, strerror(errno));
289
	} else if (state->not_new)
290
		return 0;
291 292 293

	create_directories(path.buf, path.len, state);
	return write_entry(ce, path.buf, state, 0);
294
}