Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-4143 Replace usage of File.deleteOnExit() with manual deletion on exit #4201

Merged
merged 14 commits into from Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -55,7 +55,7 @@ public class RestoreManager extends BackupRestoreManager {
public RestoreManager(BackupRequest req) throws BackupException {
super(req);
}

//////////////////////////////////////////////////////////////////////////

public String restore() throws BackupException {
Expand Down Expand Up @@ -84,7 +84,7 @@ public String restore() throws BackupException {
throw new BackupException("Restore Error" + e.toString(), e);
}
}

//////////////////////////////////////////////////////////////////////////

@Override
Expand All @@ -95,11 +95,11 @@ void init() throws BackupException {
initWithNoSpecifiedBackupFile();
else
initWithSpecifiedBackupFile();

tempRestoreDir = new File(request.domainsDir, request.domainName +
"_" + System.currentTimeMillis());
}

//////////////////////////////////////////////////////////////////////////

private void initWithSpecifiedBackupFile() throws BackupException {
Expand All @@ -119,47 +119,47 @@ private void initWithSpecifiedBackupFile() throws BackupException {

request.domainDir = new File(request.domainsDir, request.domainName);
}

if(!FileUtils.safeIsDirectory(request.domainDir))
if (!request.domainDir.mkdirs())
throw new BackupException("backup-res.CantCreateDomainDir",
request.domainDir);

backupDir = new File(request.domainDir, Constants.BACKUP_DIR);

// It's NOT an error to not exist. The domain may not exist
// currently and, besides, they are specifying the backup-file
// It's NOT an error to not exist. The domain may not exist
// currently and, besides, they are specifying the backup-file
// from anywhere potentially...
if(!FileUtils.safeIsDirectory(backupDir))
backupDir = null;

//throw new BackupException("NOT YET IMPLEMENTED");

}

//////////////////////////////////////////////////////////////////////////

private void initWithNoSpecifiedBackupFile() throws BackupException {
// if they did NOT specify a backupFile, then we *must* have a
// if they did NOT specify a backupFile, then we *must* have a
// pre-existing backups directory in a pre-existing domain directory.

if(!FileUtils.safeIsDirectory(request.domainDir))
throw new BackupException("backup-res.NoDomainDir",
request.domainDir);

backupDir = getBackupDirectory(request);

// It's an error to not exist...
if(!FileUtils.safeIsDirectory(backupDir))
throw new BackupException("backup-res.NoBackupDir", backupDir);

BackupFilenameManager bfmgr = new BackupFilenameManager(backupDir,
BackupFilenameManager bfmgr = new BackupFilenameManager(backupDir,
request.domainName);
request.backupFile = bfmgr.latest();

//request.backupFile = getNewestZip(backupDir);
}

//////////////////////////////////////////////////////////////////////////

/*
Expand All @@ -169,7 +169,7 @@ private File getNewestZip(File dir) throws BackupException
long newestTime = 0;

File[] zips = dir.listFiles(new ZipFilenameFilter());

for(int i = 0; i < zips.length; i++)
{
//long time = zips[i].lastModified();
Expand All @@ -188,32 +188,32 @@ private File getNewestZip(File dir) throws BackupException

System.out.println(msg);
}

if(newestFile == null)
throw new BackupException("backup-res.NoBackupFiles", dir);

return newestFile;
}
*/
//////////////////////////////////////////////////////////////////////////

private void copyBackups() throws IOException {
private void copyBackups() throws IOException {

File domainBackupDir =
File domainBackupDir =
new File(request.domainDir, Constants.BACKUP_DIR);

/**
* If an existing backup directory does not exist then there
* is nothing to copy.
*/
if(!FileUtils.safeIsDirectory(domainBackupDir))
return;

File tempRestoreDirBackups = new File(tempRestoreDir,
Constants.BACKUP_DIR);
FileUtils.copyTree(domainBackupDir, tempRestoreDirBackups);
}

//////////////////////////////////////////////////////////////////////////

private void atomicSwap(File domainDir, String domainName,
Expand All @@ -236,7 +236,7 @@ private void atomicSwap(File domainDir, String domainName,
oldDir = new File(request.domainsDir,
domainName + OLD_DOMAIN_SUFFIX + System.currentTimeMillis());
}

// Move the current domain to the side.
// On Error -- just fail and delete the new files
if (configOnly) {
Expand Down Expand Up @@ -268,11 +268,11 @@ private void atomicSwap(File domainDir, String domainName,
throw new BackupException("backup-res.CantRevertOldDomain",
domainDir);
throw new BackupException("backup-res.CantRenameRestoredDomain");
}
}

FileUtils.whack(oldDir);
}

//////////////////////////////////////////////////////////////////////////

private String readAndDeletePropsFile(boolean isConfigBackup) {
Expand All @@ -289,7 +289,7 @@ private String readAndDeletePropsFile(boolean isConfigBackup) {
else
propsFile = new File(request.domainDir, Constants.PROPS_FILENAME);

if (request.verbose == true || request.terse != true) {
if (request.verbose || !request.terse) {
if (isConfigBackup) {
mesg = StringHelper.get("backup-res.SuccessfulConfigRestore",
request.domainName, request.domainDir);
Expand All @@ -299,20 +299,19 @@ private String readAndDeletePropsFile(boolean isConfigBackup) {
}
}

if (request.verbose == true) {
if (request.verbose) {
Status status = new Status();
mesg += "\n" + status.read(propsFile, false);
}

if (!propsFile.delete())
propsFile.deleteOnExit();


FileUtils.deleteFileNowOrLater(propsFile);

return mesg;
}

/** zip is platform dependent -- so non-default permissions are gone!
*/

private void setPermissions() {
File backups = new File(request.domainDir, "backups");
File bin = new File(request.domainDir, "bin");
Expand All @@ -323,48 +322,48 @@ private void setPermissions() {
// note that makeExecutable(File f) will make all the files under f
// executable if f happens to be a directory.
BackupUtils.makeExecutable(bin);

BackupUtils.protect(backups);
BackupUtils.protect(config);
BackupUtils.protect(masterPassword);
BackupUtils.protect(webtmp);

// Jan 19, 2005 -- rolled back the fix for 6206176. It has been decided
// that this is not a bug but rather a security feature.
//FileUtils.whack(webtmp);

// fix: 6206176 -- instead of setting permissions for the tmp dir,
// fix: 6206176 -- instead of setting permissions for the tmp dir,
// we just delete it. This will allow, say, user 'A' to do the restore,
// and then allow user 'B' (including root) to start the domain without
// getting a web-container error.
// see: bug 6194504 for the tmp dir details
//old:
//new:
//old:
//new:
}

//////////////////////////////////////////////////////////////////////////

private void sanityCheckExplodedFiles() throws BackupException {
// Is the "magic" properties file where it is supposed to be?

File statusFile = new File(tempRestoreDir, Constants.PROPS_FILENAME);

if(!statusFile.exists()) {
// cleanup -- we are officially failing the restore!
FileUtils.whack(tempRestoreDir);
throw new BackupException(
"backup-res.RestoreError.CorruptBackupFile.NoStatusFile",
request.domainName);
}
}
}

//////////////////////////////////////////////////////////////////////////

private void checkDomainName() throws BackupException {
Status status = new Status();
status.read(request.backupFile);
String buDomainName = status.getDomainName();

if (buDomainName == null) {
//this means the backup zip is bad...
throw new BackupException(StringHelper.get(
Expand Down
Expand Up @@ -150,7 +150,7 @@ long getInternalTimestamp(File f) {
void delete() {
if(statusFile != null && !statusFile.delete()) {
// TBD warning message
statusFile.deleteOnExit();
FileUtils.deleteOnExit(statusFile);
}
}

Expand Down
Expand Up @@ -44,6 +44,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import com.sun.enterprise.util.io.FileUtils;
import org.glassfish.api.admin.CommandException;

/**
Expand Down Expand Up @@ -115,7 +117,7 @@ private String createDBLog(final String dbHome) throws Exception {
if (fDBLog.exists() && !fDBLog.canWrite()) {
System.out.println(localManager.getString("UnableToAccessDatabaseLog", getLogFileName(), dbLogFilePath));
System.out.println(localManager.getString("ContinueStartingDatabase"));
// if exist but not able to write then create a temporary
// if exist but not able to write then create a temporary
// log file and persist on starting the database
dbLogFilePath = createTempLogFile();
} else if (!fDBLog.exists()) {
Expand Down Expand Up @@ -144,7 +146,7 @@ private String createTempLogFile() throws CommandException {
String tempFileName = "";
try {
final File fTemp = File.createTempFile("foo", null);
fTemp.deleteOnExit();
FileUtils.deleteOnExit(fTemp);
tempFileName = fTemp.toString();
} catch (IOException ioe) {
final StringManager localManager = StringManager.getManager(this.getClass());
Expand Down
Expand Up @@ -49,6 +49,7 @@
// Portions Copyright [2017] [Payara Foundation and/or its affiliates]
package org.glassfish.admingui.handlers;

import com.sun.enterprise.util.io.FileUtils;
import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
Expand Down Expand Up @@ -98,7 +99,7 @@ public WoodstockHandler() {
* <p>
* This handler will delete file from temp directory</p>
* @param handlerCtx
* @throws IOException
* @throws IOException
*/
@Handler(id = "deleteFileFromTempDir",
input = {
Expand Down Expand Up @@ -180,7 +181,7 @@ public static void uploadFileToTempDir(HandlerContext handlerCtx) {
prefix = prefix + new SecureRandom().nextInt(100000);
}
tmpFile = File.createTempFile(prefix, suffix);
tmpFile.deleteOnExit();
FileUtils.deleteOnExit(tmpFile);
if (logger.isLoggable(Level.FINE)) {
logger.fine(GuiUtil.getCommonMessage("log.writeToTmpFile"));
}
Expand Down
Expand Up @@ -46,14 +46,15 @@
import java.security.PrivilegedAction;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import com.sun.enterprise.util.io.FileUtils;
import org.glassfish.appclient.client.acc.config.LogService;

/**
Expand All @@ -76,7 +77,7 @@
public class ACCLogger extends Logger {

private static final String ACC_LOGGER_NAME = "GlassFish.ACC";

private static final Level DEFAULT_ACC_LOG_LEVEL = Level.INFO;

public ACCLogger(final LogService logService) throws IOException {
Expand Down Expand Up @@ -153,7 +154,7 @@ private static Handler createHandler(final LogService logService, final Level le
handler.setFormatter(new SimpleFormatter());
handler.setLevel(level);
File lockFile = new File(filePath + ".lck");
lockFile.deleteOnExit();
FileUtils.deleteOnExit(lockFile);
return handler;
}

Expand Down
Expand Up @@ -45,6 +45,7 @@
import com.sun.enterprise.deployment.archivist.ArchivistFactory;
import com.sun.enterprise.util.LocalStringManager;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.util.io.FileUtils;

import java.io.*;
import java.net.URI;
Expand Down Expand Up @@ -134,7 +135,7 @@ public static URI getURI(final File f) throws URISyntaxException {
public static File writeTextToTempFile(String content, String prefix, String suffix, boolean retainTempFiles) throws IOException, FileNotFoundException {
File result = File.createTempFile(prefix, suffix);
if ( ! retainTempFiles) {
result.deleteOnExit();
FileUtils.deleteOnExit(result);
}
try (BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(result)))) {
Expand Down
Expand Up @@ -40,6 +40,7 @@

package org.glassfish.appclient.client.jws.boot;

import com.sun.enterprise.util.io.FileUtils;
import org.glassfish.appclient.client.acc.AppClientContainer;
import org.glassfish.appclient.client.acc.JWSACCClassLoader;
import org.glassfish.appclient.common.Util;
Expand Down Expand Up @@ -375,7 +376,7 @@ public static void refreshPolicy(File policyFile) {
private static File writeTextToTempFile(String content, String prefix, String suffix, boolean retainTempFiles) throws IOException, FileNotFoundException {
File result = File.createTempFile(prefix, suffix);
if ( ! retainTempFiles) {
result.deleteOnExit();
FileUtils.deleteOnExit(result);
}
try (BufferedWriter wtr =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(result)))){
Expand Down