CleanUpAcrFilesTask.java 12.3 KB
/*
 * 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;
    }
}