Class: VimRecovery::Swapfile

Inherits:
File
  • Object
show all
Defined in:
lib/vim_recovery/swapfile.rb

Defined Under Namespace

Classes: Block0

Constant Summary collapse

VALID_BLOCK_IDS =
%w{b0 bc bC bd}.freeze
B0_MAGIC_LONG =
0x30313233
B0_MAGIC_INT =
0x20212223
B0_MAGIC_SHORT =
0x10111213
B0_MAGIC_CHAR =
0x55
B0_DIRTY =
0x55
B0_FF_MASK =
3
B0_SAME_DIR =
4
B0_HAS_FENC =
8
EOL_UNIX =
0
EOL_DOS =

NL

1
EOL_MAC =

CR NL

2
EOL =

CR

{
  EOL_UNIX => :unix,
  EOL_DOS  => :dos,
  EOL_MAC  => :mac
}.freeze
HEADER_FORMAT =
[
  'A2',   # identifier, "b0"
  'A10',  # version, e.g., "VIM 7.4"
  'V',    # page_size
  'V',    # mtime (not used)
  'V',    # inode
  'V',    # pid
  'A40',  # username or uid
  'A40',  # hostname
  'A890', # filename
  'a8',   # crypt seed (for encrypted swapfiles)
  'C',    # flags
  'C',    # dirty (0x00/0x55)
  #'l!',   # magic_long
  #'i!',   # magic_int
  #'s!',   # magic_short
  #'c',    # magic_char
].join ''

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.swapfile?(name) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/vim_recovery/swapfile.rb', line 51

def self.swapfile?(name)
  open(name) { |f| f.valid_block0? }
end

Instance Method Details

#encrypted?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/vim_recovery/swapfile.rb', line 55

def encrypted?
  # "b0" not encrypted
  # "bc" encrypted (zip)
  # "bC" encrypted (blowfish)
  # "bd" encrypted (blowfish2)
  block0.id[1] != '0'
end

#file_formatObject



103
104
105
106
107
# File 'lib/vim_recovery/swapfile.rb', line 103

def file_format
  # "Zero means it's not set (compatible with Vim 6.x), otherwise it's
  # EOL_UNIX + 1, EOL_DOS + 1 or EOL_MAC + 1."
  EOL[(block0.flags & B0_FF_MASK) - 1]
end

#has_file_encoding?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/vim_recovery/swapfile.rb', line 99

def has_file_encoding?
  B0_HAS_FENC & block0.flags > 0
end

#modified?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/vim_recovery/swapfile.rb', line 63

def modified?
  block0.dirty == B0_DIRTY
end

#mtimeObject



67
68
69
# File 'lib/vim_recovery/swapfile.rb', line 67

def mtime
  block0.mtime == 0 ? super : Time.at(block0.mtime)
end

#original_filenameObject



75
76
77
# File 'lib/vim_recovery/swapfile.rb', line 75

def original_filename
  block0.fname unless block0.fname.empty?
end

#pidObject



71
72
73
# File 'lib/vim_recovery/swapfile.rb', line 71

def pid
  block0.pid
end

#same_dir?Boolean

Swap file is in directory of edited file (see “:help directory”).

Returns:

  • (Boolean)


95
96
97
# File 'lib/vim_recovery/swapfile.rb', line 95

def same_dir?
  B0_SAME_DIR & block0.flags > 0
end

#still_running?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/vim_recovery/swapfile.rb', line 79

def still_running?
  Process.getpgid pid
  true
rescue Errno::ESRCH
  false
end

#valid_block0?Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/vim_recovery/swapfile.rb', line 109

def valid_block0?
  rewind
  VALID_BLOCK_IDS.include?(read 2)
end