File: path.c

package info (click to toggle)
drgn 0.0.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,892 kB
  • sloc: python: 59,081; ansic: 51,400; awk: 423; makefile: 339; sh: 113
file content (153 lines) | stat: -rw-r--r-- 3,859 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
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
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
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later

#include <stdbool.h>
#include <string.h>

#include "drgn_internal.h"
#include "dwarf_constants.h"
#include "path.h"
#include "util.h"

bool path_iterator_next(struct path_iterator *it, const char **component_ret,
			size_t *component_len_ret)
{
	while (it->num_components) {
		struct nstring *cur = &it->components[it->num_components - 1];
		while (cur->len > 0) {
			if (cur->str[cur->len - 1] == '/') {
				if (cur->len == 1) {
					/*
					 * This is an absolute path. Emit an
					 * empty component. Components joined
					 * before this one and remaining ".."
					 * components are not meaningful.
					 */
					it->num_components = 0;
					it->dot_dot = 0;
					*component_ret = "";
					*component_len_ret = 0;
					return true;
				}
				/* Skip redundant slashes. */
				cur->len--;
				continue;
			}

			size_t component_start = cur->len - 1;
			while (component_start > 0 &&
			       cur->str[component_start - 1] != '/')
				component_start--;
			size_t component_len = cur->len - component_start;
			cur->len = component_start;
			if (component_len == 1 &&
			    cur->str[component_start] == '.') {
				/* Skip "." components. */
			} else if (component_len == 2 &&
				   cur->str[component_start] == '.' &&
				   cur->str[component_start + 1] == '.') {
				/* Count ".." components. */
				it->dot_dot++;
			} else if (it->dot_dot) {
				it->dot_dot--;
			} else {
				*component_ret = &cur->str[component_start];
				*component_len_ret = component_len;
				return true;
			}
		}
		it->num_components--;
	}

	if (it->dot_dot) {
		/*
		 * Leftover ".." components must be above the current
		 * directory.
		 */
		it->dot_dot--;
		*component_ret = "..";
		*component_len_ret = 2;
		return true;
	}
	return false;
}

bool path_ends_with(struct path_iterator *haystack,
		    struct path_iterator *needle)
{
	for (;;) {
		const char *h_component, *n_component;
		size_t h_component_len, n_component_len;

		if (!path_iterator_next(needle, &n_component, &n_component_len))
			return true;

		if (!path_iterator_next(haystack, &h_component,
					&h_component_len))
			return false;

		if (h_component_len != n_component_len ||
		    memcmp(h_component, n_component, h_component_len) != 0)
			return false;
	}
}

bool die_matches_filename(Dwarf_Die *die, const char *filename)
{
	if (!filename || !filename[0])
		return true;

	struct nstring die_components[2];
	struct path_iterator die_path = {
		.components = die_components,
	};

	Dwarf_Die cu_die;
	Dwarf_Attribute attr_mem, *attr;
	attr = dwarf_attr_integrate(dwarf_diecu(die, &cu_die, NULL, NULL),
				    DW_AT_comp_dir, &attr_mem);
	const char *path = dwarf_formstring(attr);
	if (path) {
		die_path.components[die_path.num_components].str = path;
		die_path.components[die_path.num_components].len = strlen(path);
		die_path.num_components++;
	}

	path = dwarf_decl_file(die);
	if (!path)
		return false;
	/*
	 * If the declaration file name is absolute, the compilation directory
	 * component will be ignored.
	 */
	die_path.components[die_path.num_components].str = path;
	die_path.components[die_path.num_components].len = strlen(path);
	die_path.num_components++;

	struct path_iterator needle = {
		.components = (struct nstring []){
			{ filename, strlen(filename) }
		},
		.num_components = 1,
	};

	return path_ends_with(&die_path, &needle);
}

LIBDRGN_PUBLIC bool drgn_filename_matches(const char *haystack,
					  const char *needle)
{
	struct path_iterator haystack_path = {
		.components = (struct nstring []){
			{ haystack, strlen(haystack) }
		},
		.num_components = 1,
	};
	struct path_iterator needle_path = {
		.components = (struct nstring []){
			{ needle, strlen(needle) }
		},
		.num_components = 1,
	};
	return path_ends_with(&haystack_path, &needle_path);
}