Module: Overcommit::Utils::FileUtils

Defined in:
lib/overcommit/utils/file_utils.rb

Overview

Utility functions for file IO.

Class Method Summary collapse

Class Method Details

When the host OS is Windows, uses the ‘dir` command to check whether `link_name` is an NTFS symbolic link. If so, it parses the target from the command output. Otherwise raises an `ArgumentError`. Delegates to `File.readlink` if the host OS is not Windows.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/overcommit/utils/file_utils.rb', line 34

def readlink(link_name)
  return File.readlink(link_name) unless Overcommit::OS.windows?

  result = win32_dir_cmd(link_name)

  unless win32_symlink?(result.stdout)
    raise ArgumentError, "#{link_name} is not a symlink"
  end

  # Extract symlink target from output, which looks like:
  #   11/13/2012 12:53 AM <SYMLINK> mysymlink [C:\Windows\Temp\somefile.txt]
  result.stdout[/\[(.+)\]/, 1]
end

When the host OS is Windows, uses the ‘mklink` command to create an NTFS symbolic link from `new_name` to `old_name`. Otherwise delegates to `File.symlink`



13
14
15
16
17
18
# File 'lib/overcommit/utils/file_utils.rb', line 13

def symlink(old_name, new_name)
  return File.symlink(old_name, new_name) unless Overcommit::OS.windows?

  result = win32_mklink_cmd(old_name, new_name)
  result.status
end

.symlink?(file_name) ⇒ Boolean

When the host OS is Windows, uses the ‘dir` command to check whether `file_name` is an NTFS symbolic link. Otherwise delegates to `File.symlink`.

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/overcommit/utils/file_utils.rb', line 23

def symlink?(file_name)
  return File.symlink?(file_name) unless Overcommit::OS.windows?

  result = win32_dir_cmd(file_name)
  win32_symlink?(result.stdout)
end