Skip to content

Commit d34b54f

Browse files
kbleesGit for Windows Build Agent
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 53a836f commit d34b54f

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
@@ -120,8 +120,6 @@ struct utsname {
120120
* trivial stubs
121121
*/
122122

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

199198
/*

compat/mingw.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,34 @@ int link(const char *oldpath, const char *newpath)
29592959
return 0;
29602960
}
29612961

2962+
int symlink(const char *target, const char *link)
2963+
{
2964+
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
2965+
int len;
2966+
2967+
/* fail if symlinks are disabled or API is not supported (WinXP) */
2968+
if (!has_symlinks) {
2969+
errno = ENOSYS;
2970+
return -1;
2971+
}
2972+
2973+
if ((len = xutftowcs_long_path(wtarget, target)) < 0
2974+
|| xutftowcs_long_path(wlink, link) < 0)
2975+
return -1;
2976+
2977+
/* convert target dir separators to backslashes */
2978+
while (len--)
2979+
if (wtarget[len] == '/')
2980+
wtarget[len] = '\\';
2981+
2982+
/* create file symlink */
2983+
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2984+
errno = err_win_to_posix(GetLastError());
2985+
return -1;
2986+
}
2987+
return 0;
2988+
}
2989+
29622990
#ifndef _WINNT_H
29632991
/*
29642992
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in

0 commit comments

Comments
 (0)