Skip to content

Commit 2a949ab

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 0cfb182 commit 2a949ab

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

compat/mingw.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,6 +2936,34 @@ int link(const char *oldpath, const char *newpath)
29362936
return 0;
29372937
}
29382938

2939+
int symlink(const char *target, const char *link)
2940+
{
2941+
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
2942+
int len;
2943+
2944+
/* fail if symlinks are disabled or API is not supported (WinXP) */
2945+
if (!has_symlinks) {
2946+
errno = ENOSYS;
2947+
return -1;
2948+
}
2949+
2950+
if ((len = xutftowcs_long_path(wtarget, target)) < 0
2951+
|| xutftowcs_long_path(wlink, link) < 0)
2952+
return -1;
2953+
2954+
/* convert target dir separators to backslashes */
2955+
while (len--)
2956+
if (wtarget[len] == '/')
2957+
wtarget[len] = '\\';
2958+
2959+
/* create file symlink */
2960+
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2961+
errno = err_win_to_posix(GetLastError());
2962+
return -1;
2963+
}
2964+
return 0;
2965+
}
2966+
29392967
#ifndef _WINNT_H
29402968
/*
29412969
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in

compat/mingw.h

Lines changed: 1 addition & 2 deletions
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 UNUSED, const char *newpath UNUSED)
129-
{ errno = ENOSYS; return -1; }
130128
static inline int fchmod(int fildes UNUSED, mode_t mode UNUSED)
131129
{ errno = ENOSYS; return -1; }
132130
#ifndef __MINGW64_VERSION_MAJOR
@@ -220,6 +218,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
220218
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
221219
int link(const char *oldpath, const char *newpath);
222220
int uname(struct utsname *buf);
221+
int symlink(const char *target, const char *link);
223222
int readlink(const char *path, char *buf, size_t bufsiz);
224223

225224
/*

0 commit comments

Comments
 (0)