| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.taskdefs; |
| 19 | |
|
| 20 | |
import java.io.File; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.InputStream; |
| 23 | |
import org.apache.tools.ant.BuildException; |
| 24 | |
import org.apache.tools.ant.Project; |
| 25 | |
import org.apache.tools.ant.Task; |
| 26 | |
import org.owasp.dependencycheck.utils.Settings; |
| 27 | |
import org.slf4j.impl.StaticLoggerBinder; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class Purge extends Task { |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
private static final String PROPERTIES_FILE = "task.properties"; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public Purge() { |
| 45 | 4 | super(); |
| 46 | |
|
| 47 | |
|
| 48 | 4 | StaticLoggerBinder.getSingleton().setTask(this); |
| 49 | 4 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 4 | private String dataDirectory = null; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public String getDataDirectory() { |
| 62 | 0 | return dataDirectory; |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void setDataDirectory(String dataDirectory) { |
| 71 | 0 | this.dataDirectory = dataDirectory; |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 4 | private boolean failOnError = true; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public boolean isFailOnError() { |
| 86 | 0 | return failOnError; |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public void setFailOnError(boolean failOnError) { |
| 95 | 0 | this.failOnError = failOnError; |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
@Override |
| 99 | |
public void execute() throws BuildException { |
| 100 | 0 | populateSettings(); |
| 101 | |
File db; |
| 102 | |
try { |
| 103 | 0 | db = new File(Settings.getDataDirectory(), "dc.h2.db"); |
| 104 | 0 | if (db.exists()) { |
| 105 | 0 | if (db.delete()) { |
| 106 | 0 | log("Database file purged; local copy of the NVD has been removed", Project.MSG_INFO); |
| 107 | |
} else { |
| 108 | 0 | final String msg = String.format("Unable to delete '%s'; please delete the file manually", db.getAbsolutePath()); |
| 109 | 0 | if (this.failOnError) { |
| 110 | 0 | throw new BuildException(msg); |
| 111 | |
} |
| 112 | 0 | log(msg, Project.MSG_ERR); |
| 113 | 0 | } |
| 114 | |
} else { |
| 115 | 0 | final String msg = String.format("Unable to purge database; the database file does not exists: %s", db.getAbsolutePath()); |
| 116 | 0 | if (this.failOnError) { |
| 117 | 0 | throw new BuildException(msg); |
| 118 | |
} |
| 119 | 0 | log(msg, Project.MSG_ERR); |
| 120 | |
} |
| 121 | 0 | } catch (IOException ex) { |
| 122 | 0 | final String msg = "Unable to delete the database"; |
| 123 | 0 | if (this.failOnError) { |
| 124 | 0 | throw new BuildException(msg); |
| 125 | |
} |
| 126 | 0 | log(msg, Project.MSG_ERR); |
| 127 | |
} finally { |
| 128 | 0 | Settings.cleanup(true); |
| 129 | 0 | } |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
protected void populateSettings() throws BuildException { |
| 140 | 3 | Settings.initialize(); |
| 141 | 3 | InputStream taskProperties = null; |
| 142 | |
try { |
| 143 | 3 | taskProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE); |
| 144 | 3 | Settings.mergeProperties(taskProperties); |
| 145 | 0 | } catch (IOException ex) { |
| 146 | 0 | final String msg = "Unable to load the dependency-check ant task.properties file."; |
| 147 | 0 | if (this.failOnError) { |
| 148 | 0 | throw new BuildException(msg, ex); |
| 149 | |
} |
| 150 | 0 | log(msg, ex, Project.MSG_WARN); |
| 151 | |
} finally { |
| 152 | 3 | if (taskProperties != null) { |
| 153 | |
try { |
| 154 | 3 | taskProperties.close(); |
| 155 | 0 | } catch (IOException ex) { |
| 156 | 0 | log("", ex, Project.MSG_DEBUG); |
| 157 | 3 | } |
| 158 | |
} |
| 159 | |
} |
| 160 | 3 | if (dataDirectory != null) { |
| 161 | 0 | Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory); |
| 162 | |
} else { |
| 163 | 3 | final File jarPath = new File(Purge.class.getProtectionDomain().getCodeSource().getLocation().getPath()); |
| 164 | 3 | final File base = jarPath.getParentFile(); |
| 165 | 3 | final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY); |
| 166 | 3 | final File dataDir = new File(base, sub); |
| 167 | 3 | Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath()); |
| 168 | |
} |
| 169 | 3 | } |
| 170 | |
} |