xref: /netbsd-src/external/cddl/osnet/dist/lib/pyzfs/common/holds.py (revision 3227e6cf668bd374971740bd6660f43cee4417ac)
1*3227e6cfSchs#! /usr/bin/python2.6
2*3227e6cfSchs#
3*3227e6cfSchs# CDDL HEADER START
4*3227e6cfSchs#
5*3227e6cfSchs# The contents of this file are subject to the terms of the
6*3227e6cfSchs# Common Development and Distribution License (the "License").
7*3227e6cfSchs# You may not use this file except in compliance with the License.
8*3227e6cfSchs#
9*3227e6cfSchs# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*3227e6cfSchs# or http://www.opensolaris.org/os/licensing.
11*3227e6cfSchs# See the License for the specific language governing permissions
12*3227e6cfSchs# and limitations under the License.
13*3227e6cfSchs#
14*3227e6cfSchs# When distributing Covered Code, include this CDDL HEADER in each
15*3227e6cfSchs# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*3227e6cfSchs# If applicable, add the following below this CDDL HEADER, with the
17*3227e6cfSchs# fields enclosed by brackets "[]" replaced with your own identifying
18*3227e6cfSchs# information: Portions Copyright [yyyy] [name of copyright owner]
19*3227e6cfSchs#
20*3227e6cfSchs# CDDL HEADER END
21*3227e6cfSchs#
22*3227e6cfSchs# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23*3227e6cfSchs#
24*3227e6cfSchs
25*3227e6cfSchs"""This module implements the "zfs holds" subcommand.
26*3227e6cfSchsThe only public interface is the zfs.holds.do_holds() function."""
27*3227e6cfSchs
28*3227e6cfSchsimport optparse
29*3227e6cfSchsimport sys
30*3227e6cfSchsimport errno
31*3227e6cfSchsimport time
32*3227e6cfSchsimport zfs.util
33*3227e6cfSchsimport zfs.dataset
34*3227e6cfSchsimport zfs.table
35*3227e6cfSchs
36*3227e6cfSchs_ = zfs.util._
37*3227e6cfSchs
38*3227e6cfSchsdef do_holds():
39*3227e6cfSchs	"""Implements the "zfs holds" subcommand."""
40*3227e6cfSchs	def usage(msg=None):
41*3227e6cfSchs		parser.print_help()
42*3227e6cfSchs		if msg:
43*3227e6cfSchs			print
44*3227e6cfSchs			parser.exit("zfs: error: " + msg)
45*3227e6cfSchs		else:
46*3227e6cfSchs			parser.exit()
47*3227e6cfSchs
48*3227e6cfSchs	u = _("""holds [-r] <snapshot> ...""")
49*3227e6cfSchs
50*3227e6cfSchs	parser = optparse.OptionParser(usage=u, prog="zfs")
51*3227e6cfSchs
52*3227e6cfSchs	parser.add_option("-r", action="store_true", dest="recursive",
53*3227e6cfSchs	    help=_("list holds recursively"))
54*3227e6cfSchs
55*3227e6cfSchs	(options, args) = parser.parse_args(sys.argv[2:])
56*3227e6cfSchs
57*3227e6cfSchs	if len(args) < 1:
58*3227e6cfSchs		usage(_("missing snapshot argument"))
59*3227e6cfSchs
60*3227e6cfSchs	fields = ("name", "tag", "timestamp")
61*3227e6cfSchs	rjustfields = ()
62*3227e6cfSchs	printing = False
63*3227e6cfSchs	gotone = False
64*3227e6cfSchs	t = zfs.table.Table(fields, rjustfields)
65*3227e6cfSchs	for ds in zfs.dataset.snapshots_fromcmdline(args, options.recursive):
66*3227e6cfSchs		gotone = True
67*3227e6cfSchs		for tag, tm in ds.get_holds().iteritems():
68*3227e6cfSchs			val = {"name": ds.name, "tag": tag,
69*3227e6cfSchs			    "timestamp": time.ctime(tm)}
70*3227e6cfSchs			t.addline(ds.name, val)
71*3227e6cfSchs			printing = True
72*3227e6cfSchs	if printing:
73*3227e6cfSchs		t.printme()
74*3227e6cfSchs	elif not gotone:
75*3227e6cfSchs		raise zfs.util.ZFSError(errno.ENOENT, _("no matching datasets"))
76