attrcache.c 11 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], 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 *old = 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
		old = git_atomic_compare_and_swap(&entry->file[file->source], file, NULL);
140 141 142

	attr_cache_unlock(cache);

143 144 145
	if (old) {
		GIT_REFCOUNT_OWN(old, NULL);
		git_attr_file__free(old);
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
	const char *base,
163
	const char *filename)
164 165 166 167 168
{
	int error = 0;
	git_buf path = GIT_BUF_INIT;
	const char *wd = git_repository_workdir(repo), *relfile;
	git_attr_cache *cache = git_repository_attr_cache(repo);
169
	git_attr_file_entry *entry = NULL;
170 171 172 173
	git_attr_file *file = NULL;

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

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

		filename = p->ptr;
181 182 183 184 185 186 187 188 189 190
	}

	relfile = filename;
	if (wd && !git__prefixcmp(relfile, wd))
		relfile += strlen(wd);

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

191
	entry = attr_cache_lookup_entry(cache, relfile);
192 193 194
	if (!entry)
		error = attr_cache_make_entry(&entry, repo, relfile);
	else if (entry->file[source] != NULL) {
195
		file = entry->file[source];
196 197 198 199 200
		GIT_REFCOUNT_INC(file);
	}

	attr_cache_unlock(cache);

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

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

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

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

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

	/* if we loaded the file, insert into and/or update cache */
	if (updated) {
		if ((error = attr_cache_upsert(cache, updated)) < 0)
			git_attr_file__free(updated);
		else {
			git_attr_file__free(file); /* offset incref from lookup */
			file = updated;
		}
240 241
	}

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

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

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

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

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

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


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;
285
	git_config_entry *entry = NULL;
286 287 288 289 290 291 292 293 294 295

	*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 */
296 297 298 299
		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') {
			if (! (error = git_sysdir_expand_global_file(&buf, &cfgval[2])))
				*out = git_buf_detach(&buf);
		} else if (cfgval) {
300
			*out = git__strdup(cfgval);
301
		}
302
	}
303
	else if (!git_sysdir_find_xdg_file(&buf, fallback)) {
304
		*out = git_buf_detach(&buf);
305
	}
306

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

	return error;
}

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

317 318 319
	if (!cache)
		return;

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

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

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

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

	git__free(cache);
}

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

	if (cache)
		return 0;

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

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

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

384 385 386 387 388 389 390 391 392 393 394 395 396 397
	/* 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
	 */
398
	if ((ret = git_strmap_new(&cache->files)) < 0 ||
399 400
	    (ret = git_strmap_new(&cache->macros)) < 0 ||
	    (ret = git_pool_init(&cache->pool, 1)) < 0)
401 402
		goto cancel;

403
	cache = git_atomic_compare_and_swap(&repo->attrcache, NULL, cache);
404 405 406
	if (cache)
		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
}