# class Chef::ReservedNames::Win32::File

### Public Class Methods

get_long_path_name(path) click to toggle source

Gets the long form of a path (ADMINI~1 -> Administrator)

    # File lib/chef/win32/file.rb, line 131
    def self.get_long_path_name(path)
      path = path.to_wstring
      size = GetLongPathNameW(path, nil, 0)
      if size == 0
        Chef::ReservedNames::Win32::Error.raise!
      end
      result = FFI::MemoryPointer.new :char, (size+1)*2
      if GetLongPathNameW(path, result, size+1) == 0
        Chef::ReservedNames::Win32::Error.raise!
      end
      result.read_wstring(size)
    end

get_short_path_name(path) click to toggle source

Gets the short form of a path (Administrator -> ADMINI~1)

    # File lib/chef/win32/file.rb, line 117
    def self.get_short_path_name(path)
      path = path.to_wstring
      size = GetShortPathNameW(path, nil, 0)
      if size == 0
        Chef::ReservedNames::Win32::Error.raise!
      end
      result = FFI::MemoryPointer.new :char, (size+1)*2
      if GetShortPathNameW(path, result, size+1) == 0
        Chef::ReservedNames::Win32::Error.raise!
      end
      result.read_wstring(size)
    end

info(file_name) click to toggle source

    # File lib/chef/win32/file.rb, line 144
    def self.info(file_name)
      Info.new(file_name)
    end

Also aliased as: stat
link(old_name, new_name) click to toggle source

Creates a symbolic link called `new_name` for the file or directory `old_name`.

This method requires Windows Vista or later to work. Otherwise, it returns nil as per MRI.

    # File lib/chef/win32/file.rb, line 36
    def self.link(old_name, new_name)
      raise Errno::ENOENT, "(#{old_name}, #{new_name})" unless ::File.exist?(old_name)
      # TODO do a check for CreateHardLinkW and
      # raise NotImplemented exception on older Windows
      old_name = encode_path(old_name)
      new_name = encode_path(new_name)
      unless CreateHardLinkW(new_name, old_name, nil)
        Chef::ReservedNames::Win32::Error.raise!
      end
    end

readlink(link_name) click to toggle source

Returns the path of the of the symbolic link referred to by `file`.

Requires Windows Vista or later. On older versions of Windows it will raise a NotImplementedError, as per MRI.

    # File lib/chef/win32/file.rb, line 90
    def self.readlink(link_name)
      raise Errno::ENOENT, link_name unless ::File.exists?(link_name)
      symlink_file_handle(link_name) do |handle|
        # Go to DeviceIoControl to get the symlink information
        # http://msdn.microsoft.com/en-us/library/windows/desktop/aa364571(v=vs.85).aspx
        reparse_buffer = FFI::MemoryPointer.new(MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
        parsed_size = FFI::Buffer.new(:long).write_long(0)
        if DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, nil, 0, reparse_buffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, parsed_size, nil) == 0
          Chef::ReservedNames::Win32::Error.raise!
        end

        # Ensure it's a symbolic link
        reparse_buffer = REPARSE_DATA_BUFFER.new(reparse_buffer)
        if reparse_buffer[:ReparseTag] != IO_REPARSE_TAG_SYMLINK
          raise Errno::EACCES, "#{link_name} is not a symlink"
        end

        # Return the link destination (strip off \??\ at the beginning, which is a local filesystem thing)
        link_dest = reparse_buffer.reparse_buffer.substitute_name
        if link_dest =~ /^\\?\?\/
          link_dest = link_dest[4..-1]
        end
        link_dest
      end
    end

stat(file_name)
Alias for: info
symlink(old_name, new_name) click to toggle source

Creates a symbolic link called `new_name` for the file or directory `old_name`.

This method requires Windows Vista or later to work. Otherwise, it returns nil as per MRI.

    # File lib/chef/win32/file.rb, line 53
    def self.symlink(old_name, new_name)
      # raise Errno::ENOENT, "(#{old_name}, #{new_name})" unless ::File.exist?(old_name)
      # TODO do a check for CreateSymbolicLinkW and
      # raise NotImplemented exception on older Windows
      flags = ::File.directory?(old_name) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0
      old_name = encode_path(old_name)
      new_name = encode_path(new_name)
      unless CreateSymbolicLinkW(new_name, old_name, flags)
        Chef::ReservedNames::Win32::Error.raise!
      end
    end

symlink?(file_name) click to toggle source

Return true if the named file is a symbolic link, false otherwise.

This method requires Windows Vista or later to work. Otherwise, it always returns false as per MRI.

    # File lib/chef/win32/file.rb, line 70
    def self.symlink?(file_name)
      is_symlink = false
      path = encode_path(file_name)
      if ::File.exists?(file_name)
        if ((GetFileAttributesW(path) & FILE_ATTRIBUTE_REPARSE_POINT) > 0)
          file_search_handle(file_name) do |handle, find_data|
            if find_data[:dw_reserved_0] == IO_REPARSE_TAG_SYMLINK
              is_symlink = true
            end
          end
        end
      end
      is_symlink
    end

verify_links_supported!() click to toggle source

    # File lib/chef/win32/file.rb, line 148
    def self.verify_links_supported!
      begin
        CreateSymbolicLinkW(nil)
      rescue Chef::Exceptions::Win32APIFunctionNotImplemented => e
        raise e
      rescue Exception
        # things are ok.
      end
    end