2002-06-19 08:08:59 +02:00
|
|
|
# Maildir folder support
|
2007-03-28 22:23:18 +02:00
|
|
|
# Copyright (C) 2002 - 2007 John Goerzen
|
2002-06-19 07:55:12 +02:00
|
|
|
# <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-19 07:55:12 +02:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-06-19 07:55:12 +02:00
|
|
|
|
2011-03-11 22:13:21 +01:00
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import re
|
|
|
|
import os
|
2002-06-19 07:55:12 +02:00
|
|
|
from Base import BaseFolder
|
2003-05-06 21:26:12 +02:00
|
|
|
from threading import Lock
|
2008-12-04 22:45:15 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
from hashlib import md5
|
|
|
|
except ImportError:
|
2008-12-24 19:22:10 +01:00
|
|
|
from md5 import md5
|
2002-06-22 06:32:29 +02:00
|
|
|
|
2011-08-16 12:16:46 +02:00
|
|
|
try: # python 2.6 has set() built in
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set
|
|
|
|
|
2011-06-13 15:22:37 +02:00
|
|
|
from offlineimap import OfflineImapError
|
|
|
|
|
2002-07-23 21:55:18 +02:00
|
|
|
uidmatchre = re.compile(',U=(\d+)')
|
2009-08-16 14:42:39 +02:00
|
|
|
timestampmatchre = re.compile('(\d+)');
|
2002-07-23 21:55:18 +02:00
|
|
|
|
2002-06-22 06:32:29 +02:00
|
|
|
timeseq = 0
|
|
|
|
lasttime = long(0)
|
2003-05-06 21:26:12 +02:00
|
|
|
timelock = Lock()
|
2002-06-22 06:32:29 +02:00
|
|
|
|
|
|
|
def gettimeseq():
|
2003-05-06 21:26:12 +02:00
|
|
|
global lasttime, timeseq, timelock
|
|
|
|
timelock.acquire()
|
|
|
|
try:
|
|
|
|
thistime = long(time.time())
|
|
|
|
if thistime == lasttime:
|
|
|
|
timeseq += 1
|
|
|
|
return (thistime, timeseq)
|
|
|
|
else:
|
|
|
|
lasttime = thistime
|
|
|
|
timeseq = 0
|
|
|
|
return (thistime, timeseq)
|
|
|
|
finally:
|
|
|
|
timelock.release()
|
2002-06-19 07:55:12 +02:00
|
|
|
|
2002-06-19 08:08:59 +02:00
|
|
|
class MaildirFolder(BaseFolder):
|
2011-09-16 10:54:24 +02:00
|
|
|
def __init__(self, root, name, sep, repository):
|
2011-09-16 10:54:22 +02:00
|
|
|
super(MaildirFolder, self).__init__(name, repository)
|
2011-09-16 10:54:24 +02:00
|
|
|
self.dofsync = self.config.getdefaultboolean("general", "fsync", True)
|
2002-06-19 08:08:59 +02:00
|
|
|
self.root = root
|
2002-08-08 04:40:18 +02:00
|
|
|
self.sep = sep
|
2002-06-20 05:33:23 +02:00
|
|
|
self.messagelist = None
|
2011-07-12 09:34:02 +02:00
|
|
|
|
|
|
|
self.wincompatible = self.config.getdefaultboolean(
|
|
|
|
"Account "+self.accountname, "maildir-windows-compatible", False)
|
|
|
|
|
|
|
|
if self.wincompatible == False:
|
|
|
|
self.infosep = ':'
|
|
|
|
else:
|
|
|
|
self.infosep = '!'
|
|
|
|
|
|
|
|
self.flagmatchre = re.compile(self.infosep + '.*2,([A-Z]+)')
|
2011-01-05 17:00:57 +01:00
|
|
|
#self.ui is set in BaseFolder.init()
|
2011-06-10 17:32:39 +02:00
|
|
|
# Cache the full folder path, as we use getfullname() very often
|
|
|
|
self._fullname = os.path.join(self.getroot(), self.getname())
|
2003-01-06 00:07:58 +01:00
|
|
|
|
2002-06-20 07:14:35 +02:00
|
|
|
def getfullname(self):
|
2011-06-10 17:32:39 +02:00
|
|
|
"""Return the absolute file path to the Maildir folder (sans cur|new)"""
|
|
|
|
return self._fullname
|
2002-06-20 07:14:35 +02:00
|
|
|
|
2002-06-20 05:33:23 +02:00
|
|
|
def getuidvalidity(self):
|
2003-04-18 04:18:34 +02:00
|
|
|
"""Maildirs have no notion of uidvalidity, so we just return a magic
|
|
|
|
token."""
|
|
|
|
return 42
|
2002-06-20 05:33:23 +02:00
|
|
|
|
2009-08-16 14:42:39 +02:00
|
|
|
#Checks to see if the given message is within the maximum age according
|
|
|
|
#to the maildir name which should begin with a timestamp
|
|
|
|
def _iswithinmaxage(self, messagename, maxage):
|
|
|
|
#In order to have the same behaviour as SINCE in an IMAP search
|
|
|
|
#we must convert this to the oldest time and then strip off hrs/mins
|
|
|
|
#from that day
|
|
|
|
oldest_time_utc = time.time() - (60*60*24*maxage)
|
|
|
|
oldest_time_struct = time.gmtime(oldest_time_utc)
|
|
|
|
oldest_time_today_seconds = ((oldest_time_struct[3] * 3600) \
|
|
|
|
+ (oldest_time_struct[4] * 60) \
|
|
|
|
+ oldest_time_struct[5])
|
|
|
|
oldest_time_utc -= oldest_time_today_seconds
|
|
|
|
|
|
|
|
timestampmatch = timestampmatchre.search(messagename)
|
|
|
|
timestampstr = timestampmatch.group()
|
|
|
|
timestamplong = long(timestampstr)
|
|
|
|
if(timestamplong < oldest_time_utc):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2002-07-16 04:26:58 +02:00
|
|
|
def _scanfolder(self):
|
2002-06-20 05:33:23 +02:00
|
|
|
"""Cache the message list. Maildir flags are:
|
|
|
|
R (replied)
|
|
|
|
S (seen)
|
|
|
|
T (trashed)
|
|
|
|
D (draft)
|
|
|
|
F (flagged)
|
|
|
|
and must occur in ASCII order."""
|
2002-07-16 04:26:58 +02:00
|
|
|
retval = {}
|
2002-06-20 05:33:23 +02:00
|
|
|
files = []
|
2002-06-20 06:14:54 +02:00
|
|
|
nouidcounter = -1 # Messages without UIDs get
|
|
|
|
# negative UID numbers.
|
2008-12-24 19:22:10 +01:00
|
|
|
foldermd5 = md5(self.getvisiblename()).hexdigest()
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
folderstr = ',FMD5=' + foldermd5
|
2002-06-20 05:33:23 +02:00
|
|
|
for dirannex in ['new', 'cur']:
|
|
|
|
fulldirname = os.path.join(self.getfullname(), dirannex)
|
2011-08-22 11:09:07 +02:00
|
|
|
files.extend(os.path.join(dirannex, filename) for
|
2009-07-07 07:52:00 +02:00
|
|
|
filename in os.listdir(fulldirname))
|
2002-06-20 05:33:23 +02:00
|
|
|
for file in files:
|
|
|
|
messagename = os.path.basename(file)
|
2009-08-16 14:42:39 +02:00
|
|
|
|
|
|
|
#check if there is a parameter for maxage / maxsize - then see if this
|
|
|
|
#message should be considered or not
|
|
|
|
maxage = self.config.getdefaultint("Account " + self.accountname, "maxage", -1)
|
|
|
|
maxsize = self.config.getdefaultint("Account " + self.accountname, "maxsize", -1)
|
|
|
|
|
|
|
|
if(maxage != -1):
|
|
|
|
isnewenough = self._iswithinmaxage(messagename, maxage)
|
|
|
|
if(isnewenough != True):
|
|
|
|
#this message is older than we should consider....
|
|
|
|
continue
|
|
|
|
|
|
|
|
#Check and see if the message is too big if the maxsize for this account is set
|
|
|
|
if(maxsize != -1):
|
2011-08-22 11:09:07 +02:00
|
|
|
size = os.path.getsize(os.path.join(self.getfullname(), file))
|
|
|
|
if(size > maxsize):
|
2009-08-16 14:42:39 +02:00
|
|
|
continue
|
|
|
|
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
foldermatch = messagename.find(folderstr) != -1
|
|
|
|
if not foldermatch:
|
2002-06-22 06:32:29 +02:00
|
|
|
# If there is no folder MD5 specified, or if it mismatches,
|
|
|
|
# assume it is a foreign (new) message and generate a
|
|
|
|
# negative uid for it
|
2002-06-20 06:14:54 +02:00
|
|
|
uid = nouidcounter
|
2002-06-22 06:39:02 +02:00
|
|
|
nouidcounter -= 1
|
2002-06-22 06:32:29 +02:00
|
|
|
else: # It comes from our folder.
|
2002-07-23 21:55:18 +02:00
|
|
|
uidmatch = uidmatchre.search(messagename)
|
2002-06-22 06:32:29 +02:00
|
|
|
uid = None
|
|
|
|
if not uidmatch:
|
|
|
|
uid = nouidcounter
|
|
|
|
nouidcounter -= 1
|
|
|
|
else:
|
|
|
|
uid = long(uidmatch.group(1))
|
2011-08-16 12:16:46 +02:00
|
|
|
#identify flags in the path name
|
2011-07-12 09:34:02 +02:00
|
|
|
flagmatch = self.flagmatchre.search(messagename)
|
2002-06-20 05:33:23 +02:00
|
|
|
if flagmatch:
|
2011-08-16 12:16:46 +02:00
|
|
|
flags = set(flagmatch.group(1))
|
|
|
|
else:
|
|
|
|
flags = set()
|
2011-08-22 11:09:07 +02:00
|
|
|
# 'filename' is 'dirannex/filename', e.g. cur/123_U=1_FMD5=1:2,S
|
2002-11-12 22:40:40 +01:00
|
|
|
retval[uid] = {'uid': uid,
|
|
|
|
'flags': flags,
|
|
|
|
'filename': file}
|
2002-07-16 04:26:58 +02:00
|
|
|
return retval
|
|
|
|
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
def quickchanged(self, statusfolder):
|
2011-04-11 18:09:07 +02:00
|
|
|
"""Returns True if the Maildir has changed"""
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
self.cachemessagelist()
|
2011-04-11 18:09:07 +02:00
|
|
|
# Folder has different uids than statusfolder => TRUE
|
|
|
|
if sorted(self.getmessageuidlist()) != \
|
|
|
|
sorted(statusfolder.getmessageuidlist()):
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
return True
|
2011-04-11 18:09:07 +02:00
|
|
|
# Also check for flag changes, it's quick on a Maildir
|
|
|
|
for (uid, message) in self.getmessagelist().iteritems():
|
|
|
|
if message['flags'] != statusfolder.getmessageflags(uid):
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
return True
|
2011-04-11 18:09:07 +02:00
|
|
|
return False #Nope, nothing changed
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
|
2002-07-16 04:26:58 +02:00
|
|
|
def cachemessagelist(self):
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
if self.messagelist is None:
|
|
|
|
self.messagelist = self._scanfolder()
|
2002-06-20 05:33:23 +02:00
|
|
|
|
|
|
|
def getmessagelist(self):
|
|
|
|
return self.messagelist
|
2002-06-19 08:08:59 +02:00
|
|
|
|
2002-06-20 06:59:57 +02:00
|
|
|
def getmessage(self, uid):
|
2011-06-10 17:32:40 +02:00
|
|
|
"""Return the content of the message"""
|
2002-07-24 01:36:44 +02:00
|
|
|
filename = self.messagelist[uid]['filename']
|
2011-06-10 17:32:40 +02:00
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
|
|
|
file = open(filepath, 'rt')
|
2002-06-20 06:59:57 +02:00
|
|
|
retval = file.read()
|
|
|
|
file.close()
|
2011-06-10 17:32:40 +02:00
|
|
|
#TODO: WHY are we replacing \r\n with \n here? And why do we
|
|
|
|
# read it as text?
|
2002-07-22 07:29:26 +02:00
|
|
|
return retval.replace("\r\n", "\n")
|
2002-06-20 06:59:57 +02:00
|
|
|
|
2006-08-22 03:09:36 +02:00
|
|
|
def getmessagetime( self, uid ):
|
|
|
|
filename = self.messagelist[uid]['filename']
|
2011-06-10 17:32:40 +02:00
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
|
|
|
st = os.stat(filepath)
|
2006-08-22 03:09:36 +02:00
|
|
|
return st.st_mtime
|
|
|
|
|
|
|
|
def savemessage(self, uid, content, flags, rtime):
|
2007-06-13 05:42:15 +02:00
|
|
|
# This function only ever saves to tmp/,
|
|
|
|
# but it calls savemessageflags() to actually save to cur/ or new/.
|
2011-06-10 17:32:40 +02:00
|
|
|
self.ui.debug('maildir', 'savemessage: called to write with flags %s '
|
|
|
|
'and content %s' % (repr(flags), repr(content)))
|
2002-06-21 03:03:30 +02:00
|
|
|
if uid < 0:
|
|
|
|
# We cannot assign a new uid.
|
|
|
|
return uid
|
2002-07-24 01:36:44 +02:00
|
|
|
if uid in self.messagelist:
|
2011-06-10 17:32:40 +02:00
|
|
|
# We already have it, just update flags.
|
2002-06-21 03:12:52 +02:00
|
|
|
self.savemessageflags(uid, flags)
|
2002-06-21 03:03:30 +02:00
|
|
|
return uid
|
2007-06-13 05:42:15 +02:00
|
|
|
|
|
|
|
# Otherwise, save the message in tmp/ and then call savemessageflags()
|
|
|
|
# to give it a permanent home.
|
2002-06-20 06:59:57 +02:00
|
|
|
tmpdir = os.path.join(self.getfullname(), 'tmp')
|
2011-06-13 15:22:37 +02:00
|
|
|
timeval, timeseq = gettimeseq()
|
|
|
|
messagename = '%d_%d.%d.%s,U=%d,FMD5=%s' % \
|
|
|
|
(timeval,
|
|
|
|
timeseq,
|
|
|
|
os.getpid(),
|
|
|
|
socket.gethostname(),
|
|
|
|
uid,
|
|
|
|
md5(self.getvisiblename()).hexdigest())
|
|
|
|
# open file and write it out
|
|
|
|
try:
|
|
|
|
fd = os.open(os.path.join(tmpdir, messagename),
|
|
|
|
os.O_EXCL|os.O_CREAT|os.O_WRONLY)
|
|
|
|
except OSError, e:
|
|
|
|
if e.errno == 17:
|
|
|
|
#FILE EXISTS ALREADY
|
|
|
|
severity = OfflineImapError.ERROR.MESSAGE
|
|
|
|
raise OfflineImapError("Unique filename %s already existing." %\
|
|
|
|
messagename, severity)
|
2007-10-19 21:29:34 +02:00
|
|
|
else:
|
2011-06-13 15:22:37 +02:00
|
|
|
raise
|
2007-03-28 22:23:18 +02:00
|
|
|
|
2011-06-13 15:22:37 +02:00
|
|
|
file = os.fdopen(fd, 'wt')
|
|
|
|
file.write(content)
|
2007-03-28 22:23:18 +02:00
|
|
|
# Make sure the data hits the disk
|
|
|
|
file.flush()
|
2008-08-02 21:55:08 +02:00
|
|
|
if self.dofsync:
|
2011-06-13 15:22:37 +02:00
|
|
|
os.fsync(fd)
|
2002-06-20 06:59:57 +02:00
|
|
|
file.close()
|
2011-06-10 17:32:40 +02:00
|
|
|
|
2006-12-02 21:54:26 +01:00
|
|
|
if rtime != None:
|
2011-06-13 15:22:37 +02:00
|
|
|
os.utime(os.path.join(tmpdir, messagename), (rtime, rtime))
|
2007-03-28 22:23:18 +02:00
|
|
|
|
2011-08-16 12:16:46 +02:00
|
|
|
self.messagelist[uid] = {'uid': uid, 'flags': set(),
|
2011-06-10 17:32:40 +02:00
|
|
|
'filename': os.path.join('tmp', messagename)}
|
|
|
|
# savemessageflags moves msg to 'cur' or 'new' as appropriate
|
2002-06-21 03:12:52 +02:00
|
|
|
self.savemessageflags(uid, flags)
|
2011-01-05 17:00:57 +01:00
|
|
|
self.ui.debug('maildir', 'savemessage: returning uid %d' % uid)
|
2002-06-21 03:03:30 +02:00
|
|
|
return uid
|
2002-06-20 06:59:57 +02:00
|
|
|
|
2002-06-20 07:14:35 +02:00
|
|
|
def getmessageflags(self, uid):
|
2002-07-24 01:36:44 +02:00
|
|
|
return self.messagelist[uid]['flags']
|
2002-06-20 07:14:35 +02:00
|
|
|
|
|
|
|
def savemessageflags(self, uid, flags):
|
2002-07-24 01:36:44 +02:00
|
|
|
oldfilename = self.messagelist[uid]['filename']
|
2011-06-10 17:32:40 +02:00
|
|
|
dir_prefix, newname = os.path.split(oldfilename)
|
2007-06-13 05:42:15 +02:00
|
|
|
tmpdir = os.path.join(self.getfullname(), 'tmp')
|
2003-06-27 02:03:07 +02:00
|
|
|
if 'S' in flags:
|
|
|
|
# If a message has been seen, it goes into the cur
|
2011-06-10 17:32:40 +02:00
|
|
|
# directory. CR debian#152482
|
|
|
|
dir_prefix = 'cur'
|
2003-06-27 02:03:07 +02:00
|
|
|
else:
|
2011-06-10 17:32:40 +02:00
|
|
|
dir_prefix = 'new'
|
2011-07-12 09:34:02 +02:00
|
|
|
|
|
|
|
infostr = self.infosep
|
|
|
|
infomatch = re.search('(' + self.infosep + '.*)$', newname)
|
2002-06-20 07:14:35 +02:00
|
|
|
if infomatch: # If the info string is present..
|
|
|
|
infostr = infomatch.group(1)
|
2011-07-12 09:34:02 +02:00
|
|
|
newname = newname.split(self.infosep)[0] # Strip off the info string.
|
2002-06-21 06:04:47 +02:00
|
|
|
infostr = re.sub('2,[A-Z]*', '', infostr)
|
2011-08-16 12:16:46 +02:00
|
|
|
infostr += '2,' + ''.join(sorted(flags))
|
2002-06-20 07:14:35 +02:00
|
|
|
newname += infostr
|
|
|
|
|
2011-06-10 17:32:40 +02:00
|
|
|
newfilename = os.path.join(dir_prefix, newname)
|
2002-06-20 07:14:35 +02:00
|
|
|
if (newfilename != oldfilename):
|
2011-08-17 10:01:39 +02:00
|
|
|
try:
|
|
|
|
os.rename(os.path.join(self.getfullname(), oldfilename),
|
|
|
|
os.path.join(self.getfullname(), newfilename))
|
|
|
|
except OSError, e:
|
|
|
|
raise OfflineImapError("Can't rename file '%s' to '%s': %s" % (
|
|
|
|
oldfilename, newfilename, e[1]),
|
|
|
|
OfflineImapError.ERROR.FOLDER)
|
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
self.messagelist[uid]['flags'] = flags
|
|
|
|
self.messagelist[uid]['filename'] = newfilename
|
2002-06-20 07:14:35 +02:00
|
|
|
|
2007-06-13 05:42:15 +02:00
|
|
|
# By now, the message had better not be in tmp/ land!
|
|
|
|
final_dir, final_name = os.path.split(self.messagelist[uid]['filename'])
|
2011-06-10 17:32:40 +02:00
|
|
|
assert final_dir != 'tmp'
|
2007-06-13 05:42:15 +02:00
|
|
|
|
2002-06-20 07:14:35 +02:00
|
|
|
def deletemessage(self, uid):
|
2011-05-07 11:11:20 +02:00
|
|
|
"""Unlinks a message file from the Maildir.
|
|
|
|
|
|
|
|
:param uid: UID of a mail message
|
|
|
|
:type uid: String
|
|
|
|
:return: Nothing, or an Exception if UID but no corresponding file
|
|
|
|
found.
|
|
|
|
"""
|
|
|
|
if not self.uidexists(uid):
|
2002-06-21 06:30:08 +02:00
|
|
|
return
|
2011-05-07 11:11:20 +02:00
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
filename = self.messagelist[uid]['filename']
|
2011-06-10 17:32:40 +02:00
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
2002-07-16 04:26:58 +02:00
|
|
|
try:
|
2011-06-10 17:32:40 +02:00
|
|
|
os.unlink(filepath)
|
2002-08-17 03:01:12 +02:00
|
|
|
except OSError:
|
2002-07-16 04:26:58 +02:00
|
|
|
# Can't find the file -- maybe already deleted?
|
|
|
|
newmsglist = self._scanfolder()
|
|
|
|
if uid in newmsglist: # Nope, try new filename.
|
2011-06-10 17:32:40 +02:00
|
|
|
filename = newmsglist[uid]['filename']
|
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
|
|
|
os.unlink(filepath)
|
2002-07-16 04:26:58 +02:00
|
|
|
# Yep -- return.
|
2002-06-20 07:14:35 +02:00
|
|
|
del(self.messagelist[uid])
|
|
|
|
|