+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
package org.ibex.mail.target;
import org.prevayler.*;
import org.ibex.mail.*;
/** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
public class FileBasedMailbox extends Mailbox.Default {
- public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; }
+ public static final long MAGIC_DATE = 0;
+
private static final char slash = File.separatorChar;
private static final WeakHashMap<String,FileBasedMailbox> instances = new WeakHashMap<String,FileBasedMailbox>();
+ 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()
private Cache cache;
public static class Cache implements Serializable {
- public final int uidValidity = new Random().nextInt();
+ public final int uidValidity = Math.abs(new Random().nextInt());
private final Hashtable<String,Entry> byname = new Hashtable<String,Entry>();
private final Hashtable<Integer,Entry> byuid = new Hashtable<Integer,Entry>();
private final ArrayList<Entry> linear = new ArrayList<Entry>();
public Cache(File dir) { this.dir = dir; }
- public void init(Prevayler prevayler) throws IOException {
+ public void init(final Prevayler prevayler) throws IOException {
dir.mkdirs();
+ long time = System.currentTimeMillis();
Log.info(this, "initializing maildir " + dir.getParent());
- boolean invalid = false;
+
+ // Drop entries whose files have vanished
ArrayList<Transaction> kill = new ArrayList<Transaction>();
- 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);
+
+ // 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()));
}
}
- 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);
- Log.info(this, " done initializing maildir " + dir.getParent());
- prevayler.takeSnapshot();
- }
- 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;
- Entry e = c.get(uid);
- c.byname.remove(e.name);
- c.linear.remove(e);
- c.byuid.remove(uid);
- }
+ // Take a snapshot for posterity
+ if (System.currentTimeMillis() - time > 1000 * 5)
+ Log.info(this, " done initializing maildir " + dir.getParent());
+ new Thread() { public void run() {
+ 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 {
- public final MIME.Headers headers;
+ private final byte[] header;
public final String name;
- private int uid;
+ public final int uid;
private boolean seen;
-
- public Entry(Cache cache, Prevayler prevayler, String name) throws IOException {
- seen = name.endsWith("s");
- this.name = seen ? name.substring(0, name.length() - 1) : name;
- headers = new MIME.Headers(new Stream(new FileInputStream(cache.dir.getParent()+slash+name)), true);
- prevayler.execute(new Transaction() {
- public void executeOn(Object o, Date now) {
- Cache cache = (Cache)o;
- synchronized(cache) {
- 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 + (seen?"s":""));
- this.seen = seen;
- if (target.exists()) return;
- new File(base + (seen?"":"s")).renameTo(target);
- }
- 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 Message.newMessage(new Stream(fis));
} 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();
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;
- do {
- name = cache.uidNext(true) + "." + ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
+ 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() + "-");
+ if (!f.exists() && !target.exists()) break;
Log.error(this, "aieeee!!!! target of add() already exists: " + target.getAbsolutePath());
- } while (f.exists() || target.exists());
+ }
FileOutputStream fo = new FileOutputStream(f);
Stream stream = new Stream(fo);
- if (message.envelope != null) {
- stream.println("X-org.ibex.mail.headers.envelope.From: " + message.envelope.from);
- stream.println("X-org.ibex.mail.headers.envelope.To: " + message.envelope.to);
- }
message.dump(stream);
fo.close();
f.renameTo(new File(fullname));
- new Cache.Entry(cache, prevayler, name);
+ f = new File(fullname);
+ if ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN) f.setLastModified(MAGIC_DATE);
+ prevayler.execute(Cache.Entry.create(f));
} catch (IOException e) { throw new MailException.IOException(e); }
+ Log.info(this, path + " <= " + message.summary());
}
private class Iterator extends Mailbox.Default.Iterator {
int cur = -1;
private Cache.Entry entry() { return cache.getLinear(cur); }
- public MIME.Headers head() { return done() ? null : entry().headers; }
+ 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 boolean recent() { return false; }
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) { prevayler.execute(new Seen(uid(), seen)); }
- public void delete() { prevayler.execute(new Delete(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 Delete implements Transaction {
- private int uid;
- public Delete(int uid) { this.uid = uid; }
- public void executeOn(Object c, Date d) { ((Cache)c).get(uid).delete((Cache)c); }
- }
- 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((Cache)c, seen); }
- }
}