From fc4f4bd09a0ef3a65514c5dc733c7b2fd6ac7ead Mon Sep 17 00:00:00 2001 From: Michael Still Date: Wed, 21 Nov 2012 21:43:12 +1100 Subject: Truncate large console logs in libvirt. Resolves bug 1081436 where they were returning a 5.5gb console log to the user. Change-Id: I0d98c83e801f8936d35789b5e8f8283f49483c4e --- nova/utils.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 464629d95..c94cd2c3e 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -1180,3 +1180,24 @@ def mkfs(fs, path, label=None): args.extend([label_opt, label]) args.append(path) execute(*args) + + +def last_bytes(file_like_object, num): + """Return num bytes from the end of the file, and remaining byte count. + + :param file_like_object: The file to read + :param num: The number of bytes to return + + :returns (data, remaining) + """ + + try: + file_like_object.seek(-num, os.SEEK_END) + except IOError, e: + if e.errno == 22: + file_like_object.seek(0, os.SEEK_SET) + else: + raise + + remaining = file_like_object.tell() + return (file_like_object.read(), remaining) -- cgit