|
| 1 | +/** |
| 2 | + * \file os_linkdir.c |
| 3 | + * \brief Creates a symbolic link to a directory. |
| 4 | + * \author Copyright (c) 2024 Jess Perkins and the Premake project |
| 5 | + */ |
| 6 | + |
| 7 | +#include <sys/stat.h> |
| 8 | +#include "premake.h" |
| 9 | + |
| 10 | +int do_linkdir(lua_State* L, const char* src, const char* dst) |
| 11 | +{ |
| 12 | +#if PLATFORM_WINDOWS |
| 13 | + // Prepend the drive letter if a relative path is given |
| 14 | + char dstPath[MAX_PATH]; |
| 15 | + char srcPath[MAX_PATH]; |
| 16 | + |
| 17 | + do_normalize(L, srcPath, src); |
| 18 | + do_normalize(L, dstPath, dst); |
| 19 | + do_translate(dstPath, '\\'); |
| 20 | + do_translate(srcPath, '\\'); |
| 21 | + |
| 22 | + // If the source path is relative, prepend the current working directory |
| 23 | + if (!do_isabsolute(src)) |
| 24 | + { |
| 25 | + char cwd[MAX_PATH]; |
| 26 | + do_getcwd(cwd, MAX_PATH); |
| 27 | + do_translate(cwd, '\\'); |
| 28 | + |
| 29 | + char relSrcPath[MAX_PATH]; |
| 30 | + snprintf(relSrcPath, MAX_PATH, "%c:%s", cwd[0], srcPath); |
| 31 | + |
| 32 | + BOOLEAN res = CreateSymbolicLinkA(dstPath, relSrcPath, SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 33 | + return res != 0; |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + BOOLEAN res = CreateSymbolicLinkA(dstPath, srcPath, SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 38 | + return res != 0; |
| 39 | + } |
| 40 | +#else |
| 41 | + int res = symlink(src, dst); |
| 42 | + return res == 0; |
| 43 | +#endif |
| 44 | +} |
| 45 | + |
| 46 | +int os_linkdir(lua_State* L) |
| 47 | +{ |
| 48 | + const char* src = luaL_checkstring(L, 1); |
| 49 | + const char* dst = luaL_checkstring(L, 2); |
| 50 | + |
| 51 | + int result = do_linkdir(L, src, dst); |
| 52 | + if (!result) |
| 53 | + { |
| 54 | + lua_pushnil(L); |
| 55 | + lua_pushfstring(L, "Unable to create link from '%s' to '%s'", src, dst); |
| 56 | + return 2; |
| 57 | + } |
| 58 | + |
| 59 | + lua_pushboolean(L, 1); |
| 60 | + return 1; |
| 61 | +} |
0 commit comments