From e66291f150d1c6d679ce4b28a4ed2ac0173f9d8b Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Thu, 8 Nov 2012 17:44:29 +0200 Subject: [PATCH] BmapBdevCopy: move block device opening Start moving the block device - specific stuff to the BmapBdevCopy class. Move the open funcion first. Change-Id: I06afd6554ec51b11f9c148a3da76dab5a9fd80f0 Signed-off-by: Artem Bityutskiy --- bmaptools/BmapCopy.py | 53 +++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/bmaptools/BmapCopy.py b/bmaptools/BmapCopy.py index 6f13eeb..8fdebc9 100644 --- a/bmaptools/BmapCopy.py +++ b/bmaptools/BmapCopy.py @@ -167,29 +167,13 @@ class BmapCopy: raise Error("cannot open image file '%s': %s" \ % (self._image_path, err)) - def _open_block_device(self): - """ Open the block device in exclusive mode. """ - - try: - self._f_dest = os.open(self._dest_path, os.O_WRONLY | os.O_EXCL) - except OSError as err: - raise Error("cannot open block device '%s' in exclusive mode: %s" \ - % (self._dest_path, err.strerror)) + def _open_destination_file(self): + """ Open the destination file. """ try: - st_mode = os.fstat(self._f_dest).st_mode - except OSError as err: - raise Error("cannot access block device '%s': %s" \ - % (self._dest_path, err.strerror)) - - self.target_is_block_device = stat.S_ISBLK(st_mode) - - # Turn the block device file descriptor into a file object - try: - self._f_dest = os.fdopen(self._f_dest, "wb") - except OSError as err: - os.close(self._f_dest) - raise Error("cannot open block device '%s': %s" \ + self._f_dest = open(self._dest_path, 'w+') + except IOError as err: + raise Error("cannot open destination file '%s': %s" \ % (self._dest_path, err)) def _tune_block_device(self): @@ -268,7 +252,7 @@ class BmapCopy: self.bmap_mapped_percent = None self.target_is_block_device = None - self._open_block_device() + self._open_destination_file() self._open_image_file() if bmap_path: @@ -476,4 +460,27 @@ class BmapBdevCopy(BmapCopy): various optimizations specific to block devices, e.g., switchint to the 'noop' I/O scheduler. """ - pass + def _open_destination_file(self): + """ Open the block device in exclusive mode. """ + + try: + self._f_dest = os.open(self._dest_path, os.O_WRONLY | os.O_EXCL) + except OSError as err: + raise Error("cannot open block device '%s' in exclusive mode: %s" \ + % (self._dest_path, err.strerror)) + + try: + st_mode = os.fstat(self._f_dest).st_mode + except OSError as err: + raise Error("cannot access block device '%s': %s" \ + % (self._dest_path, err.strerror)) + + self.target_is_block_device = stat.S_ISBLK(st_mode) + + # Turn the block device file descriptor into a file object + try: + self._f_dest = os.fdopen(self._f_dest, "wb") + except OSError as err: + os.close(self._f_dest) + raise Error("cannot open block device '%s': %s" \ + % (self._dest_path, err)) -- 2.7.4