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