backup-log.c 9.11 KB
Newer Older
1 2
#include "cache.h"
#include "backup-log.h"
3
#include "blob.h"
4
#include "lockfile.h"
5
#include "object-store.h"
6
#include "revision.h"
7
#include "strbuf.h"
Duy Nguyen's avatar
Duy Nguyen committed
8
#include "worktree.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

void bkl_append(struct strbuf *output, const char *path,
		const struct object_id *from,
		const struct object_id *to)
{
	if (oideq(from, to))
		return;

	/*
	 * Do paths with \n in them really exist? At least it's not
	 * often seen to justify the support. Just drop them otherwise
	 * we break the line-based format.
	 */
	if (strchr(path, '\n'))
		return;

	strbuf_addf(output, "%s %s %s\t%s\n", oid_to_hex(from),
		    oid_to_hex(to), git_committer_info(0), path);
}

static int bkl_write_unlocked(const char *path, struct strbuf *new_log)
{
	int fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0666);
	if (fd == -1)
		return error_errno(_("unable to open %s"), path);
	if (write_in_full(fd, new_log->buf, new_log->len) < 0) {
		close(fd);
		return error_errno(_("unable to update %s"), path);
	}
	close(fd);
	return 0;
}

int bkl_write(const char *path, struct strbuf *new_log)
{
	struct lock_file lk;
	int ret;

	ret = hold_lock_file_for_update(&lk, path, LOCK_REPORT_ON_ERROR);
	if (ret == -1)
		return -1;
	ret = bkl_write_unlocked(path, new_log);
	/*
	 * We do not write the the .lock file and append to the real one
	 * instead to reduce update cost. So we can't commit even in
	 * successful case.
	 */
	rollback_lock_file(&lk);
	return ret;
}
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

int bkl_parse_entry(struct strbuf *sb, struct bkl_entry *re)
{
	char *email_end, *message;
	const char *p = sb->buf;

	/* old SP new SP name <email> SP time TAB msg LF */
	if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
	    parse_oid_hex(p, &re->old_oid, &p) || *p++ != ' ' ||
	    parse_oid_hex(p, &re->new_oid, &p) || *p++ != ' ' ||
	    !(email_end = strchr(p, '>')) ||
	    email_end[1] != ' ')
		return -1;	/* corrupt? */
	re->email = p;
	re->timestamp = parse_timestamp(email_end + 2, &message, 10);
	if (!re->timestamp ||
	    !message || message[0] != ' ' ||
	    (message[1] != '+' && message[1] != '-') ||
	    !isdigit(message[2]) || !isdigit(message[3]) ||
	    !isdigit(message[4]) || !isdigit(message[5]))
		return -1; /* corrupt? */
	email_end[1] = '\0';
	re->tz = strtol(message + 1, NULL, 10);
	if (message[6] != '\t')
		message += 6;
	else
		message += 7;
	sb->buf[sb->len - 1] = '\0'; /* no LF */
	re->path = message;
	return 0;
}

static char *find_beginning_of_line(char *bob, char *scan)
{
	while (bob < scan && *(--scan) != '\n')
		; /* keep scanning backwards */
	/*
	 * Return either beginning of the buffer, or LF at the end of
	 * the previous line.
	 */
	return scan;
}

int bkl_parse_file_reverse(const char *path,
			   int (*parse)(struct strbuf *line, void *data),
			   void *data)
{
	struct strbuf sb = STRBUF_INIT;
	FILE *logfp;
	long pos;
	int ret = 0, at_tail = 1;

	logfp = fopen(path, "r");
	if (!logfp) {
		if (errno == ENOENT || errno == ENOTDIR)
			return 0;
		return -1;
	}

	/* Jump to the end */
	if (fseek(logfp, 0, SEEK_END) < 0)
		ret = error_errno(_("cannot seek back in %s"), path);
	pos = ftell(logfp);
	while (!ret && 0 < pos) {
		int cnt;
		size_t nread;
		char buf[BUFSIZ];
		char *endp, *scanp;

		/* Fill next block from the end */
		cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
		if (fseek(logfp, pos - cnt, SEEK_SET)) {
			ret = error_errno(_("cannot seek back in %s"), path);
			break;
		}
		nread = fread(buf, cnt, 1, logfp);
		if (nread != 1) {
			ret = error_errno(_("cannot read %d bytes from %s"),
					  cnt, path);
			break;
		}
		pos -= cnt;

		scanp = endp = buf + cnt;
		if (at_tail && scanp[-1] == '\n')
			/* Looking at the final LF at the end of the file */
			scanp--;
		at_tail = 0;

		while (buf < scanp) {
			/*
			 * terminating LF of the previous line, or the beginning
			 * of the buffer.
			 */
			char *bp;

			bp = find_beginning_of_line(buf, scanp);

			if (*bp == '\n') {
				/*
				 * The newline is the end of the previous line,
				 * so we know we have complete line starting
				 * at (bp + 1). Prefix it onto any prior data
				 * we collected for the line and process it.
				 */
				strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
				scanp = bp;
				endp = bp + 1;
				ret = parse(&sb, data);
				strbuf_reset(&sb);
				if (ret)
					break;
			} else if (!pos) {
				/*
				 * We are at the start of the buffer, and the
				 * start of the file; there is no previous
				 * line, and we have everything for this one.
				 * Process it, and we can end the loop.
				 */
				strbuf_splice(&sb, 0, 0, buf, endp - buf);
				ret = parse(&sb, data);
				strbuf_reset(&sb);
				break;
			}

			if (bp == buf) {
				/*
				 * We are at the start of the buffer, and there
				 * is more file to read backwards. Which means
				 * we are in the middle of a line. Note that we
				 * may get here even if *bp was a newline; that
				 * just means we are at the exact end of the
				 * previous line, rather than some spot in the
				 * middle.
				 *
				 * Save away what we have to be combined with
				 * the data from the next read.
				 */
				strbuf_splice(&sb, 0, 0, buf, endp - buf);
				break;
			}
		}

	}
	if (!ret && sb.len)
		BUG("reverse reflog parser had leftover data");

	fclose(logfp);
	strbuf_release(&sb);
	return ret;
}

int bkl_parse_file(const char *path,
		   int (*parse)(struct strbuf *line, void *data),
		   void *data)
{
	struct strbuf sb = STRBUF_INIT;
	FILE *logfp;
	int ret = 0;

	logfp = fopen(path, "r");
	if (!logfp) {
		if (errno == ENOENT || errno == ENOTDIR)
			return 0;
		return -1;
	}

	while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
		ret = parse(&sb, data);
	fclose(logfp);
	strbuf_release(&sb);
	return ret;
}
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

struct prune_options {
	struct repository *repo;
	FILE *fp;
	timestamp_t expire;
	struct strbuf copy;
};

static int good_oid(struct repository *r, const struct object_id *oid)
{
	if (is_null_oid(oid))
		return 0;

	return oid_object_info(r, oid, NULL) == OBJ_BLOB;
}

static int prune_parse(struct strbuf *line, void *data)
{
	struct prune_options *opts = data;
	struct bkl_entry entry;

	strbuf_reset(&opts->copy);
	strbuf_addbuf(&opts->copy, line);

	if (bkl_parse_entry(line, &entry))
		return -1;

	if (entry.timestamp < opts->expire)
		return 0;

	if (oideq(&entry.old_oid, &entry.new_oid))
		return 0;

	if (!good_oid(opts->repo, &entry.old_oid) ||
	    !good_oid(opts->repo, &entry.new_oid))
		return 0;

	if (!opts->fp)
		return -1;

	fputs(opts->copy.buf, opts->fp);
	return 0;
}

int bkl_prune(struct repository *r, const char *path, timestamp_t expire)
{
	struct lock_file lk;
	struct prune_options opts;
	int ret;

	ret = hold_lock_file_for_update(&lk, path, 0);
	if (ret == -1) {
		if (errno == ENOTDIR || errno == ENOENT)
			return 0;
		return error(_("failed to lock '%s'"), path);
	}
	opts.repo = r;
	opts.expire = expire;
	opts.fp = fdopen_lock_file(&lk, "w");
	strbuf_init(&opts.copy, 0);

	ret = bkl_parse_file(path, prune_parse, &opts);
	if (ret < 0)
		rollback_lock_file(&lk);
	else
		ret = commit_lock_file(&lk);
	strbuf_release(&opts.copy);
	return ret;
}
Duy Nguyen's avatar
Duy Nguyen committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

void bkl_prune_all_or_die(struct repository *r, timestamp_t expire)
{
	struct worktree **worktrees, **p;
	char *bkl_path;

	bkl_path = repo_git_path(r, "common/gitdir.bkl");
	if (bkl_prune(r, bkl_path, expire))
		die(_("failed to prune %s"), "gitdir.bkl");
	free(bkl_path);

	worktrees = get_worktrees(0);
	for (p = worktrees; *p; p++) {
		struct worktree *wt = *p;

316
		if (bkl_prune(r, worktree_git_path(r, wt, "index.bkl"), expire)) {
Duy Nguyen's avatar
Duy Nguyen committed
317 318 319 320 321 322
			if (wt->id)
				die(_("failed to prune %s on working tree '%s'"),
				    "index.bkl", wt->id);
			else
				die(_("failed to prune %s"), "index.bkl");
		}
323
		if (bkl_prune(r, worktree_git_path(r, wt, "worktree.bkl"), expire)) {
Duy Nguyen's avatar
Duy Nguyen committed
324 325 326 327 328 329 330 331 332
			if (wt->id)
				die(_("failed to prune %s on working tree '%s'"),
				    "worktree.bkl", wt->id);
			else
				die(_("failed to prune %s"), "worktree.bkl");
		}
	}
	free_worktrees(worktrees);
}
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

struct pending_cb {
	struct rev_info *revs;
	unsigned flags;
};

static void add_blob_to_pending(const struct object_id *oid,
				const char *path,
				struct pending_cb *cb)
{
	struct blob *blob;

	if (!good_oid(cb->revs->repo, oid))
		return;

	blob = lookup_blob(cb->revs->repo, oid);
	blob->object.flags |= cb->flags;
	add_pending_object(cb->revs, &blob->object, path);
}

static int add_pending(struct strbuf *line, void *cb)
{
	struct bkl_entry entry;

	if (bkl_parse_entry(line, &entry))
		return -1;

	add_blob_to_pending(&entry.old_oid, entry.path, cb);
	add_blob_to_pending(&entry.new_oid, entry.path, cb);
	return 0;
}

static void add_backup_log_to_pending(const char *path, struct pending_cb *cb)
{
	bkl_parse_file(path, add_pending, cb);
}

void add_backup_logs_to_pending(struct rev_info *revs, unsigned flags)
{
	struct worktree **worktrees, **p;
	char *path;
	struct pending_cb cb;

	cb.revs = revs;
	cb.flags = flags;

	worktrees = get_worktrees(0);
	for (p = worktrees; *p; p++) {
		struct worktree *wt = *p;

383
		path = xstrdup(worktree_git_path(revs->repo, wt, "index.bkl"));
384 385 386
		add_backup_log_to_pending(path, &cb);
		free(path);

387
		path = xstrdup(worktree_git_path(revs->repo, wt, "worktree.bkl"));
388 389 390 391 392 393 394 395 396
		add_backup_log_to_pending(path, &cb);
		free(path);
	}
	free_worktrees(worktrees);

	path = git_pathdup("common/gitdir.bkl");
	add_backup_log_to_pending(path, &cb);
	free(path);
}