Module: Lich::Util::Update::FileWriter

Defined in:
documented/common/update/file_writer.rb

Overview

Atomic file writing utilities for safe, transactional updates.

Provides the write-temp-then-rename pattern and SHA map building for detecting file changes. Methods in this module are called from the update system and from script initialization; script authors do not use this module directly.

Constant Summary collapse

STALE_STAGING_AGE =

A staging file is only left behind when its writer was hard-killed: SIGKILL, power loss, or a Process.exit! (which skips ensure blocks), such as the shutdown watchdog's forced exit when a shutdown overruns its deadline mid-write. One older than this is treated as such a leftover and removed after the next successful write of the same file. The age is large so that a writer stalled on a slow or hung drive keeps its staging file; if one is removed anyway, that writer's rename fails and the live file is left untouched.

24 * 60 * 60

Class Method Summary collapse

Class Method Details

.build_local_sha_map(dir, pattern = '*.lic') ⇒ Hash<String, String>

Builds filename => git-SHA map for files in directory.

Parameters:

  • dir (String) —

    directory path

  • pattern (String) (defaults to: '*.lic') —

    glob pattern (default: '*.lic')

Returns:



198
199
200
201
202
203
# File 'documented/common/update/file_writer.rb', line 198

def self.build_local_sha_map(dir, pattern = '*.lic')
  Dir[File.join(dir, pattern)].each_with_object({}) do |path, map|
    body = File.binread(path)
    map[File.basename(path)] = Digest::SHA1.hexdigest("blob #{body.size}\0#{body}")
  end
end

.safe_write(path, content) ⇒ void

This method returns an undefined value.

Atomically replaces the file at path with content.

The content is written to a temporary file in the same directory and then renamed over path. The live file is never moved away, so a reader (for example a script start resolving the name) sees either the old content or the new content, never a missing file. File.rename replaces an existing destination on POSIX (rename(2)) and on Windows (MoveFileExW with MOVEFILE_REPLACE_EXISTING).

The temporary name carries the process and thread so two writers of the same file (two sessions sharing a scripts directory, or a manual sync during the login sync) never share one. It ends in .tmp, so neither the script resolver nor any sync SHA glob (+*.lic+, *.yaml, *.xml) ever matches it.

The temporary file is removed however safe_write exits: an exception, a Thread#kill, or the process exiting while the background login sync is mid-write. Only a hard kill (SIGKILL, power loss, or a Process.exit! such as the shutdown watchdog's forced exit) can leave one; see STALE_STAGING_AGE.

After a successful write, leftovers beside path are removed (best effort): a .old left by an older version (unless it holds exactly content), and a .tmp left by an older version or a staging file of this name, once either is older than STALE_STAGING_AGE. If the file system kept an existing name that differs from +path+'s only in case, the file is renamed to +path+'s exact name.

Parameters:

  • path (String) —

    target file path

  • content (String) —

    file content to write

Raises:

  • (StandardError) —

    if the write or rename fails; path is left untouched and the temporary file is removed. Nothing else is touched either: a .old left by an interrupted older update is not renamed back, even when path is missing.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'documented/common/update/file_writer.rb', line 68

def self.safe_write(path, content)
  tmp = "#{path}.#{Process.pid}.#{Thread.current.object_id}.tmp"
  begin
    File.binwrite(tmp, content)
    File.rename(tmp, path)
  ensure
    # After a successful rename the staging file no longer exists.
    begin
      File.delete(tmp) if File.exist?(tmp)
    rescue StandardError
      nil # the write error, if any, is the one worth reporting
    end
  end
  remove_leftovers(path, content)
end