Path: blob/main/cddl/contrib/opensolaris/lib/pyzfs/common/util.py
39563 views
#! /usr/bin/python2.61#2# CDDL HEADER START3#4# The contents of this file are subject to the terms of the5# Common Development and Distribution License (the "License").6# You may not use this file except in compliance with the License.7#8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9# or http://www.opensolaris.org/os/licensing.10# See the License for the specific language governing permissions11# and limitations under the License.12#13# When distributing Covered Code, include this CDDL HEADER in each14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.15# If applicable, add the following below this CDDL HEADER, with the16# fields enclosed by brackets "[]" replaced with your own identifying17# information: Portions Copyright [yyyy] [name of copyright owner]18#19# CDDL HEADER END20#21# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.22#2324"""This module provides utility functions for ZFS.25zfs.util.dev -- a file object of /dev/zfs """2627import gettext28import errno29import os30import solaris.misc31# Note: this module (zfs.util) should not import zfs.ioctl, because that32# would introduce a circular dependency3334errno.ECANCELED = 4735errno.ENOTSUP = 483637dev = open("/dev/zfs", "w")3839try:40_ = gettext.translation("SUNW_OST_OSLIB", "/usr/lib/locale",41fallback=True).gettext42except:43_ = solaris.misc.gettext4445def default_repr(self):46"""A simple __repr__ function."""47if self.__slots__:48str = "<" + self.__class__.__name__49for v in self.__slots__:50str += " %s: %r" % (v, getattr(self, v))51return str + ">"52else:53return "<%s %s>" % \54(self.__class__.__name__, repr(self.__dict__))5556class ZFSError(StandardError):57"""This exception class represents a potentially user-visible58ZFS error. If uncaught, it will be printed and the process will59exit with exit code 1.6061errno -- the error number (eg, from ioctl(2))."""6263__slots__ = "why", "task", "errno"64__repr__ = default_repr6566def __init__(self, eno, task=None, why=None):67"""Create a ZFS exception.68eno -- the error number (errno)69task -- a string describing the task that failed70why -- a string describing why it failed (defaults to71strerror(eno))"""7273self.errno = eno74self.task = task75self.why = why7677def __str__(self):78s = ""79if self.task:80s += self.task + ": "81if self.why:82s += self.why83else:84s += self.strerror85return s8687__strs = {88errno.EPERM: _("permission denied"),89errno.ECANCELED:90_("delegated administration is disabled on pool"),91errno.EINTR: _("signal received"),92errno.EIO: _("I/O error"),93errno.ENOENT: _("dataset does not exist"),94errno.ENOSPC: _("out of space"),95errno.EEXIST: _("dataset already exists"),96errno.EBUSY: _("dataset is busy"),97errno.EROFS:98_("snapshot permissions cannot be modified"),99errno.ENAMETOOLONG: _("dataset name is too long"),100errno.ENOTSUP: _("unsupported version"),101errno.EAGAIN: _("pool I/O is currently suspended"),102}103104__strs[errno.EACCES] = __strs[errno.EPERM]105__strs[errno.ENXIO] = __strs[errno.EIO]106__strs[errno.ENODEV] = __strs[errno.EIO]107__strs[errno.EDQUOT] = __strs[errno.ENOSPC]108109@property110def strerror(self):111return ZFSError.__strs.get(self.errno, os.strerror(self.errno))112113def nicenum(num):114"""Return a nice string (eg "1.23M") for this integer."""115index = 0;116n = num;117118while n >= 1024:119n /= 1024120index += 1121122u = " KMGTPE"[index]123if index == 0:124return "%u" % n;125elif n >= 100 or num & ((1024*index)-1) == 0:126# it's an exact multiple of its index, or it wouldn't127# fit as floating point, so print as an integer128return "%u%c" % (n, u)129else:130# due to rounding, it's tricky to tell what precision to131# use; try each precision and see which one fits132for i in (2, 1, 0):133s = "%.*f%c" % (i, float(num) / (1<<(10*index)), u)134if len(s) <= 5:135return s136137def append_with_opt(option, opt, value, parser):138"""A function for OptionParser which appends a tuple (opt, value)."""139getattr(parser.values, option.dest).append((opt, value))140141142143