Module: Agent::Lock::Freeze

Defined in:
lib/agent/lock/freeze.rb

Overview

Optional teeth, for the scope you want nobody to touch at all.

An advisory lock works because every agent checks it. That covers the agents that check. chflags uchg on macOS makes the file unwritable by anything, checking or not, which is the right answer for a handful of files that must not move while something else runs.

It is opt-in for three reasons. It is macOS only, since Linux's chattr +i needs root. It denies the holder too, so it fits a freeze rather than a file you are editing. And a session that dies with files frozen leaves a tree where git checkout and rm -rf fail with "Operation not permitted", which is why every frozen path is written into the lock: the thaw does not depend on the process that froze them still being alive.

Defined Under Namespace

Classes: TooBroad

Constant Summary collapse

LIMIT =

A freeze walks and flags every matched file, so a scope covering a whole checkout is a mistake rather than an instruction.

500
BATCH =

How many paths one chflags invocation is given.

200
UNTOUCHABLE =

Path segments no scope may freeze through, at any depth.

.git is git's bookkeeping, not content: with the index or a ref frozen, git add and git commit fail with "Operation not permitted", and a linked worktree whose .git file is frozen cannot be removed. That holds just as well one level down, for a submodule's .git file or the .git directory of a clone dropped into the tree, so those are left alone too, while the files beside them are frozen like any others. . and .. are a second spelling of a path: .* matches . itself, so .*/** used to answer ./lib/cli.rb.

%w[. .. .git].freeze

Class Method Summary collapse

Class Method Details

.apply(paths, tree:, wide: false) ⇒ Array<String>

Returns what was frozen.

Parameters:

  • paths (Array<String>) —

    relative to the tree root

  • tree (Tree)
  • wide (Boolean) (defaults to: false) —

    allow a freeze wider than LIMIT

Returns:

  • (Array<String>) —

    what was frozen

Raises:



98
99
100
101
102
103
104
105
106
107
# File 'lib/agent/lock/freeze.rb', line 98

def apply(paths, tree:, wide: false)
  raise TooBroad, "--enforce needs macOS; this is #{RUBY_PLATFORM}" unless supported?
  return [] if paths.empty?

  if paths.size > LIMIT && !wide
    raise TooBroad, "#{paths.size} files is wider than a freeze should be (limit #{LIMIT}); pass --allow-wide"
  end

  chflags("uchg", paths, tree)
end

.chflags(flag, paths, tree) ⇒ Array<String>

Only the files chflags actually accepted come back.

A batch that fails is retried one file at a time, because the usual reason is a single path somebody else owns, and reporting the whole batch as frozen would leave the lock claiming protection it does not have. Reporting the whole batch as failed would be just as wrong.

Parameters:

  • flag (String) —

    uchg or nouchg

  • paths (Array<String>) —

    relative to the tree root

  • tree (Tree)

Returns:

  • (Array<String>) —

    relative paths that are now flagged



132
133
134
135
136
137
138
# File 'lib/agent/lock/freeze.rb', line 132

def chflags(flag, paths, tree)
  return [] unless supported?

  paths.each_slice(BATCH)
       .flat_map { |batch| flag_batch(flag, existing(batch, tree)) }
       .map { |path| path.delete_prefix("#{tree.root}/") }
end

.clear(paths, tree:) ⇒ Array<String>

Best effort on purpose: a path that has since been deleted or already thawed is not a reason to leave the rest frozen.

Parameters:

  • paths (Array<String>, nil) —

    relative to the tree root

  • tree (Tree)

Returns:

  • (Array<String>) —

    what was thawed



115
116
117
118
119
# File 'lib/agent/lock/freeze.rb', line 115

def clear(paths, tree:)
  return [] if paths.nil? || paths.empty?

  chflags("nouchg", paths, tree)
end

.existing(batch, tree) ⇒ Array<String>

Returns absolute paths that are still there to flag.

Parameters:

  • batch (Array<String>) —

    relative to the tree root

  • tree (Tree)

Returns:

  • (Array<String>) —

    absolute paths that are still there to flag



143
144
145
# File 'lib/agent/lock/freeze.rb', line 143

def existing(batch, tree)
  batch.map { |rel| File.join(tree.root, rel) }.select { |path| File.exist?(path) }
end

.flag_batch(flag, absolute) ⇒ Array<String>

Returns the ones the command accepted.

Parameters:

  • flag (String) —

    uchg or nouchg

  • absolute (Array<String>) —

    absolute paths

Returns:

  • (Array<String>) —

    the ones the command accepted



150
151
152
153
154
155
# File 'lib/agent/lock/freeze.rb', line 150

def flag_batch(flag, absolute)
  return [] if absolute.empty?
  return absolute if run(flag, absolute)

  absolute.select { |path| run(flag, [path]) }
end

.matches(scope, tree) ⇒ Array<String>

Dotfiles match like any other file. Without FNM_DOTMATCH, * and ** skip them, so conf/.env stayed writable while the command reported conf/** frozen, and .env and credential keys are what this is most often pointed at. Each file is answered once, since two alternatives of a brace can match one file, and it would then count twice toward LIMIT.

Parameters:

Returns:

  • (Array<String>) —

    the files it would freeze, relative to the root



60
61
62
63
64
65
66
67
# File 'lib/agent/lock/freeze.rb', line 60

def matches(scope, tree)
  store = store_prefix(tree)
  Dir.glob(recursive(scope.pattern), base: tree.root, flags: File::FNM_EXTGLOB | File::FNM_DOTMATCH)
     .reject { |rel| rel.split("/").intersect?(UNTOUCHABLE) || (store && rel.start_with?(store)) }
     .select { |rel| File.file?(File.join(tree.root, rel)) }
     .uniq
     .sort
end

.recursive(pattern) ⇒ String

A trailing ** means "everything under here" to this gem, and to anybody typing it. It does not mean that to Dir.glob, where a bare ** at the end matches one level, exactly like *. Only **/ walks down. So a scope that covers a subtree is spelled out before globbing.

Parameters:

  • pattern (String)

Returns:

  • (String)


92
# File 'lib/agent/lock/freeze.rb', line 92

def recursive(pattern) = pattern.end_with?("**") ? "#{pattern}/*" : pattern

.run(flag, paths) ⇒ Boolean

Returns whether the command reported success.

Parameters:

  • flag (String) —

    uchg or nouchg

  • paths (Array<String>) —

    absolute paths

Returns:

  • (Boolean) —

    whether the command reported success



160
# File 'lib/agent/lock/freeze.rb', line 160

def run(flag, paths) = system("chflags", flag, *paths, out: File::NULL, err: File::NULL) || false

.store_prefix(tree) ⇒ String?

Where the lock store sits when it sits inside the tree, which it does outside git for a tree rooted at or above the home directory, and wherever $AGENT_LOCK_DIR says. Inside git it is under .git, which is already left alone. The file store renames a lock into place, and a frozen lock refuses that, so the freeze that had just run would be recorded nowhere.

Parameters:

Returns:

  • (String, nil) —

    relative to the root, with a trailing slash; nil when the store is outside the tree



79
80
81
82
83
# File 'lib/agent/lock/freeze.rb', line 79

def store_prefix(tree)
  dir = tree.store_dir
  dir = File.realpath(dir) if File.exist?(dir)
  "#{dir.delete_prefix("#{tree.root}/")}/" if dir.start_with?("#{tree.root}/")
end

.supported? ⇒ Boolean

Returns:

  • (Boolean)


48
# File 'lib/agent/lock/freeze.rb', line 48

def supported? = RUBY_PLATFORM.include?("darwin")