xref: /illumos-gate/usr/src/lib/pyzfs/common/holds.py (revision 3b862e9a9ce59d5dbf0177b9eb293109fde6bf36)
1#! /usr/bin/python2.4
2#
3# CDDL HEADER START
4#
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.
8#
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.
13#
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]
19#
20# CDDL HEADER END
21#
22# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23# Use is subject to license terms.
24#
25
26"""This module implements the "zfs holds" subcommand.
27The only public interface is the zfs.holds.do_holds() function."""
28
29import optparse
30import sys
31import errno
32import time
33import zfs.util
34import zfs.dataset
35import zfs.table
36
37_ = zfs.util._
38
39def do_holds():
40	"""Implements the "zfs holds" subcommand."""
41	def usage(msg=None):
42		parser.print_help()
43		if msg:
44			print
45			parser.exit("zfs: error: " + msg)
46		else:
47			parser.exit()
48
49	u = _("""holds [-r] <snapshot> ...""")
50
51	parser = optparse.OptionParser(usage=u, prog="zfs")
52
53	parser.add_option("-r", action="store_true", dest="recursive",
54	    help=_("list holds recursively"))
55
56	(options, args) = parser.parse_args(sys.argv[2:])
57
58	if len(args) < 1:
59		usage(_("missing snapshot argument"))
60
61	fields = ("name", "tag", "timestamp")
62	rjustfields = ()
63	printing = False
64	gotone = False
65	t = zfs.table.Table(fields, rjustfields)
66	for ds in zfs.dataset.snapshots_fromcmdline(args, options.recursive):
67		gotone = True
68		for tag, tm in ds.get_holds().iteritems():
69			val = {"name": ds.name, "tag": tag,
70			    "timestamp": time.ctime(tm)}
71			t.addline(ds.name, val)
72			printing = True
73	if printing:
74		t.printme()
75	elif not gotone:
76		raise zfs.util.ZFSError(errno.ENOENT, _("no matching datasets"))
77