1219089Spjd#! /usr/bin/python2.6
2219089Spjd#
3219089Spjd# CDDL HEADER START
4219089Spjd#
5219089Spjd# The contents of this file are subject to the terms of the
6219089Spjd# Common Development and Distribution License (the "License").
7219089Spjd# You may not use this file except in compliance with the License.
8219089Spjd#
9219089Spjd# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10219089Spjd# or http://www.opensolaris.org/os/licensing.
11219089Spjd# See the License for the specific language governing permissions
12219089Spjd# and limitations under the License.
13219089Spjd#
14219089Spjd# When distributing Covered Code, include this CDDL HEADER in each
15219089Spjd# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16219089Spjd# If applicable, add the following below this CDDL HEADER, with the
17219089Spjd# fields enclosed by brackets "[]" replaced with your own identifying
18219089Spjd# information: Portions Copyright [yyyy] [name of copyright owner]
19219089Spjd#
20219089Spjd# CDDL HEADER END
21219089Spjd#
22219089Spjd# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23219089Spjd#
24219089Spjd
25219089Spjd"""This module implements the "zfs holds" subcommand.
26219089SpjdThe only public interface is the zfs.holds.do_holds() function."""
27219089Spjd
28219089Spjdimport optparse
29219089Spjdimport sys
30219089Spjdimport errno
31219089Spjdimport time
32219089Spjdimport zfs.util
33219089Spjdimport zfs.dataset
34219089Spjdimport zfs.table
35219089Spjd
36219089Spjd_ = zfs.util._
37219089Spjd
38219089Spjddef do_holds():
39219089Spjd	"""Implements the "zfs holds" subcommand."""
40219089Spjd	def usage(msg=None):
41219089Spjd		parser.print_help()
42219089Spjd		if msg:
43219089Spjd			print
44219089Spjd			parser.exit("zfs: error: " + msg)
45219089Spjd		else:
46219089Spjd			parser.exit()
47219089Spjd
48219089Spjd	u = _("""holds [-r] <snapshot> ...""")
49219089Spjd
50219089Spjd	parser = optparse.OptionParser(usage=u, prog="zfs")
51219089Spjd
52219089Spjd	parser.add_option("-r", action="store_true", dest="recursive",
53219089Spjd	    help=_("list holds recursively"))
54219089Spjd
55219089Spjd	(options, args) = parser.parse_args(sys.argv[2:])
56219089Spjd
57219089Spjd	if len(args) < 1:
58219089Spjd		usage(_("missing snapshot argument"))
59219089Spjd
60219089Spjd	fields = ("name", "tag", "timestamp")
61219089Spjd	rjustfields = ()
62219089Spjd	printing = False
63219089Spjd	gotone = False
64219089Spjd	t = zfs.table.Table(fields, rjustfields)
65219089Spjd	for ds in zfs.dataset.snapshots_fromcmdline(args, options.recursive):
66219089Spjd		gotone = True
67219089Spjd		for tag, tm in ds.get_holds().iteritems():
68219089Spjd			val = {"name": ds.name, "tag": tag,
69219089Spjd			    "timestamp": time.ctime(tm)}
70219089Spjd			t.addline(ds.name, val)
71219089Spjd			printing = True
72219089Spjd	if printing:
73219089Spjd		t.printme()
74219089Spjd	elif not gotone:
75219089Spjd		raise zfs.util.ZFSError(errno.ENOENT, _("no matching datasets"))
76