From 354f11fc9f417ba777e2e010597e72a013ae5d23 Mon Sep 17 00:00:00 2001 From: David Kupka Date: Fri, 21 Nov 2014 06:30:17 -0500 Subject: [PATCH] ipa-restore: Check if directory is provided + better errors. https://fedorahosted.org/freeipa/ticket/4683 Reviewed-By: Tomas Babej --- ipaserver/install/ipa_restore.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ipaserver/install/ipa_restore.py b/ipaserver/install/ipa_restore.py index 93f176d302a49319940555a0be3037620143e1f3..f290bae4dc6455bb22c4e726e72efe98205d970e 100644 --- a/ipaserver/install/ipa_restore.py +++ b/ipaserver/install/ipa_restore.py @@ -152,6 +152,9 @@ class Restore(admintool.AdminTool): else: self.backup_dir = dirname + if not os.path.isdir(dirname): + raise self.option_parser.error("must provide path to backup directory") + if options.gpg_keyring: if (not os.path.exists(options.gpg_keyring + '.pub') or not os.path.exists(options.gpg_keyring + '.sec')): @@ -213,7 +216,10 @@ class Restore(admintool.AdminTool): try: dirsrv = services.knownservices.dirsrv - self.read_header() + try: + self.read_header() + except IOError as e: + raise admintool.ScriptError('Cannot read backup metadata: %s' % e) # These two checks would normally be in the validate method but # we need to know the type of backup we're dealing with. if (self.backup_type != 'FULL' and not options.data_only and @@ -546,9 +552,9 @@ class Restore(admintool.AdminTool): Read the backup file header that contains the meta data about this particular backup. ''' - fd = open(self.header) - config = SafeConfigParser() - config.readfp(fd) + with open(self.header) as fd: + config = SafeConfigParser() + config.readfp(fd) self.backup_type = config.get('ipa', 'type') self.backup_time = config.get('ipa', 'time') -- 2.1.0