Skip to content

Commit 96a715f

Browse files
kbleesdscho
authored andcommitted
Win32: implement basic symlink() functionality (file symlinks only)
Implement symlink() that always creates file symlinks. Fails with ENOSYS if symlinks are disabled or unsupported. Note: CreateSymbolicLinkW() was introduced with symlink support in Windows Vista. For compatibility with Windows XP, we need to load it dynamically and fail gracefully if it isnt's available. Signed-off-by: Karsten Blees <[email protected]>
1 parent ec1e4a3 commit 96a715f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

compat/mingw.c

+28
Original file line numberDiff line numberDiff line change
@@ -2712,6 +2712,34 @@ int link(const char *oldpath, const char *newpath)
27122712
return 0;
27132713
}
27142714

2715+
int symlink(const char *target, const char *link)
2716+
{
2717+
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
2718+
int len;
2719+
2720+
/* fail if symlinks are disabled or API is not supported (WinXP) */
2721+
if (!has_symlinks) {
2722+
errno = ENOSYS;
2723+
return -1;
2724+
}
2725+
2726+
if ((len = xutftowcs_long_path(wtarget, target)) < 0
2727+
|| xutftowcs_long_path(wlink, link) < 0)
2728+
return -1;
2729+
2730+
/* convert target dir separators to backslashes */
2731+
while (len--)
2732+
if (wtarget[len] == '/')
2733+
wtarget[len] = '\\';
2734+
2735+
/* create file symlink */
2736+
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2737+
errno = err_win_to_posix(GetLastError());
2738+
return -1;
2739+
}
2740+
return 0;
2741+
}
2742+
27152743
#ifndef _WINNT_H
27162744
/*
27172745
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in

compat/mingw.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ struct utsname {
125125
* trivial stubs
126126
*/
127127

128-
static inline int symlink(const char *oldpath, const char *newpath)
129-
{ errno = ENOSYS; return -1; }
130128
static inline int fchmod(int fildes, mode_t mode)
131129
{ errno = ENOSYS; return -1; }
132130
#ifndef __MINGW64_VERSION_MAJOR
@@ -217,6 +215,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
217215
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
218216
int link(const char *oldpath, const char *newpath);
219217
int uname(struct utsname *buf);
218+
int symlink(const char *target, const char *link);
220219
int readlink(const char *path, char *buf, size_t bufsiz);
221220

222221
/*

0 commit comments

Comments
 (0)