2N/A#! /usr/bin/python2.6
2N/A#
2N/A# CDDL HEADER START
2N/A#
2N/A# The contents of this file are subject to the terms of the
2N/A# Common Development and Distribution License (the "License").
2N/A# You may not use this file except in compliance with the License.
2N/A#
2N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A# or http://www.opensolaris.org/os/licensing.
2N/A# See the License for the specific language governing permissions
2N/A# and limitations under the License.
2N/A#
2N/A# When distributing Covered Code, include this CDDL HEADER in each
2N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A# If applicable, add the following below this CDDL HEADER, with the
2N/A# fields enclosed by brackets "[]" replaced with your own identifying
2N/A# information: Portions Copyright [yyyy] [name of copyright owner]
2N/A#
2N/A# CDDL HEADER END
2N/A#
2N/A# Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A#
2N/A
2N/A"""This module provides utility functions for ZFS.
2N/Azfs.util.dev -- a file object of /dev/zfs """
2N/A
2N/Aimport gettext
2N/Aimport errno
2N/Aimport os
2N/Aimport solaris.misc
2N/Aimport sys
2N/A# Note: this module (zfs.util) should not import zfs.ioctl, because that
2N/A# would introduce a circular dependency
2N/A
2N/Aerrno.ECANCELED = 47
2N/Aerrno.ENOTSUP = 48
2N/A
2N/Adev = open("/dev/zfs", "w")
2N/A
2N/Atry:
2N/A _ = gettext.translation("SUNW_OST_OSLIB", "/usr/lib/locale",
2N/A fallback=True).gettext
2N/Aexcept:
2N/A _ = solaris.misc.gettext
2N/A
2N/Adef default_repr(self):
2N/A """A simple __repr__ function."""
2N/A if self.__slots__:
2N/A str = "<" + self.__class__.__name__
2N/A for v in self.__slots__:
2N/A str += " %s: %r" % (v, getattr(self, v))
2N/A return str + ">"
2N/A else:
2N/A return "<%s %s>" % \
2N/A (self.__class__.__name__, repr(self.__dict__))
2N/A
2N/Aclass ZFSError(StandardError):
2N/A """This exception class represents a potentially user-visible
2N/A ZFS error. If uncaught, it will be printed and the process will
2N/A exit with exit code 1.
2N/A
2N/A errno -- the error number (eg, from ioctl(2))."""
2N/A
2N/A __slots__ = "why", "task", "errno"
2N/A __repr__ = default_repr
2N/A
2N/A def __init__(self, eno, task=None, why=None):
2N/A """Create a ZFS exception.
2N/A eno -- the error number (errno)
2N/A task -- a string describing the task that failed
2N/A why -- a string describing why it failed (defaults to
2N/A strerror(eno))"""
2N/A
2N/A self.errno = eno
2N/A self.task = task
2N/A self.why = why
2N/A
2N/A def __str__(self):
2N/A s = ""
2N/A if self.task:
2N/A s += self.task + ": "
2N/A if self.why:
2N/A s += self.why
2N/A else:
2N/A s += self.strerror
2N/A return s
2N/A
2N/A __strs = {
2N/A errno.EPERM: _("permission denied"),
2N/A errno.ECANCELED:
2N/A _("delegated administration is disabled on pool"),
2N/A errno.EINTR: _("signal received"),
2N/A errno.EIO: _("I/O error"),
2N/A errno.ENOENT: _("dataset does not exist"),
2N/A errno.ENOSPC: _("out of space"),
2N/A errno.EEXIST: _("dataset already exists"),
2N/A errno.EBUSY: _("dataset is busy"),
2N/A errno.EROFS:
2N/A _("snapshot permissions cannot be modified"),
2N/A errno.ENAMETOOLONG: _("dataset name is too long"),
2N/A errno.ENOTSUP: _("unsupported version"),
2N/A errno.EAGAIN: _("pool I/O is currently suspended"),
2N/A }
2N/A
2N/A __strs[errno.EACCES] = __strs[errno.EPERM]
2N/A __strs[errno.ENXIO] = __strs[errno.EIO]
2N/A __strs[errno.ENODEV] = __strs[errno.EIO]
2N/A __strs[errno.EDQUOT] = __strs[errno.ENOSPC]
2N/A
2N/A @property
2N/A def strerror(self):
2N/A if self.errno == 0:
2N/A return ""
2N/A return ZFSError.__strs.get(self.errno, os.strerror(self.errno))
2N/A
2N/Adef nicenum(num):
2N/A """Return a nice string (eg "1.23M") for this integer."""
2N/A index = 0;
2N/A n = num;
2N/A
2N/A while n >= 1024:
2N/A n /= 1024
2N/A index += 1
2N/A
2N/A u = " KMGTPE"[index]
2N/A if index == 0:
2N/A return "%u" % n;
2N/A elif n >= 100 or num & ((1024*index)-1) == 0:
2N/A # it's an exact multiple of its index, or it wouldn't
2N/A # fit as floating point, so print as an integer
2N/A return "%u%c" % (n, u)
2N/A else:
2N/A # due to rounding, it's tricky to tell what precision to
2N/A # use; try each precision and see which one fits
2N/A for i in (2, 1, 0):
2N/A s = "%.*f%c" % (i, float(num) / (1<<(10*index)), u)
2N/A if len(s) <= 5:
2N/A return s
2N/A
2N/Adef append_with_opt(option, opt, value, parser):
2N/A """A function for OptionParser which appends a tuple (opt, value)."""
2N/A getattr(parser.values, option.dest).append((opt, value))
2N/A
2N/Aimport optparse
2N/A
2N/Aclass ZFSOptionParser(optparse.OptionParser):
2N/A """This option parser class raises errors as exception strings
2N/A rather than exiting immediately."""
2N/A
2N/A def error(self, msg=None):
2N/A if msg:
2N/A raise ZFSError(0, msg)
2N/A raise ZFSError(0, "")
2N/A
2N/A def exit(self, msg=None):
2N/A if msg:
2N/A raise ZFSError(0, msg)
2N/A sys.exit(0)