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