From bc16b292e0ab855df6a14476e109ec403cd67f1a Mon Sep 17 00:00:00 2001 From: adam Date: Sat, 23 Oct 2004 23:44:35 +0000 Subject: [PATCH] total overhaul of FileBasedMailbox darcs-hash:20041023234435-5007d-d8126eda39a16ce6ca365ab306785bfea51ec991.gz --- src/org/ibex/mail/target/FileBasedMailbox.java | 174 ++++++++++++------------ 1 file changed, 84 insertions(+), 90 deletions(-) diff --git a/src/org/ibex/mail/target/FileBasedMailbox.java b/src/org/ibex/mail/target/FileBasedMailbox.java index 4322673..e98176a 100644 --- a/src/org/ibex/mail/target/FileBasedMailbox.java +++ b/src/org/ibex/mail/target/FileBasedMailbox.java @@ -15,9 +15,9 @@ public class FileBasedMailbox extends Mailbox.Default { public static final long MAGIC_DATE = 0; - public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; } private static final char slash = File.separatorChar; private static final WeakHashMap instances = new WeakHashMap(); + public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; } public Mailbox slash(String name, boolean create) { return getFileBasedMailbox(path.getAbsolutePath()+slash+name, create); } // FIXME: should be a File() @@ -56,91 +56,106 @@ public class FileBasedMailbox extends Mailbox.Default { public void init(final Prevayler prevayler) throws IOException { dir.mkdirs(); Log.info(this, "initializing maildir " + dir.getParent()); - boolean invalid = false; + + // Drop entries whose files have vanished ArrayList kill = new ArrayList(); - for(String s : byname.keySet()) { - String name = dir.getParent() + slash + s; - if (!new File(name).exists() && !new File(name+"s").exists()) { - Log.error(this, "dropping message " + name); - kill.add(new Drop(byname.get(s).uid())); - } - } + for(String s : byname.keySet()) + if (!new File(dir.getParent() + slash + s).exists()) + kill.add(byname.get(s).delete(null)); for(Transaction t : kill) prevayler.execute(t); - for(String file : new File(dir.getParent()).list()) - if (file.charAt(0)!='.' && !(new File(dir.getParent() + slash + file).isDirectory())) { - if (get(file) == null) new Entry(this, prevayler, file); - else if ((new File(dir.getParent() + slash + file).lastModified() == MAGIC_DATE) != get(file).seen()) - prevayler.execute(new Seen(get(file).uid(), !get(file).seen())); + + // Make entries for new files which have appeared + for(String file : new File(dir.getParent()).list()) { + File f = new File(dir.getParent() + slash + file); + if (file.charAt(0)!='.' && !(f.isDirectory())) { + Entry e = get(file); + if (e == null) + prevayler.execute(Entry.create(f)); + else if ((f.lastModified() == MAGIC_DATE) != e.seen()) + prevayler.execute(e.seen(f, !e.seen())); } + } + + // Take a snapshot for posterity Log.info(this, " done initializing maildir " + dir.getParent()); new Thread() { public void run() { - try { prevayler.takeSnapshot(); } catch (Exception e) { Log.error(this, e); } - } }.start(); + try { prevayler.takeSnapshot(); } catch (Exception e) { Log.error(this, e); } } }.start(); } public synchronized int size() { return linear.size(); } - public synchronized int uidNext(boolean increment) { return increment ? uidNext++ : uidNext; } + public synchronized int uidNext() { return uidNext; } public synchronized Entry get(int uid) { return byuid.get(uid); } public synchronized Entry getLinear(int num) { return linear.get(num); } - public synchronized Entry get(String name) { - if (name.endsWith("s")) name = name.substring(0, name.length() - 1); - return byname.get(name); - } + public synchronized Entry get(String name) { return byname.get(name); } public static class Entry implements Serializable { private final byte[] header; public final String name; - private int uid; + public final int uid; private boolean seen; - - public MIME.Headers headers() { return new MIME.Headers(new Stream(new ByteArrayInputStream(header)), true); } - public Entry(Cache cache, Prevayler prevayler, String name) throws IOException { - File f = new File(cache.dir.getParent()+slash+name); - final boolean seen = f.lastModified() == MAGIC_DATE; - this.name = name; - header = new MIME.Headers(new Stream(new FileInputStream(f)), true).toString().getBytes(); - prevayler.execute(new Transaction() { - public void executeOn(Object o, Date now) { - Cache cache = (Cache)o; - synchronized(cache) { - Entry.this.seen = seen; - Entry.this.uid = cache.uidNext(true); - cache.linear.add(Entry.this); - cache.byuid.put(Entry.this.uid, Entry.this); - cache.byname.put(Entry.this.name, Entry.this); - } } }); - } + private Entry(String name, boolean seen, int uid, byte[] header) { + this.name = name; this.seen = seen; this.uid = uid; this.header = header; } + + // Accessors ////////////////////////////////////////////////////////////////////////////// - public int uid() { return uid; } public boolean seen() { return seen; } - public void seen(Cache cache, boolean seen) { - String base = cache.dir.getParent() + slash + name; - File target = new File(base); - if (!target.exists()) target = new File(base + "s"); - target.setLastModified(seen ? MAGIC_DATE : System.currentTimeMillis()); - } - public void delete(Cache cache) { - String base = cache.dir.getParent() + slash + name; - File target = new File(base); - if (!target.exists()) target = new File(base + "s"); - if (target.exists()) target.delete(); - } + public MIME.Headers headers() { return new MIME.Headers(new Stream(new ByteArrayInputStream(header)), true); } public Message message(Cache cache) { try { - String base = cache.dir.getParent() + slash + name; - File target = new File(base); - if (!target.exists()) target = new File(base + "s"); FileInputStream fis = null; try { - fis = new FileInputStream(target); - return new Message(new Stream(fis), new Message.Envelope(null, null, new Date(target.lastModified()))); + fis = new FileInputStream(cache.dir.getParent() + slash + name); + return new Message(new Stream(fis), new Message.Envelope(null, null, null)); } finally { if (fis != null) fis.close(); } } catch (IOException e) { throw new MailException.IOException(e); } catch (Message.Malformed e) { throw new MailException(e.getMessage()); } } + + // Transactions ////////////////////////////////////////////////////////////////////////////// + + public static Transaction create(File f) throws IOException { + final boolean seen = f.lastModified() == MAGIC_DATE; + final String name = f.getName(); + final byte[] header = new MIME.Headers(new Stream(new FileInputStream(f)), true).toString().getBytes(); + return new Transaction() { + public void executeOn(Object o, Date now) { + Cache cache = (Cache)o; + synchronized(cache) { + Entry e = new Entry(name, seen, cache.uidNext++, header); + cache.linear.add(e); + cache.byuid.put(e.uid, e); + cache.byname.put(e.name, e); + } } }; + } + + public Transaction seen(File f, final boolean seen) { + f.setLastModified(seen ? MAGIC_DATE : System.currentTimeMillis()); + final int uid = this.uid; + return new Transaction() { + public void executeOn(Object o, Date now) { + Cache cache = (Cache)o; + synchronized(cache) { + cache.get(uid).seen = seen; + } } }; + } + public Transaction delete(File f) { + if (f != null && f.exists()) f.delete(); + final int uid = this.uid; + return new Transaction() { + public void executeOn(Object o, Date now) { + Cache cache = (Cache)o; + synchronized(cache) { + Cache.Entry e = cache.get(uid); + cache.byname.remove(e.name); + cache.linear.remove(e); + cache.byuid.remove(uid); + } } }; + } } } + // Helpers ////////////////////////////////////////////////////////////////////////////// + private static void rmDashRf(File f) throws IOException { if (!f.isDirectory()) { f.delete(); return; } String[] children = f.list(); @@ -185,13 +200,13 @@ public class FileBasedMailbox extends Mailbox.Default { return (String[])vec.copyInto(new String[vec.size()]); } - public int uidNext() { return cache.uidNext(false); } + public int uidNext() { return cache.uidNext(); } public synchronized void add(Message message, int flags) { Log.info(path, message.summary()); try { - String name; String fullname; File target; File f; - while(true) { - name = cache.uidNext(true) + "."; + String name, fullname; File target, f; + for(int i = cache.uidNext(); ; i++) { + name = i + "."; fullname = path.getAbsolutePath() + slash + name; target = new File(fullname); f = new File(target.getCanonicalPath() + "-"); @@ -207,46 +222,25 @@ public class FileBasedMailbox extends Mailbox.Default { message.dump(stream); fo.close(); f.renameTo(new File(fullname)); + f = new File(fullname); if ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN) f.setLastModified(MAGIC_DATE); - new Cache.Entry(cache, prevayler, name); + prevayler.execute(Cache.Entry.create(f)); } catch (IOException e) { throw new MailException.IOException(e); } } private class Iterator extends Mailbox.Default.Iterator { int cur = -1; private Cache.Entry entry() { return cache.getLinear(cur); } + private File file() { return new File(cache.dir.getParent() + slash + entry().name); } public MIME.Headers head() { return done() ? null : entry().headers(); } public boolean done() { return cur >= cache.size(); } public boolean next() { cur++; return !done(); } public boolean seen() { return done() ? false : entry().seen(); } public int num() { return cur+1; } // EUDORA insists that message numbers start at 1, not 0 - public int uid() { return done() ? -1 : entry().uid(); } - public Message cur() { return done() ? null : entry().message(cache); } - public void seen(boolean seen) { - entry().seen((Cache)prevayler.prevalentSystem(), seen); - prevayler.execute(new Seen(uid(), seen)); - } - public void delete() { - entry().delete((Cache)prevayler.prevalentSystem()); - prevayler.execute(new Drop(entry().uid())); - } + public int uid() { return done() ? -1 : entry().uid; } + public void delete() { prevayler.execute(entry().delete(file())); } + public void seen(boolean seen) { prevayler.execute(entry().seen(file(), seen)); } + public Message cur() { return entry().message(cache); } } - private static class Seen implements Transaction { - private int uid; - private boolean seen; - public Seen(int uid, boolean seen) { this.uid = uid; this.seen = seen; } - public void executeOn(Object c, Date d) { ((Cache)c).get(uid).seen = seen; } - } - public static class Drop implements Transaction { - int uid; - public Drop(int uid) { this.uid = uid; } - public void executeOn(Object o, Date now) { - Cache c = (Cache)o; - Cache.Entry e = c.get(uid); - c.byname.remove(e.name); - c.linear.remove(e); - c.byuid.remove(uid); - } - } } -- 1.7.10.4