-
Notifications
You must be signed in to change notification settings - Fork 740
/
CtagsUtil.java
226 lines (198 loc) · 7.99 KB
/
CtagsUtil.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2019, Chris Fraire <cfraire@me.com>.
*/
package org.opengrok.indexer.util;
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.Nullable;
import org.opengrok.indexer.analysis.Ctags;
import org.opengrok.indexer.analysis.Definitions;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.logger.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class CtagsUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(CtagsUtil.class);
public static final String SYSTEM_CTAGS_PROPERTY = "org.opengrok.indexer.analysis.Ctags";
/** Private to enforce static. */
private CtagsUtil() {
}
/**
* Check that {@code ctags} program exists and is working.
* @param ctagsBinary name of the ctags program or path
* @return true if the program works, false otherwise
*/
public static boolean validate(String ctagsBinary) {
if (!isUniversalCtags(ctagsBinary)) {
return false;
}
return canProcessFiles(RuntimeEnvironment.getInstance().getSourceRootFile());
}
/**
* Run ctags program on a known temporary file to be created under given path and see if it was possible
* to get some symbols.
* @param baseDir directory to use for storing the temporary file
* @return true if at least one symbol was found, false otherwise
*/
private static boolean canProcessFiles(File baseDir) {
Path inputPath;
try {
inputPath = File.createTempFile("ctagsValidation", ".c", baseDir).toPath();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "cannot create temporary file in ''{0}''", baseDir);
return false;
}
final String resourceFileName = "sample.c";
ClassLoader classLoader = CtagsUtil.class.getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream(resourceFileName)) {
if (inputStream == null) {
LOGGER.log(Level.SEVERE, "cannot get resource URL of ''{0}'' for ctags check",
resourceFileName);
return false;
}
Files.copy(inputStream, inputPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "cannot copy ''{0}'' to ''{1}'' for ctags check: {2}",
new Object[]{resourceFileName, inputPath, e});
return false;
}
Ctags ctags = new Ctags();
try {
Definitions definitions = ctags.doCtags(inputPath.toString());
if (definitions != null && definitions.numberOfSymbols() > 1) {
return true;
}
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "cannot determine whether ctags can produce definitions", e);
} finally {
ctags.close();
try {
Files.delete(inputPath);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "cannot delete ''{0}''", inputPath);
}
}
return false;
}
@Nullable
public static String getCtagsVersion(String ctagsBinary) {
Executor executor = new Executor(new String[]{ctagsBinary, "--version"});
executor.exec(false);
String output = executor.getOutputString();
if (output != null) {
output = output.trim();
}
return output;
}
private static boolean isUniversalCtags(String ctagsBinary) {
String version = getCtagsVersion(ctagsBinary);
boolean isUniversalCtags = version != null && version.contains("Universal Ctags");
if (version == null || !isUniversalCtags) {
LOGGER.log(Level.SEVERE, "Error: No Universal Ctags found !\n"
+ "(tried running " + "{0}" + ")\n"
+ "Please use the -c option to specify path to a "
+ "Universal Ctags program.\n"
+ "Or set it in Java system property {1}",
new Object[]{ctagsBinary, SYSTEM_CTAGS_PROPERTY});
return false;
}
return true;
}
/**
* Gets the base set of languages by executing {@code --list-languages} for the specified binary.
* @return empty list on failure to run, or a defined list
*/
public static Set<String> getLanguages(String ctagsBinary) {
Executor executor = new Executor(new String[]{ctagsBinary, "--list-languages"});
int rc = executor.exec(false);
String output = executor.getOutputString();
if (output == null || rc != 0) {
LOGGER.log(Level.WARNING, "Failed to get Ctags languages");
return Collections.emptySet();
}
output = output.replaceAll("\\s+\\[disabled]", "");
String[] split = output.split("(?m)$");
Set<String> result = new HashSet<>();
for (String lang : split) {
lang = lang.trim();
if (!lang.isEmpty()) {
result.add(lang);
}
}
return result;
}
/**
* Deletes Ctags temporary files left over after terminating Ctags processes
* in case of timeout or Ctags crash, @see Ctags#doCtags.
*/
public static void deleteTempFiles() {
Set<String> dirs = new HashSet<>(Arrays.asList(System.getProperty("java.io.tmpdir"),
System.getenv("TMPDIR"), System.getenv("TMP")));
if (SystemUtils.IS_OS_UNIX) {
// hard-coded TMPDIR in Universal Ctags on Unix.
dirs.add("/tmp");
}
dirs = dirs.stream()
.filter(Objects::nonNull)
.collect(Collectors.toSet());
for (String directoryName : dirs) {
File directory = new File(directoryName);
if (!directory.isDirectory()) {
continue;
}
LOGGER.log(Level.FINER, "deleting Ctags temporary files in directory ''{0}''", directoryName);
deleteTempFiles(directory);
}
}
private static void deleteTempFiles(File directory) {
final Pattern pattern = Pattern.compile("tags\\.\\S{6}"); // ctags uses this pattern to call mkstemp()
File[] files = directory.listFiles((dir, name) -> {
Matcher matcher = pattern.matcher(name);
return matcher.find();
});
if (Objects.isNull(files)) {
return;
}
for (File file : files) {
if (file.isFile()) {
try {
Files.delete(file.toPath());
} catch (IOException exception) {
LOGGER.log(Level.WARNING, String.format("cannot delete file '%s'", file), exception);
}
}
}
}
}