Author: toad
Date: 2005-11-07 18:59:08 +0000 (Mon, 07 Nov 2005)
New Revision: 7492

Added:
   trunk/freenet/src/freenet/support/io/FilenameGenerator.java
Modified:
   trunk/freenet/src/freenet/client/ArchiveManager.java
   trunk/freenet/src/freenet/node/Node.java
   trunk/freenet/src/freenet/node/Version.java
   trunk/freenet/src/freenet/support/io/TempBucketFactory.java
Log:
141:
Compiles again.
Now all we have to do is make TMCI actually use the new fetch code.
Oh, and implement *all* of the insert code, fix a few bugs, implement some form 
of updatable keys... :)

Modified: trunk/freenet/src/freenet/client/ArchiveManager.java
===================================================================
--- trunk/freenet/src/freenet/client/ArchiveManager.java        2005-11-07 
18:32:32 UTC (rev 7491)
+++ trunk/freenet/src/freenet/client/ArchiveManager.java        2005-11-07 
18:59:08 UTC (rev 7492)
@@ -22,6 +22,7 @@
 import freenet.support.Logger;
 import freenet.support.PaddedEphemerallyEncryptedBucket;
 import freenet.support.io.FileBucket;
+import freenet.support.io.FilenameGenerator;

 /**
  * Cache of recently decoded archives:
@@ -47,17 +48,17 @@
         * @param cacheDir The directory in which to store cached data.
         * @param random A random source for the encryption keys used by stored 
files.
         */
-       ArchiveManager(int maxHandlers, long maxCachedData, long 
maxArchiveSize, long maxArchivedFileSize, int maxCachedElements, File cacheDir, 
RandomSource random) {
+       public ArchiveManager(int maxHandlers, long maxCachedData, long 
maxArchiveSize, long maxArchivedFileSize, int maxCachedElements, RandomSource 
random, FilenameGenerator filenameGenerator) {
                maxArchiveHandlers = maxHandlers;
                archiveHandlers = new LRUHashtable();
                cachedElements = new LRUHashtable();
                this.maxCachedElements = maxCachedElements;
                this.maxCachedData = maxCachedData;
-               this.cacheDir = cacheDir;
                storedData = new LRUHashtable();
                this.maxArchiveSize = maxArchiveSize;
                this.maxArchivedFileSize = maxArchivedFileSize;
                this.random = random;
+               this.filenameGenerator = filenameGenerator;
        }

        final RandomSource random;
@@ -97,10 +98,10 @@
        final long maxCachedData;
        /** Currently cached data in bytes */
        long cachedData;
-       /** Cache directory */
-       final File cacheDir;
        /** Map from ArchiveKey to ArchiveStoreElement */
        final LRUHashtable storedData;
+       /** Filename generator */
+       final FilenameGenerator filenameGenerator;

        /**
         * Create an archive handler. This does not need to know how to
@@ -353,10 +354,7 @@
         * go over the maximum size. Will obviously keep its key when we move 
it to main.
         */
        private TempStoreElement makeTempStoreBucket(long size) {
-               byte[] randomFilename = new byte[16]; // should be plenty
-               random.nextBytes(randomFilename);
-               String filename = HexUtil.bytesToHex(randomFilename);
-               File myFile = new File(cacheDir, filename);
+               File myFile = filenameGenerator.makeRandomFilename();
                FileBucket fb = new FileBucket(myFile, false, true, false);

                byte[] cipherKey = new byte[32];

Modified: trunk/freenet/src/freenet/node/Node.java
===================================================================
--- trunk/freenet/src/freenet/node/Node.java    2005-11-07 18:32:32 UTC (rev 
7491)
+++ trunk/freenet/src/freenet/node/Node.java    2005-11-07 18:59:08 UTC (rev 
7492)
@@ -54,6 +54,7 @@
 import freenet.support.Logger;
 import freenet.support.PaddedEphemerallyEncryptedBucketFactory;
 import freenet.support.SimpleFieldSet;
+import freenet.support.io.FilenameGenerator;
 import freenet.support.io.TempBucketFactory;

 /**
@@ -117,12 +118,15 @@
     final PacketSender ps;
     final NodeDispatcher dispatcher;
     final String filenamesPrefix;
+    final FilenameGenerator tempFilenameGenerator;
     static short MAX_HTL = 10;
-    private static final int EXIT_STORE_FILE_NOT_FOUND = 1;
-    private static final int EXIT_STORE_IOEXCEPTION = 2;
-    private static final int EXIT_STORE_OTHER = 3;
-    private static final int EXIT_USM_DIED = 4;
+    static final int EXIT_STORE_FILE_NOT_FOUND = 1;
+    static final int EXIT_STORE_IOEXCEPTION = 2;
+    static final int EXIT_STORE_OTHER = 3;
+    static final int EXIT_USM_DIED = 4;
     public static final int EXIT_YARROW_INIT_FAILED = 5;
+    static final int EXIT_TEMP_INIT_ERROR = 6;
+    
     public final long bootID;
     public final long startupTime;

@@ -130,6 +134,14 @@
     final ArchiveManager archiveManager;
     final BucketFactory tempBucketFactory;

+    // Client stuff that needs to be configged - FIXME
+    static final int MAX_ARCHIVE_HANDLERS = 200; // don't take up much RAM... 
FIXME
+    static final long MAX_CACHED_ARCHIVE_DATA = 32*1024*1024; // make a fixed 
fraction of the store by default? FIXME
+    static final long MAX_ARCHIVE_SIZE = 1024*1024; // ??? FIXME
+    static final long MAX_ARCHIVED_FILE_SIZE = 1024*1024; // arbitrary... FIXME
+    static final int MAX_CACHED_ELEMENTS = 1024; // equally arbitrary! FIXME 
hopefully we can cache many of these though
+    
+    
     /**
      * Read all storable settings (identity etc) from the node file.
      * @param filename The name of the file to read from.
@@ -308,7 +320,15 @@
         bootID = random.nextLong();
         localStreamContexts = new Hashtable();
         peers.writePeers();
-        tempBucketFactory = new PaddedEphemerallyEncryptedBucketFactory(new 
TempBucketFactory("temp", true));
+        try {
+                       tempFilenameGenerator = new FilenameGenerator(random, 
true, new File("temp"), "temp-");
+               } catch (IOException e) {
+                       Logger.error(this, "Could not create temp bucket 
factory: "+e, e);
+                       System.exit(EXIT_TEMP_INIT_ERROR);
+                       throw new Error();
+               }
+               tempBucketFactory = new 
PaddedEphemerallyEncryptedBucketFactory(new 
TempBucketFactory(tempFilenameGenerator), random, 1024);
+               archiveManager = new ArchiveManager(MAX_ARCHIVE_HANDLERS, 
MAX_CACHED_ARCHIVE_DATA, MAX_ARCHIVE_SIZE, MAX_ARCHIVED_FILE_SIZE, 
MAX_CACHED_ELEMENTS, random, tempFilenameGenerator);
     }

     void start(SwapRequestInterval interval) {

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2005-11-07 18:32:32 UTC (rev 
7491)
+++ trunk/freenet/src/freenet/node/Version.java 2005-11-07 18:59:08 UTC (rev 
7492)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       public static final int buildNumber = 140;
+       public static final int buildNumber = 141;

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

Added: trunk/freenet/src/freenet/support/io/FilenameGenerator.java
===================================================================
--- trunk/freenet/src/freenet/support/io/FilenameGenerator.java 2005-11-07 
18:32:32 UTC (rev 7491)
+++ trunk/freenet/src/freenet/support/io/FilenameGenerator.java 2005-11-07 
18:59:08 UTC (rev 7492)
@@ -0,0 +1,53 @@
+package freenet.support.io;
+
+import java.io.File;
+import java.io.IOException;
+
+import freenet.crypt.RandomSource;
+import freenet.support.HexUtil;
+
+public class FilenameGenerator {
+
+       final RandomSource random;
+       final String prefix;
+       final File tmpDir;
+       
+       public FilenameGenerator(RandomSource random, boolean wipeFiles, File 
dir, String prefix) throws IOException {
+               this.random = random;
+               this.prefix = prefix;
+               if (dir == null)
+                       tmpDir = new File(System.getProperty("java.io.tmpdir"));
+               else
+                       tmpDir = dir;
+               if(!dir.exists()) {
+                       dir.mkdir();
+               }
+               if(!(tmpDir.isDirectory() && tmpDir.canRead() && 
tmpDir.canWrite()))
+                       throw new IOException("Not a directory or cannot 
read/write: "+tmpDir);
+               
+               if(wipeFiles) {
+                       File[] filenames = tmpDir.listFiles();
+                       if(filenames != null) {
+                               for(int i=0;i<filenames.length;i++) {
+                                       File f = filenames[i];
+                                       String name = f.getName();
+                                       if((File.separatorChar == '\\' && 
name.toLowerCase().startsWith(prefix.toLowerCase()) ||
+                                                       
name.startsWith(prefix))) {
+                                               f.delete();
+                                       }
+                               }
+                       }
+               }
+       }
+
+       public File makeRandomFilename() {
+               byte[] randomFilename = new byte[8]; // should be plenty
+               while(true) {
+                       random.nextBytes(randomFilename);
+                       String filename = prefix + 
HexUtil.bytesToHex(randomFilename);
+                       File ret = new File(tmpDir, filename);
+                       if(!ret.exists()) return ret;
+               }
+       }
+
+}

Modified: trunk/freenet/src/freenet/support/io/TempBucketFactory.java
===================================================================
--- trunk/freenet/src/freenet/support/io/TempBucketFactory.java 2005-11-07 
18:32:32 UTC (rev 7491)
+++ trunk/freenet/src/freenet/support/io/TempBucketFactory.java 2005-11-07 
18:59:08 UTC (rev 7492)
@@ -37,52 +37,16 @@
        private static TempBucketHook hook = DONT_HOOK;
        private static boolean logDebug=true;

-       private final RandomSource random;
-       private final String prefix;
-
+       private final FilenameGenerator filenameGenerator;
+       
        public static long defaultIncrement = 4096;

        // Storage accounting disabled by default.
-       public TempBucketFactory(String temp, boolean wipeFiles, RandomSource 
random, String prefix) throws IOException {
+       public TempBucketFactory(FilenameGenerator filenameGenerator) {
                logDebug = Logger.shouldLog(Logger.DEBUG,this);
-               tmpDir = new File(temp);
-               if (tmpDir == null)
-                       tmpDir = new File(System.getProperty("java.io.tmpdir"));
-               this.random = random;
-               this.prefix = prefix;
-               if(!tmpDir.exists()) {
-                       tmpDir.mkdir();
-               }
-               if(!(tmpDir.isDirectory() && tmpDir.canRead() && 
tmpDir.canWrite()))
-                       throw new IOException("Not a directory or cannot 
read/write: "+tmpDir);
-               
-               if(wipeFiles) {
-                       File[] filenames = tmpDir.listFiles();
-                       if(filenames != null) {
-                               for(int i=0;i<filenames.length;i++) {
-                                       File f = filenames[i];
-                                       String name = f.getName();
-                                       if((File.separatorChar == '\\' && 
name.toLowerCase().startsWith(prefix.toLowerCase()) ||
-                                                       
name.startsWith(prefix))) {
-                                               f.delete();
-                                       }
-                               }
-                       }
-               }
-               //     Core.logger.log(this, "Creating TempBucketFactory, 
tmpDir = "+
-               //                  (tmpDir == null ? "(null)" : tmpDir),
-               //                  new Exception("debug"), Logger.DEBUG);
+               this.filenameGenerator = filenameGenerator;
        }

-       public TempBucketFactory(RandomSource random) throws IOException {
-               this(System.getProperty("java.io.tmpdir"), true, random, 
"freenet-temp-");
-               if (logDebug)
-                       Logger.debug(
-                               this,
-                               "Creating TempBucketFactory, tmpDir = " + 
tmpDir,
-                               new Exception("debug"));
-       }
-
        public Bucket makeBucket(long size) throws IOException {
                return makeBucket(size, 1.25F, defaultIncrement);
        }
@@ -106,33 +70,8 @@
        public TempFileBucket makeBucket(long size, float factor, long 
increment)
                throws IOException {
                logDebug = Logger.shouldLog(Logger.DEBUG,this);
-               File f = null;
-               do {
-                       if (tmpDir != null) {
-                               f =
-                                       new File(
-                                               tmpDir,
-                                               "tbf_"
-                                                       + Long.toHexString(
-                                                               
Math.abs(random.nextInt())));
-                               if (logDebug)
-                                       Logger.debug(
-                                               this,
-                                               "Temp file in " + tmpDir);
-                       } else {
-                               f =
-                                       new File(
-                                               "tbf_"
-                                                       + Long.toHexString(
-                                                               
Math.abs(random.nextInt())));
-                               if (logDebug)
-                                       Logger.debug(this, "Temp file in pwd");
-                       }
-               } while (f.exists());
+               File f = filenameGenerator.makeRandomFilename();

-               //System.err.println("TEMP BUCKET CREATED: " + 
f.getAbsolutePath());
-               //(new Exception("creating TempBucket")).printStackTrace();
-
                if (logDebug)
                        Logger.debug(
                                this,


Reply via email to