Skip to content

Commit fff83df

Browse files
committed
Win32: implement basic symlink() (file symlinks only)
Implement symlink() that always creates file symlinks. Fails with ENOSYS if symlinks are disabled or the CreateSymbolicLinkW() API is missing. Signed-off-by: Karsten Blees <[email protected]>
1 parent 8f259bc commit fff83df

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

compat/mingw.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ static int retry_ask_yes_no(int *tries, const char *format, ...)
231231
return result;
232232
}
233233

234+
235+
DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD);
236+
234237
/* Normalizes NT paths as returned by some low-level APIs. */
235238
static wchar_t *normalize_ntpath(wchar_t *wbuf)
236239
{
@@ -2009,6 +2012,34 @@ int link(const char *oldpath, const char *newpath)
20092012
return 0;
20102013
}
20112014

2015+
int symlink(const char *oldpath, const char *newpath)
2016+
{
2017+
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
2018+
int len;
2019+
2020+
/* fail if symlinks are disabled or API is not supported (WinXP) */
2021+
if (!has_symlinks || !INIT_PROC_ADDR(CreateSymbolicLinkW)) {
2022+
errno = ENOSYS;
2023+
return -1;
2024+
}
2025+
2026+
if ((len = xutftowcs_long_path(woldpath, oldpath)) < 0
2027+
|| xutftowcs_long_path(wnewpath, newpath) < 0)
2028+
return -1;
2029+
2030+
/* convert target dir separators to backslashes */
2031+
while (len--)
2032+
if (woldpath[len] == '/')
2033+
woldpath[len] = '\\';
2034+
2035+
/* create file symlink */
2036+
if (!CreateSymbolicLinkW(wnewpath, woldpath, 0)) {
2037+
errno = err_win_to_posix(GetLastError());
2038+
return -1;
2039+
}
2040+
return 0;
2041+
}
2042+
20122043
typedef struct _REPARSE_DATA_BUFFER {
20132044
DWORD ReparseTag;
20142045
WORD ReparseDataLength;

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ struct itimerval {
9393
* trivial stubs
9494
*/
9595

96-
static inline int symlink(const char *oldpath, const char *newpath)
97-
{ errno = ENOSYS; return -1; }
9896
static inline int fchmod(int fildes, mode_t mode)
9997
{ errno = ENOSYS; return -1; }
10098
#ifndef __MINGW64_VERSION_MAJOR
@@ -180,6 +178,7 @@ struct passwd *getpwuid(uid_t uid);
180178
int setitimer(int type, struct itimerval *in, struct itimerval *out);
181179
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
182180
int link(const char *oldpath, const char *newpath);
181+
int symlink(const char *oldpath, const char *newpath);
183182
int readlink(const char *path, char *buf, size_t bufsiz);
184183

185184
/*

0 commit comments

Comments
 (0)