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
|
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.bootstrap;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.impl.utils.DefaultTempFolder;
import org.sonar.api.utils.TempFolder;
import org.springframework.context.annotation.Bean;
import static org.sonar.core.util.FileUtils.deleteQuietly;
public class GlobalTempFolderProvider {
private static final Logger LOG = LoggerFactory.getLogger(GlobalTempFolderProvider.class);
private static final long CLEAN_MAX_AGE = TimeUnit.DAYS.toMillis(21);
static final String TMP_NAME_PREFIX = ".sonartmp_";
@Bean("GlobalTempFolder")
public TempFolder provide(ScannerProperties scannerProps, SonarUserHome userHome) {
var workingPathName = StringUtils.defaultIfBlank(scannerProps.property(CoreProperties.GLOBAL_WORKING_DIRECTORY), CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE);
var workingPath = Paths.get(workingPathName);
if (!workingPath.isAbsolute()) {
var home = userHome.getPath();
workingPath = home.resolve(workingPath).normalize();
}
try {
cleanTempFolders(workingPath);
} catch (IOException e) {
LOG.error(String.format("failed to clean global working directory: %s", workingPath), e);
}
var tempDir = createTempFolder(workingPath);
return new DefaultTempFolder(tempDir.toFile(), true);
}
private static Path createTempFolder(Path workingPath) {
try {
Path realPath = workingPath;
if (Files.isSymbolicLink(realPath)) {
realPath = realPath.toRealPath();
}
Files.createDirectories(realPath);
} catch (IOException e) {
throw new IllegalStateException("Failed to create working path: " + workingPath, e);
}
try {
return Files.createTempDirectory(workingPath, TMP_NAME_PREFIX);
} catch (IOException e) {
throw new IllegalStateException("Failed to create temporary folder in " + workingPath, e);
}
}
private static void cleanTempFolders(Path path) throws IOException {
if (Files.exists(path)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) {
for (Path p : stream) {
deleteQuietly(p.toFile());
}
}
}
}
private static class CleanFilter implements DirectoryStream.Filter<Path> {
@Override
public boolean accept(Path path) {
if (!Files.exists(path)) {
return false;
}
if (!path.getFileName().toString().startsWith(TMP_NAME_PREFIX)) {
return false;
}
long threshold = System.currentTimeMillis() - CLEAN_MAX_AGE;
// we could also check the timestamp in the name, instead
BasicFileAttributes attrs;
try {
attrs = Files.readAttributes(path, BasicFileAttributes.class);
} catch (IOException ioe) {
LOG.error(String.format("Couldn't read file attributes for %s : ", path), ioe);
return false;
}
long creationTime = attrs.creationTime().toMillis();
return creationTime < threshold;
}
}
}
|