default implementation of recent()
[org.ibex.mail.git] / src / org / ibex / mail / target / FileBasedMailbox.java
index a7eccd4..8d44198 100644 (file)
+// 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.*;
 import org.ibex.util.*;
 import org.ibex.io.*;
-import org.ibex.net.*;
 import java.io.*;
+import java.nio.*;
+import java.nio.channels.*;
 import java.net.*;
 import java.util.*;
 import java.text.*;
 
-// FIXME: we can omit UIDNEXT!
-// FIXME use directory date/time as UIDNEXT and file date/time as UID; need to 'correct' file date/time after changes
-
 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
 public class FileBasedMailbox extends Mailbox.Default {
 
-    public String toString() { return "[FileBasedMailbox " + path + "]"; }
+    public static final long MAGIC_DATE = 0;
 
     private static final char slash = File.separatorChar;
-    private static final Hashtable instances = new Hashtable();
-    public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
-        FileBasedMailbox ret = (FileBasedMailbox)instances.get(path);
-        if (ret != null) return ret;
-        File f = new File(path);
-        if (!create && !f.exists()) return null;
-        instances.put(path, ret = new FileBasedMailbox(path));
-        return ret;
-    }
+    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); }
 
-    public static final FilenameFilter filter = new FilenameFilter() {
-            public boolean accept(File f, String s) {
-                return s.indexOf('.') != -1;
-            } };
+    // FIXME: should be a File()
+    public static synchronized FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
+        try {
+            FileBasedMailbox ret = instances.get(path);
+            if (ret == null) {
+                if (!create && !(new File(path).exists())) return null;
+                instances.put(path, ret = new FileBasedMailbox(new File(path)));
+            }
+            return ret;
+        } catch (Exception e) {
+            Log.error(FileBasedMailbox.class, e);
+            return null;
+        }
+    }
 
 
     // Instance //////////////////////////////////////////////////////////////////////////////
 
-    private String path;
-    private File uidNext;
-    private FileBasedMailbox(String path) throws MailException {
-        new File(this.path = path).mkdirs();
-        uidNext(false);
-    }
+    private File path;
+    private FileLock lock;
+    private Prevayler prevayler;
+    private Cache cache;
+
+    public static class Cache implements Serializable {
+        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            final File dir;
+        private                 int uidNext = 0;
+
+        public Cache(File dir) { this.dir = dir; }
+
+        public void init(final Prevayler prevayler) throws IOException {
+            dir.mkdirs();
+            long time = System.currentTimeMillis();
+            Log.info(this, "initializing maildir " + dir.getParent());
+
+            // Drop entries whose files have vanished
+            ArrayList<Transaction> kill = new ArrayList<Transaction>();
+            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()));
+                }
+            }
 
-    public Mailbox          slash(String name, boolean create) {
-        return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); }
+            // 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 String[] children() {
-        Vec vec = new Vec();
-        String[] list = new File(path).list();
-        for(int i=0; i<list.length; i++) {
-            if (!(new File(path + slash + list[i]).isDirectory())) continue;
-            vec.addElement(list[i]);
+       
+        public synchronized int size() { return linear.size(); }
+        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) { return byname.get(name); }
+
+        public static class Entry implements Serializable {
+            private final byte[] header;
+            public final String name;
+            public final int uid;
+            private boolean seen;
+            private Entry(String name, boolean seen, int uid, byte[] header) {
+                this.name = name; this.seen = seen; this.uid = uid; this.header = header; }
+
+            // Accessors //////////////////////////////////////////////////////////////////////////////
+
+            public boolean seen() { return seen; }
+            public MIME.Headers headers() { return new MIME.Headers(new Stream(new ByteArrayInputStream(header)), true); }
+            public Message message(Cache cache) { try {
+                FileInputStream fis = null;
+                try {
+                    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);
+                            } } };
+            }
         }
-        String[] ret = new String[vec.size()];
-        vec.copyInto(ret);
-        return ret;
     }
 
-    public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
-    public int              uidValidity() { return (int)(new File(path).lastModified() & 0xffffffL); }
+    // Helpers //////////////////////////////////////////////////////////////////////////////
+
+    private static void rmDashRf(File f) throws IOException {
+        if (!f.isDirectory()) { f.delete(); return; }
+        String[] children = f.list();
+        for(int i=0; i<children.length; i++) rmDashRf(new File(f.getAbsolutePath() + slash + children[i]));
+        f.delete();
+    }
+
+    private FileBasedMailbox(File path) throws MailException, IOException, ClassNotFoundException {
+        this.path = path;
+        path.mkdirs();
+
+        // acquire lock
+        lock = new RandomAccessFile(this.path.getAbsolutePath() + slash + ".lock", "rw").getChannel().tryLock();
+        if (lock == null) throw new IOException("unable to lock FileBasedMailbox");
 
-    public int uidNext() { return uidNext(false); }
-    public int uidNext(boolean inc) {
+        File cacheDir = new File(path.getAbsolutePath() + slash + ".cache");
         try {
-            uidNext = new File(path + slash + "UIDNEXT");
-            if (!uidNext.exists()) {
-                File tmp = new File(uidNext + "-");
-                PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
-                pw.println("1");
-                pw.flush();
-                pw.close();
-                tmp.renameTo(uidNext);
-                return 1;
-            }
-            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
-            int ret = Integer.parseInt(br.readLine());
-            if (inc) {
-                File tmp = new File(uidNext + "-");
-                PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
-                pw.println(ret+1);
-                pw.flush();
-                pw.close();
-                tmp.renameTo(uidNext);
-            }
-            return ret;
-        } catch (IOException e) { throw new MailException.IOException(e); }
-    }        
+            prevayler = PrevaylerFactory.createPrevayler(new Cache(cacheDir), cacheDir.getAbsolutePath());
+            cache = (Cache)prevayler.prevalentSystem();
+        } catch (Throwable t) {
+            Log.warn(this, "error while attempting to reconstitute a FileBasedMailbox.cache:");
+            Log.warn(this, t);
+            Log.warn(this, "discarding cache...");
+            rmDashRf(cacheDir);
+            prevayler = PrevaylerFactory.createPrevayler(new Cache(cacheDir), cacheDir.getAbsolutePath());
+            cache = (Cache)prevayler.prevalentSystem();
+        }
+        cache.init(prevayler);
+    }
+
 
+    public Mailbox.Iterator  iterator()           { return new Iterator(); }
+    public int               uidValidity()        { return cache.uidValidity; }
     public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
+    public String[] children() {
+        Vec vec = new Vec();
+        String[] list = path.list();
+        for(int i=0; i<list.length; i++) {
+            File f = new File(path.getAbsolutePath() + slash + list[i]);
+            if (f.isDirectory() && f.getName().charAt(0) != '.') vec.addElement(list[i]);
+        }
+        return (String[])vec.copyInto(new String[vec.size()]);
+    }
+
+    public int uidNext() { return cache.uidNext(); }
     public synchronized void add(Message message, int flags) {
-        Log.error(this, "adding message to ["+toString()+"]:\n" + message.summary());
         try {
-            int num = new File(path).list(filter).length;
-           String name = path + slash + uidNext(true) + "." +
-                ((flags & Mailbox.Flag.DELETED) == Mailbox.Flag.DELETED ? "x" : "") +
-                ((flags & Mailbox.Flag.DRAFT) == Mailbox.Flag.DRAFT ? "d" : "") +
-                ((flags & Mailbox.Flag.RECENT) == Mailbox.Flag.RECENT ? "r" : "") +
-                ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
-                ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
-                ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
-            File target = new File(name);
-            File f = new File(target.getCanonicalPath() + "-");
+            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());
+            }
             FileOutputStream fo = new FileOutputStream(f);
-            message.dump(new Stream(fo));
+            Stream stream = new Stream(fo);
+            message.dump(stream);
             fo.close();
-            f.renameTo(target);
-            Log.error(this, " done writing to " + target);
+            f.renameTo(new File(fullname));
+            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 String[] names;
-        private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
-        public Iterator() { names = new File(path).list(filter); }
-
-        public Message cur() {
-           if (cur >= names.length) return null;
-            try {
-                File file = new File(path + File.separatorChar + names[cur]);
-                return new Message(null, null, new Stream(new FileInputStream(file)));
-            } catch (IOException e) { throw new MailException.IOException(e);
-            } catch (Message.Malformed e) { throw new MailException(e.getMessage()); }
-        }
-        public boolean next() {
-            cur++;
-            if (cur >= names.length) return false;
-            String name = names[cur].substring(names[cur].indexOf('.') + 1);
-           if (!(new File(path + File.separatorChar + names[cur])).exists()) return next();
-            seen     = name.indexOf('s') != -1;
-            deleted  = name.indexOf('x') != -1;
-            flagged  = name.indexOf('f') != -1;
-            draft    = name.indexOf('d') != -1;
-            answered = name.indexOf('a') != -1;
-            recent   = name.indexOf('r') != -1;
-            return true;
-        }
-        public int num() { return cur; }
-        public int uid() {
-            try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.')));
-            } catch (NumberFormatException nfe) {
-                Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1));
-                return -1; } }
-
-        private void fixflags() {
-            String newName =
-                names[cur].substring(0, names[cur].indexOf('.') + 1) +
-                (seen ? "s" : "") +
-                (deleted ? "x" : "") +
-                (flagged ? "f" : "") +
-                (draft ? "d" : "") +
-                (recent ? "r" : "") +
-                (answered ? "a" : "");
-            new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName));
-            names[cur] = newName;
-        }
-
-        public void    delete() {
-           new File(path + File.separatorChar + names[cur]).delete();
-           // FIXME remove from list?
-       }
-        public void    set(String key, String val) { throw new MailException("not supported"); }
-        public String  get(String key) { throw new MailException("not supported"); }
-
-        public boolean seen() { return seen; }
-        public boolean deleted() { return deleted; }
-        public boolean flagged() { return flagged; }
-        public boolean draft() { return draft; }
-        public boolean answered() { return answered; }
-        public boolean recent() { return recent; }
-        public void    seen(boolean on) { seen = on; fixflags(); }
-        public void    deleted(boolean on) { deleted = on; fixflags(); }
-        public void    flagged(boolean on) { flagged = on; fixflags(); }
-        public void    draft(boolean on) { draft = on; fixflags(); }
-        public void    answered(boolean on) { answered = on; fixflags(); }
-        public void    recent(boolean on) { recent = on; fixflags(); }
-
+        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 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 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); }
     }
+
 }