def self.create_junction(to, from)
to.tr!('/', "\\")
from.tr!('/', "\\")
to_path = 0.chr * 260
from_path = 0.chr * 260
buf_target = 0.chr * 260
if GetFullPathName(from, from_path.size, from_path, 0) == 0
raise StandardError, 'GetFullPathName() failed: ' + get_last_error
end
if GetFullPathName(to, to_path.size, to_path, 0) == 0
raise StandardError, 'GetFullPathName() failed: ' + get_last_error
end
to_path = to_path.split(0.chr).first
from_path = from_path.split(0.chr).first
rv = CreateDirectory(to_path, 0)
if rv == 0 && rv != ERROR_ALREADY_EXISTS
raise StandardError, 'CreateDirectory() failed: ' + get_last_error
end
handle = CreateFile(
to_path,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
0
)
if handle == INVALID_HANDLE_VALUE
raise StandardError, 'CreateFile() failed: ' + get_last_error
end
buf_target = buf_target.split(0.chr).first
buf_target = "\\??\\" << from_path
length = buf_target.size * 2
wide_string = multi_to_wide(buf_target)
rdb = [
"0xA0000003L".hex,
wide_string.size + 12,
0,
0,
wide_string.size,
wide_string.size + 2,
0,
wide_string
].pack('LSSSSSSa' + (wide_string.size + 4).to_s)
bytes = [0].pack('L')
bool = DeviceIoControl(
handle,
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_ANY_ACCESS),
rdb,
rdb.size,
0,
0,
bytes,
0
)
unless bool
error = 'DeviceIoControl() failed: ' + get_last_error
RemoveDirectory(to)
CloseHandle(handle)
raise error
end
CloseHandle(handle)
self
end