Author: toad
Date: 2006-08-31 12:58:56 +0000 (Thu, 31 Aug 2006)
New Revision: 10303

Modified:
   trunk/freenet/src/freenet/config/FilePersistentConfig.java
Log:
Minor improvements to freenet.ini persistence code.
It is unlikely that this fixes the occasionally-losing-the-port bug, but it 
should at least provide better logging next time.

Modified: trunk/freenet/src/freenet/config/FilePersistentConfig.java
===================================================================
--- trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-08-31 
01:22:46 UTC (rev 10302)
+++ trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-08-31 
12:58:56 UTC (rev 10303)
@@ -33,7 +33,7 @@
        public FilePersistentConfig(File f) throws IOException {
                this.filename = f;
                this.tempFilename = new File(f.getPath()+".tmp");
-               boolean filenameExists = f.exists();
+               boolean filenameExists = filename.exists();
                boolean tempFilenameExists = tempFilename.exists();
                if(filenameExists && !filename.canWrite()) {
                        Logger.error(this, "Warning: Cannot write to config 
file: "+filename);
@@ -49,12 +49,14 @@
                                        initialLoad(filename);
                                        return;
                                } catch (FileNotFoundException e) {
-                                       System.err.println("No config file 
found, creating new: "+f);
+                                       System.err.println("Cannot open config 
file "+filename+" : "+e+" - checking for temp file "+tempFilename);
                                } catch (EOFException e) {
-                                       System.err.println("Empty config file 
"+f);
+                                       System.err.println("Empty config file 
"+filename+" (end of file)");
                                }
                                // Other IOE's indicate a more serious problem.
                        } else {
+                               // We probably won't be able to write it either.
+                               System.err.println("Cannot read config file 
"+filename);
                                throw new IOException("Cannot read config 
file");
                        }
                }
@@ -63,14 +65,14 @@
                                try {
                                        initialLoad(tempFilename);
                                } catch (FileNotFoundException e) {
-                                       System.err.println("No config file 
found, creating new: "+f);
+                                       System.err.println("Cannot open temp 
config file either: "+tempFilename+" : "+e);
                                } // Other IOE's indicate a more serious 
problem.
                        } else {
-                               throw new IOException("Cannot read config 
file");
+                               System.err.println("Cannot read (temp) config 
file "+tempFilename);
+                               throw new IOException("Cannot read (temp) 
config file "+tempFilename);
                        }
-               } else {
-                       System.err.println("No config file found, creating new: 
"+f);
                }
+               System.err.println("No config file found, creating new: "+f);
        }

        /** Load the config file into a SimpleFieldSet. 
@@ -126,14 +128,26 @@
                if(Logger.shouldLog(Logger.MINOR, this))
                        Logger.minor(this, "fs = "+fs);
                FileOutputStream fos = new FileOutputStream(tempFilename);
-               BufferedWriter bw = new BufferedWriter(new 
OutputStreamWriter(fos, "UTF-8"));
-               synchronized(this) {
-                       fs.writeTo(bw);
+               try {
+                       BufferedWriter bw = new BufferedWriter(new 
OutputStreamWriter(fos, "UTF-8"));
+                       synchronized(this) {
+                               fs.writeTo(bw);
+                       }
+                       bw.close();
+               } catch (IOException e) {
+                       fos.close();
+                       throw e;
                }
-               bw.close();
                if(!tempFilename.renameTo(filename)) {
-                       filename.delete();
-                       tempFilename.renameTo(filename);
+                       if(!filename.delete()) {
+                               Logger.error(this, "Could not delete old config 
file "+filename);
+                       }
+                       if(!tempFilename.renameTo(filename)) {
+                               Logger.error(this, "Could not move new config 
file "+tempFilename+" over old "+filename);
+                               System.err.println("Written and moved 
"+filename);
+                       }
+               } else {
+                       System.err.println("Written "+filename);
                }
        }



Reply via email to