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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25 """This module provides utility functions for ZFS.
26 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")
41 _
= gettext
.translation("SUNW_OST_OSLIB", "/usr/lib/locale",
42 fallback
=True).gettext
44 _
= solaris
.misc
.gettext
46 def default_repr(self
):
47 """A simple __repr__ function."""
49 str = "<" + self
.__class
__.__name
__
50 for v
in self
.__slots
__:
51 str += " %s: %r" % (v
, getattr(self
, v
))
55 (self
.__class
__.__name
__, repr(self
.__dict
__))
57 class ZFSError(StandardError):
58 """This exception class represents a potentially user-visible
59 ZFS error. If uncaught, it will be printed and the process will
60 exit with exit code 1.
62 errno -- the error number (eg, from ioctl(2))."""
64 __slots__
= "why", "task", "errno"
65 __repr__
= default_repr
67 def __init__(self
, eno
, task
=None, why
=None):
68 """Create a ZFS exception.
69 eno -- the error number (errno)
70 task -- a string describing the task that failed
71 why -- a string describing why it failed (defaults to
89 errno
.EPERM
: _("permission denied"),
91 _("delegated administration is disabled on pool"),
92 errno
.EINTR
: _("signal received"),
93 errno
.EIO
: _("I/O error"),
94 errno
.ENOENT
: _("dataset does not exist"),
95 errno
.ENOSPC
: _("out of space"),
96 errno
.EEXIST
: _("dataset already exists"),
97 errno
.EBUSY
: _("dataset is busy"),
99 _("snapshot permissions cannot be modified"),
100 errno
.ENAMETOOLONG
: _("dataset name is too long"),
101 errno
.ENOTSUP
: _("unsupported version"),
102 errno
.EAGAIN
: _("pool I/O is currently suspended"),
105 __strs
[errno
.EACCES
] = __strs
[errno
.EPERM
]
106 __strs
[errno
.ENXIO
] = __strs
[errno
.EIO
]
107 __strs
[errno
.ENODEV
] = __strs
[errno
.EIO
]
108 __strs
[errno
.EDQUOT
] = __strs
[errno
.ENOSPC
]
112 return ZFSError
.__strs
.get(self
.errno
, os
.strerror(self
.errno
))
115 """Return a nice string (eg "1.23M") for this integer."""
126 elif n
>= 100 or num
& ((1024*index
)-1) == 0:
127 # it's an exact multiple of its index, or it wouldn't
128 # fit as floating point, so print as an integer
129 return "%u%c" % (n
, u
)
131 # due to rounding, it's tricky to tell what precision to
132 # use; try each precision and see which one fits
134 s
= "%.*f%c" % (i
, float(num
) / (1<<(10*index
)), u
)
138 def append_with_opt(option
, opt
, value
, parser
):
139 """A function for OptionParser which appends a tuple (opt, value)."""
140 getattr(parser
.values
, option
.dest
).append((opt
, value
))