backup_utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 qubes.qubes import QubesException
  30. from thread_monitor import *
  31. from datetime import datetime
  32. from string import replace
  33. path_re = re.compile(r"[a-zA-Z0-9/:.,_+=() -]*")
  34. path_max_len = 512
  35. def fill_appvms_list(dialog):
  36. dialog.appvm_combobox.clear()
  37. dialog.appvm_combobox.addItem("dom0")
  38. dialog.appvm_combobox.setCurrentIndex(0) #current selected is null ""
  39. for vm in dialog.qvm_collection.values():
  40. if vm.is_appvm() and vm.internal:
  41. continue
  42. if vm.is_template() and vm.installed_by_rpm:
  43. continue
  44. if vm.is_running() and vm.qid != 0:
  45. dialog.appvm_combobox.addItem(vm.name)
  46. def enable_dir_line_edit(dialog, boolean):
  47. dialog.dir_line_edit.setEnabled(boolean)
  48. dialog.select_path_button.setEnabled(boolean)
  49. def get_path_for_vm(vm, service_name):
  50. if not vm:
  51. return None
  52. proc = vm.run("QUBESRPC %s dom0" % service_name, passio_popen=True)
  53. proc.stdin.close()
  54. untrusted_path = proc.stdout.readline(path_max_len)
  55. if len(untrusted_path) == 0:
  56. return None
  57. if path_re.match(untrusted_path):
  58. assert '../' not in untrusted_path
  59. assert '\0' not in untrusted_path
  60. return untrusted_path.strip()
  61. else:
  62. return None
  63. def select_path_button_clicked(dialog, select_file = False):
  64. backup_location = str(dialog.dir_line_edit.text())
  65. file_dialog = QFileDialog()
  66. file_dialog.setReadOnly(True)
  67. if select_file:
  68. file_dialog_function = file_dialog.getOpenFileName
  69. else:
  70. file_dialog_function = file_dialog.getExistingDirectory
  71. new_appvm = None
  72. new_path = None
  73. if dialog.appvm_combobox.currentIndex() != 0: #An existing appvm chosen
  74. new_appvm = str(dialog.appvm_combobox.currentText())
  75. vm = dialog.qvm_collection.get_vm_by_name(new_appvm)
  76. if vm:
  77. new_path = get_path_for_vm(vm, "qubes.SelectFile" if select_file
  78. else "qubes.SelectDirectory")
  79. else:
  80. new_path = file_dialog_function(dialog, "Select backup location.",
  81. backup_location if backup_location
  82. else '/')
  83. if new_path != None:
  84. new_path = unicode(new_path)
  85. if os.path.basename(new_path) == 'qubes.xml':
  86. backup_location = os.path.dirname(new_path)
  87. else:
  88. backup_location = new_path
  89. dialog.dir_line_edit.setText(backup_location)
  90. if (new_path or new_appvm) and len(backup_location) > 0:
  91. dialog.select_dir_page.emit(SIGNAL("completeChanged()"))
  92. def simulate_long_lasting_proces(period, progress_callback):
  93. for i in range(period):
  94. progress_callback((i*100)/period)
  95. time.sleep(1)
  96. progress_callback(100)
  97. return 0