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