Module: Omnizip::LinkHandler

Defined in:
lib/omnizip/link_handler.rb,
lib/omnizip/link_handler/hard_link.rb,
lib/omnizip/link_handler/symbolic_link.rb

Overview

Handles symbolic and hard link operations with platform detection

Defined Under Namespace

Classes: HardLink, SymbolicLink

Class Method Summary collapse

Class Method Details

Create a hard link



70
71
72
73
74
75
76
77
78
# File 'lib/omnizip/link_handler.rb', line 70

def create_hardlink(target, link_path)
  unless hardlink_supported?
    raise Omnizip::Error,
          "Hard links are not supported on #{RUBY_PLATFORM}"
  end

  FileUtils.mkdir_p(File.dirname(link_path))
  File.link(target, link_path)
end

Create a symbolic link



59
60
61
62
63
64
65
66
67
# File 'lib/omnizip/link_handler.rb', line 59

def create_symlink(target, link_path)
  unless symlink_supported?
    raise Omnizip::Error,
          "Symbolic links are not supported on #{RUBY_PLATFORM}"
  end

  FileUtils.mkdir_p(File.dirname(link_path))
  File.symlink(target, link_path)
end

Detect the type of link (or nil if not a link)



50
51
52
53
54
55
56
# File 'lib/omnizip/link_handler.rb', line 50

def detect_link(path)
  return nil unless supported?
  return :symlink if symlink?(path)
  return :hardlink if hardlink?(path)

  nil
end

Create a HardLink instance from a filesystem path



109
110
111
112
113
114
115
116
117
# File 'lib/omnizip/link_handler.rb', line 109

def hard_link_from_path(path, original_path)
  return nil unless hardlink?(path)

  HardLink.new(
    target: original_path,
    path: path,
    inode: inode_number(path),
  )
end

.hardlink?(path) ⇒ Boolean

Detect if a path is a hard link

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
# File 'lib/omnizip/link_handler.rb', line 38

def hardlink?(path)
  return false unless hardlink_supported?
  return false unless File.exist?(path)
  return false if File.directory?(path)

  stat = File.stat(path)
  stat.nlink > 1
rescue StandardError
  false
end

Check if hard links are supported

Returns:

  • (Boolean)


24
25
26
# File 'lib/omnizip/link_handler.rb', line 24

def hardlink_supported?
  supported? && Omnizip::Platform.supports_hardlinks?
end

.inode_number(path) ⇒ Object

Get inode number for hard link tracking



91
92
93
94
95
96
97
98
# File 'lib/omnizip/link_handler.rb', line 91

def inode_number(path)
  return nil unless hardlink_supported?
  return nil unless File.exist?(path)

  File.stat(path).ino
rescue StandardError
  nil
end

Read the target of a symbolic link



81
82
83
84
85
86
87
88
# File 'lib/omnizip/link_handler.rb', line 81

def read_link_target(link_path)
  unless symlink_supported?
    raise Omnizip::Error,
          "Symbolic links are not supported on #{RUBY_PLATFORM}"
  end

  File.readlink(link_path)
end

.supported?Boolean

Check if the platform supports symbolic links

Returns:

  • (Boolean)


10
11
12
# File 'lib/omnizip/link_handler.rb', line 10

def supported?
  !windows_platform?
end

Create a SymbolicLink instance from a filesystem path



101
102
103
104
105
106
# File 'lib/omnizip/link_handler.rb', line 101

def symbolic_link_from_path(path)
  return nil unless symlink?(path)

  target = read_link_target(path)
  SymbolicLink.new(target: target, path: path)
end

.symlink?(path) ⇒ Boolean

Detect if a path is a symbolic link

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/omnizip/link_handler.rb', line 29

def symlink?(path)
  return false unless symlink_supported?

  File.symlink?(path)
rescue StandardError
  false
end

Check if symbolic links are supported on this platform. Delegates to Platform but also gates on LinkHandler#supported? (Windows is treated as unsupported here for historical compatibility — even if Windows Developer Mode would allow symlinks, callers should opt into that explicitly via Platform).

Returns:

  • (Boolean)


19
20
21
# File 'lib/omnizip/link_handler.rb', line 19

def symlink_supported?
  supported? && Omnizip::Platform.supports_symlinks?
end