def savemessage(self, uid, content, flags, rtime): # This function only ever saves to tmp/, # but it calls savemessageflags() to actually save to cur/ or new/. ui = UIBase.getglobalui() ui.debug('perscon', 'savemessage: called to write with flags %s and content %s' % \ (repr(flags), "<?>")) if uid < 0: # We cannot assign a new uid. return uid if uid in self.messagelist: # We already have it. self.savemessageflags(uid, flags) return uid msguid = "IMAP.%s.%s.%s" % (self.accountname, self.name, uid) atts = {} flags.sort () msg = EmailJSON.convert_mail_to_dict(content, self.name, msguid, flags, rtime, atts) msg = EmailJSON.tojson(msg) try: r = self.repository.rpc("doc/%s" % msguid, data=msg) except urllib2.HTTPError as e: print e.read () print msg os._exit(1) self.messagelist[uid] = {'uid': uid, 'flags': flags } ui.debug('perscon', 'savemessage: returning uid %d' % uid) return uid
def savemessage(self, uid, content, flags, rtime): # This function only ever saves to tmp/, # but it calls savemessageflags() to actually save to cur/ or new/. ui = UIBase.getglobalui() ui.debug('perscon', 'savemessage: called to write with flags %s and content %s' % \ (repr(flags), "<?>")) if uid < 0: # We cannot assign a new uid. return uid if uid in self.messagelist: # We already have it. self.savemessageflags(uid, flags) return uid msguid = "IMAP.%s.%s.%s" % (self.accountname, self.name, uid) atts = {} flags.sort() msg = EmailJSON.convert_mail_to_dict(content, self.name, msguid, flags, rtime, atts) msg = EmailJSON.tojson(msg) try: r = self.repository.rpc("doc/%s" % msguid, data=msg) except urllib2.HTTPError as e: print e.read() print msg os._exit(1) self.messagelist[uid] = {'uid': uid, 'flags': flags} ui.debug('perscon', 'savemessage: returning uid %d' % uid) return uid
def savemessage(self, uid, content, flags, rtime): # This function only ever saves to tmp/, # but it calls savemessageflags() to actually save to cur/ or new/. ui = UIBase.getglobalui() ui.debug('maildir', 'savemessage: called to write with flags %s and content %s' % \ (repr(flags), "<not shown>")) if uid < 0: # We cannot assign a new uid. return uid if uid in self.messagelist: # We already have it. self.savemessageflags(uid, flags) return uid # Otherwise, save the message in tmp/ and then call savemessageflags() # to give it a permanent home. tmpdir = os.path.join(self.getfullname(), 'tmp') messagename = None attempts = 0 while 1: if attempts > 15: raise IOError, "Couldn't write to file %s" % messagename timeval, timeseq = gettimeseq() messagename = '%d_%d.%d.%s,U=%d,FMD5=%s%s' % \ (timeval, timeseq, os.getpid(), socket.gethostname(), uid, md5.new(self.getvisiblename()).hexdigest(), self.extension) if os.path.exists(os.path.join(tmpdir, messagename)): time.sleep(2) attempts += 1 else: break tmpmessagename = messagename.split(',')[0] ui.debug('maildir', 'savemessage: using temporary name %s' % tmpmessagename) file = open(os.path.join(tmpdir, tmpmessagename), "wt") # write out the JSON atts = {} msg = EmailJSON.convert_mail_to_dict(content, self.name, uid, flags, rtime, atts) # write atts first, as they will be GCed if the main msg doesnt land for u in atts: attdir = os.path.join(self.getfullname(), '_att') fout = open(os.path.join(attdir, u), 'wb') fout.write(atts[u]) fout.flush() fout.close() msguid = os.path.splitext(messagename)[0] msg['_uid'] = msguid simplejson.dump(msg, file, indent=2) # Make sure the data hits the disk file.flush() if self.dofsync: os.fsync(file.fileno()) file.close() if rtime != None: os.utime(os.path.join(tmpdir,tmpmessagename), (rtime,rtime)) ui.debug('maildir', 'savemessage: moving from %s to %s' % \ (tmpmessagename, messagename)) if tmpmessagename != messagename: # then rename it os.link(os.path.join(tmpdir, tmpmessagename), os.path.join(tmpdir, messagename)) os.unlink(os.path.join(tmpdir, tmpmessagename)) if self.dofsync: try: # fsync the directory (safer semantics in Linux) fd = os.open(tmpdir, os.O_RDONLY) os.fsync(fd) os.close(fd) except: pass self.messagelist[uid] = {'uid': uid, 'flags': [], 'filename': os.path.join(tmpdir, messagename)} self.savemessageflags(uid, flags) ui.debug('maildir', 'savemessage: returning uid %d' % uid) return uid