Skip to content

Commit

Permalink
Bug 572761 Fix some FindBugs warnings and switch to SpotBugs
Browse files Browse the repository at this point in the history
Fix some more warnings for file delete etc.

Change-Id: I16f168fcaf10279ebde5bb80c44940d49162eb3b
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=572761
  • Loading branch information
ajohnson1 committed Apr 15, 2021
1 parent 1a2a711 commit a36cb83
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -44,6 +44,10 @@ public final class Messages extends NLS
public static String ContextDerivedData_Error_OperationNotFound;
public static String Converters_Error_InvalidEnumValue;
public static String DisplayFileResult_Label_NoFile;
public static String FileUtils_FailedToDeleteDirectory;
public static String FileUtils_FailedToMakeDirectory;
public static String FileUtils_FileUtils_FailedToDeleteFile;
public static String FileUtils_ZipSlip;
public static String Filter_Error_IllegalCharacters;
public static String Filter_Error_InvalidRegex;
public static String Filter_Error_Parsing;
Expand All @@ -57,6 +61,9 @@ public final class Messages extends NLS
public static String HtmlOutputter_Label_FirstObjects;
public static String HtmlOutputter_Label_AllNObjects;
public static String HtmlOutputter_Msg_TreeIsLimited;
public static String ITestResult_Error;
public static String ITestResult_Success;
public static String ITestResult_Warning;
public static String PageSnippets_Label_HideUnhide;
public static String PageSnippets_Label_UnhideHide;
public static String PageSnippets_Label_CreatedBy;
Expand Down Expand Up @@ -109,6 +116,7 @@ public final class Messages extends NLS
public static String ResultRenderer_Label_TableOfContents;
public static String RunRegisterdReport_Error_UnknownReport;
public static String SpecFactory_Error_MissingTemplate;
public static String TestSuite_FailedToUnzipReport;
public static String TextOutputter_PieChart;
public static String TextOutputter_Slice;
public static String TextResult_Label_Links;
Expand All @@ -121,14 +129,6 @@ public final class Messages extends NLS
public static String ConsoleProgressListener_UNKNOWN;
public static String ConsoleProgressListener_WARNING;

public static String ITestResult_Error;
public static String ITestResult_Success;
public static String ITestResult_Warning;

public static String TestSuite_FailedToUnzipReport;
public static String FileUtils_FailedToMakeDirectory;
public static String FileUtils_ZipSlip;

static
{
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2008, 2020 SAP AG and IBM Corporation.
* Copyright (c) 2008, 2021 SAP AG and IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -177,7 +177,8 @@ private void copyIcons() throws IOException
if (!icon2name.isEmpty())
{
File iconDir = new File(directory, DIR_ICONS);
iconDir.mkdir();
if (!iconDir.mkdir())
ReportPlugin.log(IStatus.WARNING, MessageUtil.format(Messages.FileUtils_FailedToMakeDirectory, iconDir));

for (Map.Entry<URI, String> entry : icon2name.entrySet())
copyResource(entry.getKey().toURL(), new File(iconDir, entry.getValue()));
Expand Down Expand Up @@ -416,7 +417,8 @@ private void prepareTempDirectory() throws IOException
copyResource(PREFIX + "code.js", new File(directory, "code.js"));

File imgDir = new File(directory, "img");
imgDir.mkdir();
if (!imgDir.mkdir())
ReportPlugin.log(IStatus.WARNING, MessageUtil.format(Messages.FileUtils_FailedToMakeDirectory, imgDir));

copyResource(PREFIX + "img/open.gif", new File(imgDir, "open.gif"));
copyResource(PREFIX + "img/success.gif", new File(imgDir, "success.gif"));
Expand All @@ -432,7 +434,8 @@ private void prepareTempDirectory() throws IOException
copyResource(PREFIX + "img/nochildren.gif", new File(imgDir, "nochildren.gif"));

File pagesDir = new File(directory, DIR_PAGES);
pagesDir.mkdir();
if (!pagesDir.mkdir())
ReportPlugin.log(IStatus.WARNING, MessageUtil.format(Messages.FileUtils_FailedToMakeDirectory, pagesDir));
}

private void copyResource(String resource, File target) throws FileNotFoundException, IOException
Expand Down Expand Up @@ -687,6 +690,8 @@ private void zipResult() throws IOException
private void zipDir(int commonPath, File zipDir, ZipOutputStream zos) throws IOException
{
String[] dirList = zipDir.list();
if (dirList == null)
return;
byte[] readBuffer = new byte[2156];
int bytesIn = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
Expand Down Expand Up @@ -116,5 +116,7 @@ ITestResult_Error=Status: error.
ITestResult_Success=Status: success.
ITestResult_Warning=Status: warning.
TestSuite_FailedToUnzipReport=Failed to unzip: {0}
FileUtils_FailedToDeleteDirectory=Failed to delete directory {0}
FileUtils_FailedToMakeDirectory=Failed to make directory {0}
FileUtils_FileUtils_FailedToDeleteFile=Failed to delete file {0}
FileUtils_ZipSlip=Zip file is outside of the target directory: {0}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.mat.report.internal.Messages;
import org.eclipse.mat.report.internal.ReportPlugin;

public final class FileUtils
{
Expand Down Expand Up @@ -83,9 +85,9 @@ public static File createTempDirectory(String prefix, File parent) throws IOExce
{
File tempFile = File.createTempFile(prefix, "", parent); //$NON-NLS-1$
if (!tempFile.delete())
throw new IOException();
throw new IOException(tempFile.getAbsolutePath());
if (!tempFile.mkdir())
throw new IOException();
throw new IOException(tempFile.getAbsolutePath());
deleterThread.add(tempFile);
return tempFile;
}
Expand Down Expand Up @@ -162,12 +164,18 @@ private void deleteDirectory(File dir)
{
if (fileArray[i].isDirectory())
deleteDirectory(fileArray[i]);
else
fileArray[i].delete();
else if (!fileArray[i].delete())
{
ReportPlugin.log(IStatus.WARNING, MessageUtil.format(Messages.FileUtils_FileUtils_FailedToDeleteFile, fileArray[i].getAbsolutePath()));
}

}
}

dir.delete();
if (!dir.delete())
{
ReportPlugin.log(IStatus.WARNING, MessageUtil.format(Messages.FileUtils_FailedToDeleteDirectory, dir.getAbsolutePath()));
}
}
}

Expand Down Expand Up @@ -198,7 +206,7 @@ public static void unzipFile(File file) throws IOException
*/
public static void unzipFile(File file, File destinationDirectory) throws IOException
{
if (!destinationDirectory.exists())
if (!destinationDirectory.isDirectory())
{
if (!destinationDirectory.mkdir())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2014, 2020 IBM Corporation
* Copyright (c) 2014, 2021 IBM Corporation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -77,7 +77,6 @@ public CreateCollectionDump()
{
// Extend the collections
List<Collection>l0 = new ArrayList<Collection>();
List<Map>l1 = new ArrayList<Map>();
for (Map m : mapTestData.maps)
{
Collection c = m.values();
Expand Down Expand Up @@ -696,6 +695,15 @@ public boolean useEmpty()
}
}

private static class ExamplePrinterStateReason extends PrinterStateReason
{
protected ExamplePrinterStateReason(int value)
{
super(value);
}

}

/**
* Maps which do have COUNT entries
*/
Expand Down Expand Up @@ -935,9 +943,7 @@ public MapTestData()
// javax.print.attribute.standard.PrinterStateReasons
for (int i = 1; i <= COUNT; ++i)
{
PrinterStateReason r = new PrinterStateReason(i)
{
};
PrinterStateReason r = new ExamplePrinterStateReason(i);
cl.put(r, Severity.REPORT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public class Messages extends NLS
public static String OpenSampleHeapDumpAction_NoEditorToOpen;
public static String OpenSnapshot_AllFiles;
public static String OpenSnapshot_AllKnownFormats;
public static String OpenSnapshot_BadName;
public static String OpenSnapshot_FileAlreadyExists;
public static String OpenSnapshot_FileMustHaveExtension;
public static String OpenSnapshot_FileNotFound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ public void run()
catch (IOException ignore)
{
MemoryAnalyserPlugin.log(ignore);
new File(FILE_NAME).delete();
if (!file.delete() && file.exists())
{
MemoryAnalyserPlugin.log(new IOException(file.getAbsolutePath()));
}
}
catch (Exception ignore)
{
Expand All @@ -297,11 +300,12 @@ public void run()

private static void saveDocument(List<Entry> copy)
{
File file = new File(FILE_NAME);
try
{
if (!copy.isEmpty())
{
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(file));
try
{
oout.writeInt(copy.size());
Expand All @@ -316,8 +320,10 @@ private static void saveDocument(List<Entry> copy)
}
else
{
File file = new File(FILE_NAME);
file.delete();
if (file.exists() && !file.delete())
{
throw new IOException(file.getPath());
}
}
}
catch (IOException ignore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ private static void saveHistory()
else
{
File file = new File(FILE_NAME);
file.delete();
if (!file.delete() && file.exists())
{
throw new IOException(file.getAbsolutePath());
}
}
}
catch (IOException ignore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ OpenSampleHeapDumpAction_ErrorOpeningEditor=Error opening editor
OpenSampleHeapDumpAction_NoEditorToOpen=No editor available to open {0}
OpenSnapshot_AllFiles=All Files
OpenSnapshot_AllKnownFormats=All Known Formats
OpenSnapshot_BadName=Bad name component {0}
OpenSnapshot_FileAlreadyExists=The file ''{0}'' already exists
OpenSnapshot_FileMustHaveExtension=The file must have the extension ''{0}''
OpenSnapshot_FileNotFound=File {0} not found
Expand Down

0 comments on commit a36cb83

Please sign in to comment.