Author: toad
Date: 2006-02-21 17:13:00 +0000 (Tue, 21 Feb 2006)
New Revision: 8085

Modified:
   trunk/freenet/src/freenet/config/FilePersistentConfig.java
   trunk/freenet/src/freenet/node/Version.java
   trunk/freenet/src/freenet/support/SimpleFieldSet.java
   trunk/freenet/src/freenet/support/io/LineReadingInputStream.java
Log:
455:
Much more tolerant of slightly corrupt config files (e.g. no End).
If there is no freenet.ini but is a freenet.ini.tmp, read that.

Modified: trunk/freenet/src/freenet/config/FilePersistentConfig.java
===================================================================
--- trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-02-21 
16:58:24 UTC (rev 8084)
+++ trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-02-21 
17:13:00 UTC (rev 8085)
@@ -2,6 +2,7 @@

 import java.io.BufferedInputStream;
 import java.io.BufferedWriter;
+import java.io.EOFException;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -32,16 +33,37 @@
        public FilePersistentConfig(File f) throws IOException {
                this.filename = f;
                this.tempFilename = new File(f.getPath()+".tmp");
-               if(f.exists()) {
-                       if(!f.canWrite()) {
-                               Logger.error(this, "Warning: Cannot write to 
config file!");
-                               System.err.println("Warning: Cannot write to 
config file.");
-                       }
+               boolean filenameExists = f.exists();
+               boolean tempFilenameExists = tempFilename.exists();
+               if(filenameExists && !filename.canWrite()) {
+                       Logger.error(this, "Warning: Cannot write to config 
file: "+filename);
+                       System.err.println("Warning: Cannot write to config 
file: "+filename);
+               }
+               if(tempFilenameExists && !tempFilename.canWrite()) {
+                       Logger.error(this, "Warning: Cannot write to config 
tempfile: "+tempFilename);
+                       System.err.println("Warning: Cannot write to config 
tempfile: "+tempFilename);
+               }
+               if(filenameExists) {
                        if(f.canRead()) {
                                try {
-                                       initialLoad();
+                                       initialLoad(filename);
+                                       return;
                                } catch (FileNotFoundException e) {
                                        System.err.println("No config file 
found, creating new: "+f);
+                               } catch (EOFException e) {
+                                       System.err.println("Empty config file 
"+f);
+                               }
+                               // Other IOE's indicate a more serious problem.
+                       } else {
+                               throw new IOException("Cannot read config 
file");
+                       }
+               }
+               if(tempFilename.exists()) {
+                       if(tempFilename.canRead()) {
+                               try {
+                                       initialLoad(tempFilename);
+                               } catch (FileNotFoundException e) {
+                                       System.err.println("No config file 
found, creating new: "+f);
                                } // Other IOE's indicate a more serious 
problem.
                        } else {
                                throw new IOException("Cannot read config 
file");
@@ -53,12 +75,12 @@

        /** Load the config file into a SimpleFieldSet. 
         * @throws IOException */
-       private void initialLoad() throws IOException {
-               FileInputStream fis = new FileInputStream(filename);
+       private void initialLoad(File toRead) throws IOException {
+               FileInputStream fis = new FileInputStream(toRead);
                BufferedInputStream bis = new BufferedInputStream(fis);
                try {
                        LineReadingInputStream lis = new 
LineReadingInputStream(bis);
-                       origConfigFileContents = new SimpleFieldSet(lis, 4096, 
256, true);
+                       origConfigFileContents = new SimpleFieldSet(lis, 4096, 
256, true, true);
                } finally {
                        try {
                                fis.close();

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-02-21 16:58:24 UTC (rev 
8084)
+++ trunk/freenet/src/freenet/node/Version.java 2006-02-21 17:13:00 UTC (rev 
8085)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 454;
+       private static final int buildNumber = 455;

        /** Oldest build of Fred we will talk to */
        private static final int lastGoodBuild = 403;

Modified: trunk/freenet/src/freenet/support/SimpleFieldSet.java
===================================================================
--- trunk/freenet/src/freenet/support/SimpleFieldSet.java       2006-02-21 
16:58:24 UTC (rev 8084)
+++ trunk/freenet/src/freenet/support/SimpleFieldSet.java       2006-02-21 
17:13:00 UTC (rev 8085)
@@ -32,10 +32,10 @@
         read(br);
     }

-    public SimpleFieldSet(LineReader lis, int maxLineLength, int 
lineBufferSize, boolean multiLevel) throws IOException {
+    public SimpleFieldSet(LineReader lis, int maxLineLength, int 
lineBufferSize, boolean multiLevel, boolean tolerant) throws IOException {
        map = new HashMap();
        this.multiLevel = multiLevel;
-       read(lis, maxLineLength, lineBufferSize);
+       read(lis, maxLineLength, lineBufferSize, tolerant);
     }

     /**
@@ -110,14 +110,19 @@
      * blah=blah
      * End
      */
-    private void read(LineReader br, int maxLength, int bufferSize) throws 
IOException {
+    private void read(LineReader br, int maxLength, int bufferSize, boolean 
tolerant) throws IOException {
         boolean firstLine = true;
         while(true) {
             String line = br.readLine(maxLength, bufferSize);
             if(line == null) {
                 if(firstLine) throw new EOFException();
-                throw new IOException();
+                if(tolerant)
+                       Logger.error(this, "No end marker");
+                else
+                       throw new IOException("No end marker");
+                return;
             }
+            if(line.length() == 0 && tolerant) continue; // ignore
             firstLine = false;
             int index = line.indexOf('=');
             if(index >= 0) {

Modified: trunk/freenet/src/freenet/support/io/LineReadingInputStream.java
===================================================================
--- trunk/freenet/src/freenet/support/io/LineReadingInputStream.java    
2006-02-21 16:58:24 UTC (rev 8084)
+++ trunk/freenet/src/freenet/support/io/LineReadingInputStream.java    
2006-02-21 17:13:00 UTC (rev 8085)
@@ -15,13 +15,16 @@
        }

        /**
-        * Read a line of US-ASCII. Used for e.g. HTTP.
+        * Read a line of US-ASCII. Used for e.g. HTTP. @return Null if end of 
file.
         */
        public String readLine(int maxLength, int bufferSize) throws 
IOException {
                StringBuffer sb = new StringBuffer(bufferSize);
                while(true) {
                        int x = read();
-                       if(x == -1) throw new EOFException();
+                       if(x == -1) {
+                               if(sb.length() == 0) return null;
+                               return sb.toString();
+                       }
                        char c = (char) x;
                        if(c == '\n') {
                                if(sb.length() > 0) {


Reply via email to