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.
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.
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]
22 # Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25 """This module implements the "zfs userspace" and "zfs groupspace" subcommands.
26 The only public interface is the zfs.userspace.do_userspace() function."""
41 # map from property name prefix -> (field name, isgroup)
43 "userused@": ("used", False),
44 "userquota@": ("quota", False),
45 "groupused@": ("used", True),
46 "groupquota@": ("quota", True),
49 def skiptype(options, prop):
50 """Return True if this property (eg "userquota@") should be skipped."""
51 (field, isgroup) = props[prop]
52 if field not in options.fields:
54 if isgroup and "posixgroup" not in options.types and \
55 "smbgroup" not in options.types:
57 if not isgroup and "posixuser" not in options.types and \
58 "smbuser" not in options.types:
62 def new_entry(options, isgroup, domain, rid):
63 """Return a dict("field": value) for this domain (string) + rid (int)"""
66 idstr = "%s-%u" % (domain, rid)
70 (typename, mapfunc) = {
71 (1, 1): ("SMB Group", lambda id: solaris.misc.sid_to_name(id, 0)),
72 (1, 0): ("POSIX Group", lambda id: grp.getgrgid(int(id)).gr_name),
73 (0, 1): ("SMB User", lambda id: solaris.misc.sid_to_name(id, 1)),
74 (0, 0): ("POSIX User", lambda id: pwd.getpwuid(int(id)).pw_name)
75 }[isgroup, bool(domain)]
77 if typename.lower().replace(" ", "") not in options.types:
83 # python's getpwuid/getgrgid is confused by ephemeral uids
84 if not options.noname and rid < 1<<31:
86 v["name"] = mapfunc(idstr)
93 # it's just a number, so pad it with spaces so
94 # that it will sort numerically
95 v["name.sort"] = "%20d" % rid
96 # fill in default values
103 def process_one_raw(acct, options, prop, elem):
104 """Update the acct dict to incorporate the
105 information from this elem from Dataset.userspace(prop)."""
107 (domain, rid, value) = elem
108 (field, isgroup) = props[prop]
110 if options.translate and domain:
112 rid = solaris.misc.sid_to_id("%s-%u" % (domain, rid),
117 key = (isgroup, domain, rid)
122 v = new_entry(options, isgroup, domain, rid)
127 # Add our value to an existing value, which may be present if
128 # options.translate is set.
129 value = v[field + ".sort"] = value + v[field + ".sort"]
132 v[field] = str(value)
134 v[field] = zfs.util.nicenum(value)
137 """Implements the "zfs userspace" and "zfs groupspace" subcommands."""
143 parser.exit("zfs: error: " + msg)
147 if sys.argv[1] == "userspace":
148 defaulttypes = "posixuser,smbuser"
150 defaulttypes = "posixgroup,smbgroup"
152 fields = ("type", "name", "used", "quota")
153 rjustfields = ("used", "quota")
154 types = ("all", "posixuser", "smbuser", "posixgroup", "smbgroup")
156 u = _("%s [-niHp] [-o field[,...]] [-sS field] ... \n") % sys.argv[1]
157 u += _(" [-t type[,...]] <filesystem|snapshot>")
158 parser = optparse.OptionParser(usage=u, prog="zfs")
160 parser.add_option("-n", action="store_true", dest="noname",
161 help=_("Print numeric ID instead of user/group name"))
162 parser.add_option("-i", action="store_true", dest="translate",
163 help=_("translate SID to posix (possibly ephemeral) ID"))
164 parser.add_option("-H", action="store_true", dest="noheaders",
165 help=_("no headers, tab delimited output"))
166 parser.add_option("-p", action="store_true", dest="parsable",
167 help=_("exact (parsable) numeric output"))
168 parser.add_option("-o", dest="fields", metavar="field[,...]",
169 default="type,name,used,quota",
170 help=_("print only these fields (eg type,name,used,quota)"))
171 parser.add_option("-s", dest="sortfields", metavar="field",
172 type="choice", choices=fields, default=list(),
173 action="callback", callback=zfs.util.append_with_opt,
174 help=_("sort field"))
175 parser.add_option("-S", dest="sortfields", metavar="field",
176 type="choice", choices=fields, #-s sets the default
177 action="callback", callback=zfs.util.append_with_opt,
178 help=_("reverse sort field"))
179 parser.add_option("-t", dest="types", metavar="type[,...]",
180 default=defaulttypes,
181 help=_("print only these types (eg posixuser,smbuser,posixgroup,smbgroup,all)"))
183 (options, args) = parser.parse_args(sys.argv[2:])
185 usage(_("wrong number of arguments"))
188 options.fields = options.fields.split(",")
189 for f in options.fields:
191 usage(_("invalid field %s") % f)
193 options.types = options.types.split(",")
194 for t in options.types:
196 usage(_("invalid type %s") % t)
198 if not options.sortfields:
199 options.sortfields = [("-s", "type"), ("-s", "name")]
201 if "all" in options.types:
202 options.types = types[1:]
204 ds = zfs.dataset.Dataset(dsname, types=("filesystem"))
206 if ds.getprop("jailed") and solaris.misc.isglobalzone():
207 options.noname = True
209 if not ds.getprop("useraccounting"):
210 print(_("Initializing accounting information on old filesystem, please wait..."))
211 ds.userspace_upgrade()
213 # gather and process accounting information
214 # Due to -i, we need to keep a dict, so we can potentially add
215 # together the posix ID and SID's usage. Grr.
217 for prop in props.keys():
218 if skiptype(options, prop):
220 for elem in ds.userspace(prop):
221 process_one_raw(acct, options, prop, elem)
225 for (opt, field) in options.sortfields:
227 n = val[field + ".sort"]
235 # it's a string; decompose it
236 # into an array of integers,
237 # each one the negative of that
239 n = [-ord(c) for c in n]
243 t = zfs.table.Table(options.fields, rjustfields)
244 for val in acct.itervalues():
245 t.addline(cmpkey(val), val)
246 t.printme(not options.noheaders)