'Saves given content to the named temporary file in the
\'tmp\' subdirectory of $CWD.
Arguments:
- filename: name of the temporary file;
- content: data to be saved.
Returns: relative path to the temporary file
that was created.'

def save_to_tmp_file(self, filename, content):

    tmpname = os.path.join('tmp', filename)
    tries = 7
    while tries:
        tries = (tries - 1)
        try:
            fd = os.open(os.path.join(self.getfullname(), tmpname), ((os.O_EXCL | os.O_CREAT) | os.O_WRONLY), 438)
            break
        except OSError as e:
            if (not hasattr(e, 'EEXIST')):
                raise
            if (e.errno == e.EEXIST):
                if tries:
                    time.sleep(0.23)
                    continue
                severity = OfflineImapError.ERROR.MESSAGE
                six.reraise(OfflineImapError, OfflineImapError(('Unique    filename    %s    already    exists.' % filename), severity), exc_info()[2])
            else:
                raise
    fd = os.fdopen(fd, 'wt')
    fd.write(content)
    fd.flush()
    if self.dofsync():
        os.fsync(fd)
    fd.close()
    return tmpname
