Skip to content

Commit f65fe79

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 ae05423 commit f65fe79

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

compat/mingw-posix.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ struct utsname {
121121
* trivial stubs
122122
*/
123123

124-
static inline int symlink(const char *oldpath UNUSED, const char *newpath UNUSED)
125-
{ errno = ENOSYS; return -1; }
126124
static inline int fchmod(int fildes UNUSED, mode_t mode UNUSED)
127125
{ errno = ENOSYS; return -1; }
128126
#ifndef __MINGW64_VERSION_MAJOR
@@ -195,6 +193,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
195193
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
196194
int link(const char *oldpath, const char *newpath);
197195
int uname(struct utsname *buf);
196+
int symlink(const char *target, const char *link);
198197
int readlink(const char *path, char *buf, size_t bufsiz);
199198

200199
/*

compat/mingw.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,34 @@ int link(const char *oldpath, const char *newpath)
29712971
return 0;
29722972
}
29732973

2974+
int symlink(const char *target, const char *link)
2975+
{
2976+
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
2977+
int len;
2978+
2979+
/* fail if symlinks are disabled or API is not supported (WinXP) */
2980+
if (!has_symlinks) {
2981+
errno = ENOSYS;
2982+
return -1;
2983+
}
2984+
2985+
if ((len = xutftowcs_long_path(wtarget, target)) < 0
2986+
|| xutftowcs_long_path(wlink, link) < 0)
2987+
return -1;
2988+
2989+
/* convert target dir separators to backslashes */
2990+
while (len--)
2991+
if (wtarget[len] == '/')
2992+
wtarget[len] = '\\';
2993+
2994+
/* create file symlink */
2995+
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2996+
errno = err_win_to_posix(GetLastError());
2997+
return -1;
2998+
}
2999+
return 0;
3000+
}
3001+
29743002
#ifndef _WINNT_H
29753003
/*
29763004
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in

0 commit comments

Comments
 (0)