Skip to content

Commit 29cc563

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

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
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_remove.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
22
* \file os_remove.c
3-
* \brief Remove a file on Windows.
3+
* \brief Remove a file or symlink to a file.
44
* \author Copyright (c) 2002-2013 Jess Perkins and the Premake project
55
*/
66

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

910
#if PLATFORM_WINDOWS
@@ -48,4 +49,45 @@ int os_remove(lua_State* L)
4849
}
4950
}
5051

52+
#else
53+
54+
int os_remove(lua_State* L)
55+
{
56+
const char* filename = luaL_checkstring(L, 1);
57+
58+
struct stat buf;
59+
if (lstat(filename, &buf) == 0 && S_ISLNK(buf.st_mode))
60+
{
61+
int res = unlink(filename);
62+
if (res == 0)
63+
{
64+
lua_pushboolean(L, 1);
65+
return 1;
66+
}
67+
else
68+
{
69+
lua_pushnil(L);
70+
lua_pushfstring(L, "Unable to unlink file '%s'", filename);
71+
lua_pushinteger(L, res);
72+
return 3;
73+
}
74+
}
75+
else
76+
{
77+
int res = remove(filename);
78+
if (res == 0)
79+
{
80+
lua_pushboolean(L, 1);
81+
return 1;
82+
}
83+
else
84+
{
85+
lua_pushnil(L);
86+
lua_pushfstring(L, "Unable to remove file '%s'", filename);
87+
lua_pushinteger(L, res);
88+
return 3;
89+
}
90+
}
91+
}
92+
5193
#endif

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
@@ -85,10 +85,10 @@ static const luaL_Reg os_functions[] = {
8585
{ "matchnext", os_matchnext },
8686
{ "matchstart", os_matchstart },
8787
{ "mkdir", os_mkdir },
88+
{ "remove", os_remove },
8889
#if PLATFORM_WINDOWS
8990
// utf8 functions for Windows (assuming posix already handle utf8)
90-
{"remove", os_remove },
91-
{"rename", os_rename },
91+
{ "rename", os_rename },
9292
#endif
9393
{ "pathsearch", os_pathsearch },
9494
{ "realpath", os_realpath },

src/host/premake.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ int os_matchstart(lua_State* L);
156156
int os_mkdir(lua_State* L);
157157
int os_pathsearch(lua_State* L);
158158
int os_realpath(lua_State* L);
159+
int os_remove(lua_State* L);
159160
#if PLATFORM_WINDOWS
160161
// utf8 versions
161-
int os_remove(lua_State* L);
162162
int os_rename(lua_State* L);
163163
#endif
164164
int os_rmdir(lua_State* L);

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)