[3378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data;
|
---|
| 3 |
|
---|
[9296] | 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
[3378] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
[5566] | 7 | import java.io.BufferedReader;
|
---|
[3378] | 8 | import java.io.File;
|
---|
[5566] | 9 | import java.io.FileFilter;
|
---|
[3378] | 10 | import java.io.IOException;
|
---|
[5566] | 11 | import java.io.PrintStream;
|
---|
| 12 | import java.lang.management.ManagementFactory;
|
---|
[7082] | 13 | import java.nio.charset.StandardCharsets;
|
---|
[7315] | 14 | import java.nio.file.Files;
|
---|
[10299] | 15 | import java.nio.file.Path;
|
---|
[3378] | 16 | import java.util.ArrayList;
|
---|
| 17 | import java.util.Date;
|
---|
| 18 | import java.util.Deque;
|
---|
| 19 | import java.util.HashSet;
|
---|
| 20 | import java.util.Iterator;
|
---|
| 21 | import java.util.LinkedList;
|
---|
| 22 | import java.util.List;
|
---|
| 23 | import java.util.Set;
|
---|
| 24 | import java.util.Timer;
|
---|
| 25 | import java.util.TimerTask;
|
---|
[10444] | 26 | import java.util.concurrent.ExecutionException;
|
---|
| 27 | import java.util.concurrent.Future;
|
---|
[11288] | 28 | import java.util.concurrent.TimeUnit;
|
---|
[3378] | 29 | import java.util.regex.Pattern;
|
---|
| 30 |
|
---|
| 31 | import org.openstreetmap.josm.Main;
|
---|
[3679] | 32 | import org.openstreetmap.josm.actions.OpenFileAction.OpenFileTask;
|
---|
[3378] | 33 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 34 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
| 35 | import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
|
---|
| 36 | import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter.Listener;
|
---|
[3461] | 37 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
[3378] | 38 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
[6357] | 39 | import org.openstreetmap.josm.gui.Notification;
|
---|
[10386] | 40 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
---|
| 41 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
---|
| 42 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
---|
| 43 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
[3378] | 44 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[6357] | 45 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[3378] | 46 | import org.openstreetmap.josm.io.OsmExporter;
|
---|
[5566] | 47 | import org.openstreetmap.josm.io.OsmImporter;
|
---|
[9296] | 48 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3378] | 49 |
|
---|
[3679] | 50 | /**
|
---|
| 51 | * Saves data layers periodically so they can be recovered in case of a crash.
|
---|
[3686] | 52 | *
|
---|
| 53 | * There are 2 directories
|
---|
| 54 | * - autosave dir: copies of the currently open data layers are saved here every
|
---|
| 55 | * PROP_INTERVAL seconds. When a data layer is closed normally, the corresponding
|
---|
| 56 | * files are removed. If this dir is non-empty on start, JOSM assumes
|
---|
| 57 | * that it crashed last time.
|
---|
| 58 | * - deleted layers dir: "secondary archive" - when autosaved layers are restored
|
---|
| 59 | * they are copied to this directory. We cannot keep them in the autosave folder,
|
---|
| 60 | * but just deleting it would be dangerous: Maybe a feature inside the file
|
---|
| 61 | * caused JOSM to crash. If the data is valuable, the user can still try to
|
---|
| 62 | * open with another versions of JOSM or fix the problem manually.
|
---|
| 63 | *
|
---|
| 64 | * The deleted layers dir keeps at most PROP_DELETED_LAYERS files.
|
---|
[10386] | 65 | *
|
---|
| 66 | * @since 3378 (creation)
|
---|
| 67 | * @since 10386 (new LayerChangeListener interface)
|
---|
[3679] | 68 | */
|
---|
[3378] | 69 | public class AutosaveTask extends TimerTask implements LayerChangeListener, Listener {
|
---|
| 70 |
|
---|
[8443] | 71 | private static final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'};
|
---|
[3378] | 72 | private static final String AUTOSAVE_DIR = "autosave";
|
---|
| 73 | private static final String DELETED_LAYERS_DIR = "autosave/deleted_layers";
|
---|
| 74 |
|
---|
[3461] | 75 | public static final BooleanProperty PROP_AUTOSAVE_ENABLED = new BooleanProperty("autosave.enabled", true);
|
---|
[3378] | 76 | public static final IntegerProperty PROP_FILES_PER_LAYER = new IntegerProperty("autosave.filesPerLayer", 1);
|
---|
| 77 | public static final IntegerProperty PROP_DELETED_LAYERS = new IntegerProperty("autosave.deletedLayersBackupCount", 5);
|
---|
[11288] | 78 | public static final IntegerProperty PROP_INTERVAL = new IntegerProperty("autosave.interval", (int) TimeUnit.MINUTES.toSeconds(5));
|
---|
[3461] | 79 | public static final IntegerProperty PROP_INDEX_LIMIT = new IntegerProperty("autosave.index-limit", 1000);
|
---|
[6357] | 80 | /** Defines if a notification should be displayed after each autosave */
|
---|
| 81 | public static final BooleanProperty PROP_NOTIFICATION = new BooleanProperty("autosave.notification", false);
|
---|
[3378] | 82 |
|
---|
[10299] | 83 | protected static final class AutosaveLayerInfo {
|
---|
| 84 | private final OsmDataLayer layer;
|
---|
[8285] | 85 | private String layerName;
|
---|
| 86 | private String layerFileName;
|
---|
| 87 | private final Deque<File> backupFiles = new LinkedList<>();
|
---|
[10299] | 88 |
|
---|
| 89 | AutosaveLayerInfo(OsmDataLayer layer) {
|
---|
| 90 | this.layer = layer;
|
---|
| 91 | }
|
---|
[3378] | 92 | }
|
---|
| 93 |
|
---|
| 94 | private final DataSetListenerAdapter datasetAdapter = new DataSetListenerAdapter(this);
|
---|
[7005] | 95 | private final Set<DataSet> changedDatasets = new HashSet<>();
|
---|
| 96 | private final List<AutosaveLayerInfo> layersInfo = new ArrayList<>();
|
---|
[3461] | 97 | private final Object layersLock = new Object();
|
---|
[7005] | 98 | private final Deque<File> deletedLayers = new LinkedList<>();
|
---|
[3378] | 99 |
|
---|
[7834] | 100 | private final File autosaveDir = new File(Main.pref.getUserDataDirectory(), AUTOSAVE_DIR);
|
---|
| 101 | private final File deletedLayersDir = new File(Main.pref.getUserDataDirectory(), DELETED_LAYERS_DIR);
|
---|
[3378] | 102 |
|
---|
[10299] | 103 | /**
|
---|
| 104 | * Replies the autosave directory.
|
---|
| 105 | * @return the autosave directory
|
---|
| 106 | * @since 10299
|
---|
| 107 | */
|
---|
| 108 | public final Path getAutosaveDir() {
|
---|
| 109 | return autosaveDir.toPath();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[10444] | 112 | /**
|
---|
| 113 | * Starts the autosave background task.
|
---|
| 114 | */
|
---|
[3378] | 115 | public void schedule() {
|
---|
| 116 | if (PROP_INTERVAL.get() > 0) {
|
---|
| 117 |
|
---|
| 118 | if (!autosaveDir.exists() && !autosaveDir.mkdirs()) {
|
---|
[6248] | 119 | Main.warn(tr("Unable to create directory {0}, autosave will be disabled", autosaveDir.getAbsolutePath()));
|
---|
[3378] | 120 | return;
|
---|
| 121 | }
|
---|
| 122 | if (!deletedLayersDir.exists() && !deletedLayersDir.mkdirs()) {
|
---|
[6248] | 123 | Main.warn(tr("Unable to create directory {0}, autosave will be disabled", deletedLayersDir.getAbsolutePath()));
|
---|
[3378] | 124 | return;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[8308] | 127 | File[] files = deletedLayersDir.listFiles();
|
---|
| 128 | if (files != null) {
|
---|
| 129 | for (File f: files) {
|
---|
| 130 | deletedLayers.add(f); // FIXME: sort by mtime
|
---|
| 131 | }
|
---|
[3378] | 132 | }
|
---|
| 133 |
|
---|
[11288] | 134 | new Timer(true).schedule(this, TimeUnit.SECONDS.toMillis(1), TimeUnit.SECONDS.toMillis(PROP_INTERVAL.get()));
|
---|
[11905] | 135 | Main.getLayerManager().addAndFireLayerChangeListener(this);
|
---|
[3378] | 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[8870] | 139 | private static String getFileName(String layerName, int index) {
|
---|
[3378] | 140 | String result = layerName;
|
---|
[6104] | 141 | for (char illegalCharacter : ILLEGAL_CHARACTERS) {
|
---|
| 142 | result = result.replaceAll(Pattern.quote(String.valueOf(illegalCharacter)),
|
---|
| 143 | '&' + String.valueOf((int) illegalCharacter) + ';');
|
---|
[3378] | 144 | }
|
---|
| 145 | if (index != 0) {
|
---|
| 146 | result = result + '_' + index;
|
---|
| 147 | }
|
---|
| 148 | return result;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | private void setLayerFileName(AutosaveLayerInfo layer) {
|
---|
| 152 | int index = 0;
|
---|
| 153 | while (true) {
|
---|
| 154 | String filename = getFileName(layer.layer.getName(), index);
|
---|
| 155 | boolean foundTheSame = false;
|
---|
| 156 | for (AutosaveLayerInfo info: layersInfo) {
|
---|
[3381] | 157 | if (info != layer && filename.equals(info.layerFileName)) {
|
---|
[3378] | 158 | foundTheSame = true;
|
---|
| 159 | break;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | if (!foundTheSame) {
|
---|
| 164 | layer.layerFileName = filename;
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | index++;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[10299] | 172 | protected File getNewLayerFile(AutosaveLayerInfo layer, Date now, int startIndex) {
|
---|
| 173 | int index = startIndex;
|
---|
[3378] | 174 | while (true) {
|
---|
[7951] | 175 | String filename = String.format("%1$s_%2$tY%2$tm%2$td_%2$tH%2$tM%2$tS%2$tL%3$s",
|
---|
[10300] | 176 | layer.layerFileName, now, index == 0 ? "" : ('_' + Integer.toString(index)));
|
---|
[10299] | 177 | File result = new File(autosaveDir, filename + '.' + Main.pref.get("autosave.extension", "osm"));
|
---|
[3378] | 178 | try {
|
---|
[10299] | 179 | if (index > PROP_INDEX_LIMIT.get())
|
---|
| 180 | throw new IOException("index limit exceeded");
|
---|
[5566] | 181 | if (result.createNewFile()) {
|
---|
[10299] | 182 | createNewPidFile(autosaveDir, filename);
|
---|
[3378] | 183 | return result;
|
---|
[5566] | 184 | } else {
|
---|
[6248] | 185 | Main.warn(tr("Unable to create file {0}, other filename will be used", result.getAbsolutePath()));
|
---|
[3378] | 186 | }
|
---|
| 187 | } catch (IOException e) {
|
---|
[10469] | 188 | Main.error(e, tr("IOError while creating file, autosave will be skipped: {0}", e.getMessage()));
|
---|
[3378] | 189 | return null;
|
---|
| 190 | }
|
---|
| 191 | index++;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[10299] | 195 | private static void createNewPidFile(File autosaveDir, String filename) {
|
---|
| 196 | File pidFile = new File(autosaveDir, filename+".pid");
|
---|
| 197 | try (PrintStream ps = new PrintStream(pidFile, "UTF-8")) {
|
---|
| 198 | ps.println(ManagementFactory.getRuntimeMXBean().getName());
|
---|
| 199 | } catch (IOException | SecurityException t) {
|
---|
| 200 | Main.error(t);
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[7029] | 204 | private void savelayer(AutosaveLayerInfo info) {
|
---|
[3378] | 205 | if (!info.layer.getName().equals(info.layerName)) {
|
---|
| 206 | setLayerFileName(info);
|
---|
| 207 | info.layerName = info.layer.getName();
|
---|
| 208 | }
|
---|
[5512] | 209 | if (changedDatasets.remove(info.layer.data)) {
|
---|
[10299] | 210 | File file = getNewLayerFile(info, new Date(), 0);
|
---|
[3378] | 211 | if (file != null) {
|
---|
| 212 | info.backupFiles.add(file);
|
---|
[4852] | 213 | new OsmExporter().exportData(file, info.layer, true /* no backup with appended ~ */);
|
---|
[3378] | 214 | }
|
---|
| 215 | }
|
---|
| 216 | while (info.backupFiles.size() > PROP_FILES_PER_LAYER.get()) {
|
---|
| 217 | File oldFile = info.backupFiles.remove();
|
---|
[9296] | 218 | if (Utils.deleteFile(oldFile, marktr("Unable to delete old backup file {0}"))) {
|
---|
| 219 | Utils.deleteFile(getPidFile(oldFile), marktr("Unable to delete old backup file {0}"));
|
---|
[3378] | 220 | }
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | @Override
|
---|
| 225 | public void run() {
|
---|
| 226 | synchronized (layersLock) {
|
---|
| 227 | try {
|
---|
| 228 | for (AutosaveLayerInfo info: layersInfo) {
|
---|
| 229 | savelayer(info);
|
---|
| 230 | }
|
---|
| 231 | changedDatasets.clear();
|
---|
[6357] | 232 | if (PROP_NOTIFICATION.get() && !layersInfo.isEmpty()) {
|
---|
[10621] | 233 | GuiHelper.runInEDT(this::displayNotification);
|
---|
[6357] | 234 | }
|
---|
[11746] | 235 | } catch (RuntimeException t) { // NOPMD
|
---|
[3378] | 236 | // Don't let exception stop time thread
|
---|
[6248] | 237 | Main.error("Autosave failed:");
|
---|
| 238 | Main.error(t);
|
---|
[3378] | 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[6357] | 243 | protected void displayNotification() {
|
---|
[10621] | 244 | new Notification(tr("Your work has been saved automatically."))
|
---|
| 245 | .setDuration(Notification.TIME_SHORT)
|
---|
| 246 | .show();
|
---|
[6357] | 247 | }
|
---|
| 248 |
|
---|
[3378] | 249 | @Override
|
---|
[10386] | 250 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
[3378] | 251 | // Do nothing
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | private void registerNewlayer(OsmDataLayer layer) {
|
---|
| 255 | synchronized (layersLock) {
|
---|
| 256 | layer.data.addDataSetListener(datasetAdapter);
|
---|
[10299] | 257 | layersInfo.add(new AutosaveLayerInfo(layer));
|
---|
[3378] | 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | @Override
|
---|
[10386] | 262 | public void layerAdded(LayerAddEvent e) {
|
---|
| 263 | if (e.getAddedLayer() instanceof OsmDataLayer) {
|
---|
| 264 | registerNewlayer((OsmDataLayer) e.getAddedLayer());
|
---|
[3378] | 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | @Override
|
---|
[10386] | 269 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
| 270 | if (e.getRemovedLayer() instanceof OsmDataLayer) {
|
---|
[3378] | 271 | synchronized (layersLock) {
|
---|
[10386] | 272 | OsmDataLayer osmLayer = (OsmDataLayer) e.getRemovedLayer();
|
---|
[3378] | 273 | osmLayer.data.removeDataSetListener(datasetAdapter);
|
---|
| 274 | Iterator<AutosaveLayerInfo> it = layersInfo.iterator();
|
---|
| 275 | while (it.hasNext()) {
|
---|
| 276 | AutosaveLayerInfo info = it.next();
|
---|
| 277 | if (info.layer == osmLayer) {
|
---|
| 278 |
|
---|
[7029] | 279 | savelayer(info);
|
---|
| 280 | File lastFile = info.backupFiles.pollLast();
|
---|
| 281 | if (lastFile != null) {
|
---|
| 282 | moveToDeletedLayersFolder(lastFile);
|
---|
| 283 | }
|
---|
| 284 | for (File file: info.backupFiles) {
|
---|
[9296] | 285 | if (Utils.deleteFile(file)) {
|
---|
| 286 | Utils.deleteFile(getPidFile(file));
|
---|
[3378] | 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | it.remove();
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | @Override
|
---|
| 298 | public void processDatasetEvent(AbstractDatasetChangedEvent event) {
|
---|
| 299 | changedDatasets.add(event.getDataset());
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[10299] | 302 | protected File getPidFile(File osmFile) {
|
---|
[5566] | 303 | return new File(autosaveDir, osmFile.getName().replaceFirst("[.][^.]+$", ".pid"));
|
---|
| 304 | }
|
---|
[6069] | 305 |
|
---|
[5566] | 306 | /**
|
---|
| 307 | * Replies the list of .osm files still present in autosave dir, that are not currently managed by another instance of JOSM.
|
---|
| 308 | * These files are hence unsaved layers from an old instance of JOSM that crashed and may be recovered by this instance.
|
---|
| 309 | * @return The list of .osm files still present in autosave dir, that are not currently managed by another instance of JOSM
|
---|
| 310 | */
|
---|
[3435] | 311 | public List<File> getUnsavedLayersFiles() {
|
---|
[7005] | 312 | List<File> result = new ArrayList<>();
|
---|
[5566] | 313 | File[] files = autosaveDir.listFiles(OsmImporter.FILE_FILTER);
|
---|
[3378] | 314 | if (files == null)
|
---|
| 315 | return result;
|
---|
[3435] | 316 | for (File file: files) {
|
---|
| 317 | if (file.isFile()) {
|
---|
[5566] | 318 | boolean skipFile = false;
|
---|
| 319 | File pidFile = getPidFile(file);
|
---|
| 320 | if (pidFile.exists()) {
|
---|
[7315] | 321 | try (BufferedReader reader = Files.newBufferedReader(pidFile.toPath(), StandardCharsets.UTF_8)) {
|
---|
[7037] | 322 | String jvmId = reader.readLine();
|
---|
| 323 | if (jvmId != null) {
|
---|
| 324 | String pid = jvmId.split("@")[0];
|
---|
| 325 | skipFile = jvmPerfDataFileExists(pid);
|
---|
[5566] | 326 | }
|
---|
[10212] | 327 | } catch (IOException | SecurityException t) {
|
---|
[6248] | 328 | Main.error(t);
|
---|
[5566] | 329 | }
|
---|
| 330 | }
|
---|
| 331 | if (!skipFile) {
|
---|
| 332 | result.add(file);
|
---|
| 333 | }
|
---|
[3378] | 334 | }
|
---|
[3435] | 335 | }
|
---|
| 336 | return result;
|
---|
| 337 | }
|
---|
[6069] | 338 |
|
---|
[10043] | 339 | private static boolean jvmPerfDataFileExists(final String jvmId) {
|
---|
[5566] | 340 | File jvmDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "hsperfdata_" + System.getProperty("user.name"));
|
---|
| 341 | if (jvmDir.exists() && jvmDir.canRead()) {
|
---|
[10608] | 342 | File[] files = jvmDir.listFiles((FileFilter) file -> file.getName().equals(jvmId) && file.isFile());
|
---|
[5566] | 343 | return files != null && files.length == 1;
|
---|
| 344 | }
|
---|
| 345 | return false;
|
---|
| 346 | }
|
---|
[3435] | 347 |
|
---|
[10444] | 348 | /**
|
---|
| 349 | * Recover the unsaved layers and open them asynchronously.
|
---|
| 350 | * @return A future that can be used to wait for the completion of this task.
|
---|
| 351 | */
|
---|
| 352 | public Future<?> recoverUnsavedLayers() {
|
---|
[3679] | 353 | List<File> files = getUnsavedLayersFiles();
|
---|
| 354 | final OpenFileTask openFileTsk = new OpenFileTask(files, null, tr("Restoring files"));
|
---|
[10444] | 355 | final Future<?> openFilesFuture = Main.worker.submit(openFileTsk);
|
---|
[10608] | 356 | return Main.worker.submit(() -> {
|
---|
| 357 | try {
|
---|
| 358 | // Wait for opened tasks to be generated.
|
---|
| 359 | openFilesFuture.get();
|
---|
| 360 | for (File f: openFileTsk.getSuccessfullyOpenedFiles()) {
|
---|
| 361 | moveToDeletedLayersFolder(f);
|
---|
[3679] | 362 | }
|
---|
[10608] | 363 | } catch (InterruptedException | ExecutionException e) {
|
---|
| 364 | Main.error(e);
|
---|
[3839] | 365 | }
|
---|
[3679] | 366 | });
|
---|
[3378] | 367 | }
|
---|
| 368 |
|
---|
[3686] | 369 | /**
|
---|
| 370 | * Move file to the deleted layers directory.
|
---|
| 371 | * If moving does not work, it will try to delete the file directly.
|
---|
| 372 | * Afterwards, if the number of deleted layers gets larger than PROP_DELETED_LAYERS,
|
---|
| 373 | * some files in the deleted layers directory will be removed.
|
---|
| 374 | *
|
---|
| 375 | * @param f the file, usually from the autosave dir
|
---|
| 376 | */
|
---|
[3559] | 377 | private void moveToDeletedLayersFolder(File f) {
|
---|
| 378 | File backupFile = new File(deletedLayersDir, f.getName());
|
---|
[5566] | 379 | File pidFile = getPidFile(f);
|
---|
[6069] | 380 |
|
---|
[3559] | 381 | if (backupFile.exists()) {
|
---|
[3686] | 382 | deletedLayers.remove(backupFile);
|
---|
[9296] | 383 | Utils.deleteFile(backupFile, marktr("Unable to delete old backup file {0}"));
|
---|
[3559] | 384 | }
|
---|
| 385 | if (f.renameTo(backupFile)) {
|
---|
| 386 | deletedLayers.add(backupFile);
|
---|
[9296] | 387 | Utils.deleteFile(pidFile);
|
---|
[3559] | 388 | } else {
|
---|
[6248] | 389 | Main.warn(String.format("Could not move autosaved file %s to %s folder", f.getName(), deletedLayersDir.getName()));
|
---|
[3686] | 390 | // we cannot move to deleted folder, so just try to delete it directly
|
---|
[9296] | 391 | if (Utils.deleteFile(f, marktr("Unable to delete backup file {0}"))) {
|
---|
| 392 | Utils.deleteFile(pidFile, marktr("Unable to delete PID file {0}"));
|
---|
[3686] | 393 | }
|
---|
[3559] | 394 | }
|
---|
| 395 | while (deletedLayers.size() > PROP_DELETED_LAYERS.get()) {
|
---|
[3675] | 396 | File next = deletedLayers.remove();
|
---|
[3686] | 397 | if (next == null) {
|
---|
| 398 | break;
|
---|
| 399 | }
|
---|
[9296] | 400 | Utils.deleteFile(next, marktr("Unable to delete archived backup file {0}"));
|
---|
[3559] | 401 | }
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[10444] | 404 | /**
|
---|
| 405 | * Mark all unsaved layers as deleted. They are still preserved in the deleted layers folder.
|
---|
| 406 | */
|
---|
[6995] | 407 | public void discardUnsavedLayers() {
|
---|
[3686] | 408 | for (File f: getUnsavedLayersFiles()) {
|
---|
[3559] | 409 | moveToDeletedLayersFolder(f);
|
---|
[3461] | 410 | }
|
---|
| 411 | }
|
---|
[3378] | 412 | }
|
---|