Util.vala 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. namespace Usm.Util {
  2. public static void delete_tree(string path) throws Error {
  3. // Creating a new Directory object for the given folder path
  4. var folder = File.new_for_path(path);
  5. // Getting a list of all files and folders inside the directory
  6. var enumerator = folder.enumerate_children("*", FileQueryInfoFlags.NONE);
  7. // Looping through each file/folder and removing them
  8. while (true) {
  9. FileInfo? info = enumerator.next_file();
  10. if (info == null) {
  11. break;
  12. }
  13. // Checking if the current item is a file or a folder
  14. if (info.get_file_type() == FileType.REGULAR) {
  15. // Removing the file
  16. var file = folder.get_child(info.get_name());
  17. file.delete();
  18. }
  19. else if (info.get_file_type() == FileType.DIRECTORY) {
  20. // Removing the folder recursively
  21. var subfolder = folder.get_child(info.get_name());
  22. delete_tree(subfolder.get_path());
  23. }
  24. }
  25. folder.delete();
  26. }
  27. public static void unarchive(string archive, string destination) throws Error {
  28. var dest = File.new_for_path(destination);
  29. if(!dest.query_exists()) {
  30. dest.make_directory();
  31. }
  32. var proc = new Subprocess.newv(new string[] { "tar", "-xf", archive, "-C", destination }, SubprocessFlags.INHERIT_FDS);
  33. if(!proc.wait_check()) {
  34. throw new IOError.FAILED(@"Could not extract \"$archive\": tar returned non-zero exit status $(proc.get_status())");
  35. }
  36. }
  37. public static void archive(string source, string archive) throws Error {
  38. var proc = new Subprocess.newv(new string[] { "tar", "-cJf", archive, "--directory", source, "." }, SubprocessFlags.INHERIT_FDS);
  39. if(!proc.wait_check()) {
  40. throw new IOError.FAILED(@"Could not archive \"$source\": tar returned non-zero exit status $(proc.get_status())");
  41. }
  42. }
  43. }