Sha256: bc121f65676ed1cf1c24add8686aee05e202577ec81fbb2237c0e18d0db036df

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Imap; end

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

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

    def transaction(&block)
      fail_in_transaction!(message: "Nested transactions are not supported")

      @savepoint = {length: length}
      block.call
      @savepoint = nil
    end

    def rollback
      fail_outside_transaction!

      rewind(@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?

      File.unlink(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 fail_in_transaction!(message: "Method not supported inside trasactions")
      raise message if savepoint
    end

    def fail_outside_transaction!
      raise "This method can only be called inside a transaction" if !savepoint
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imap-backup-11.0.0.rc1 lib/imap/backup/serializer/mbox.rb