Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds symlink to windows #1

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions std/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -3246,6 +3246,41 @@ if ((isSomeFiniteCharInputRange!RO || isConvertibleToString!RO) &&
}
}

version(Windows)
{
import core.sys.windows.w32api:_WIN32_WINNT;;
static if(_WIN32_WINNT >= 0x600) //WindowsVista or later
{
void symlink(string original, string link)
{
import core.stdc.string:strlen;
import core.sys.windows.winbase;
import core.sys.windows.windef:DWORD, MAX_PATH, LPWSTR;
import std.utf:toUTF16z;
import std.file:FileException;

DWORD typeFlag = 0; //File
if(std.file.isDir(original))
typeFlag = SYMBOLIC_LINK_FLAG_DIRECTORY;
typeFlag|= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;

if(link.length > MAX_PATH) link = `\\?\`~link;
if(original.length > MAX_PATH) original = `\\?\`~original;

if(!CreateSymbolicLinkW(link.toUTF16z, original.toUTF16z, typeFlag))
{
LPWSTR strBuffer;
DWORD length = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, null, GetLastError(),0, cast(LPWSTR)&strBuffer, 0, null);
wchar[] str = new wchar[length];
str[] = strBuffer[0..str.length];
LocalFree(strBuffer);
import std.conv;
throw new FileException(original, str.to!string);
}
}
}
}

version (Posix) @safe unittest
{
if (system_directory.exists)
Expand Down