Skip to content

Commit 3f48630

Browse files
committed
Fixed removal of links on Unix-like OSes, updated tests against future regressions
1 parent 7f2a0c8 commit 3f48630

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
lines changed

src/host/os_islink.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ int os_islink(lua_State* L)
3131
#else
3232
{
3333
struct stat buf;
34-
if (lstat(path, &buf) == 0) {
34+
if (lstat(path, &buf) == 0)
35+
{
3536
lua_pushboolean(L, S_ISLNK(buf.st_mode));
3637
return 1;
3738
}

src/host/os_linkdir.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ int do_linkdir(lua_State* L, const char* src, const char* dst)
3434
GetCurrentDirectoryW(MAX_PATH, cwd);
3535

3636
// Convert the source path to a relative path
37-
wchar_t relSrcPath[MAX_PATH];
38-
swprintf(relSrcPath, MAX_PATH, L"%c:%s", cwd[0], wSrcPath);
37+
wchar_t relSrcPath[MAX_PATH + 2];
38+
swprintf(relSrcPath, MAX_PATH + 2, L"%c:%s", cwd[0], wSrcPath);
39+
relSrcPath[MAX_PATH + 1] = L'\0';
3940

4041
BOOLEAN res = CreateSymbolicLinkW(wDstPath, relSrcPath, SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
4142
return res != 0;
@@ -46,8 +47,27 @@ int do_linkdir(lua_State* L, const char* src, const char* dst)
4647
return res != 0;
4748
}
4849
#else
49-
int res = symlink(src, dst);
50-
return res == 0;
50+
(void)L;
51+
if (!do_isabsolute(src))
52+
{
53+
char cwd[PATH_MAX];
54+
if (!do_getcwd(cwd, PATH_MAX))
55+
{
56+
return FALSE;
57+
}
58+
59+
char relSrcPath[2 * PATH_MAX + 1];
60+
snprintf(relSrcPath, 2 * PATH_MAX + 1, "%s/%s", cwd, src);
61+
relSrcPath[2 * PATH_MAX] = '\0';
62+
63+
int res = symlink(relSrcPath, dst);
64+
return res == 0;
65+
}
66+
else
67+
{
68+
int res = symlink(src, dst);
69+
return res == 0;
70+
}
5171
#endif
5272
}
5373

src/host/os_linkfile.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ int do_linkfile(lua_State* L, const char* src, const char* dst)
3333
GetCurrentDirectoryW(MAX_PATH, cwd);
3434

3535
// Convert the source path to a relative path
36-
wchar_t relSrcPath[MAX_PATH];
37-
swprintf(relSrcPath, MAX_PATH, L"%c:%s", cwd[0], wSrcPath);
36+
wchar_t relSrcPath[MAX_PATH + 2];
37+
swprintf(relSrcPath, MAX_PATH + 2, L"%c:%s", cwd[0], wSrcPath);
38+
relSrcPath[MAX_PATH + 1] = L'\0';
3839

3940
BOOLEAN res = CreateSymbolicLinkW(wDstPath, relSrcPath, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
4041
return res != 0;
@@ -45,8 +46,27 @@ int do_linkfile(lua_State* L, const char* src, const char* dst)
4546
return res != 0;
4647
}
4748
#else
48-
int res = symlink(src, dst);
49-
return res == 0;
49+
(void)L;
50+
if (!do_isabsolute(src))
51+
{
52+
char cwd[PATH_MAX];
53+
if (!do_getcwd(cwd, PATH_MAX))
54+
{
55+
return FALSE;
56+
}
57+
58+
char relSrcPath[2 * PATH_MAX + 1];
59+
snprintf(relSrcPath, 2 * PATH_MAX + 1, "%s/%s", cwd, src);
60+
relSrcPath[2 * PATH_MAX] = '\0';
61+
62+
int res = symlink(relSrcPath, dst);
63+
return res == 0;
64+
}
65+
else
66+
{
67+
int res = symlink(src, dst);
68+
return res == 0;
69+
}
5070
#endif
5171
}
5272

src/host/os_rmdir.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* \author Copyright (c) 2002-2013 Jess Perkins and the Premake project
55
*/
66

7+
#include <sys/stat.h>
78
#include <stdlib.h>
89
#include "premake.h"
910

@@ -23,7 +24,15 @@ int os_rmdir(lua_State* L)
2324

2425
z = RemoveDirectoryW(wide_path);
2526
#else
26-
z = (0 == rmdir(path));
27+
struct stat buf;
28+
if (lstat(path, &buf) == 0 && S_ISLNK(buf.st_mode))
29+
{
30+
z = (0 == unlink(path));
31+
}
32+
else
33+
{
34+
z = (0 == rmdir(path));
35+
}
2736
#endif
2837

2938
if (!z)

src/host/premake.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ static const luaL_Reg os_functions[] = {
8787
{ "mkdir", os_mkdir },
8888
#if PLATFORM_WINDOWS
8989
// utf8 functions for Windows (assuming posix already handle utf8)
90-
{"remove", os_remove },
91-
{"rename", os_rename },
90+
{ "remove", os_remove },
91+
{ "rename", os_rename },
9292
#endif
9393
{ "pathsearch", os_pathsearch },
9494
{ "realpath", os_realpath },

tests/base/test_os.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,18 @@
6969
function suite.linkdir()
7070
test.istrue(os.linkdir("folder/subfolder", "folder/subfolder2"))
7171
test.istrue(os.islink("folder/subfolder2"))
72-
os.rmdir("folder/subfolder2")
72+
test.istrue(os.rmdir("folder/subfolder2"))
73+
test.isfalse(os.islink("folder/subfolder2"))
7374
end
7475

7576
function suite.linkfile()
7677
test.istrue(os.linkfile("folder/ok.lua", "folder/ok2.lua"))
7778
test.istrue(os.islink("folder/ok2.lua"))
78-
os.remove("folder/ok2.lua")
79+
test.istrue(os.remove("folder/ok2.lua"))
80+
test.isfalse(os.islink("folder/ok2.lua"))
7981
end
8082

8183

82-
8384
--
8485
-- os.matchdirs() tests
8586
--

0 commit comments

Comments
 (0)