5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
22 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 # Use is subject to license terms.
26 """This module provides utility functions for ZFS.
27 zfs.util.dev -- a file object of /dev/zfs """
32 # Note: this module (zfs.util) should not import zfs.ioctl, because that
33 # would introduce a circular dependency
38 dev = open("/dev/zfs", "w")
40 _ = gettext.translation("SUNW_OST_OSLIB", "/usr/lib/locale",
41 fallback=True).gettext
43 def default_repr(self):
44 """A simple __repr__ function."""
46 str = "<" + self.__class__.__name__
47 for v in self.__slots__:
48 str += " %s: %r" % (v, getattr(self, v))
52 (self.__class__.__name__, repr(self.__dict__))
54 class ZFSError(StandardError):
55 """This exception class represents a potentially user-visible
56 ZFS error. If uncaught, it will be printed and the process will
57 exit with exit code 1.
59 errno -- the error number (eg, from ioctl(2))."""
61 __slots__ = "why", "task", "errno"
62 __repr__ = default_repr
64 def __init__(self, eno, task=None, why=None):
65 """Create a ZFS exception.
66 eno -- the error number (errno)
67 task -- a string describing the task that failed
68 why -- a string describing why it failed (defaults to
86 errno.EPERM: _("permission denied"),
88 _("delegated administration is disabled on pool"),
89 errno.EINTR: _("signal received"),
90 errno.EIO: _("I/O error"),
91 errno.ENOENT: _("dataset does not exist"),
92 errno.ENOSPC: _("out of space"),
93 errno.EEXIST: _("dataset already exists"),
94 errno.EBUSY: _("dataset is busy"),
96 _("snapshot permissions cannot be modified"),
97 errno.ENAMETOOLONG: _("dataset name is too long"),
98 errno.ENOTSUP: _("unsupported version"),
99 errno.EAGAIN: _("pool I/O is currently suspended"),
102 __strs[errno.EACCES] = __strs[errno.EPERM]
103 __strs[errno.ENXIO] = __strs[errno.EIO]
104 __strs[errno.ENODEV] = __strs[errno.EIO]
105 __strs[errno.EDQUOT] = __strs[errno.ENOSPC]
109 return ZFSError.__strs.get(self.errno, os.strerror(self.errno))
112 """Return a nice string (eg "1.23M") for this integer."""
123 elif n >= 100 or num & ((1024*index)-1) == 0:
124 # it's an exact multiple of its index, or it wouldn't
125 # fit as floating point, so print as an integer
126 return "%u%c" % (n, u)
128 # due to rounding, it's tricky to tell what precision to
129 # use; try each precision and see which one fits
131 s = "%.*f%c" % (i, float(num) / (1<<(10*index)), u)
135 def append_with_opt(option, opt, value, parser):
136 """A function for OptionParser which appends a tuple (opt, value)."""
137 getattr(parser.values, option.dest).append((opt, value))