Module: Robocap::SDK::VaultLayout

Defined in:
lib/robocap/sdk/vault_layout.rb

Class Method Summary collapse

Class Method Details

.atomic_write_bytes(target, data) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/robocap/sdk/vault_layout.rb', line 19

def atomic_write_bytes(target, data)
  target = Pathname(target)
  tmp = nil
  begin
    ensure_private_dir(target.parent)
    tmp = Tempfile.new(['.tmp_', ''], target.parent.to_s, binmode: true)
    tmp.write(data)
    tmp.flush
    tmp.fsync rescue nil
    tmp.close
    File.rename(tmp.path, target.to_s)
    tmp = nil
  rescue SystemCallError => exc
    raise Error.new(
      code: ErrorCode::ERR_VAULT_IO,
      message: "Failed to write #{target}",
      detail: { path: target.to_s, reason: exc.message },
    )
  ensure
    if tmp
      tmp.close unless tmp.closed?
      File.unlink(tmp.path) if File.exist?(tmp.path)
    end
  end
end

.atomic_write_text(target, text, encoding: 'UTF-8') ⇒ Object



45
46
47
# File 'lib/robocap/sdk/vault_layout.rb', line 45

def atomic_write_text(target, text, encoding: 'UTF-8')
  atomic_write_bytes(target, text.encode(encoding).b)
end

.ensure_private_dir(path) ⇒ Object



13
14
15
16
17
# File 'lib/robocap/sdk/vault_layout.rb', line 13

def ensure_private_dir(path)
  path = Pathname(path)
  FileUtils.mkdir_p(path)
  File.chmod(0o700, path) unless Gem.win_platform?
end