Sha256: 43ac59ded4e6830c9408d38dd27c74c950f337a9816d7efbd09144d9799405a9

Contents?: true

Size: 1.79 KB

Versions: 12

Compression:

Stored size: 1.79 KB

Contents

require "imap/backup/serializer/transaction"

module Imap; end

module Imap::Backup
  class Serializer::Mbox
    attr_reader :folder_path
    attr_reader :savepoint

    def initialize(folder_path)
      @folder_path = folder_path
      @tsx = nil
    end

    def transaction(&block)
      tsx.fail_in_transaction!(:transaction, message: "nested transactions are not supported")

      tsx.begin({savepoint: {length: length}}) do
        block.call
      rescue StandardError => e
        rollback
        raise e
      rescue SignalException => e
        Logger.logger.error "#{self.class} handling #{e.class}"
        rollback
        raise e
      end
    end

    def rollback
      tsx.fail_outside_transaction!(:rollback)

      rewind(tsx.data[:savepoint][:length])
    end

    def valid?
      exist?
    end

    def append(message)
      File.open(pathname, "ab") do |file|
        file.write message
      end
    end

    def read(offset, length)
      File.open(pathname, "rb") do |f|
        f.seek offset
        f.read length
      end
    end

    def delete
      return if !exist?

      FileUtils.rm(pathname)
    end

    def exist?
      File.exist?(pathname)
    end

    def length
      return nil if !exist?

      File.stat(pathname).size
    end

    def pathname
      "#{folder_path}.mbox"
    end

    def rename(new_path)
      if exist?
        old_pathname = pathname
        @folder_path = new_path
        File.rename(old_pathname, pathname)
      else
        @folder_path = new_path
      end
    end

    def touch
      File.open(pathname, "a") {}
    end

    private

    def rewind(length)
      File.open(pathname, File::RDWR | File::CREAT, 0o644) do |f|
        f.truncate(length)
      end
    end

    def tsx
      @tsx ||= Serializer::Transaction.new(owner: self)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
imap-backup-14.4.1 lib/imap/backup/serializer/mbox.rb
imap-backup-14.4.0 lib/imap/backup/serializer/mbox.rb
imap-backup-14.3.0 lib/imap/backup/serializer/mbox.rb
imap-backup-14.2.0 lib/imap/backup/serializer/mbox.rb
imap-backup-14.1.1 lib/imap/backup/serializer/mbox.rb
imap-backup-14.1.0 lib/imap/backup/serializer/mbox.rb
imap-backup-14.0.0 lib/imap/backup/serializer/mbox.rb
imap-backup-13.4.0 lib/imap/backup/serializer/mbox.rb
imap-backup-13.3.0 lib/imap/backup/serializer/mbox.rb
imap-backup-13.2.0 lib/imap/backup/serializer/mbox.rb
imap-backup-13.1.0 lib/imap/backup/serializer/mbox.rb
imap-backup-13.0.0 lib/imap/backup/serializer/mbox.rb