Vala: rm_rf example added.

This commit is contained in:
Kolan Sh 2016-02-06 01:34:05 +03:00
parent 959791dcec
commit 4970944181
4 changed files with 78 additions and 0 deletions

27
vala/rm_rf/rm_rf.vala Normal file
View File

@ -0,0 +1,27 @@
void rm_rf (File directory) throws Error {
var children = directory.enumerate_children ("standard::*",
FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
FileInfo fileinfo = null;
while ((fileinfo = children.next_file (null)) != null ) {
File child = directory.resolve_relative_path (fileinfo.get_name ());
if (fileinfo.get_file_type () == FileType.DIRECTORY) {
rm_rf (child);
} else {
print (@"rm $(child.get_path())\n");
child.delete();
}
}
print (@"rmdir $(directory.get_path())\n");
directory.delete();
}
int main () {
try {
rm_rf (File.new_for_path("/tmp/aaa"));
} catch (Error e) {
stderr.puts ("Error: $(e.message)\n");
return -1;
}
return 0;
}

View File

@ -0,0 +1,43 @@
async void rm_rf_async (File directory, Cancellable? cancellable) throws Error {
if (cancellable.is_cancelled()) return;
var children = yield directory.enumerate_children_async (FileAttribute.STANDARD_NAME,
FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
Priority.DEFAULT, cancellable);
GLib.List<FileInfo> fileinfos;
while ((fileinfos = yield children.next_files_async (8, Priority.DEFAULT,
cancellable)) != null) {
foreach (var child_finfo in fileinfos) {
var child = directory.get_child (child_finfo.get_name());
var ftype = child.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
if (ftype == FileType.DIRECTORY) {
yield rm_rf_async (child, cancellable);
} else {
print (@"rm $(child.get_path())\n");
child.delete (cancellable);
}
}
}
print (@"rmdir $(directory.get_path())\n");
directory.delete (cancellable);
}
int main () {
//string temp_dir = DirUtils.make_tmp ("my-XXXXXX-dir");
//stdout.printf ("make_tmp (\"my-XXXXXXX-dir\") = %s\n", temp_dir);
// async example
var loop = new MainLoop();
rm_rf_async.begin(File.new_for_path("/tmp/aaa"), null,
(obj, res) => {
try {
rm_rf_async.end(res);
loop.quit();
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
//return -1;
}
});
loop.run();
return 0;
}

4
vala/rm_rf/run.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
mkdir -p /tmp/aaa/bbb && touch /tmp/aaa/ccc.txt /tmp/aaa/bbb/ddd.txt
valac --pkg=gio-2.0 --Xcc='-w' rm_rf.vala && ./rm_rf && ls -l /tmp

4
vala/rm_rf/run_async.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
mkdir -p /tmp/aaa/bbb && touch /tmp/aaa/ccc.txt /tmp/aaa/bbb/ddd.txt
valac --pkg=gio-2.0 --Xcc='-w' rm_rf_async.vala && ./rm_rf_async && ls -l /tmp