Changeset 9296 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-01-04T01:06:23+01:00 (8 years ago)
Author:
Don-vip
Message:

fix #12249 - Fix Findbugs warnings "Exceptional return value of java.io.File.delete() ignored"

Location:
trunk/src/org/openstreetmap/josm
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/SessionLoadAction.java

    r9280 r9296  
    163163                } finally {
    164164                    if (tempFile) {
    165                         if (!file.delete()) {
    166                             file.deleteOnExit();
    167                         }
     165                        Utils.deleteFile(file);
    168166                        file = null;
    169167                    }
  • trunk/src/org/openstreetmap/josm/data/AutosaveTask.java

    r8870 r9296  
    22package org.openstreetmap.josm.data;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56
     
    4041import org.openstreetmap.josm.io.OsmExporter;
    4142import org.openstreetmap.josm.io.OsmImporter;
     43import org.openstreetmap.josm.tools.Utils;
    4244
    4345/**
     
    194196        while (info.backupFiles.size() > PROP_FILES_PER_LAYER.get()) {
    195197            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}"));
    203200            }
    204201        }
     
    273270                        }
    274271                        for (File file: info.backupFiles) {
    275                             if (file.delete()) {
    276                                 getPidFile(file).delete();
     272                            if (Utils.deleteFile(file)) {
     273                                Utils.deleteFile(getPidFile(file));
    277274                            }
    278275                        }
     
    369366        if (backupFile.exists()) {
    370367            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}"));
    374369        }
    375370        if (f.renameTo(backupFile)) {
    376371            deletedLayers.add(backupFile);
    377             pidFile.delete();
     372            Utils.deleteFile(pidFile);
    378373        } else {
    379374            Main.warn(String.format("Could not move autosaved file %s to %s folder", f.getName(), deletedLayersDir.getName()));
    380375            // 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}"));
    385378            }
    386379        }
     
    390383                break;
    391384            }
    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}"));
    395386        }
    396387    }
  • trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java

    r9243 r9296  
    314314            }
    315315        }
    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());
    320318        }
    321319    }
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r9246 r9296  
    22package org.openstreetmap.josm.data;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56
     
    872873        File tmpFile = new File(prefFile + "_tmp");
    873874        Utils.copyFile(tmpFile, prefFile);
    874         if (!tmpFile.delete()) {
    875             Main.warn(tr("Unable to delete temporary file {0}", tmpFile.getAbsolutePath()));
    876         }
     875        Utils.deleteFile(tmpFile, marktr("Unable to delete temporary file {0}"));
    877876
    878877        setCorrectPermissions(prefFile);
  • trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java

    r9280 r9296  
    2323import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2424import org.openstreetmap.josm.tools.HttpClient;
     25import org.openstreetmap.josm.tools.Utils;
    2526import org.xml.sax.SAXException;
    2627
     
    132133                    Main.info(tr("Unpacking {0} into {1}", file.getAbsolutePath(), file.getParent()));
    133134                    unzipFileRecursively(file, file.getParent());
    134                     file.delete();
     135                    Utils.deleteFile(file);
    135136                }
    136137            }
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java

    r9270 r9296  
    666666                }
    667667
    668                 if (toDelete.getFile().delete()) {
     668                if (Utils.deleteFile(toDelete.getFile())) {
    669669                    Main.info("File "+toDelete.getFile()+" deleted. ");
    670670                } else {
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r9280 r9296  
    335335                    File lfile = new File(localPath.get(1));
    336336                    if (lfile.exists()) {
    337                         lfile.delete();
     337                        Utils.deleteFile(lfile);
    338338                    }
    339339                }
  • trunk/src/org/openstreetmap/josm/io/OsmExporter.java

    r8846 r9296  
    9393            if (noBackup || !Main.pref.getBoolean("save.keepbackup", false)) {
    9494                if (tmpFile != null) {
    95                     tmpFile.delete();
     95                    Utils.deleteFile(tmpFile);
    9696                }
    9797            }
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r9171 r9296  
    326326        // remove old stuff or whole update process is broken
    327327        for (File file: siteCacheFiles) {
    328             file.delete();
     328            Utils.deleteFile(file);
    329329        }
    330330    }
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r8923 r9296  
    22package org.openstreetmap.josm.tools;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56
     
    366367                        Main.info("Copying old preferences file to new location");
    367368                        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}"));
    371370                    } catch (IOException e) {
    372371                        Main.error(e);
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r9073 r9296  
    190190    public boolean rename(File from, File to) {
    191191        if (to.exists())
    192             to.delete();
     192            Utils.deleteFile(to);
    193193        return from.renameTo(to);
    194194    }
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r9280 r9296  
    22package org.openstreetmap.josm.tools;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56import static org.openstreetmap.josm.tools.I18n.trn;
     
    446447                    if (file.isDirectory()) {
    447448                        deleteDirectory(file);
    448                     } else if (!file.delete()) {
    449                         Main.warn("Unable to delete file: "+file.getPath());
     449                    } else {
     450                        deleteFile(file);
    450451                    }
    451452                }
     
    453454        }
    454455        return path.delete();
     456    }
     457
     458    /**
     459     * Deletes a file and log a default warning if the deletion fails.
     460     * @param file file to delete
     461     * and must contain a single parameter <code>{0}</code> for the file path
     462     * @return {@code true} if and only if the file is successfully deleted; {@code false} otherwise
     463     * @since XXXX
     464     */
     465    public static boolean deleteFile(File file) {
     466        return deleteFile(file, marktr("Unable to delete file {0}"));
     467    }
     468
     469    /**
     470     * Deletes a file and log a configurable warning if the deletion fails.
     471     * @param file file to delete
     472     * @param warnMsg warning message. It will be translated with {@code tr()}
     473     * and must contain a single parameter <code>{0}</code> for the file path
     474     * @return {@code true} if and only if the file is successfully deleted; {@code false} otherwise
     475     * @since XXXX
     476     */
     477    public static boolean deleteFile(File file, String warnMsg) {
     478        boolean result = file.delete();
     479        if (!result) {
     480            Main.warn(tr(warnMsg, file.getPath()));
     481        }
     482        return result;
    455483    }
    456484
Note: See TracChangeset for help on using the changeset viewer.