Ticket #12249: delete_file.patch
File delete_file.patch, 13.7 KB (added by , 9 years ago) |
---|
-
src/org/openstreetmap/josm/actions/SessionLoadAction.java
163 163 viewport = reader.getViewport(); 164 164 } finally { 165 165 if (tempFile) { 166 if (!file.delete()) { 167 file.deleteOnExit(); 168 } 166 Utils.deleteFile(file); 169 167 file = null; 170 168 } 171 169 } -
src/org/openstreetmap/josm/data/AutosaveTask.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.data; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 6 7 import java.io.BufferedReader; … … 39 40 import org.openstreetmap.josm.gui.util.GuiHelper; 40 41 import org.openstreetmap.josm.io.OsmExporter; 41 42 import org.openstreetmap.josm.io.OsmImporter; 43 import org.openstreetmap.josm.tools.Utils; 42 44 43 45 /** 44 46 * Saves data layers periodically so they can be recovered in case of a crash. … … 193 195 } 194 196 while (info.backupFiles.size() > PROP_FILES_PER_LAYER.get()) { 195 197 File oldFile = info.backupFiles.remove(); 196 if (!oldFile.delete()) { 197 Main.warn(tr("Unable to delete old backup file {0}", oldFile.getAbsolutePath())); 198 } else { 199 File pidFile = getPidFile(oldFile); 200 if (!pidFile.delete()) { 201 Main.warn(tr("Unable to delete old backup file {0}", pidFile.getAbsolutePath())); 202 } 198 if (Utils.deleteFile(oldFile, marktr("Unable to delete old backup file {0}"))) { 199 Utils.deleteFile(getPidFile(oldFile), marktr("Unable to delete old backup file {0}")); 203 200 } 204 201 } 205 202 } … … 272 269 moveToDeletedLayersFolder(lastFile); 273 270 } 274 271 for (File file: info.backupFiles) { 275 if ( file.delete()) {276 getPidFile(file).delete();272 if (Utils.deleteFile(file)) { 273 Utils.deleteFile(getPidFile(file)); 277 274 } 278 275 } 279 276 … … 368 365 369 366 if (backupFile.exists()) { 370 367 deletedLayers.remove(backupFile); 371 if (!backupFile.delete()) { 372 Main.warn(String.format("Could not delete old backup file %s", backupFile)); 373 } 368 Utils.deleteFile(backupFile, marktr("Unable to delete old backup file {0}")); 374 369 } 375 370 if (f.renameTo(backupFile)) { 376 371 deletedLayers.add(backupFile); 377 pidFile.delete();372 Utils.deleteFile(pidFile); 378 373 } else { 379 374 Main.warn(String.format("Could not move autosaved file %s to %s folder", f.getName(), deletedLayersDir.getName())); 380 375 // we cannot move to deleted folder, so just try to delete it directly 381 if (!f.delete()) { 382 Main.warn(String.format("Could not delete backup file %s", f)); 383 } else if (!pidFile.delete()) { 384 Main.warn(String.format("Could not delete PID file %s", pidFile)); 376 if (Utils.deleteFile(f, marktr("Unable to delete backup file {0}"))) { 377 Utils.deleteFile(pidFile, marktr("Unable to delete PID file {0}")); 385 378 } 386 379 } 387 380 while (deletedLayers.size() > PROP_DELETED_LAYERS.get()) { … … 389 382 if (next == null) { 390 383 break; 391 384 } 392 if (!next.delete()) { 393 Main.warn(String.format("Could not delete archived backup file %s", next)); 394 } 385 Utils.deleteFile(next, marktr("Unable to delete archived backup file {0}")); 395 386 } 396 387 } 397 388 -
src/org/openstreetmap/josm/data/CustomConfigurator.java
313 313 } 314 314 } 315 315 } 316 try { 317 f.delete(); 318 } catch (Exception e) { 319 log("Warning: Can not delete file "+f.getPath()+": "+e.getMessage()); 316 if (!Utils.deleteFile(f)) { 317 log("Warning: Can not delete file "+f.getPath()); 320 318 } 321 319 } 322 320 -
src/org/openstreetmap/josm/data/Preferences.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.data; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 6 7 import java.awt.Color; … … 859 860 860 861 File tmpFile = new File(prefFile + "_tmp"); 861 862 Utils.copyFile(tmpFile, prefFile); 862 if (!tmpFile.delete()) { 863 Main.warn(tr("Unable to delete temporary file {0}", tmpFile.getAbsolutePath())); 864 } 863 Utils.deleteFile(tmpFile, marktr("Unable to delete temporary file {0}")); 865 864 866 865 setCorrectPermissions(prefFile); 867 866 setCorrectPermissions(backupFile); -
src/org/openstreetmap/josm/gui/io/DownloadFileTask.java
21 21 import org.openstreetmap.josm.gui.PleaseWaitDialog; 22 22 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 23 23 import org.openstreetmap.josm.tools.HttpClient; 24 import org.openstreetmap.josm.tools.Utils; 24 25 import org.xml.sax.SAXException; 25 26 26 27 /** … … 130 131 if (unpack) { 131 132 Main.info(tr("Unpacking {0} into {1}", file.getAbsolutePath(), file.getParent())); 132 133 unzipFileRecursively(file, file.getParent()); 133 file.delete();134 Utils.deleteFile(file); 134 135 } 135 136 } 136 137 } catch (MalformedURLException e) { -
src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java
808 808 ImageViewerDialog.showImage(this, null); 809 809 } 810 810 811 if ( toDelete.getFile().delete()) {811 if (Utils.deleteFile(toDelete.getFile())) { 812 812 Main.info("File "+toDelete.getFile()+" deleted. "); 813 813 } else { 814 814 JOptionPane.showMessageDialog( -
src/org/openstreetmap/josm/io/CachedFile.java
335 335 if (localPath.size() == 2) { 336 336 File lfile = new File(localPath.get(1)); 337 337 if (lfile.exists()) { 338 lfile.delete();338 Utils.deleteFile(lfile); 339 339 } 340 340 } 341 341 Main.pref.putCollection(prefKey, null); -
src/org/openstreetmap/josm/io/OsmExporter.java
92 92 doSave(file, layer); 93 93 if (noBackup || !Main.pref.getBoolean("save.keepbackup", false)) { 94 94 if (tmpFile != null) { 95 tmpFile.delete();95 Utils.deleteFile(tmpFile); 96 96 } 97 97 } 98 98 layer.onPostSaveToFile(); -
src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
325 325 } 326 326 // remove old stuff or whole update process is broken 327 327 for (File file: siteCacheFiles) { 328 file.delete();328 Utils.deleteFile(file); 329 329 } 330 330 } 331 331 -
src/org/openstreetmap/josm/tools/PlatformHookOsx.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.tools; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 6 7 import java.awt.Image; … … 365 366 Main.pref.getPreferencesDirectory().mkdirs(); 366 367 Main.info("Copying old preferences file to new location"); 367 368 Utils.copyFile(oldPref, newPref); 368 if (!oldPref.delete()) { 369 Main.warn("Unable to delete old preferences file: "+oldPref.getPath()); 370 } 369 Utils.deleteFile(oldPref, marktr("Unable to delete old preferences file {0}")); 371 370 } catch (IOException e) { 372 371 Main.error(e); 373 372 error = true; -
src/org/openstreetmap/josm/tools/PlatformHookWindows.java
189 189 @Override 190 190 public boolean rename(File from, File to) { 191 191 if (to.exists()) 192 to.delete();192 Utils.deleteFile(to); 193 193 return from.renameTo(to); 194 194 } 195 195 -
src/org/openstreetmap/josm/tools/Utils.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.tools; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import static org.openstreetmap.josm.tools.I18n.trn; 6 7 … … 450 451 for (File file : files) { 451 452 if (file.isDirectory()) { 452 453 deleteDirectory(file); 453 } else if (!file.delete()){454 Main.warn("Unable to delete file: "+file.getPath());454 } else { 455 deleteFile(file); 455 456 } 456 457 } 457 458 } … … 460 461 } 461 462 462 463 /** 464 * Deletes a file and log a default warning if the deletion fails. 465 * @param file file to delete 466 * and must contain a single parameter <code>{0}</code> for the file path 467 * @return {@code true} if and only if the file is successfully deleted; {@code false} otherwise 468 * @since XXXX 469 */ 470 public static boolean deleteFile(File file) { 471 return deleteFile(file, marktr("Unable to delete file {0}")); 472 } 473 474 /** 475 * Deletes a file and log a configurable warning if the deletion fails. 476 * @param file file to delete 477 * @param warnMsg warning message. It will be translated with {@code tr()} 478 * and must contain a single parameter <code>{0}</code> for the file path 479 * @return {@code true} if and only if the file is successfully deleted; {@code false} otherwise 480 * @since XXXX 481 */ 482 public static boolean deleteFile(File file, String warnMsg) { 483 boolean result = file.delete(); 484 if (!result) { 485 Main.warn(tr(warnMsg, file.getPath())); 486 } 487 return result; 488 } 489 490 /** 463 491 * <p>Utility method for closing a {@link java.io.Closeable} object.</p> 464 492 * 465 493 * @param c the closeable object. May be null. -
test/unit/org/openstreetmap/josm/io/session/SessionWriterTest.java
27 27 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 28 28 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 29 29 import org.openstreetmap.josm.tools.MultiMap; 30 import org.openstreetmap.josm.tools.Utils; 30 31 31 32 /** 32 33 * Unit tests for Session writing. … … 107 108 fail(e.getMessage()); 108 109 } finally { 109 110 if (file.exists()) { 110 file.delete();111 Utils.deleteFile(file); 111 112 } 112 113 } 113 114 }