1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 
8 #include "attr_file.h"
9 
10 #include "repository.h"
11 #include "filebuf.h"
12 #include "attrcache.h"
13 #include "buf_text.h"
14 #include "git2/blob.h"
15 #include "git2/tree.h"
16 #include "blob.h"
17 #include "index.h"
18 #include "wildmatch.h"
19 #include <ctype.h>
20 
attr_file_free(git_attr_file * file)21 static void attr_file_free(git_attr_file *file)
22 {
23 	bool unlock = !git_mutex_lock(&file->lock);
24 	git_attr_file__clear_rules(file, false);
25 	git_pool_clear(&file->pool);
26 	if (unlock)
27 		git_mutex_unlock(&file->lock);
28 	git_mutex_free(&file->lock);
29 
30 	git__memzero(file, sizeof(*file));
31 	git__free(file);
32 }
33 
git_attr_file__new(git_attr_file ** out,git_attr_file_entry * entry,git_attr_file_source source)34 int git_attr_file__new(
35 	git_attr_file **out,
36 	git_attr_file_entry *entry,
37 	git_attr_file_source source)
38 {
39 	git_attr_file *attrs = git__calloc(1, sizeof(git_attr_file));
40 	GIT_ERROR_CHECK_ALLOC(attrs);
41 
42 	if (git_mutex_init(&attrs->lock) < 0) {
43 		git_error_set(GIT_ERROR_OS, "failed to initialize lock");
44 		goto on_error;
45 	}
46 
47 	if (git_pool_init(&attrs->pool, 1) < 0)
48 		goto on_error;
49 
50 	GIT_REFCOUNT_INC(attrs);
51 	attrs->entry  = entry;
52 	attrs->source = source;
53 	*out = attrs;
54 	return 0;
55 
56 on_error:
57 	git__free(attrs);
58 	return -1;
59 }
60 
git_attr_file__clear_rules(git_attr_file * file,bool need_lock)61 int git_attr_file__clear_rules(git_attr_file *file, bool need_lock)
62 {
63 	unsigned int i;
64 	git_attr_rule *rule;
65 
66 	if (need_lock && git_mutex_lock(&file->lock) < 0) {
67 		git_error_set(GIT_ERROR_OS, "failed to lock attribute file");
68 		return -1;
69 	}
70 
71 	git_vector_foreach(&file->rules, i, rule)
72 		git_attr_rule__free(rule);
73 	git_vector_free(&file->rules);
74 
75 	if (need_lock)
76 		git_mutex_unlock(&file->lock);
77 
78 	return 0;
79 }
80 
git_attr_file__free(git_attr_file * file)81 void git_attr_file__free(git_attr_file *file)
82 {
83 	if (!file)
84 		return;
85 	GIT_REFCOUNT_DEC(file, attr_file_free);
86 }
87 
attr_file_oid_from_index(git_oid * oid,git_repository * repo,const char * path)88 static int attr_file_oid_from_index(
89 	git_oid *oid, git_repository *repo, const char *path)
90 {
91 	int error;
92 	git_index *idx;
93 	size_t pos;
94 	const git_index_entry *entry;
95 
96 	if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
97 		(error = git_index__find_pos(&pos, idx, path, 0, 0)) < 0)
98 		return error;
99 
100 	if (!(entry = git_index_get_byindex(idx, pos)))
101 		return GIT_ENOTFOUND;
102 
103 	*oid = entry->id;
104 	return 0;
105 }
106 
git_attr_file__load(git_attr_file ** out,git_repository * repo,git_attr_session * attr_session,git_attr_file_entry * entry,git_attr_file_source source,git_attr_file_parser parser,bool allow_macros)107 int git_attr_file__load(
108 	git_attr_file **out,
109 	git_repository *repo,
110 	git_attr_session *attr_session,
111 	git_attr_file_entry *entry,
112 	git_attr_file_source source,
113 	git_attr_file_parser parser,
114 	bool allow_macros)
115 {
116 	int error = 0;
117 	git_tree *tree = NULL;
118 	git_tree_entry *tree_entry = NULL;
119 	git_blob *blob = NULL;
120 	git_buf content = GIT_BUF_INIT;
121 	const char *content_str;
122 	git_attr_file *file;
123 	struct stat st;
124 	bool nonexistent = false;
125 	int bom_offset;
126 	git_bom_t bom;
127 	git_oid id;
128 	git_object_size_t blobsize;
129 
130 	*out = NULL;
131 
132 	switch (source) {
133 	case GIT_ATTR_FILE__IN_MEMORY:
134 		/* in-memory attribute file doesn't need data */
135 		break;
136 	case GIT_ATTR_FILE__FROM_INDEX: {
137 		if ((error = attr_file_oid_from_index(&id, repo, entry->path)) < 0 ||
138 			(error = git_blob_lookup(&blob, repo, &id)) < 0)
139 			return error;
140 
141 		/* Do not assume that data straight from the ODB is NULL-terminated;
142 		 * copy the contents of a file to a buffer to work on */
143 		blobsize = git_blob_rawsize(blob);
144 
145 		GIT_ERROR_CHECK_BLOBSIZE(blobsize);
146 		git_buf_put(&content, git_blob_rawcontent(blob), (size_t)blobsize);
147 		break;
148 	}
149 	case GIT_ATTR_FILE__FROM_FILE: {
150 		int fd = -1;
151 
152 		/* For open or read errors, pretend that we got ENOTFOUND. */
153 		/* TODO: issue warning when warning API is available */
154 
155 		if (p_stat(entry->fullpath, &st) < 0 ||
156 			S_ISDIR(st.st_mode) ||
157 			(fd = git_futils_open_ro(entry->fullpath)) < 0 ||
158 			(error = git_futils_readbuffer_fd(&content, fd, (size_t)st.st_size)) < 0)
159 			nonexistent = true;
160 
161 		if (fd >= 0)
162 			p_close(fd);
163 
164 		break;
165 	}
166 	case GIT_ATTR_FILE__FROM_HEAD: {
167 		if ((error = git_repository_head_tree(&tree, repo)) < 0 ||
168 		    (error = git_tree_entry_bypath(&tree_entry, tree, entry->path)) < 0 ||
169 		    (error = git_blob_lookup(&blob, repo, git_tree_entry_id(tree_entry))) < 0)
170 			goto cleanup;
171 
172 		/*
173 		 * Do not assume that data straight from the ODB is NULL-terminated;
174 		 * copy the contents of a file to a buffer to work on.
175 		 */
176 		blobsize = git_blob_rawsize(blob);
177 
178 		GIT_ERROR_CHECK_BLOBSIZE(blobsize);
179 		if ((error = git_buf_put(&content,
180 			git_blob_rawcontent(blob), (size_t)blobsize)) < 0)
181 			goto cleanup;
182 
183 		break;
184 	}
185 	default:
186 		git_error_set(GIT_ERROR_INVALID, "unknown file source %d", source);
187 		return -1;
188 	}
189 
190 	if ((error = git_attr_file__new(&file, entry, source)) < 0)
191 		goto cleanup;
192 
193 	/* advance over a UTF8 BOM */
194 	content_str = git_buf_cstr(&content);
195 	bom_offset = git_buf_text_detect_bom(&bom, &content);
196 
197 	if (bom == GIT_BOM_UTF8)
198 		content_str += bom_offset;
199 
200 	/* store the key of the attr_reader; don't bother with cache
201 	 * invalidation during the same attr reader session.
202 	 */
203 	if (attr_session)
204 		file->session_key = attr_session->key;
205 
206 	if (parser && (error = parser(repo, file, content_str, allow_macros)) < 0) {
207 		git_attr_file__free(file);
208 		goto cleanup;
209 	}
210 
211 	/* write cache breakers */
212 	if (nonexistent)
213 		file->nonexistent = 1;
214 	else if (source == GIT_ATTR_FILE__FROM_INDEX)
215 		git_oid_cpy(&file->cache_data.oid, git_blob_id(blob));
216 	else if (source == GIT_ATTR_FILE__FROM_HEAD)
217 		git_oid_cpy(&file->cache_data.oid, git_tree_id(tree));
218 	else if (source == GIT_ATTR_FILE__FROM_FILE)
219 		git_futils_filestamp_set_from_stat(&file->cache_data.stamp, &st);
220 	/* else always cacheable */
221 
222 	*out = file;
223 
224 cleanup:
225 	git_blob_free(blob);
226 	git_tree_entry_free(tree_entry);
227 	git_tree_free(tree);
228 	git_buf_dispose(&content);
229 
230 	return error;
231 }
232 
git_attr_file__out_of_date(git_repository * repo,git_attr_session * attr_session,git_attr_file * file)233 int git_attr_file__out_of_date(
234 	git_repository *repo,
235 	git_attr_session *attr_session,
236 	git_attr_file *file)
237 {
238 	if (!file)
239 		return 1;
240 
241 	/* we are never out of date if we just created this data in the same
242 	 * attr_session; otherwise, nonexistent files must be invalidated
243 	 */
244 	if (attr_session && attr_session->key == file->session_key)
245 		return 0;
246 	else if (file->nonexistent)
247 		return 1;
248 
249 	switch (file->source) {
250 	case GIT_ATTR_FILE__IN_MEMORY:
251 		return 0;
252 
253 	case GIT_ATTR_FILE__FROM_FILE:
254 		return git_futils_filestamp_check(
255 			&file->cache_data.stamp, file->entry->fullpath);
256 
257 	case GIT_ATTR_FILE__FROM_INDEX: {
258 		int error;
259 		git_oid id;
260 
261 		if ((error = attr_file_oid_from_index(
262 				&id, repo, file->entry->path)) < 0)
263 			return error;
264 
265 		return (git_oid__cmp(&file->cache_data.oid, &id) != 0);
266 	}
267 
268 	case GIT_ATTR_FILE__FROM_HEAD: {
269 		git_tree *tree;
270 		int error;
271 
272 		if ((error = git_repository_head_tree(&tree, repo)) < 0)
273 			return error;
274 
275 		error = git_oid__cmp(&file->cache_data.oid, git_tree_id(tree));
276 
277 		git_tree_free(tree);
278 		return error;
279 	}
280 
281 	default:
282 		git_error_set(GIT_ERROR_INVALID, "invalid file type %d", file->source);
283 		return -1;
284 	}
285 }
286 
287 static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
288 static void git_attr_rule__clear(git_attr_rule *rule);
289 static bool parse_optimized_patterns(
290 	git_attr_fnmatch *spec,
291 	git_pool *pool,
292 	const char *pattern);
293 
git_attr_file__parse_buffer(git_repository * repo,git_attr_file * attrs,const char * data,bool allow_macros)294 int git_attr_file__parse_buffer(
295 	git_repository *repo, git_attr_file *attrs, const char *data, bool allow_macros)
296 {
297 	const char *scan = data, *context = NULL;
298 	git_attr_rule *rule = NULL;
299 	int error = 0;
300 
301 	/* If subdir file path, convert context for file paths */
302 	if (attrs->entry && git_path_root(attrs->entry->path) < 0 &&
303 	    !git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
304 		context = attrs->entry->path;
305 
306 	if (git_mutex_lock(&attrs->lock) < 0) {
307 		git_error_set(GIT_ERROR_OS, "failed to lock attribute file");
308 		return -1;
309 	}
310 
311 	while (!error && *scan) {
312 		/* Allocate rule if needed, otherwise re-use previous rule */
313 		if (!rule) {
314 			rule = git__calloc(1, sizeof(*rule));
315 			GIT_ERROR_CHECK_ALLOC(rule);
316 		} else
317 			git_attr_rule__clear(rule);
318 
319 		rule->match.flags = GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO;
320 
321 		/* Parse the next "pattern attr attr attr" line */
322 		if ((error = git_attr_fnmatch__parse(&rule->match, &attrs->pool, context, &scan)) < 0 ||
323 		    (error = git_attr_assignment__parse(repo, &attrs->pool, &rule->assigns, &scan)) < 0)
324 		{
325 			if (error != GIT_ENOTFOUND)
326 				goto out;
327 			error = 0;
328 			continue;
329 		}
330 
331 		if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO) {
332 			/* TODO: warning if macro found in file below repo root */
333 			if (!allow_macros)
334 				continue;
335 			if ((error = git_attr_cache__insert_macro(repo, rule)) < 0)
336 				goto out;
337 		} else if ((error = git_vector_insert(&attrs->rules, rule)) < 0)
338 			goto out;
339 
340 		rule = NULL;
341 	}
342 
343 out:
344 	git_mutex_unlock(&attrs->lock);
345 	git_attr_rule__free(rule);
346 
347 	return error;
348 }
349 
git_attr_file__name_hash(const char * name)350 uint32_t git_attr_file__name_hash(const char *name)
351 {
352 	uint32_t h = 5381;
353 	int c;
354 
355 	GIT_ASSERT_ARG(name);
356 
357 	while ((c = (int)*name++) != 0)
358 		h = ((h << 5) + h) + c;
359 	return h;
360 }
361 
git_attr_file__lookup_one(git_attr_file * file,git_attr_path * path,const char * attr,const char ** value)362 int git_attr_file__lookup_one(
363 	git_attr_file *file,
364 	git_attr_path *path,
365 	const char *attr,
366 	const char **value)
367 {
368 	size_t i;
369 	git_attr_name name;
370 	git_attr_rule *rule;
371 
372 	*value = NULL;
373 
374 	name.name = attr;
375 	name.name_hash = git_attr_file__name_hash(attr);
376 
377 	git_attr_file__foreach_matching_rule(file, path, i, rule) {
378 		size_t pos;
379 
380 		if (!git_vector_bsearch(&pos, &rule->assigns, &name)) {
381 			*value = ((git_attr_assignment *)
382 					  git_vector_get(&rule->assigns, pos))->value;
383 			break;
384 		}
385 	}
386 
387 	return 0;
388 }
389 
git_attr_file__load_standalone(git_attr_file ** out,const char * path)390 int git_attr_file__load_standalone(git_attr_file **out, const char *path)
391 {
392 	git_buf content = GIT_BUF_INIT;
393 	git_attr_file *file = NULL;
394 	int error;
395 
396 	if ((error = git_futils_readbuffer(&content, path)) < 0)
397 		goto out;
398 
399 	/*
400 	 * Because the cache entry is allocated from the file's own pool, we
401 	 * don't have to free it - freeing file+pool will free cache entry, too.
402 	 */
403 
404 	if ((error = git_attr_file__new(&file, NULL, GIT_ATTR_FILE__FROM_FILE)) < 0 ||
405 	    (error = git_attr_file__parse_buffer(NULL, file, content.ptr, true)) < 0 ||
406 	    (error = git_attr_cache__alloc_file_entry(&file->entry, NULL, path, &file->pool)) < 0)
407 		goto out;
408 
409 	*out = file;
410 out:
411 	if (error < 0)
412 		git_attr_file__free(file);
413 	git_buf_dispose(&content);
414 
415 	return error;
416 }
417 
git_attr_fnmatch__match(git_attr_fnmatch * match,git_attr_path * path)418 bool git_attr_fnmatch__match(
419 	git_attr_fnmatch *match,
420 	git_attr_path *path)
421 {
422 	const char *relpath = path->path;
423 	const char *filename;
424 	int flags = 0;
425 
426 	/*
427 	 * If the rule was generated in a subdirectory, we must only
428 	 * use it for paths inside that directory. We can thus return
429 	 * a non-match if the prefixes don't match.
430 	 */
431 	if (match->containing_dir) {
432 		if (match->flags & GIT_ATTR_FNMATCH_ICASE) {
433 			if (git__strncasecmp(path->path, match->containing_dir, match->containing_dir_length))
434 				return 0;
435 		} else {
436 			if (git__prefixcmp(path->path, match->containing_dir))
437 				return 0;
438 		}
439 
440 		relpath += match->containing_dir_length;
441 	}
442 
443 	if (match->flags & GIT_ATTR_FNMATCH_ICASE)
444 		flags |= WM_CASEFOLD;
445 
446 	if (match->flags & GIT_ATTR_FNMATCH_FULLPATH) {
447 		filename = relpath;
448 		flags |= WM_PATHNAME;
449 	} else {
450 		filename = path->basename;
451 	}
452 
453 	if ((match->flags & GIT_ATTR_FNMATCH_DIRECTORY) && !path->is_dir) {
454 		bool samename;
455 
456 		/*
457 		 * for attribute checks or checks at the root of this match's
458 		 * containing_dir (or root of the repository if no containing_dir),
459 		 * do not match.
460 		 */
461 		if (!(match->flags & GIT_ATTR_FNMATCH_IGNORE) ||
462 			path->basename == relpath)
463 			return false;
464 
465 		/* fail match if this is a file with same name as ignored folder */
466 		samename = (match->flags & GIT_ATTR_FNMATCH_ICASE) ?
467 			!strcasecmp(match->pattern, relpath) :
468 			!strcmp(match->pattern, relpath);
469 
470 		if (samename)
471 			return false;
472 
473 		return (wildmatch(match->pattern, relpath, flags) == WM_MATCH);
474 	}
475 
476 	return (wildmatch(match->pattern, filename, flags) == WM_MATCH);
477 }
478 
git_attr_rule__match(git_attr_rule * rule,git_attr_path * path)479 bool git_attr_rule__match(
480 	git_attr_rule *rule,
481 	git_attr_path *path)
482 {
483 	bool matched = git_attr_fnmatch__match(&rule->match, path);
484 
485 	if (rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE)
486 		matched = !matched;
487 
488 	return matched;
489 }
490 
git_attr_rule__lookup_assignment(git_attr_rule * rule,const char * name)491 git_attr_assignment *git_attr_rule__lookup_assignment(
492 	git_attr_rule *rule, const char *name)
493 {
494 	size_t pos;
495 	git_attr_name key;
496 	key.name = name;
497 	key.name_hash = git_attr_file__name_hash(name);
498 
499 	if (git_vector_bsearch(&pos, &rule->assigns, &key))
500 		return NULL;
501 
502 	return git_vector_get(&rule->assigns, pos);
503 }
504 
git_attr_path__init(git_attr_path * info,const char * path,const char * base,git_dir_flag dir_flag)505 int git_attr_path__init(
506 	git_attr_path *info, const char *path, const char *base, git_dir_flag dir_flag)
507 {
508 	ssize_t root;
509 
510 	/* build full path as best we can */
511 	git_buf_init(&info->full, 0);
512 
513 	if (git_path_join_unrooted(&info->full, path, base, &root) < 0)
514 		return -1;
515 
516 	info->path = info->full.ptr + root;
517 
518 	/* remove trailing slashes */
519 	while (info->full.size > 0) {
520 		if (info->full.ptr[info->full.size - 1] != '/')
521 			break;
522 		info->full.size--;
523 	}
524 	info->full.ptr[info->full.size] = '\0';
525 
526 	/* skip leading slashes in path */
527 	while (*info->path == '/')
528 		info->path++;
529 
530 	/* find trailing basename component */
531 	info->basename = strrchr(info->path, '/');
532 	if (info->basename)
533 		info->basename++;
534 	if (!info->basename || !*info->basename)
535 		info->basename = info->path;
536 
537 	switch (dir_flag)
538 	{
539 	case GIT_DIR_FLAG_FALSE:
540 		info->is_dir = 0;
541 		break;
542 
543 	case GIT_DIR_FLAG_TRUE:
544 		info->is_dir = 1;
545 		break;
546 
547 	case GIT_DIR_FLAG_UNKNOWN:
548 	default:
549 		info->is_dir = (int)git_path_isdir(info->full.ptr);
550 		break;
551 	}
552 
553 	return 0;
554 }
555 
git_attr_path__free(git_attr_path * info)556 void git_attr_path__free(git_attr_path *info)
557 {
558 	git_buf_dispose(&info->full);
559 	info->path = NULL;
560 	info->basename = NULL;
561 }
562 
563 /*
564  * From gitattributes(5):
565  *
566  * Patterns have the following format:
567  *
568  * - A blank line matches no files, so it can serve as a separator for
569  *   readability.
570  *
571  * - A line starting with # serves as a comment.
572  *
573  * - An optional prefix ! which negates the pattern; any matching file
574  *   excluded by a previous pattern will become included again. If a negated
575  *   pattern matches, this will override lower precedence patterns sources.
576  *
577  * - If the pattern ends with a slash, it is removed for the purpose of the
578  *   following description, but it would only find a match with a directory. In
579  *   other words, foo/ will match a directory foo and paths underneath it, but
580  *   will not match a regular file or a symbolic link foo (this is consistent
581  *   with the way how pathspec works in general in git).
582  *
583  * - If the pattern does not contain a slash /, git treats it as a shell glob
584  *   pattern and checks for a match against the pathname without leading
585  *   directories.
586  *
587  * - Otherwise, git treats the pattern as a shell glob suitable for consumption
588  *   by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
589  *   not match a / in the pathname. For example, "Documentation/\*.html" matches
590  *   "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
591  *   slash matches the beginning of the pathname; for example, "/\*.c" matches
592  *   "cat-file.c" but not "mozilla-sha1/sha1.c".
593  */
594 
595 /*
596  * Determine the length of trailing spaces. Escaped spaces do not count as
597  * trailing whitespace.
598  */
trailing_space_length(const char * p,size_t len)599 static size_t trailing_space_length(const char *p, size_t len)
600 {
601 	size_t n, i;
602 	for (n = len; n; n--) {
603 		if (p[n-1] != ' ' && p[n-1] != '\t')
604 			break;
605 
606 		/*
607 		 * Count escape-characters before space. In case where it's an
608 		 * even number of escape characters, then the escape char itself
609 		 * is escaped and the whitespace is an unescaped whitespace.
610 		 * Otherwise, the last escape char is not escaped and the
611 		 * whitespace in an escaped whitespace.
612 		 */
613 		i = n;
614 		while (i > 1 && p[i-2] == '\\')
615 			i--;
616 		if ((n - i) % 2)
617 			break;
618 	}
619 	return len - n;
620 }
621 
unescape_spaces(char * str)622 static size_t unescape_spaces(char *str)
623 {
624 	char *scan, *pos = str;
625 	bool escaped = false;
626 
627 	if (!str)
628 		return 0;
629 
630 	for (scan = str; *scan; scan++) {
631 		if (!escaped && *scan == '\\') {
632 			escaped = true;
633 			continue;
634 		}
635 
636 		/* Only insert the escape character for escaped non-spaces */
637 		if (escaped && !git__isspace(*scan))
638 			*pos++ = '\\';
639 
640 		*pos++ = *scan;
641 		escaped = false;
642 	}
643 
644 	if (pos != scan)
645 		*pos = '\0';
646 
647 	return (pos - str);
648 }
649 
650 /*
651  * This will return 0 if the spec was filled out,
652  * GIT_ENOTFOUND if the fnmatch does not require matching, or
653  * another error code there was an actual problem.
654  */
git_attr_fnmatch__parse(git_attr_fnmatch * spec,git_pool * pool,const char * context,const char ** base)655 int git_attr_fnmatch__parse(
656 	git_attr_fnmatch *spec,
657 	git_pool *pool,
658 	const char *context,
659 	const char **base)
660 {
661 	const char *pattern, *scan;
662 	int slash_count, allow_space;
663 	bool escaped;
664 
665 	GIT_ASSERT_ARG(spec);
666 	GIT_ASSERT_ARG(base && *base);
667 
668 	if (parse_optimized_patterns(spec, pool, *base))
669 		return 0;
670 
671 	spec->flags = (spec->flags & GIT_ATTR_FNMATCH__INCOMING);
672 	allow_space = ((spec->flags & GIT_ATTR_FNMATCH_ALLOWSPACE) != 0);
673 
674 	pattern = *base;
675 
676 	while (!allow_space && git__isspace(*pattern))
677 		pattern++;
678 
679 	if (!*pattern || *pattern == '#' || *pattern == '\n' ||
680 	    (*pattern == '\r' && *(pattern + 1) == '\n')) {
681 		*base = git__next_line(pattern);
682 		return GIT_ENOTFOUND;
683 	}
684 
685 	if (*pattern == '[' && (spec->flags & GIT_ATTR_FNMATCH_ALLOWMACRO) != 0) {
686 		if (strncmp(pattern, "[attr]", 6) == 0) {
687 			spec->flags = spec->flags | GIT_ATTR_FNMATCH_MACRO;
688 			pattern += 6;
689 		}
690 		/* else a character range like [a-e]* which is accepted */
691 	}
692 
693 	if (*pattern == '!' && (spec->flags & GIT_ATTR_FNMATCH_ALLOWNEG) != 0) {
694 		spec->flags = spec->flags | GIT_ATTR_FNMATCH_NEGATIVE;
695 		pattern++;
696 	}
697 
698 	slash_count = 0;
699 	escaped = false;
700 	/* Scan until a non-escaped whitespace. */
701 	for (scan = pattern; *scan != '\0'; ++scan) {
702 		char c = *scan;
703 
704 		if (c == '\\' && !escaped) {
705 			escaped = true;
706 			continue;
707 		} else if (git__isspace(c) && !escaped) {
708 			if (!allow_space || (c != ' ' && c != '\t' && c != '\r'))
709 				break;
710 		} else if (c == '/') {
711 			spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH;
712 			slash_count++;
713 
714 			if (slash_count == 1 && pattern == scan)
715 				pattern++;
716 		} else if (git__iswildcard(c) && !escaped) {
717 			/* remember if we see an unescaped wildcard in pattern */
718 			spec->flags = spec->flags | GIT_ATTR_FNMATCH_HASWILD;
719 		}
720 
721 		escaped = false;
722 	}
723 
724 	*base = scan;
725 
726 	if ((spec->length = scan - pattern) == 0)
727 		return GIT_ENOTFOUND;
728 
729 	/*
730 	 * Remove one trailing \r in case this is a CRLF delimited
731 	 * file, in the case of Icon\r\r\n, we still leave the first
732 	 * \r there to match against.
733 	 */
734 	if (pattern[spec->length - 1] == '\r')
735 		if (--spec->length == 0)
736 			return GIT_ENOTFOUND;
737 
738 	/* Remove trailing spaces. */
739 	spec->length -= trailing_space_length(pattern, spec->length);
740 
741 	if (spec->length == 0)
742 		return GIT_ENOTFOUND;
743 
744 	if (pattern[spec->length - 1] == '/') {
745 		spec->length--;
746 		spec->flags = spec->flags | GIT_ATTR_FNMATCH_DIRECTORY;
747 		if (--slash_count <= 0)
748 			spec->flags = spec->flags & ~GIT_ATTR_FNMATCH_FULLPATH;
749 	}
750 
751 	if (context) {
752 		char *slash = strrchr(context, '/');
753 		size_t len;
754 		if (slash) {
755 			/* include the slash for easier matching */
756 			len = slash - context + 1;
757 			spec->containing_dir = git_pool_strndup(pool, context, len);
758 			spec->containing_dir_length = len;
759 		}
760 	}
761 
762 	spec->pattern = git_pool_strndup(pool, pattern, spec->length);
763 
764 	if (!spec->pattern) {
765 		*base = git__next_line(pattern);
766 		return -1;
767 	} else {
768 		/* strip '\' that might have been used for internal whitespace */
769 		spec->length = unescape_spaces(spec->pattern);
770 	}
771 
772 	return 0;
773 }
774 
parse_optimized_patterns(git_attr_fnmatch * spec,git_pool * pool,const char * pattern)775 static bool parse_optimized_patterns(
776 	git_attr_fnmatch *spec,
777 	git_pool *pool,
778 	const char *pattern)
779 {
780 	if (!pattern[1] && (pattern[0] == '*' || pattern[0] == '.')) {
781 		spec->flags = GIT_ATTR_FNMATCH_MATCH_ALL;
782 		spec->pattern = git_pool_strndup(pool, pattern, 1);
783 		spec->length = 1;
784 
785 		return true;
786 	}
787 
788 	return false;
789 }
790 
sort_by_hash_and_name(const void * a_raw,const void * b_raw)791 static int sort_by_hash_and_name(const void *a_raw, const void *b_raw)
792 {
793 	const git_attr_name *a = a_raw;
794 	const git_attr_name *b = b_raw;
795 
796 	if (b->name_hash < a->name_hash)
797 		return 1;
798 	else if (b->name_hash > a->name_hash)
799 		return -1;
800 	else
801 		return strcmp(b->name, a->name);
802 }
803 
git_attr_assignment__free(git_attr_assignment * assign)804 static void git_attr_assignment__free(git_attr_assignment *assign)
805 {
806 	/* name and value are stored in a git_pool associated with the
807 	 * git_attr_file, so they do not need to be freed here
808 	 */
809 	assign->name = NULL;
810 	assign->value = NULL;
811 	git__free(assign);
812 }
813 
merge_assignments(void ** old_raw,void * new_raw)814 static int merge_assignments(void **old_raw, void *new_raw)
815 {
816 	git_attr_assignment **old = (git_attr_assignment **)old_raw;
817 	git_attr_assignment *new = (git_attr_assignment *)new_raw;
818 
819 	GIT_REFCOUNT_DEC(*old, git_attr_assignment__free);
820 	*old = new;
821 	return GIT_EEXISTS;
822 }
823 
git_attr_assignment__parse(git_repository * repo,git_pool * pool,git_vector * assigns,const char ** base)824 int git_attr_assignment__parse(
825 	git_repository *repo,
826 	git_pool *pool,
827 	git_vector *assigns,
828 	const char **base)
829 {
830 	int error;
831 	const char *scan = *base;
832 	git_attr_assignment *assign = NULL;
833 
834 	GIT_ASSERT_ARG(assigns && !assigns->length);
835 
836 	git_vector_set_cmp(assigns, sort_by_hash_and_name);
837 
838 	while (*scan && *scan != '\n') {
839 		const char *name_start, *value_start;
840 
841 		/* skip leading blanks */
842 		while (git__isspace(*scan) && *scan != '\n') scan++;
843 
844 		/* allocate assign if needed */
845 		if (!assign) {
846 			assign = git__calloc(1, sizeof(git_attr_assignment));
847 			GIT_ERROR_CHECK_ALLOC(assign);
848 			GIT_REFCOUNT_INC(assign);
849 		}
850 
851 		assign->name_hash = 5381;
852 		assign->value = git_attr__true;
853 
854 		/* look for magic name prefixes */
855 		if (*scan == '-') {
856 			assign->value = git_attr__false;
857 			scan++;
858 		} else if (*scan == '!') {
859 			assign->value = git_attr__unset; /* explicit unspecified state */
860 			scan++;
861 		} else if (*scan == '#') /* comment rest of line */
862 			break;
863 
864 		/* find the name */
865 		name_start = scan;
866 		while (*scan && !git__isspace(*scan) && *scan != '=') {
867 			assign->name_hash =
868 				((assign->name_hash << 5) + assign->name_hash) + *scan;
869 			scan++;
870 		}
871 		if (scan == name_start) {
872 			/* must have found lone prefix (" - ") or leading = ("=foo")
873 			 * or end of buffer -- advance until whitespace and continue
874 			 */
875 			while (*scan && !git__isspace(*scan)) scan++;
876 			continue;
877 		}
878 
879 		/* allocate permanent storage for name */
880 		assign->name = git_pool_strndup(pool, name_start, scan - name_start);
881 		GIT_ERROR_CHECK_ALLOC(assign->name);
882 
883 		/* if there is an equals sign, find the value */
884 		if (*scan == '=') {
885 			for (value_start = ++scan; *scan && !git__isspace(*scan); ++scan);
886 
887 			/* if we found a value, allocate permanent storage for it */
888 			if (scan > value_start) {
889 				assign->value = git_pool_strndup(pool, value_start, scan - value_start);
890 				GIT_ERROR_CHECK_ALLOC(assign->value);
891 			}
892 		}
893 
894 		/* expand macros (if given a repo with a macro cache) */
895 		if (repo != NULL && assign->value == git_attr__true) {
896 			git_attr_rule *macro =
897 				git_attr_cache__lookup_macro(repo, assign->name);
898 
899 			if (macro != NULL) {
900 				unsigned int i;
901 				git_attr_assignment *massign;
902 
903 				git_vector_foreach(&macro->assigns, i, massign) {
904 					GIT_REFCOUNT_INC(massign);
905 
906 					error = git_vector_insert_sorted(
907 						assigns, massign, &merge_assignments);
908 					if (error < 0 && error != GIT_EEXISTS) {
909 						git_attr_assignment__free(assign);
910 						return error;
911 					}
912 				}
913 			}
914 		}
915 
916 		/* insert allocated assign into vector */
917 		error = git_vector_insert_sorted(assigns, assign, &merge_assignments);
918 		if (error < 0 && error != GIT_EEXISTS)
919 			return error;
920 
921 		/* clear assign since it is now "owned" by the vector */
922 		assign = NULL;
923 	}
924 
925 	if (assign != NULL)
926 		git_attr_assignment__free(assign);
927 
928 	*base = git__next_line(scan);
929 
930 	return (assigns->length == 0) ? GIT_ENOTFOUND : 0;
931 }
932 
git_attr_rule__clear(git_attr_rule * rule)933 static void git_attr_rule__clear(git_attr_rule *rule)
934 {
935 	unsigned int i;
936 	git_attr_assignment *assign;
937 
938 	if (!rule)
939 		return;
940 
941 	if (!(rule->match.flags & GIT_ATTR_FNMATCH_IGNORE)) {
942 		git_vector_foreach(&rule->assigns, i, assign)
943 			GIT_REFCOUNT_DEC(assign, git_attr_assignment__free);
944 		git_vector_free(&rule->assigns);
945 	}
946 
947 	/* match.pattern is stored in a git_pool, so no need to free */
948 	rule->match.pattern = NULL;
949 	rule->match.length = 0;
950 }
951 
git_attr_rule__free(git_attr_rule * rule)952 void git_attr_rule__free(git_attr_rule *rule)
953 {
954 	git_attr_rule__clear(rule);
955 	git__free(rule);
956 }
957 
git_attr_session__init(git_attr_session * session,git_repository * repo)958 int git_attr_session__init(git_attr_session *session, git_repository *repo)
959 {
960 	GIT_ASSERT_ARG(repo);
961 
962 	memset(session, 0, sizeof(*session));
963 	session->key = git_atomic32_inc(&repo->attr_session_key);
964 
965 	return 0;
966 }
967 
git_attr_session__free(git_attr_session * session)968 void git_attr_session__free(git_attr_session *session)
969 {
970 	if (!session)
971 		return;
972 
973 	git_buf_dispose(&session->sysdir);
974 	git_buf_dispose(&session->tmp);
975 
976 	memset(session, 0, sizeof(git_attr_session));
977 }
978