attrcache.c 11.1 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "attrcache.h"

10 11 12 13 14 15 16 17 18 19 20
#include "repository.h"
#include "attr_file.h"
#include "config.h"
#include "sysdir.h"
#include "ignore.h"

GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache)
{
	GIT_UNUSED(cache); /* avoid warning if threading is off */

	if (git_mutex_lock(&cache->lock) < 0) {
21
		git_error_set(GIT_ERROR_OS, "unable to get attr cache lock");
22 23 24 25 26 27 28 29 30 31 32
		return -1;
	}
	return 0;
}

GIT_INLINE(void) attr_cache_unlock(git_attr_cache *cache)
{
	GIT_UNUSED(cache); /* avoid warning if threading is off */
	git_mutex_unlock(&cache->lock);
}

33
GIT_INLINE(git_attr_file_entry *) attr_cache_lookup_entry(
34 35
	git_attr_cache *cache, const char *path)
{
36
	return git_strmap_get(cache->files, path);
37 38
}

39 40
int git_attr_cache__alloc_file_entry(
	git_attr_file_entry **out,
41
	git_repository *repo,
42 43 44 45
	const char *base,
	const char *path,
	git_pool *pool)
{
46
	size_t baselen = 0, pathlen = strlen(path);
47 48
	size_t cachesize = sizeof(git_attr_file_entry) + pathlen + 1;
	git_attr_file_entry *ce;
49

50 51 52 53 54 55 56 57
	if (base != NULL && git_path_root(path) < 0) {
		baselen = strlen(base);
		cachesize += baselen;

		if (baselen && base[baselen - 1] != '/')
			cachesize++;
	}

58
	ce = git_pool_mallocz(pool, cachesize);
59
	GIT_ERROR_CHECK_ALLOC(ce);
60

61
	if (baselen) {
62
		memcpy(ce->fullpath, base, baselen);
63 64 65 66

		if (base[baselen - 1] != '/')
			ce->fullpath[baselen++] = '/';
	}
67
	memcpy(&ce->fullpath[baselen], path, pathlen);
68

69 70 71
	if (git_path_validate_workdir_with_len(repo, ce->fullpath, pathlen + baselen) < 0)
		return -1;

72 73 74 75 76 77 78 79
	ce->path = &ce->fullpath[baselen];
	*out = ce;

	return 0;
}

/* call with attrcache locked */
static int attr_cache_make_entry(
80
	git_attr_file_entry **out, git_repository *repo, const char *path)
81 82
{
	git_attr_cache *cache = git_repository_attr_cache(repo);
83
	git_attr_file_entry *entry = NULL;
84
	int error;
85

86 87
	if ((error = git_attr_cache__alloc_file_entry(&entry, repo,
		git_repository_workdir(repo), path, &cache->pool)) < 0)
88
		return error;
89

90 91
	if ((error = git_strmap_set(cache->files, entry->path, entry)) < 0)
		return error;
92

93
	*out = entry;
94 95 96 97 98 99
	return error;
}

/* insert entry or replace existing if we raced with another thread */
static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file)
{
100
	git_attr_file_entry *entry;
101 102 103 104 105
	git_attr_file *old;

	if (attr_cache_lock(cache) < 0)
		return -1;

106
	entry = attr_cache_lookup_entry(cache, file->entry->path);
107

108
	GIT_REFCOUNT_OWN(file, entry);
109
	GIT_REFCOUNT_INC(file);
110

111 112 113 114
	/*
	 * Replace the existing value if another thread has
	 * created it in the meantime.
	 */
115
	old = git_atomic_swap(entry->file[file->source.type], file);
116 117 118 119 120 121 122 123 124 125 126 127 128

	if (old) {
		GIT_REFCOUNT_OWN(old, NULL);
		git_attr_file__free(old);
	}

	attr_cache_unlock(cache);
	return 0;
}

static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
{
	int error = 0;
129
	git_attr_file_entry *entry;
130
	git_attr_file *oldfile = NULL;
131 132 133

	if (!file)
		return 0;
134

135 136 137
	if ((error = attr_cache_lock(cache)) < 0)
		return error;

138
	if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL)
139
		oldfile = git_atomic_compare_and_swap(&entry->file[file->source.type], file, NULL);
140 141 142

	attr_cache_unlock(cache);

143 144 145
	if (oldfile == file) {
		GIT_REFCOUNT_OWN(file, NULL);
		git_attr_file__free(file);
146
	}
147 148 149 150

	return error;
}

151 152 153 154 155
/* Look up cache entry and file.
 * - If entry is not present, create it while the cache is locked.
 * - If file is present, increment refcount before returning it, so the
 *   cache can be unlocked and it won't go away.
 */
156 157
static int attr_cache_lookup(
	git_attr_file **out_file,
158
	git_attr_file_entry **out_entry,
159
	git_repository *repo,
160
	git_attr_session *attr_session,
161
	git_attr_file_source *source)
162 163 164
{
	int error = 0;
	git_buf path = GIT_BUF_INIT;
165 166
	const char *wd = git_repository_workdir(repo);
	const char *filename;
167
	git_attr_cache *cache = git_repository_attr_cache(repo);
168
	git_attr_file_entry *entry = NULL;
169 170 171
	git_attr_file *file = NULL;

	/* join base and path as needed */
172
	if (source->base != NULL && git_path_root(source->filename) < 0) {
173 174
		git_buf *p = attr_session ? &attr_session->tmp : &path;

175
		if (git_buf_joinpath(p, source->base, source->filename) < 0 ||
176
		    git_path_validate_workdir_buf(repo, p) < 0)
177
			return -1;
178 179

		filename = p->ptr;
180 181
	} else {
		filename = source->filename;
182 183
	}

184 185
	if (wd && !git__prefixcmp(filename, wd))
		filename += strlen(wd);
186 187 188 189 190

	/* check cache for existing entry */
	if ((error = attr_cache_lock(cache)) < 0)
		goto cleanup;

191 192 193 194 195 196
	entry = attr_cache_lookup_entry(cache, filename);

	if (!entry) {
		error = attr_cache_make_entry(&entry, repo, filename);
	} else if (entry->file[source->type] != NULL) {
		file = entry->file[source->type];
197 198 199 200 201
		GIT_REFCOUNT_INC(file);
	}

	attr_cache_unlock(cache);

202
cleanup:
203 204
	*out_file  = file;
	*out_entry = entry;
205

206
	git_buf_dispose(&path);
207 208 209 210 211 212
	return error;
}

int git_attr_cache__get(
	git_attr_file **out,
	git_repository *repo,
213
	git_attr_session *attr_session,
214
	git_attr_file_source *source,
215 216
	git_attr_file_parser parser,
	bool allow_macros)
217 218 219
{
	int error = 0;
	git_attr_cache *cache = git_repository_attr_cache(repo);
220
	git_attr_file_entry *entry = NULL;
Russell Belfer committed
221
	git_attr_file *file = NULL, *updated = NULL;
222

223
	if ((error = attr_cache_lookup(&file, &entry, repo, attr_session, source)) < 0)
Russell Belfer committed
224
		return error;
225

Russell Belfer committed
226
	/* load file if we don't have one or if existing one is out of date */
227 228
	if (!file ||
	    (error = git_attr_file__out_of_date(repo, attr_session, file, source)) > 0)
229
		error = git_attr_file__load(&updated, repo, attr_session,
230
		                            entry, source, parser,
231
		                            allow_macros);
Russell Belfer committed
232 233 234

	/* if we loaded the file, insert into and/or update cache */
	if (updated) {
235
		if ((error = attr_cache_upsert(cache, updated)) < 0) {
Russell Belfer committed
236
			git_attr_file__free(updated);
237
		} else {
Russell Belfer committed
238 239 240
			git_attr_file__free(file); /* offset incref from lookup */
			file = updated;
		}
241 242
	}

Russell Belfer committed
243 244 245 246 247
	/* if file could not be loaded */
	if (error < 0) {
		/* remove existing entry */
		if (file) {
			attr_cache_remove(cache, file);
248
			git_attr_file__free(file); /* offset incref from lookup */
Russell Belfer committed
249 250 251 252
			file = NULL;
		}
		/* no error if file simply doesn't exist */
		if (error == GIT_ENOTFOUND) {
253
			git_error_clear();
Russell Belfer committed
254 255
			error = 0;
		}
256 257
	}

258
	*out = file;
259 260 261 262 263
	return error;
}

bool git_attr_cache__is_cached(
	git_repository *repo,
264
	git_attr_file_source_t source_type,
265 266 267
	const char *filename)
{
	git_attr_cache *cache = git_repository_attr_cache(repo);
268
	git_attr_file_entry *entry;
269
	git_strmap *files;
270

271
	if (!cache || !(files = cache->files))
272 273
		return false;

274
	if ((entry = git_strmap_get(files, filename)) == NULL)
275 276
		return false;

277
	return entry && (entry->file[source_type] != NULL);
278 279 280 281 282 283 284 285
}


static int attr_cache__lookup_path(
	char **out, git_config *cfg, const char *key, const char *fallback)
{
	git_buf buf = GIT_BUF_INIT;
	int error;
286
	git_config_entry *entry = NULL;
287 288 289 290 291 292 293 294 295 296

	*out = NULL;

	if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0)
		return error;

	if (entry) {
		const char *cfgval = entry->value;

		/* expand leading ~/ as needed */
297 298 299 300
		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') {
			if (! (error = git_sysdir_expand_global_file(&buf, &cfgval[2])))
				*out = git_buf_detach(&buf);
		} else if (cfgval) {
301
			*out = git__strdup(cfgval);
302
		}
303
	}
304
	else if (!git_sysdir_find_xdg_file(&buf, fallback)) {
305
		*out = git_buf_detach(&buf);
306
	}
307

308
	git_config_entry_free(entry);
309
	git_buf_dispose(&buf);
310 311 312 313 314 315

	return error;
}

static void attr_cache__free(git_attr_cache *cache)
{
316 317
	bool unlock;

318 319 320
	if (!cache)
		return;

321
	unlock = (attr_cache_lock(cache) == 0);
322

323
	if (cache->files != NULL) {
324 325
		git_attr_file_entry *entry;
		git_attr_file *file;
326 327
		int i;

328 329
		git_strmap_foreach_value(cache->files, entry, {
			for (i = 0; i < GIT_ATTR_FILE_NUM_SOURCES; ++i) {
330
				if ((file = git_atomic_swap(entry->file[i], NULL)) != NULL) {
331 332
					GIT_REFCOUNT_OWN(file, NULL);
					git_attr_file__free(file);
333 334
				}
			}
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
		});
		git_strmap_free(cache->files);
	}

	if (cache->macros != NULL) {
		git_attr_rule *rule;

		git_strmap_foreach_value(cache->macros, rule, {
			git_attr_rule__free(rule);
		});
		git_strmap_free(cache->macros);
	}

	git_pool_clear(&cache->pool);

	git__free(cache->cfg_attr_file);
	cache->cfg_attr_file = NULL;

	git__free(cache->cfg_excl_file);
	cache->cfg_excl_file = NULL;

356
	if (unlock)
357
		attr_cache_unlock(cache);
358 359 360 361 362
	git_mutex_free(&cache->lock);

	git__free(cache);
}

363
int git_attr_cache__init(git_repository *repo)
364 365 366
{
	int ret = 0;
	git_attr_cache *cache = git_repository_attr_cache(repo);
367
	git_config *cfg = NULL;
368 369 370 371 372

	if (cache)
		return 0;

	cache = git__calloc(1, sizeof(git_attr_cache));
373
	GIT_ERROR_CHECK_ALLOC(cache);
374 375 376

	/* set up lock */
	if (git_mutex_init(&cache->lock) < 0) {
377
		git_error_set(GIT_ERROR_OS, "unable to initialize lock for attr cache");
378 379 380 381
		git__free(cache);
		return -1;
	}

382 383 384
	if ((ret = git_repository_config_snapshot(&cfg, repo)) < 0)
		goto cancel;

385 386 387 388 389 390 391 392 393 394 395 396 397 398
	/* cache config settings for attributes and ignores */
	ret = attr_cache__lookup_path(
		&cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG);
	if (ret < 0)
		goto cancel;

	ret = attr_cache__lookup_path(
		&cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG);
	if (ret < 0)
		goto cancel;

	/* allocate hashtable for attribute and ignore file contents,
	 * hashtable for attribute macros, and string pool
	 */
399
	if ((ret = git_strmap_new(&cache->files)) < 0 ||
400 401
	    (ret = git_strmap_new(&cache->macros)) < 0 ||
	    (ret = git_pool_init(&cache->pool, 1)) < 0)
402 403
		goto cancel;

404
	if (git_atomic_compare_and_swap(&repo->attrcache, NULL, cache) != NULL)
405 406
		goto cancel; /* raced with another thread, free this but no error */

407 408
	git_config_free(cfg);

409
	/* insert default macros */
410
	return git_attr_add_macro(repo, "binary", "-diff -merge -text -crlf");
411 412 413

cancel:
	attr_cache__free(cache);
414
	git_config_free(cfg);
415 416 417
	return ret;
}

418
int git_attr_cache_flush(git_repository *repo)
419 420 421 422 423 424
{
	git_attr_cache *cache;

	/* this could be done less expensively, but for now, we'll just free
	 * the entire attrcache and let the next use reinitialize it...
	 */
425
	if (repo && (cache = git_atomic_swap(repo->attrcache, NULL)) != NULL)
426
		attr_cache__free(cache);
427 428

	return 0;
429 430 431 432 433
}

int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
{
	git_attr_cache *cache = git_repository_attr_cache(repo);
434 435 436
	git_attr_rule *preexisting;
	bool locked = false;
	int error = 0;
437

438 439 440 441 442 443 444 445 446 447
	/*
	 * Callers assume that if we return success, that the
	 * macro will have been adopted by the attributes cache.
	 * Thus, we have to free the macro here if it's not being
	 * added to the cache.
	 *
	 * TODO: generate warning log if (macro->assigns.length == 0)
	 */
	if (macro->assigns.length == 0) {
		git_attr_rule__free(macro);
448
		goto out;
449
	}
450

451 452 453 454 455 456
	if ((error = attr_cache_lock(cache)) < 0)
		goto out;
	locked = true;

	if ((preexisting = git_strmap_get(cache->macros, macro->match.pattern)) != NULL)
	    git_attr_rule__free(preexisting);
457

458 459 460 461 462 463
	if ((error = git_strmap_set(cache->macros, macro->match.pattern, macro)) < 0)
	    goto out;

out:
	if (locked)
		attr_cache_unlock(cache);
464
	return error;
465 466 467 468 469 470 471
}

git_attr_rule *git_attr_cache__lookup_macro(
	git_repository *repo, const char *name)
{
	git_strmap *macros = git_repository_attr_cache(repo)->macros;

472
	return git_strmap_get(macros, name);
473
}