diff options
Diffstat (limited to 'lpm.c')
-rw-r--r-- | lpm.c | 40 |
1 files changed, 30 insertions, 10 deletions
@@ -59,6 +59,18 @@ static int lpm_hash(lua_State* L) { return 1; } +int lpm_symlink(lua_State* L) { + if (symlink(luaL_checkstring(L, 1), luaL_checkstring(L, 2))) + return luaL_error(L, "can't create symlink %s: %s", luaL_checkstring(L, 2), strerror(errno)); + return 0; +} + +int lpm_chmod(lua_State* L) { + if (chmod(luaL_checkstring(L, 1), luaL_checkinteger(L, 2))) + return luaL_error(L, "can't chmod %s: %s", luaL_checkstring(L, 1), strerror(errno)); + return 0; +} + /** BEGIN STOLEN LITE CODE **/ #if _WIN32 @@ -257,7 +269,7 @@ static int lpm_stat(lua_State *L) { free(wpath); #else struct stat s; - int err = stat(path, &s); + int err = lstat(path, &s); #endif if (err < 0) { lua_pushnil(L); @@ -266,6 +278,21 @@ static int lpm_stat(lua_State *L) { } lua_newtable(L); +#if __linux__ + if (S_ISLNK(s.st_mode)) { + char buffer[PATH_MAX]; + ssize_t len = readlink(path, buffer, sizeof(buffer)); + if (len < 0) + return 0; + lua_pushlstring(L, buffer, len); + } else + lua_pushnil(L); + lua_setfield(L, -2, "symlink"); + if (S_ISLNK(s.st_mode)) + err = stat(path, &s); + if (err) + return 1; +#endif lua_pushinteger(L, s.st_mtime); lua_setfield(L, -2, "modified"); @@ -280,15 +307,6 @@ static int lpm_stat(lua_State *L) { lua_pushnil(L); } lua_setfield(L, -2, "type"); - -#if __linux__ - if (S_ISDIR(s.st_mode)) { - if (lstat(path, &s) == 0) { - lua_pushboolean(L, S_ISLNK(s.st_mode)); - lua_setfield(L, -2, "symlink"); - } - } -#endif return 1; } /** END STOLEN LITE CODE **/ @@ -475,6 +493,8 @@ static const luaL_Reg system_lib[] = { { "mkdir", lpm_mkdir }, // Makes a directory. { "rmdir", lpm_rmdir }, // Removes a directory. { "hash", lpm_hash }, // Returns a hex sha256 hash. + { "symlink", lpm_symlink }, // Creates a symlink. + { "chmod", lpm_chmod }, // Chmod's a file. { "init", lpm_init }, // Initializes a git repository with the specified remote. { "fetch", lpm_fetch }, // Updates a git repository with the specified remote. { "reset", lpm_reset }, // Updates a git repository to the specified commit/hash/branch. |