Skip to content

Commit 1adf97b

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 7fd7555 commit 1adf97b

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
@@ -2225,6 +2225,34 @@ int link(const char *oldpath, const char *newpath)
22252225
return 0;
22262226
}
22272227

2228+
int symlink(const char *target, const char *link)
2229+
{
2230+
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
2231+
int len;
2232+
2233+
/* fail if symlinks are disabled or API is not supported (WinXP) */
2234+
if (!has_symlinks) {
2235+
errno = ENOSYS;
2236+
return -1;
2237+
}
2238+
2239+
if ((len = xutftowcs_long_path(wtarget, target)) < 0
2240+
|| xutftowcs_long_path(wlink, link) < 0)
2241+
return -1;
2242+
2243+
/* convert target dir separators to backslashes */
2244+
while (len--)
2245+
if (wtarget[len] == '/')
2246+
wtarget[len] = '\\';
2247+
2248+
/* create file symlink */
2249+
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2250+
errno = err_win_to_posix(GetLastError());
2251+
return -1;
2252+
}
2253+
return 0;
2254+
}
2255+
22282256
#ifndef _WINNT_H
22292257
/*
22302258
* 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
@@ -123,8 +123,6 @@ struct utsname {
123123
* trivial stubs
124124
*/
125125

126-
static inline int symlink(const char *oldpath, const char *newpath)
127-
{ errno = ENOSYS; return -1; }
128126
static inline int fchmod(int fildes, mode_t mode)
129127
{ errno = ENOSYS; return -1; }
130128
#ifndef __MINGW64_VERSION_MAJOR
@@ -215,6 +213,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
215213
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
216214
int link(const char *oldpath, const char *newpath);
217215
int uname(struct utsname *buf);
216+
int symlink(const char *target, const char *link);
218217
int readlink(const char *path, char *buf, size_t bufsiz);
219218

220219
/*

0 commit comments

Comments
 (0)