attrcache.c 10.8 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 42 43 44
	const char *base,
	const char *path,
	git_pool *pool)
{
45
	size_t baselen = 0, pathlen = strlen(path);
46 47
	size_t cachesize = sizeof(git_attr_file_entry) + pathlen + 1;
	git_attr_file_entry *ce;
48

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

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

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

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

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

68 69 70 71 72 73 74 75
	ce->path = &ce->fullpath[baselen];
	*out = ce;

	return 0;
}

/* call with attrcache locked */
static int attr_cache_make_entry(
76
	git_attr_file_entry **out, git_repository *repo, const char *path)
77 78
{
	git_attr_cache *cache = git_repository_attr_cache(repo);
79
	git_attr_file_entry *entry = NULL;
80
	int error;
81

82 83 84
	if ((error = git_attr_cache__alloc_file_entry(&entry, git_repository_workdir(repo),
						      path, &cache->pool)) < 0)
		return error;
85

86 87
	if ((error = git_strmap_set(cache->files, entry->path, entry)) < 0)
		return error;
88

89
	*out = entry;
90 91 92 93 94 95
	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)
{
96
	git_attr_file_entry *entry;
97 98 99 100 101
	git_attr_file *old;

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

102
	entry = attr_cache_lookup_entry(cache, file->entry->path);
103

104
	GIT_REFCOUNT_OWN(file, entry);
105
	GIT_REFCOUNT_INC(file);
106

107 108 109 110 111
	/*
	 * Replace the existing value if another thread has
	 * created it in the meantime.
	 */
	old = git__swap(entry->file[file->source], file);
112 113 114 115 116 117 118 119 120 121 122 123 124

	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;
125
	git_attr_file_entry *entry;
126
	git_attr_file *old = NULL;
127 128 129

	if (!file)
		return 0;
130

131 132 133
	if ((error = attr_cache_lock(cache)) < 0)
		return error;

134
	if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL)
135
		old = git__compare_and_swap(&entry->file[file->source], file, NULL);
136 137 138

	attr_cache_unlock(cache);

139 140 141
	if (old) {
		GIT_REFCOUNT_OWN(old, NULL);
		git_attr_file__free(old);
142
	}
143 144 145 146

	return error;
}

147 148 149 150 151
/* 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.
 */
152 153
static int attr_cache_lookup(
	git_attr_file **out_file,
154
	git_attr_file_entry **out_entry,
155
	git_repository *repo,
156
	git_attr_session *attr_session,
157
	git_attr_file_source source,
158
	const char *base,
159
	const char *filename)
160 161 162 163 164
{
	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);
165
	git_attr_file_entry *entry = NULL;
166 167 168 169
	git_attr_file *file = NULL;

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

		if (git_buf_joinpath(p, base, filename) < 0)
173
			return -1;
174 175

		filename = p->ptr;
176 177 178 179 180 181 182 183 184 185
	}

	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;

186
	entry = attr_cache_lookup_entry(cache, relfile);
187 188 189
	if (!entry)
		error = attr_cache_make_entry(&entry, repo, relfile);
	else if (entry->file[source] != NULL) {
190
		file = entry->file[source];
191 192 193 194 195
		GIT_REFCOUNT_INC(file);
	}

	attr_cache_unlock(cache);

196
cleanup:
197 198
	*out_file  = file;
	*out_entry = entry;
199

200
	git_buf_dispose(&path);
201 202 203 204 205 206
	return error;
}

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

219
	if ((error = attr_cache_lookup(
220
			&file, &entry, repo, attr_session, source, base, filename)) < 0)
Russell Belfer committed
221
		return error;
222

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

	/* 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;
		}
235 236
	}

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

252
	*out = file;
253 254 255 256 257
	return error;
}

bool git_attr_cache__is_cached(
	git_repository *repo,
258
	git_attr_file_source source,
259 260 261
	const char *filename)
{
	git_attr_cache *cache = git_repository_attr_cache(repo);
262
	git_attr_file_entry *entry;
263
	git_strmap *files;
264

265
	if (!cache || !(files = cache->files))
266 267
		return false;

268
	if ((entry = git_strmap_get(files, filename)) == NULL)
269 270
		return false;

271
	return entry && (entry->file[source] != NULL);
272 273 274 275 276 277 278 279
}


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;
280
	git_config_entry *entry = NULL;
281 282 283 284 285 286 287 288 289 290

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

302
	git_config_entry_free(entry);
303
	git_buf_dispose(&buf);
304 305 306 307 308 309

	return error;
}

static void attr_cache__free(git_attr_cache *cache)
{
310 311
	bool unlock;

312 313 314
	if (!cache)
		return;

315
	unlock = (attr_cache_lock(cache) == 0);
316

317
	if (cache->files != NULL) {
318 319
		git_attr_file_entry *entry;
		git_attr_file *file;
320 321
		int i;

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

350
	if (unlock)
351
		attr_cache_unlock(cache);
352 353 354 355 356
	git_mutex_free(&cache->lock);

	git__free(cache);
}

357
int git_attr_cache__init(git_repository *repo)
358 359 360
{
	int ret = 0;
	git_attr_cache *cache = git_repository_attr_cache(repo);
361
	git_config *cfg = NULL;
362 363 364 365 366

	if (cache)
		return 0;

	cache = git__calloc(1, sizeof(git_attr_cache));
367
	GIT_ERROR_CHECK_ALLOC(cache);
368 369 370

	/* set up lock */
	if (git_mutex_init(&cache->lock) < 0) {
371
		git_error_set(GIT_ERROR_OS, "unable to initialize lock for attr cache");
372 373 374 375
		git__free(cache);
		return -1;
	}

376 377 378
	if ((ret = git_repository_config_snapshot(&cfg, repo)) < 0)
		goto cancel;

379 380 381 382 383 384 385 386 387 388 389 390 391 392
	/* 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
	 */
393 394
	if ((ret = git_strmap_new(&cache->files)) < 0 ||
	    (ret = git_strmap_new(&cache->macros)) < 0)
395 396
		goto cancel;

397 398
	git_pool_init(&cache->pool, 1);

399 400 401 402
	cache = git__compare_and_swap(&repo->attrcache, NULL, cache);
	if (cache)
		goto cancel; /* raced with another thread, free this but no error */

403 404
	git_config_free(cfg);

405
	/* insert default macros */
406
	return git_attr_add_macro(repo, "binary", "-diff -merge -text -crlf");
407 408 409

cancel:
	attr_cache__free(cache);
410
	git_config_free(cfg);
411 412 413
	return ret;
}

414
int git_attr_cache_flush(git_repository *repo)
415 416 417 418 419 420 421 422
{
	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...
	 */
	if (repo && (cache = git__swap(repo->attrcache, NULL)) != NULL)
		attr_cache__free(cache);
423 424

	return 0;
425 426 427 428 429
}

int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
{
	git_attr_cache *cache = git_repository_attr_cache(repo);
430 431 432
	git_attr_rule *preexisting;
	bool locked = false;
	int error = 0;
433

434 435 436 437 438 439 440 441 442 443
	/*
	 * 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);
444
		goto out;
445
	}
446

447 448 449 450 451 452
	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);
453

454 455 456 457 458 459
	if ((error = git_strmap_set(cache->macros, macro->match.pattern, macro)) < 0)
	    goto out;

out:
	if (locked)
		attr_cache_unlock(cache);
460
	return error;
461 462 463 464 465 466 467
}

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

468
	return git_strmap_get(macros, name);
469
}