Skip to content

Commit 7be401e

Browse files
petrkodlgitster
authored andcommitted
MinGW: a hardlink implementation
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b3debd2 commit 7be401e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

compat/mingw.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,3 +1118,24 @@ void mingw_open_html(const char *unixpath)
11181118
printf("Launching default browser to display HTML ...\n");
11191119
ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0);
11201120
}
1121+
1122+
int link(const char *oldpath, const char *newpath)
1123+
{
1124+
typedef BOOL WINAPI (*T)(const char*, const char*, LPSECURITY_ATTRIBUTES);
1125+
static T create_hard_link = NULL;
1126+
if (!create_hard_link) {
1127+
create_hard_link = (T) GetProcAddress(
1128+
GetModuleHandle("kernel32.dll"), "CreateHardLinkA");
1129+
if (!create_hard_link)
1130+
create_hard_link = (T)-1;
1131+
}
1132+
if (create_hard_link == (T)-1) {
1133+
errno = ENOSYS;
1134+
return -1;
1135+
}
1136+
if (!create_hard_link(newpath, oldpath, NULL)) {
1137+
errno = err_win_to_posix(GetLastError());
1138+
return -1;
1139+
}
1140+
return 0;
1141+
}

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ static inline int readlink(const char *path, char *buf, size_t bufsiz)
6767
{ errno = ENOSYS; return -1; }
6868
static inline int symlink(const char *oldpath, const char *newpath)
6969
{ errno = ENOSYS; return -1; }
70-
static inline int link(const char *oldpath, const char *newpath)
71-
{ errno = ENOSYS; return -1; }
7270
static inline int fchmod(int fildes, mode_t mode)
7371
{ errno = ENOSYS; return -1; }
7472
static inline int fork(void)
@@ -134,6 +132,7 @@ int getpagesize(void); /* defined in MinGW's libgcc.a */
134132
struct passwd *getpwuid(int uid);
135133
int setitimer(int type, struct itimerval *in, struct itimerval *out);
136134
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
135+
int link(const char *oldpath, const char *newpath);
137136

138137
/*
139138
* replacements of existing functions

0 commit comments

Comments
 (0)