/** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
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<String,FileBasedMailbox> instances = new WeakHashMap<String,FileBasedMailbox>();
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);
+ File f = new File(cache.dir.getParent()+slash+name);
+ seen = f.lastModified() == MAGIC_DATE;
+ this.name = name;
+ headers = new MIME.Headers(new Stream(new FileInputStream(f)), true);
prevayler.execute(new Transaction() {
public void executeOn(Object o, Date now) {
Cache cache = (Cache)o;
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);
+ String base = cache.dir.getParent() + slash + name;
+ File target = new File(base);
+ if (!target.exists()) target = new File(base + "s");
+ target.setLastModified(seen ? System.currentTimeMillis() : MAGIC_DATE);
}
public void delete(Cache cache) {
String base = cache.dir.getParent() + slash + name;
try {
String name; String fullname; File target; File f;
do {
- name = cache.uidNext(true) + "." + ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
+ name = cache.uidNext(true) + ".";
fullname = path.getAbsolutePath() + slash + name;
target = new File(fullname);
f = new File(target.getCanonicalPath() + "-");
message.dump(stream);
fo.close();
f.renameTo(new File(fullname));
+ if ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN) f.setLastModified(MAGIC_DATE);
new Cache.Entry(cache, prevayler, name);
} catch (IOException e) { throw new MailException.IOException(e); }
}