Ticket #2322: do_not_destroy_prefs.patch

File do_not_destroy_prefs.patch, 2.9 KB (added by xeen, 17 years ago)

Backups old prefs, writes new prefs to tempfile first. If disk becomes full or josm crashes only the tempfile should be broken. At least, there should be a working backup.

  • src/org/openstreetmap/josm/data/Preferences.java

     
    1212import java.io.InputStreamReader;
    1313import java.io.OutputStreamWriter;
    1414import java.io.PrintWriter;
     15import java.nio.channels.FileChannel;
    1516import java.util.ArrayList;
    1617import java.util.Arrays;
    1718import java.util.Collection;
     
    4344public class Preferences {
    4445
    4546    /**
    46      * Internal storage for the preferenced directory.
     47     * Internal storage for the preference directory.
    4748     * Do not access this variable directly!
    4849     * @see #getPreferencesDirFile()
    4950     */
     
    290291        properties.put("josm.version", AboutAction.getVersionString());
    291292        try {
    292293            setSystemProperties();
     294            String prefFile = getPreferencesDir() + "preferences";
     295
     296            // Backup old preferences
     297            copyFile(new File(prefFile), new File(prefFile + "_backup"));
     298
    293299            final PrintWriter out = new PrintWriter(new OutputStreamWriter(
    294                     new FileOutputStream(getPreferencesDir() + "preferences"), "utf-8"), false);
     300                    new FileOutputStream(prefFile + "_tmp"), "utf-8"), false);
    295301            for (final Entry<String, String> e : properties.entrySet()) {
    296302                String s = defaults.get(e.getKey());
    297303                /* don't save default values */
     
    300306                }
    301307            }
    302308            out.close();
     309
     310            File tmpFile = new File(prefFile + "_tmp");
     311            copyFile(tmpFile, new File(prefFile));
     312            tmpFile.delete();
     313
    303314        } catch (final IOException e) {
    304315            e.printStackTrace();
    305316            // do not message anything, since this can be called from strange
     
    307318        }
    308319    }
    309320
     321    /**
     322     * Simple file copy function that will overwrite the target file
     323     * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA)
     324     * @param in
     325     * @param out
     326     * @throws IOException
     327     */
     328    public static void copyFile(File in, File out) throws IOException  {
     329        FileChannel inChannel = new FileInputStream(in).getChannel();
     330        FileChannel outChannel = new FileOutputStream(out).getChannel();
     331        try {
     332            inChannel.transferTo(0, inChannel.size(),
     333                    outChannel);
     334        }
     335        catch (IOException e) {
     336            throw e;
     337        }
     338        finally {
     339            if (inChannel != null) {
     340                inChannel.close();
     341            }
     342            if (outChannel != null) {
     343                outChannel.close();
     344            }
     345        }
     346    }
     347
     348
    310349    public void load() throws IOException {
    311350        properties.clear();
    312351        final BufferedReader in = new BufferedReader(new InputStreamReader(