CleanUpAcrFilesTask.java
12.3 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.pdfg.common.AESProperties
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.pdfg.impl;
import com.adobe.pdfg.common.AESProperties;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class CleanUpAcrFilesTask
implements Runnable {
public static final boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
private static volatile boolean IS_TASK_ACTIVE = false;
private List<File> listOfFolders = null;
private Set<String> usersSet = Collections.synchronizedSet(new HashSet());
private long jobExpiration = 1000 * (long)AESProperties.getJobExpirationSeconds();
private static Logger logger = LoggerFactory.getLogger((String)"PDFG Acr Cleanup Daemon");
private static final Pattern[] patternArray = new Pattern[]{Pattern.compile(".*Acr.*"), Pattern.compile("A3D.*"), Pattern.compile("PDF.*"), Pattern.compile("~pr.*"), Pattern.compile("~jp.*"), Pattern.compile("ado.*"), Pattern.compile("A9R.*"), Pattern.compile("pp.*"), Pattern.compile("~DF.*"), Pattern.compile(".*pmt.*"), Pattern.compile("i000.*.tmp"), Pattern.compile("lil.*.tmp"), Pattern.compile(".*\\.tmp"), Pattern.compile(".*\\.cvr")};
private static final long MILLISECONDS_PER_SECOND = 1000;
public CleanUpAcrFilesTask() {
if (this.jobExpiration == 0) {
this.jobExpiration = 86400000;
}
logger.debug("Cleanup expiration time for Acr*.tmp files is set to " + this.jobExpiration + " ms.");
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void run() {
logger.debug("Woken up at " + new Date());
if (!IS_TASK_ACTIVE) {
try {
IS_TASK_ACTIVE = true;
this.cleanUp();
Object var2_1 = null;
IS_TASK_ACTIVE = false;
}
catch (Throwable var1_3) {
Object var2_2 = null;
IS_TASK_ACTIVE = false;
throw var1_3;
}
} else {
logger.debug("Previous cleanup is still in progress.");
}
}
public boolean addUser(String userName) {
int indx = userName.indexOf("\\");
if (indx == -1) {
indx = userName.indexOf("/");
}
return this.usersSet.add(userName.substring(indx + 1));
}
public boolean removeUser(String userName) {
int indx = userName.indexOf("\\");
if (indx == -1) {
indx = userName.indexOf("/");
}
return this.usersSet.remove(userName.substring(indx + 1));
}
public void populateUsersSet(Map<String, String> usersMap) {
if (usersMap == null || usersMap.isEmpty()) {
return;
}
for (String user : usersMap.keySet()) {
boolean result = this.addUser(user);
if (result) continue;
logger.debug("Could not add user " + user + " to the users set");
}
}
private void cleanUp() {
if (isWindows) {
logger.info("Performing cleanup of old Acr*.tmp files");
this.populateListOfFolders();
if (this.listOfFolders != null && this.listOfFolders.size() > 0) {
logger.info("Cleaning up directories: " + this.listOfFolders);
long currentTime = System.currentTimeMillis();
for (File folder : this.listOfFolders) {
this.cleanUpFolder(folder, currentTime);
}
}
}
}
private boolean cleanUpFolder(File file, long currentTime) {
logger.debug("Cleaning up folder: " + file.getAbsolutePath());
boolean bCleanedAll = true;
File[] files = file.listFiles();
if (files != null && files.length != 0) {
block0 : for (int count = 0; count < files.length; ++count) {
File currentFile = files[count];
if (!currentFile.isFile()) continue;
for (Pattern pattern : patternArray) {
if (!pattern.matcher(currentFile.getName()).find()) continue;
long timeModified = currentFile.lastModified();
if (currentTime - timeModified > this.jobExpiration) {
boolean bCleanedCurrent = currentFile.delete();
if (bCleanedCurrent) {
logger.debug("Successful file deletion: " + currentFile.getAbsolutePath());
} else {
logger.debug("File deletion failure: " + currentFile.getAbsolutePath());
}
bCleanedAll = bCleanedAll && bCleanedCurrent;
continue block0;
}
logger.debug("File too young to be cleaned up: " + currentFile.getAbsolutePath());
bCleanedAll = false;
continue block0;
}
}
}
return bCleanedAll;
}
void populateListOfFolders() {
if (isWindows) {
this.listOfFolders = new ArrayList<File>();
String systemTemp = CleanUpAcrFilesTask.calculateSystemPath("TEMP");
String systemTmp = CleanUpAcrFilesTask.calculateSystemPath("TMP");
String userTemp = System.getenv("TEMP");
String userTmp = System.getenv("TMP");
File dir = null;
if (!systemTemp.equals(systemTmp) && (dir = new File(systemTmp)).exists() && dir.isDirectory()) {
this.listOfFolders.add(dir);
}
if ((dir = new File(systemTemp)).exists() && dir.isDirectory()) {
this.listOfFolders.add(dir);
}
if (!userTmp.equals(userTemp)) {
dir = new File(userTemp);
if (dir.exists() && dir.isDirectory()) {
this.listOfFolders.add(dir);
}
this.addTruncatedTempDir(userTemp);
this.addPDFGUsersTempDirs(userTemp);
}
if ((dir = new File(userTmp)).exists() && dir.isDirectory()) {
this.listOfFolders.add(dir);
}
this.addTruncatedTempDir(userTmp);
this.addPDFGUsersTempDirs(userTmp);
}
}
private void addTruncatedTempDir(String tempDirPath) {
if (tempDirPath.contains("\\temp") || tempDirPath.contains("\\Temp")) {
File truncatedTempDir;
int indx = tempDirPath.lastIndexOf("\\temp");
if (indx == -1) {
indx = tempDirPath.lastIndexOf("\\Temp");
}
if (indx != -1 && tempDirPath.length() - indx > 6 && (truncatedTempDir = this.getUserTempWithoutSessionId(tempDirPath)) != null) {
this.listOfFolders.add(truncatedTempDir);
}
}
}
private File getUserTempWithoutSessionId(String tempDirPath) {
String truncatedTempDirPath = new File(tempDirPath).getParent();
if (truncatedTempDirPath == null) {
return null;
}
int indx = truncatedTempDirPath.lastIndexOf("\\temp");
if (indx == -1) {
indx = truncatedTempDirPath.lastIndexOf("\\Temp");
}
if (indx == -1 || truncatedTempDirPath.length() - indx > 6) {
return null;
}
File dir = new File(truncatedTempDirPath);
if (!dir.exists() || !dir.isDirectory()) {
return null;
}
return dir;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void addPDFGUsersTempDirs(String dirPath) {
File dir = null;
String currentUser = System.getenv("USERNAME");
if (currentUser == null) {
logger.debug("Could not retrieve the environment variable USERNAME form the environment");
return;
}
String currentUserShort = null;
if (currentUser.length() > 8) {
currentUserShort = currentUser.substring(0, 6);
currentUserShort = currentUserShort.toUpperCase();
}
if (dirPath.indexOf(currentUser) != -1) {
Set<String> set = this.usersSet;
synchronized (set) {
Iterator<String> iter = this.usersSet.iterator();
while (iter.hasNext()) {
String userTempDirPath = CleanUpAcrFilesTask.susbstituteCurrentUserForPDFGUser(dirPath, currentUser, iter.next());
dir = new File(userTempDirPath);
if (dir.exists() && dir.isDirectory()) {
this.listOfFolders.add(dir);
}
this.addTruncatedTempDir(userTempDirPath);
}
}
}
if (currentUserShort != null && dirPath.indexOf(currentUserShort) != -1) {
Set<String> set = this.usersSet;
synchronized (set) {
Iterator<String> iter = this.usersSet.iterator();
while (iter.hasNext()) {
String userTempDirPath = CleanUpAcrFilesTask.susbstituteCurrentUserForPDFGUser(dirPath, currentUserShort, iter.next());
dir = new File(userTempDirPath);
if (dir.exists() && dir.isDirectory()) {
this.listOfFolders.add(dir);
}
this.addTruncatedTempDir(userTempDirPath);
}
}
}
}
private static String susbstituteCurrentUserForPDFGUser(String dirPath, String currentUser, String pdfgUser) {
int indx = dirPath.indexOf(currentUser);
String pdfgUserDirPath = null;
pdfgUserDirPath = "".concat(dirPath.substring(0, indx)).concat(pdfgUser);
int indxOfNextSeparator = dirPath.indexOf("\\", indx);
if (indxOfNextSeparator != -1) {
pdfgUserDirPath = pdfgUserDirPath.concat(dirPath.substring(indxOfNextSeparator));
}
return pdfgUserDirPath;
}
private static String calculateSystemPath(String key) {
String path = "";
try {
int index;
String[] find_system_value_command = new String[]{"REG", "QUERY", "\"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\"", "/v", key};
Process prc = Runtime.getRuntime().exec(find_system_value_command);
InputStream is = prc.getInputStream();
BufferedReader bfr = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = "";
while ((line = bfr.readLine()) != null && !line.contains(key)) {
}
if (line != null) {
line = line.trim();
}
int indexStartOfValue = line.indexOf("REG_EXPAND_SZ");
int indexStartOfValue2 = line.indexOf("REG_SZ");
indexStartOfValue = indexStartOfValue == -1 ? -1 : indexStartOfValue + "REG_EXPAND_SZ".length();
indexStartOfValue2 = indexStartOfValue2 == -1 ? -1 : indexStartOfValue2 + "REG_SZ".length();
int n = index = indexStartOfValue >= indexStartOfValue2 ? indexStartOfValue : indexStartOfValue2;
if (index >= 0) {
if ((line = line.substring(index).trim()).startsWith("%")) {
int second_percentage_sign = line.indexOf(37, 1);
if (second_percentage_sign != -1) {
String env = line.substring(1, second_percentage_sign);
path = System.getenv(env) + line.substring(second_percentage_sign + 1);
}
} else {
path = line;
}
}
prc.waitFor();
}
catch (Exception e) {
logger.warn("Error while retrieving system environment variable: " + key, (Throwable)e);
}
return path;
}
}