Menu

#215 Full hard drive loses data

2.0
open
nobody
None
5
2023-03-21
2023-03-21
anon
No

As in the title.

Similar to https://sourceforge.net/p/rachota/bugs/148/ and https://sourceforge.net/p/rachota/bugs/55/ I suppose, just a different trigger (full hard drive).

Discussion

  • anon

    anon - 2023-03-21

    I think this should be sufficient to fix the issue (although not all are necessary)

    commit f14645021e3358ff3599e39cfc1bd775a7593127
    
        Use temporary file for writing
    
    diff --git a/src/main/java/org/cesilko/rachota/core/Plan.java b/src/main/java/org/cesilko/rachota/core/Plan.java
    index 9b2f009..b91b274 100644
    --- a/src/main/java/org/cesilko/rachota/core/Plan.java
    +++ b/src/main/java/org/cesilko/rachota/core/Plan.java
    @@ -355,9 +355,10 @@ public class Plan {
             String location = (String) Settings.getDefault().getSetting("userDir");
             if (isBackup) location = location + File.separator + "backup_diary.xml";
             else location = location + File.separator + "diary_" + year + "_" + week + ".xml";
    +        File tmp_location = new File(location + ".tmp");
             try {
                 String encoding = (String) Settings.getDefault().getSetting("systemEncoding");
    -            PrintStream stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(location)), false, encoding);
    +            PrintStream stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(tmp_location)), false, encoding);
                 stream.println("<?xml version=\"1.1\" encoding=\"" + encoding + "\"?>");
                 stream.println("<!--");
                 stream.println("    Rachota 2.4 diary file - editing not recommended");
    @@ -377,6 +378,7 @@ public class Plan {
                 stream.println("</week>");
                 stream.flush();
                 stream.close();
    +            tmp_location.renameTo(new File(location));
             } catch (IOException e) {
                 JOptionPane.showMessageDialog(null, Translator.getTranslation("ERROR.WRITE_ERROR", new String[] {location}), Translator.getTranslation("ERROR.ERROR_TITLE"), JOptionPane.ERROR_MESSAGE);
             }
    
    commit 9eec422007da0ed0552cadc9f591c3905bf43d9d
    
        Backing up 1 instead of 2 weeks
    
    diff --git a/src/main/java/org/cesilko/rachota/core/Plan.java b/src/main/java/org/cesilko/rachota/core/Plan.java
    index 4e512a7..9b2f009 100644
    --- a/src/main/java/org/cesilko/rachota/core/Plan.java
    +++ b/src/main/java/org/cesilko/rachota/core/Plan.java
    @@ -246,7 +246,7 @@ public class Plan {
         public static boolean savePlan() {
             Plan plan = Plan.getDefault();
             Calendar calendar = Calendar.getInstance();
    -        calendar.add(Calendar.WEEK_OF_YEAR, -2);
    +        calendar.add(Calendar.WEEK_OF_YEAR, -1);
             int thisWeek = calendar.get(Calendar.WEEK_OF_YEAR);
             int thisYear = calendar.get(Calendar.YEAR);
             String nextWeekToSave = plan.getNextWeekToSave(thisYear, thisWeek);
    
    commit 64c407c0b760756407aeb29ca1114aa7ab951979
    
        Avoid flushing diaryFile every line
    
    diff --git a/src/main/java/org/cesilko/rachota/core/Plan.java b/src/main/java/org/cesilko/rachota/core/Plan.java
    index ce69f46..4e512a7 100644
    --- a/src/main/java/org/cesilko/rachota/core/Plan.java
    +++ b/src/main/java/org/cesilko/rachota/core/Plan.java
    @@ -357,7 +357,7 @@ public class Plan {
             else location = location + File.separator + "diary_" + year + "_" + week + ".xml";
             try {
                 String encoding = (String) Settings.getDefault().getSetting("systemEncoding");
    -            PrintStream stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(location)), true, encoding);
    +            PrintStream stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(location)), false, encoding);
                 stream.println("<?xml version=\"1.1\" encoding=\"" + encoding + "\"?>");
                 stream.println("<!--");
                 stream.println("    Rachota 2.4 diary file - editing not recommended");
    

    There are 3 changes there, one implement the temporary file for writing, one avoiding overwriting the past week's data, the last one is just a performance improvement (it avoids flushing the file for each line).

     
  • anon

    anon - 2023-03-21

    I haven't tested the modified code again (don't want to fill up my hard drive just to test), but it should work.

     
  • anon

    anon - 2023-03-21

    Can we also not save the settings every 30 seconds? It shouldn't ever be necessary, the only time settings is changed is when settings dialog is closed and OK button is pressed...

    (and if something is corrupted this will corrupt the settings file also)

    commit 476ce5f7d6cc1a85ce17cd679df0adc6995a0144
    
        Avoid saving settings every 30 seconds
    
    diff --git a/src/main/java/org/cesilko/rachota/core/Task.java b/src/main/java/org/cesilko/rachota/c>
    index 5e554fd..ea5d1a3 100644
    --- a/src/main/java/org/cesilko/rachota/core/Task.java
    +++ b/src/main/java/org/cesilko/rachota/core/Task.java
    @@ -428,7 +428,6 @@ public class Task implements ClockListener {
             int savingPeriod = Integer.parseInt((String) Settings.getDefault().getSetting("savingPerio>
             if (lastSaving > savingPeriod) {
                 Plan.savePlan();
    -            Settings.saveSettings();
                 lastSaving = 0;
             }
             long backupAge = new Date().getTime() - Long.parseLong(System.getProperty("backupCreated")>
    
     

Log in to post a comment.