backup_utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. import re
  23. import sys
  24. import os
  25. from PyQt4.QtCore import *
  26. from PyQt4.QtGui import *
  27. import subprocess
  28. import time
  29. from .thread_monitor import *
  30. path_re = re.compile(r"[a-zA-Z0-9/:.,_+=() -]*")
  31. path_max_len = 512
  32. def fill_appvms_list(dialog):
  33. dialog.appvm_combobox.clear()
  34. dialog.appvm_combobox.addItem("dom0")
  35. dialog.appvm_combobox.setCurrentIndex(0) #current selected is null ""
  36. for vm in dialog.qvm_collection.values():
  37. if vm.is_appvm() and vm.internal:
  38. continue
  39. if vm.is_template() and vm.installed_by_rpm:
  40. continue
  41. if vm.is_running() and vm.qid != 0:
  42. dialog.appvm_combobox.addItem(vm.name)
  43. def enable_dir_line_edit(dialog, boolean):
  44. dialog.dir_line_edit.setEnabled(boolean)
  45. dialog.select_path_button.setEnabled(boolean)
  46. def get_path_for_vm(vm, service_name):
  47. if not vm:
  48. return None
  49. proc = vm.run("QUBESRPC %s dom0" % service_name, passio_popen=True)
  50. proc.stdin.close()
  51. untrusted_path = proc.stdout.readline(path_max_len)
  52. if len(untrusted_path) == 0:
  53. return None
  54. if path_re.match(untrusted_path):
  55. assert '../' not in untrusted_path
  56. assert '\0' not in untrusted_path
  57. return untrusted_path.strip()
  58. else:
  59. return None
  60. def select_path_button_clicked(dialog, select_file = False):
  61. backup_location = str(dialog.dir_line_edit.text())
  62. file_dialog = QFileDialog()
  63. file_dialog.setReadOnly(True)
  64. if select_file:
  65. file_dialog_function = file_dialog.getOpenFileName
  66. else:
  67. file_dialog_function = file_dialog.getExistingDirectory
  68. new_appvm = None
  69. new_path = None
  70. if dialog.appvm_combobox.currentIndex() != 0: #An existing appvm chosen
  71. new_appvm = str(dialog.appvm_combobox.currentText())
  72. vm = dialog.qvm_collection.get_vm_by_name(new_appvm)
  73. if vm:
  74. new_path = get_path_for_vm(vm, "qubes.SelectFile" if select_file
  75. else "qubes.SelectDirectory")
  76. else:
  77. new_path = file_dialog_function(dialog,
  78. dialog.tr("Select backup location."),
  79. backup_location if backup_location else '/')
  80. if new_path != None:
  81. if os.path.basename(new_path) == 'qubes.xml':
  82. backup_location = os.path.dirname(new_path)
  83. else:
  84. backup_location = new_path
  85. dialog.dir_line_edit.setText(backup_location)
  86. if (new_path or new_appvm) and len(backup_location) > 0:
  87. dialog.select_dir_page.emit(SIGNAL("completeChanged()"))
  88. def simulate_long_lasting_proces(period, progress_callback):
  89. for i in range(period):
  90. progress_callback((i*100)/period)
  91. time.sleep(1)
  92. progress_callback(100)
  93. return 0