Created
November 18, 2011 17:32
-
-
Save mmzyk/1377135 to your computer and use it in GitHub Desktop.
Win API File abstraction calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/win32/api/file' | |
require 'chef/win32/error' | |
require 'chef/win32/unicode' | |
class Chef | |
module Win32 | |
class File | |
class << self | |
include Chef::Win32::API::File | |
def symlink?(file_name) | |
is_symlink = false | |
if ((GetFileAttributesW(windows_encode_path(file_name)) & FILE_ATTRIBUTE_REPARSE_POINT) > 0) | |
find_data = WIN32_FIND_DATA.new | |
if FindFirstFileW(path, find_data) == INVALID_HANDLE_VALUE | |
Chef::Win32::Error.raise! | |
end | |
if find_data[:dw_reserved_0] == IO_REPARSE_TAG_SYMLINK | |
is_symlink = true | |
end | |
end | |
is_symlink | |
end | |
def readlink(link_name) | |
out = FFI::MemoryPointer.new MAX_PATH | |
#returns 0 on failure, returns buffer size on success OR if buffer size is too small, returns needed buffer size | |
#GetFilePathNameByHandle fails in interesting ways with remote paths, see: | |
#http://stackoverflow.com/questions/221417/how-do-i-programmatically-access-the-target-path-of-a-windows-symbolic-link | |
pointer = FFI::MemoryPointer.from_string(windows_encode_path(link_name)) | |
buffer_size = GetFinalPathNameByHandleW(pointer, out, out.size, FILE_NAME_NORMALIZED) | |
if (buffer_size == 0 || buffer_size >= MAX_PATH) | |
Chef::Win32::Error.raise! #could be misleading if problem is too small buffer size as GetLastError won't report failure | |
end | |
out.get_string | |
end | |
def windows_encode_path(path) | |
("\\\\?\\" << path).to_wstring | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment