xref: /titanic_44/usr/src/lib/pyzfs/common/holds.py (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
1*6d52f363SLori Alt#! /usr/bin/python2.6
2842727c2SChris Kirby#
3842727c2SChris Kirby# CDDL HEADER START
4842727c2SChris Kirby#
5842727c2SChris Kirby# The contents of this file are subject to the terms of the
6842727c2SChris Kirby# Common Development and Distribution License (the "License").
7842727c2SChris Kirby# You may not use this file except in compliance with the License.
8842727c2SChris Kirby#
9842727c2SChris Kirby# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10842727c2SChris Kirby# or http://www.opensolaris.org/os/licensing.
11842727c2SChris Kirby# See the License for the specific language governing permissions
12842727c2SChris Kirby# and limitations under the License.
13842727c2SChris Kirby#
14842727c2SChris Kirby# When distributing Covered Code, include this CDDL HEADER in each
15842727c2SChris Kirby# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16842727c2SChris Kirby# If applicable, add the following below this CDDL HEADER, with the
17842727c2SChris Kirby# fields enclosed by brackets "[]" replaced with your own identifying
18842727c2SChris Kirby# information: Portions Copyright [yyyy] [name of copyright owner]
19842727c2SChris Kirby#
20842727c2SChris Kirby# CDDL HEADER END
21842727c2SChris Kirby#
22*6d52f363SLori Alt# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23842727c2SChris Kirby#
24842727c2SChris Kirby
25842727c2SChris Kirby"""This module implements the "zfs holds" subcommand.
26842727c2SChris KirbyThe only public interface is the zfs.holds.do_holds() function."""
27842727c2SChris Kirby
28842727c2SChris Kirbyimport optparse
29842727c2SChris Kirbyimport sys
30842727c2SChris Kirbyimport errno
31842727c2SChris Kirbyimport time
32842727c2SChris Kirbyimport zfs.util
33842727c2SChris Kirbyimport zfs.dataset
34842727c2SChris Kirbyimport zfs.table
35842727c2SChris Kirby
36842727c2SChris Kirby_ = zfs.util._
37842727c2SChris Kirby
38842727c2SChris Kirbydef do_holds():
39842727c2SChris Kirby	"""Implements the "zfs holds" subcommand."""
40842727c2SChris Kirby	def usage(msg=None):
41842727c2SChris Kirby		parser.print_help()
42842727c2SChris Kirby		if msg:
43842727c2SChris Kirby			print
44842727c2SChris Kirby			parser.exit("zfs: error: " + msg)
45842727c2SChris Kirby		else:
46842727c2SChris Kirby			parser.exit()
47842727c2SChris Kirby
48842727c2SChris Kirby	u = _("""holds [-r] <snapshot> ...""")
49842727c2SChris Kirby
50842727c2SChris Kirby	parser = optparse.OptionParser(usage=u, prog="zfs")
51842727c2SChris Kirby
52842727c2SChris Kirby	parser.add_option("-r", action="store_true", dest="recursive",
53842727c2SChris Kirby	    help=_("list holds recursively"))
54842727c2SChris Kirby
55842727c2SChris Kirby	(options, args) = parser.parse_args(sys.argv[2:])
56842727c2SChris Kirby
57842727c2SChris Kirby	if len(args) < 1:
58842727c2SChris Kirby		usage(_("missing snapshot argument"))
59842727c2SChris Kirby
60842727c2SChris Kirby	fields = ("name", "tag", "timestamp")
61842727c2SChris Kirby	rjustfields = ()
62842727c2SChris Kirby	printing = False
63d7747cbcSChris Kirby	gotone = False
64842727c2SChris Kirby	t = zfs.table.Table(fields, rjustfields)
65842727c2SChris Kirby	for ds in zfs.dataset.snapshots_fromcmdline(args, options.recursive):
66d7747cbcSChris Kirby		gotone = True
67842727c2SChris Kirby		for tag, tm in ds.get_holds().iteritems():
68842727c2SChris Kirby			val = {"name": ds.name, "tag": tag,
69842727c2SChris Kirby			    "timestamp": time.ctime(tm)}
70842727c2SChris Kirby			t.addline(ds.name, val)
71842727c2SChris Kirby			printing = True
72842727c2SChris Kirby	if printing:
73842727c2SChris Kirby		t.printme()
74d7747cbcSChris Kirby	elif not gotone:
75d7747cbcSChris Kirby		raise zfs.util.ZFSError(errno.ENOENT, _("no matching datasets"))
76