Class: Pikuri::Tool::Workspace::Cwd

Inherits:
Pikuri::Tool::Workspace show all
Defined in:
lib/pikuri/tool/workspace.rb

Overview

Locks read+write to a single subtree (the root: passed at construction, with symlinks resolved). cwd equals the root.

Containment algorithm

#resolve walks up the input path to its deepest existing ancestor, realpath‘s that ancestor (resolving any symlinks in the existing portion), then verifies the resolved anchor is @root or a descendant. Four cases the algorithm must handle:

  1. lib/foo.rb (exists) → existing = full path, anchor in root → returns the realpath’d file.

  2. lib/new/dir/foo.rb (intermediates missing) → walks up to @root/lib, anchor in root → returns the intended new path (caller mkdir_ps the parent before writing).

  3. lib/../../etc/passwd (.. escape) → cleanpath collapses .. syntactically, walks land outside @rootError.

  4. link/foo.rb where link → /etc (symlink escape) → walks to link (which exists), realpath resolves through the symlink to /etc, outside @rootError.

Pure lexical normalization (cleanpath + prefix check) catches cases 1–3 but misses case 4. The walk-up step adds the realpath pass on the existing prefix, closing that gap.

Instance Method Summary collapse

Constructor Details

#initialize(root:) ⇒ Cwd

Returns a new instance of Cwd.

Parameters:

  • root (String, Pathname)

    absolute (or working-directory- relative) path to anchor the workspace at. realpath‘d once so subsequent comparisons happen in canonical form.

Raises:

  • (Errno::ENOENT)

    if root does not exist; surfaces immediately so misconfigured callers fail loudly.



106
107
108
# File 'lib/pikuri/tool/workspace.rb', line 106

def initialize(root:)
  @root = Pathname.new(root).realpath
end

Instance Method Details

#cwdPathname

Returns:

  • (Pathname)


111
112
113
# File 'lib/pikuri/tool/workspace.rb', line 111

def cwd
  @root
end

#resolve_for_read(path) ⇒ Pathname

Parameters:

  • path (String)

Returns:

  • (Pathname)

Raises:



118
119
120
# File 'lib/pikuri/tool/workspace.rb', line 118

def resolve_for_read(path)
  resolve(path)
end

#resolve_for_write(path) ⇒ Pathname

Parameters:

  • path (String)

Returns:

  • (Pathname)

Raises:



125
126
127
# File 'lib/pikuri/tool/workspace.rb', line 125

def resolve_for_write(path)
  resolve(path)
end