summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2015-12-28 18:59:43 +0800
committerruki <[email protected]>2015-12-28 18:59:43 +0800
commitad9b35fd836a38d72c39efc112300ab5d92503ba (patch)
tree0c84be2250e27952fb6821b77fd96c3167a78513 /core/src
parente0dab0305d1967b57b12d2deb94e4253bf7cde06 (diff)
add clean all
Diffstat (limited to 'core/src')
-rwxr-xr-xcore/src/xmake/machine.c2
-rwxr-xr-xcore/src/xmake/makefile1
-rwxr-xr-xcore/src/xmake/os/emptydir.c79
-rwxr-xr-xcore/src/xmake/os/rmdir.c70
4 files changed, 150 insertions, 2 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 5935cab50..ead646ea9 100755
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -66,6 +66,7 @@ tb_int_t xm_os_rename(lua_State* lua);
tb_int_t xm_os_exists(lua_State* lua);
tb_int_t xm_os_setenv(lua_State* lua);
tb_int_t xm_os_getenv(lua_State* lua);
+tb_int_t xm_os_emptydir(lua_State* lua);
tb_int_t xm_os_strerror(lua_State* lua);
// the path functions
@@ -101,6 +102,7 @@ static luaL_Reg const g_os_functions[] =
, { "exists", xm_os_exists }
, { "setenv", xm_os_setenv }
, { "getenv", xm_os_getenv }
+, { "emptydir", xm_os_emptydir }
, { "strerror", xm_os_strerror }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index ca2449d0b..276a1b64b 100755
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -30,6 +30,7 @@ xmake_C_FILES += \
os/exists \
os/setenv \
os/getenv \
+ os/emptydir \
os/strerror \
path/relative \
path/absolute \
diff --git a/core/src/xmake/os/emptydir.c b/core/src/xmake/os/emptydir.c
new file mode 100755
index 000000000..7251006f2
--- /dev/null
+++ b/core/src/xmake/os/emptydir.c
@@ -0,0 +1,79 @@
+/*!The Automatic Cross-platform Build Tool
+ *
+ * XMake is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * XMake is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with XMake;
+ * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
+ *
+ * Copyright (C) 2009 - 2015, ruki All rights reserved.
+ *
+ * @author ruki
+ * @file emptydir.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "emptydir"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * private implementation
+ */
+static tb_bool_t xm_os_emptydir_walk(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv)
+{
+ // check
+ tb_bool_t* is_emptydir = (tb_bool_t*)priv;
+ tb_assert_and_check_return_val(path && info && is_emptydir, tb_false);
+
+ // is emptydir?
+ if (info->type == TB_FILE_TYPE_DIRECTORY || info->type == TB_FILE_TYPE_FILE)
+ {
+ // not emptydir
+ *is_emptydir = tb_false;
+
+ // break
+ return tb_false;
+ }
+
+ // continue
+ return tb_true;
+}
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_os_emptydir(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the directory
+ tb_char_t const* dir = luaL_checkstring(lua, 1);
+ tb_check_return_val(dir, 0);
+
+ // done os.emptydir(dir)
+ tb_bool_t is_emptydir = tb_true;
+ tb_directory_walk(dir, tb_false, tb_true, xm_os_emptydir_walk, &is_emptydir);
+
+ // is emptydir?
+ lua_pushboolean(lua, is_emptydir);
+
+ // ok
+ return 1;
+}
diff --git a/core/src/xmake/os/rmdir.c b/core/src/xmake/os/rmdir.c
index 2daba8207..9128870f2 100755
--- a/core/src/xmake/os/rmdir.c
+++ b/core/src/xmake/os/rmdir.c
@@ -33,6 +33,51 @@
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
+ * private implementation
+ */
+static tb_bool_t xm_os_rmdir_empty(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv)
+{
+ // check
+ tb_bool_t* is_emptydir = (tb_bool_t*)priv;
+ tb_assert_and_check_return_val(path && info && is_emptydir, tb_false);
+
+ // is emptydir?
+ if (info->type == TB_FILE_TYPE_DIRECTORY || info->type == TB_FILE_TYPE_FILE)
+ {
+ // not emptydir
+ *is_emptydir = tb_false;
+
+ // break
+ return tb_false;
+ }
+
+ // continue
+ return tb_true;
+}
+static tb_bool_t xm_os_rmdir_remove(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv)
+{
+ // check
+ tb_assert_and_check_return_val(path, tb_false);
+
+ // is directory?
+ if (info->type == TB_FILE_TYPE_DIRECTORY)
+ {
+ // is emptydir?
+ tb_bool_t is_emptydir = tb_true;
+ tb_directory_walk(path, tb_false, tb_true, xm_os_rmdir_empty, &is_emptydir);
+
+ // trace
+ tb_trace_d("path: %s, emptydir: %u", path, is_emptydir);
+
+ // remove empty directory
+ if (is_emptydir) tb_directory_remove(path);
+ }
+
+ // continue
+ return tb_true;
+}
+
+/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_os_rmdir(lua_State* lua)
@@ -44,8 +89,29 @@ tb_int_t xm_os_rmdir(lua_State* lua)
tb_char_t const* path = luaL_checkstring(lua, 1);
tb_check_return_val(path, 0);
- // done os.rmdir(path)
- lua_pushboolean(lua, tb_directory_remove(path));
+ // only remove empty directory?
+ tb_bool_t rmempty = lua_toboolean(lua, 2);
+ if (rmempty)
+ {
+ // remove all empty directories
+ tb_directory_walk(path, tb_true, tb_false, xm_os_rmdir_remove, tb_null);
+
+ // remove empty root directory
+ tb_bool_t is_emptydir = tb_true;
+ tb_directory_walk(path, tb_false, tb_true, xm_os_rmdir_empty, &is_emptydir);
+ if (is_emptydir) tb_directory_remove(path);
+
+ // trace
+ tb_trace_d("path: %s, emptydir: %u", path, is_emptydir);
+
+ // ok?
+ lua_pushboolean(lua, !tb_file_info(path, tb_null));
+ }
+ else
+ {
+ // done os.rmdir(path)
+ lua_pushboolean(lua, tb_directory_remove(path));
+ }
// ok
return 1;