[freenet-dev] [freenet-cvs] r15915 - in trunk/freenet/src/freenet: clients/http/bookmark config l10n node node/fcp node/updater pluginmanager support support/io

Matthew Toseland toad at amphibian.dyndns.org
Wed Nov 28 14:21:23 UTC 2007


The title's are all blank for the default bookmarks for me: the "description" 
is just part of the name.

On Thursday 22 November 2007 07:55, you wrote:
> Author: nextgens
> Date: 2007-11-22 07:55:28 +0000 (Thu, 22 Nov 2007)
> New Revision: 15915
> 
> Modified:
>    trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java
>    trunk/freenet/src/freenet/config/FilePersistentConfig.java
>    trunk/freenet/src/freenet/l10n/L10n.java
>    trunk/freenet/src/freenet/node/Node.java
>    trunk/freenet/src/freenet/node/OpennetManager.java
>    trunk/freenet/src/freenet/node/PeerManager.java
>    trunk/freenet/src/freenet/node/Persister.java
>    trunk/freenet/src/freenet/node/fcp/ClientGet.java
>    trunk/freenet/src/freenet/node/fcp/FCPServer.java
>    trunk/freenet/src/freenet/node/updater/RevocationChecker.java
>    trunk/freenet/src/freenet/pluginmanager/PluginManager.java
>    trunk/freenet/src/freenet/support/FileLoggerHook.java
>    trunk/freenet/src/freenet/support/SimpleFieldSet.java
>    trunk/freenet/src/freenet/support/io/FileUtil.java
> Log:
> 	* Replace most calls to File.renameTo by FileUtil.renameTo
> 	* Restore the l10n overrides from a backup file if it exists
> 	* Restore bookmarks from a backup if it exists
> 
> It appears to work... but I didn't test it enough to be sure. Make backups 
before updating ;)
> 
> Modified: 
trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java
> ===================================================================
> --- trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java	
2007-11-21 23:43:52 UTC (rev 15914)
> +++ trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java	
2007-11-22 07:55:28 UTC (rev 15915)
> @@ -19,6 +19,8 @@
>  import freenet.support.StringArray;
>  import freenet.support.URLEncodedFormatException;
>  
> +import freenet.support.io.Closer;
> +import freenet.support.io.FileUtil;
>  import java.io.File;
>  import java.io.FileWriter;
>  import java.io.IOException;
> @@ -32,6 +34,7 @@
>      public static final BookmarkCategory PROTECTED_CATEGORY = new 
BookmarkCategory("/protected");
>      private final HashMap bookmarks = new HashMap();
>      private final File bookmarksFile = new 
File("bookmarks.dat").getAbsoluteFile();
> +    private final File backupBookmarksFile = new 
File(bookmarksFile.getParentFile(), bookmarksFile.getName()+".bak");
>      private boolean isSavingBookmarks = false;
>  
>      public BookmarkManager(NodeClientCore n, SimpleFieldSet oldConfig) {
> @@ -89,14 +92,26 @@
>                  storeBookmarks();
>              }
>  
> -            if (bookmarksFile.exists() && bookmarksFile.canRead() && 
bookmarksFile.length() > 0) {
> -                Logger.normal(this, "Attempting to read the bookmark file 
from " + bookmarksFile.toString());
> -                SimpleFieldSet sfs = SimpleFieldSet.readFrom(bookmarksFile, 
false, true);
> -                readBookmarks(MAIN_CATEGORY, sfs);
> -            }
> +            // Read the backup file if necessary
> +            if(!bookmarksFile.exists() || bookmarksFile.length() == 0)
> +                throw new IOException();
> +            Logger.normal(this, "Attempting to read the bookmark file 
from " + bookmarksFile.toString());
> +            SimpleFieldSet sfs = SimpleFieldSet.readFrom(bookmarksFile, 
false, true);
> +            readBookmarks(MAIN_CATEGORY, sfs);
>          } catch (MalformedURLException mue) {
>          } catch (IOException ioe) {
>              Logger.error(this, "Error reading the bookmark file (" + 
bookmarksFile.toString() + "):" + ioe.getMessage(), ioe);
> +            
> +            try {
> +                if (backupBookmarksFile.exists() && 
backupBookmarksFile.canRead() && backupBookmarksFile.length() > 0) {
> +                    Logger.normal(this, "Attempting to read the backup 
bookmark file from " + backupBookmarksFile.toString());
> +                    SimpleFieldSet sfs = 
SimpleFieldSet.readFrom(backupBookmarksFile, false, true);
> +                    readBookmarks(MAIN_CATEGORY, sfs);
> +                } else
> +                    Logger.error(this, "We couldn't find the backup 
either! - "+FileUtil.getCanonicalFile(backupBookmarksFile));
> +            } catch (IOException e) {
> +                Logger.error(this, "Error reading the backup bookmark 
file !" + e.getMessage(), e);
> +            }
>          }
>      }
>  
> @@ -351,21 +366,16 @@
>          }
>          FileWriter fw = null;
>          try {
> -            File tmp = File.createTempFile("bookmark", ".bak", 
bookmarksFile.getParentFile());
> -            fw = new FileWriter(tmp);
> +            fw = new FileWriter(backupBookmarksFile);
>              sfs.writeTo(fw);
> -            if (!tmp.renameTo(bookmarksFile)) {
> -                Logger.error(this, "Unable to rename " + tmp.toString() + " 
to " + bookmarksFile.toString());
> +            
> +            if (!FileUtil.renameTo(backupBookmarksFile, bookmarksFile)) {
> +                Logger.error(this, "Unable to rename " + 
backupBookmarksFile.toString() + " to " + bookmarksFile.toString());
>              }
>          } catch (IOException ioe) {
>              Logger.error(this, "An error has occured saving the bookmark 
file :" + ioe.getMessage(), ioe);
>          } finally {
> -            try {
> -                if (fw != null) {
> -                    fw.close();
> -                }
> -            } catch (IOException e) {
> -            }
> +            Closer.close(fw);
>              
>              synchronized (bookmarks) {
>                  isSavingBookmarks = false;
> 
> Modified: trunk/freenet/src/freenet/config/FilePersistentConfig.java
> ===================================================================
> --- trunk/freenet/src/freenet/config/FilePersistentConfig.java	2007-11-21 
23:43:52 UTC (rev 15914)
> +++ trunk/freenet/src/freenet/config/FilePersistentConfig.java	2007-11-22 
07:55:28 UTC (rev 15915)
> @@ -15,6 +15,7 @@
>  
>  import freenet.support.Logger;
>  import freenet.support.SimpleFieldSet;
> +import freenet.support.io.FileUtil;
>  import freenet.support.io.LineReadingInputStream;
>  
>  /**
> @@ -146,19 +147,7 @@
>  			fos.close();
>  			throw e;
>  		}
> -		if(!tempFilename.renameTo(filename)) {
> -			if(!filename.delete()) {
> -				Logger.error(this, "Could not delete old config file "+filename);
> -				System.err.println("Could not delete old config file "+filename+" - we 
need to delete it in order to replace it with the new config 
file "+tempFilename);
> -			}
> -			if(!tempFilename.renameTo(filename)) {
> -				Logger.error(this, "Could not move new config file "+tempFilename+" 
over old "+filename);
> -				System.err.println("Could not move new config file "+tempFilename+" 
over old "+filename);
> -			} else {
> -				System.err.println("Written "+tempFilename+" and moved to "+filename);
> -			}
> -		} else {
> -			System.err.println("Written "+filename);
> -		}
> +                
> +                FileUtil.renameTo(tempFilename, filename);
>  	}
>  }
> 
> Modified: trunk/freenet/src/freenet/l10n/L10n.java
> ===================================================================
> --- trunk/freenet/src/freenet/l10n/L10n.java	2007-11-21 23:43:52 UTC (rev 
15914)
> +++ trunk/freenet/src/freenet/l10n/L10n.java	2007-11-22 07:55:28 UTC (rev 
15915)
> @@ -3,7 +3,6 @@
>  * http://www.gnu.org/ for further details of the GPL. */
>  package freenet.l10n;
>  
> -import java.io.BufferedOutputStream;
>  import java.io.File;
>  import java.io.FileOutputStream;
>  import java.io.IOException;
> @@ -15,6 +14,8 @@
>  import freenet.support.HTMLNode;
>  import freenet.support.Logger;
>  import freenet.support.SimpleFieldSet;
> +import freenet.support.io.Closer;
> +import freenet.support.io.FileUtil;
>  
>  /**
>  * This class provides a trivial internationalization framework to a Freenet 
node.
> @@ -50,11 +51,18 @@
>  		File tmpFile = new File(L10n.PREFIX + selected + L10n.OVERRIDE_SUFFIX);
>  		
>  		try {
> -			if(tmpFile.exists() && tmpFile.canRead()) {
> +			if(tmpFile.exists() && tmpFile.canRead() && tmpFile.length() > 0) {
>  				Logger.normal(this, "Override file detected : let's try to load it");
>  				translationOverride = SimpleFieldSet.readFrom(tmpFile, false, false);
> -			} else
> +			} else {
> +                                // try to restore a backup
> +                                File backup = new 
File(tmpFile.getParentFile(), tmpFile.getName()+".bak");
> +                                if(backup.exists() && backup.length() > 0) 
{
> +                                    Logger.normal(this, "Override-backup 
file detected : let's try to load it");
> +                                    translationOverride = 
SimpleFieldSet.readFrom(backup, false, false);
> +                                }
>  				translationOverride = null;
> +                        }
>  			
>  		} catch (IOException e) {
>  			translationOverride = null;
> @@ -125,32 +133,22 @@
>  	
>  	private static void _saveTranslationFile() {
>  		FileOutputStream fos = null;
> -		BufferedOutputStream bos = null;
>  		File finalFile = new File(L10n.PREFIX + L10n.getSelectedLanguage() + 
L10n.OVERRIDE_SUFFIX);
>  		
>  		try {
>  			// We don't set deleteOnExit on it : if the save operation fails, we 
want a backup
> -			File tempFile = new File(finalFile.getPath() + "-" + 
System.currentTimeMillis() + ".tmp");
> +			File tempFile = new File(finalFile.getParentFile(), 
finalFile.getName()+".bak");
>  			Logger.minor("L10n", "The temporary filename is : " + tempFile);
>  			
>  			fos = new FileOutputStream(tempFile);
> -			bos = new BufferedOutputStream(fos);
> +                        L10n.translationOverride.writeTo(fos);
>  			
> -			bos.write(L10n.translationOverride.toOrderedString().getBytes("UTF-8"));
> -			bos.flush();
> -			
> -			
> -			tempFile.renameTo(finalFile);
> -			tempFile.delete();
> -			
> +			FileUtil.renameTo(tempFile, finalFile);
>  			Logger.normal("L10n", "Override file saved successfully!");
>  		} catch (IOException e) {
>  			Logger.error("L10n", "Error while saving the translation override: "+ 
e.getMessage(), e);
>  		} finally {
> -			try {
> -				if(bos != null) bos.close();
> -				if(fos != null) fos.close();
> -			} catch (IOException e) {}
> +			Closer.close(fos);
>  		}
>  	}
>  	
> 
> Modified: trunk/freenet/src/freenet/node/Node.java
> ===================================================================
> --- trunk/freenet/src/freenet/node/Node.java	2007-11-21 23:43:52 UTC (rev 
15914)
> +++ trunk/freenet/src/freenet/node/Node.java	2007-11-22 07:55:28 UTC (rev 
15915)
> @@ -108,6 +108,8 @@
>  import freenet.support.api.LongCallback;
>  import freenet.support.api.ShortCallback;
>  import freenet.support.api.StringCallback;
> +import freenet.support.io.Closer;
> +import freenet.support.io.FileUtil;
>  import freenet.support.transport.ip.HostnameSyntaxException;
>  
>  /**
> @@ -479,20 +481,9 @@
>  			BufferedWriter bw = new BufferedWriter(osr);
>  			fs.writeTo(bw);
>  			bw.close();
> -			if(!backup.renameTo(orig)) {
> -				orig.delete();
> -				if(!backup.renameTo(orig)) {
> -					Logger.error(this, "Could not rename new node file "+backup+" 
to "+orig);
> -				}
> -			}
> +                        FileUtil.renameTo(backup, orig);
>  		} catch (IOException e) {
> -			if(fos != null) {
> -				try {
> -					fos.close();
> -				} catch (IOException e1) {
> -					Logger.error(this, "Cannot close "+backup+": "+e1, e1);
> -				}
> -			}
> +                        Closer.close(fos);
>  		}
>  	}
>  
> 
> Modified: trunk/freenet/src/freenet/node/OpennetManager.java
> ===================================================================
> --- trunk/freenet/src/freenet/node/OpennetManager.java	2007-11-21 23:43:52 
UTC (rev 15914)
> +++ trunk/freenet/src/freenet/node/OpennetManager.java	2007-11-22 07:55:28 
UTC (rev 15915)
> @@ -32,6 +32,8 @@
>  import freenet.support.SimpleFieldSet;
>  import freenet.support.SizeUtil;
>  import freenet.support.io.ByteArrayRandomAccessThing;
> +import freenet.support.io.Closer;
> +import freenet.support.io.FileUtil;
>  import freenet.support.transport.ip.HostnameSyntaxException;
>  
>  /**
> @@ -152,21 +154,10 @@
>  			BufferedWriter bw = new BufferedWriter(osr);
>  			fs.writeTo(bw);
>  			bw.close();
> -			if(!backup.renameTo(orig)) {
> -				orig.delete();
> -				if(!backup.renameTo(orig)) {
> -					Logger.error(this, "Could not rename new node file "+backup+" 
to "+orig);
> -				}
> -			}
> +                        FileUtil.renameTo(backup, orig);
>  		} catch (IOException e) {
> -			if(fos != null) {
> -				try {
> -					fos.close();
> -				} catch (IOException e1) {
> -					Logger.error(this, "Cannot close "+backup+": "+e1, e1);
> -				}
> -			}
> -		}
> +                        Closer.close(fos);
> +                }
>  	}
>  
>  	private void readFile(File filename) throws IOException {
> 
> Modified: trunk/freenet/src/freenet/node/PeerManager.java
> ===================================================================
> --- trunk/freenet/src/freenet/node/PeerManager.java	2007-11-21 23:43:52 UTC 
(rev 15914)
> +++ trunk/freenet/src/freenet/node/PeerManager.java	2007-11-22 07:55:28 UTC 
(rev 15915)
> @@ -34,6 +34,7 @@
>  import freenet.support.Logger;
>  import freenet.support.ShortBuffer;
>  import freenet.support.SimpleFieldSet;
> +import freenet.support.io.FileUtil;
>  
>  /**
>   * @author amphibian
> @@ -886,13 +887,7 @@
>                  return; // don't overwrite old file!
>              }
>              File fnam = new File(filename);
> -            if (!new File(f).renameTo(fnam)) {
> -                fnam.delete();
> -                if (!new File(f).renameTo(fnam)) {
> -                    Logger.error(this, "Could not rename " + f + " to "
> -                            + filename + " writing peers");
> -                }
> -            }
> +            FileUtil.renameTo(new File(f), fnam);
>          }
>      }
>  
> 
> Modified: trunk/freenet/src/freenet/node/Persister.java
> ===================================================================
> --- trunk/freenet/src/freenet/node/Persister.java	2007-11-21 23:43:52 UTC 
(rev 15914)
> +++ trunk/freenet/src/freenet/node/Persister.java	2007-11-22 07:55:28 UTC 
(rev 15915)
> @@ -12,6 +12,7 @@
>  import freenet.support.Logger;
>  import freenet.support.OOMHandler;
>  import freenet.support.SimpleFieldSet;
> +import freenet.support.io.FileUtil;
>  
>  class Persister implements Runnable {
>  
> @@ -84,21 +85,8 @@
>  				Logger.error(this, "Caught while closing: "+e, e);
>  				return;
>  			}
> -			// Try an atomic rename
> -			if(!persistTemp.renameTo(persistTarget)) {
> -				// Not supported on some systems (Windows)
> -				if(!persistTarget.delete()) {
> -					if(persistTarget.exists()) {
> -						Logger.error(this, "Could not delete "+persistTarget+" - check 
permissions");
> -					}
> -				}
> -				if(!persistTemp.renameTo(persistTarget)) {
> -					Logger.error(this, "Could not rename "+persistTemp+" 
to "+persistTarget+
> -							(persistTarget.exists() ? " (target exists)" : "")+
> -							(persistTemp.exists() ? " (source exists)" : "")+
> -							" - check permissions");
> -				}
> -			}
> +
> +                        FileUtil.renameTo(persistTemp, persistTarget);
>  		} catch (FileNotFoundException e) {
>  			Logger.error(this, "Could not store throttle data to disk: "+e, e);
>  			return;
> 
> Modified: trunk/freenet/src/freenet/node/fcp/ClientGet.java
> ===================================================================
> --- trunk/freenet/src/freenet/node/fcp/ClientGet.java	2007-11-21 23:43:52 
UTC (rev 15914)
> +++ trunk/freenet/src/freenet/node/fcp/ClientGet.java	2007-11-22 07:55:28 
UTC (rev 15915)
> @@ -27,6 +27,7 @@
>  import freenet.support.api.Bucket;
>  import freenet.support.io.CannotCreateFromFieldSetException;
>  import freenet.support.io.FileBucket;
> +import freenet.support.io.FileUtil;
>  import freenet.support.io.NullBucket;
>  import freenet.support.io.SerializableToFieldSetBucketUtil;
>  
> @@ -399,7 +400,7 @@
>  				// Write to temp file, then rename over filename
>  				FileOutputStream fos = null;
>  				boolean closed = false;
> -				if(!tempFile.renameTo(targetFile)) {
> +				if(!FileUtil.renameTo(tempFile, targetFile)) {
>  					postFetchProtocolErrorMessage = new 
ProtocolErrorMessage(ProtocolErrorMessage.COULD_NOT_RENAME_FILE, false, null, 
identifier, global);
>  					// Don't delete temp file, user might want it.
>  				}
> 
> Modified: trunk/freenet/src/freenet/node/fcp/FCPServer.java
> ===================================================================
> --- trunk/freenet/src/freenet/node/fcp/FCPServer.java	2007-11-21 23:43:52 
UTC (rev 15914)
> +++ trunk/freenet/src/freenet/node/fcp/FCPServer.java	2007-11-22 07:55:28 
UTC (rev 15915)
> @@ -45,6 +45,8 @@
>  import freenet.support.api.IntCallback;
>  import freenet.support.api.LongCallback;
>  import freenet.support.api.StringCallback;
> +import freenet.support.io.Closer;
> +import freenet.support.io.FileUtil;
>  
>  /**
>   * FCP server process.
> @@ -576,28 +578,32 @@
>  		try {
>  			synchronized(persistenceSync) {
>  				toFree = core.persistentTempBucketFactory.grabBucketsToFree();
> +                                
> +                                File compressedTemp = new 
File(persistentDownloadsTempFile+".gz");
> +				File compressedFinal = new 
File(persistentDownloadsFile.toString()+".gz");
> +                                FileOutputStream fos = null;
> +                                BufferedOutputStream bos = null;
> +                                GZIPOutputStream gos = null;
> +                                OutputStreamWriter osw = null;
> +                                BufferedWriter w = null;
>  				try {
> -					File compressedTemp = new File(persistentDownloadsTempFile+".gz");
> -					File compressedFinal = new 
File(persistentDownloadsFile.toString()+".gz");
> -					FileOutputStream fos = new FileOutputStream(compressedTemp);
> -					BufferedOutputStream bos = new BufferedOutputStream(fos);
> -					GZIPOutputStream gos = new GZIPOutputStream(bos);
> -					OutputStreamWriter osw = new OutputStreamWriter(gos, "UTF-8");
> -					BufferedWriter w = new BufferedWriter(osw);
> +					fos = new FileOutputStream(compressedTemp);
> +					bos = new BufferedOutputStream(fos);
> +					gos = new GZIPOutputStream(bos);
> +					osw = new OutputStreamWriter(gos, "UTF-8");
> +					w = new BufferedWriter(osw);
>  					w.write(Integer.toString(persistentRequests.length)+ '\n');
>  					for(int i=0;i<persistentRequests.length;i++)
> -						persistentRequests[i].write(w);
> -					w.close();
> -					if(!compressedTemp.renameTo(compressedFinal)) {
> -						if(logMINOR) Logger.minor(this, "Rename failed");
> -						compressedFinal.delete();
> -						if(!compressedTemp.renameTo(compressedFinal)) {
> -							Logger.error(this, "Could not rename persisted requests temp 
file "+persistentDownloadsTempFile+".gz to "+persistentDownloadsFile);
> -						}
> -					}
> +						persistentRequests[i].write(w);                                        
>  				} catch (IOException e) {
>  					Logger.error(this, "Cannot write persistent requests to disk: "+e);
> +                                        Closer.close(w);
> +                                        Closer.close(osw);
> +                                        Closer.close(gos);
> +                                        Closer.close(bos);
> +                                        Closer.close(fos);
>  				}
> +                                FileUtil.renameTo(compressedTemp, 
compressedTemp);
>  			}
>  			if(logMINOR) Logger.minor(this, "Stored persistent requests");
>  		} finally {
> 
> Modified: trunk/freenet/src/freenet/node/updater/RevocationChecker.java
> ===================================================================
> --- trunk/freenet/src/freenet/node/updater/RevocationChecker.java	2007-11-21 
23:43:52 UTC (rev 15914)
> +++ trunk/freenet/src/freenet/node/updater/RevocationChecker.java	2007-11-22 
07:55:28 UTC (rev 15915)
> @@ -15,6 +15,7 @@
>  import freenet.node.RequestStarter;
>  import freenet.support.Logger;
>  import freenet.support.io.FileBucket;
> +import freenet.support.io.FileUtil;
>  
>  /**
>   * Fetches the revocation key. Each time it starts, it will try to fetch it 
until it has 3 DNFs. If it ever finds it, it will
> @@ -167,13 +168,7 @@
>  			Logger.error(this, "No temporary binary blob file moving it: may not be 
able to propagate revocation, bug???");
>  			return;
>  		}
> -		if(!tmpBlobFile.renameTo(blobFile)) {
> -			blobFile.delete();
> -			if(!tmpBlobFile.renameTo(blobFile)) {
> -				Logger.error(this, "Not able to rename binary blob for revocation 
fetcher: "+tmpBlobFile+" -> "+blobFile+" - may not be able to tell other 
peers about this revocation");
> -				System.err.println("Not able to rename binary blob for revocation 
fetcher: "+tmpBlobFile+" -> "+blobFile+" - may not be able to tell other 
peers about this revocation");
> -			}
> -		}
> +                FileUtil.renameTo(tmpBlobFile, blobFile);
>  	}
>  
>  	public void onFailure(FetchException e, ClientGetter state) {
> 
> Modified: trunk/freenet/src/freenet/pluginmanager/PluginManager.java
> ===================================================================
> --- trunk/freenet/src/freenet/pluginmanager/PluginManager.java	2007-11-21 
23:43:52 UTC (rev 15914)
> +++ trunk/freenet/src/freenet/pluginmanager/PluginManager.java	2007-11-22 
07:55:28 UTC (rev 15915)
> @@ -35,6 +35,7 @@
>  import freenet.support.api.HTTPRequest;
>  import freenet.support.api.StringArrCallback;
>  import freenet.support.io.Closer;
> +import freenet.support.io.FileUtil;
>  
>  public class PluginManager {
>  
> @@ -525,7 +526,7 @@
>  				Closer.close(pluginInputStream);
>  			}
>                          
> -			if (!tempPluginFile.renameTo(pluginFile)) {
> +			if (!FileUtil.renameTo(tempPluginFile, pluginFile)) {
>  				Logger.error(this, "could not rename temp file to plugin file");
>  				throw new PluginNotFoundException("could not rename temp file to plugin 
file");
>  			}
> 
> Modified: trunk/freenet/src/freenet/support/FileLoggerHook.java
> ===================================================================
> --- trunk/freenet/src/freenet/support/FileLoggerHook.java	2007-11-21 
23:43:52 UTC (rev 15914)
> +++ trunk/freenet/src/freenet/support/FileLoggerHook.java	2007-11-22 
07:55:28 UTC (rev 15915)
> @@ -1,7 +1,6 @@
>  package freenet.support;
>  
>  import java.io.BufferedOutputStream;
> -import java.io.ByteArrayOutputStream;
>  import java.io.DataInputStream;
>  import java.io.File;
>  import java.io.FileInputStream;
> @@ -10,7 +9,6 @@
>  import java.io.OutputStream;
>  import java.io.OutputStreamWriter;
>  import java.io.PrintStream;
> -import java.io.PrintWriter;
>  import java.net.InetAddress;
>  import java.text.DateFormat;
>  import java.text.SimpleDateFormat;
> @@ -27,6 +25,7 @@
>  import java.util.zip.GZIPOutputStream;
>  
>  import freenet.node.Version;
> +import freenet.support.io.FileUtil;
>  
>  /**
>   * Converted the old StandardLogger to Ian's loggerhook interface.
> @@ -300,8 +299,7 @@
>  											"Closing alt on change caught " + e);
>  								}
>  								if(previousFile != null) {
> -									previousFile.delete();
> -									latestFile.renameTo(previousFile);
> +                                                                        
FileUtil.renameTo(latestFile, previousFile);
>  									latestFile.delete();
>  								} else {
>  									latestFile.delete();
> @@ -505,8 +503,7 @@
>  		java.util.Arrays.sort(files);
>  		long lastStartTime = -1;
>  		File oldFile = null;
> -		previousFile.delete();
> -		latestFile.renameTo(previousFile);
> +                FileUtil.renameTo(latestFile, previousFile);
>  		boolean logMINOR = Logger.shouldLog(Logger.MINOR, this);
>  		for(int i=0;i<files.length;i++) {
>  			File f = files[i];
> 
> Modified: trunk/freenet/src/freenet/support/SimpleFieldSet.java
> ===================================================================
> --- trunk/freenet/src/freenet/support/SimpleFieldSet.java	2007-11-21 
23:43:52 UTC (rev 15914)
> +++ trunk/freenet/src/freenet/support/SimpleFieldSet.java	2007-11-22 
07:55:28 UTC (rev 15915)
> @@ -18,7 +18,12 @@
>  import java.util.Map;
>  
>  import freenet.node.FSParseException;
> +import freenet.support.io.Closer;
>  import freenet.support.io.LineReader;
> +import java.io.BufferedOutputStream;
> +import java.io.BufferedWriter;
> +import java.io.OutputStream;
> +import java.io.OutputStreamWriter;
>  
>  /**
>   * @author amphibian
> @@ -671,17 +676,40 @@
>  			SimpleFieldSet fs = new SimpleFieldSet(br, allowMultiple, shortLived);
>  			return fs;
>  		} finally {
> -			try {
> -				if(br != null) br.close();
> -				if(isr != null) isr.close();
> -				if(bis != null) bis.close();
> -			} catch (IOException e) {}			
> -		}
> +                        Closer.close(br);
> +                        Closer.close(isr);
> +                        Closer.close(bis);
> +                }
>  	}
>  	
>  	public static SimpleFieldSet readFrom(File f, boolean allowMultiple, 
boolean shortLived) throws IOException {
>  		return readFrom(new FileInputStream(f), allowMultiple, shortLived);
>  	}
> +        
> +        public void writeTo(OutputStream os) {
> +            BufferedOutputStream bos = null;
> +            OutputStreamWriter osw = null;
> +            BufferedWriter bw = null;
> +            
> +            try {
> +                bos = new BufferedOutputStream(os);
> +                try {
> +                    osw = new OutputStreamWriter(bos, "UTF-8");
> +                } catch (UnsupportedEncodingException e) {
> +                    Logger.error(SimpleFieldSet.class, "Impossible: " + e, 
e);
> +                    os.close();
> +                    return;
> +                }
> +                bw = new BufferedWriter(osw);
> +                writeTo(bw);
> +            } catch (IOException ioe) {
> +                Logger.error("SimpleFieldSet", "An exception has occured 
while saving the SFS :"+ioe.getMessage(), ioe);
> +            }finally {
> +                Closer.close(bw);
> +                Closer.close(osw);
> +                Closer.close(bos);
> +            }
> +        }
>  
>  	public int getInt(String key, int def) {
>  		String s = get(key);
> 
> Modified: trunk/freenet/src/freenet/support/io/FileUtil.java
> ===================================================================
> --- trunk/freenet/src/freenet/support/io/FileUtil.java	2007-11-21 23:43:52 
UTC (rev 15914)
> +++ trunk/freenet/src/freenet/support/io/FileUtil.java	2007-11-22 07:55:28 
UTC (rev 15915)
> @@ -4,7 +4,6 @@
>  package freenet.support.io;
>  
>  import java.io.BufferedInputStream;
> -import java.io.BufferedOutputStream;
>  import java.io.DataInputStream;
>  import java.io.EOFException;
>  import java.io.File;
> @@ -137,13 +136,35 @@
>  			if(fos != null) fos.close();
>  		}
>  		
> -		if(file.renameTo(target))
> +		if(FileUtil.renameTo(file, target))
>  			return true;
>  		else {
>  			file.delete();
>  			return false;
>  		}
>  	}
> +        
> +        public static boolean renameTo(File orig, File dest) {
> +            // Try an atomic rename
> +            // Shall we prevent symlink-race-conditions here ?
> +            
> +            if (!orig.renameTo(dest)) {
> +                // Not supported on some systems (Windows)
> +                if (!dest.delete()) {
> +                    if (dest.exists()) {
> +                        Logger.error("FileUtil", "Could not delete " + dest 
+ " - check permissions");
> +                    }
> +                }
> +                if (!orig.renameTo(dest)) {
> +                    Logger.error("FileUtil", "Could not rename " + orig + " 
to " + dest +
> +                            (dest.exists() ? " (target exists)" : "") +
> +                            (orig.exists() ? " (source exists)" : "") +
> +                            " - check permissions");
> +                    return false;
> +                }
> +            }
> +            return true;
> +        }
>  
>  	public static String sanitize(String s) {
>  		StringBuffer sb = new StringBuffer(s.length());
> 
> _______________________________________________
> cvs mailing list
> cvs at freenetproject.org
> http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs
> 
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://emu.freenetproject.org/pipermail/devl/attachments/20071128/c96bd93b/attachment.pgp 


More information about the Devl mailing list