return params
def get_partition_id(switch, params):
- part = params[0]
- if part.startswith("/vice"):
- part = part[5:]
- elif part.startswith("vice"):
- part = part[4:]
-
- if part.isnumeric():
- part = int(part)
- elif len(part) == 1 and part >= "a" and part <= "z":
- part = bytes(part)[0] - 97
- elif len(part) == 2 and part[0] >= "a" and part[0] <= "z" and part[1] >= "a" and part[1] <= "z":
- part = (bytes(part)[0] - 97) * 26 | bytes(part)[1] - 97
- else:
- raise RuntimeError("Unparseable partition ID '" + params[0] + "'")
-
- if part > 255:
- raise RuntimeError("Partition ID '" + params[0] + "' out of range")
- return part
+ from afs.lib.partition import part2id
+ return part2id(params[0])
def get_auth(switch, params):
return params
--- /dev/null
+#
+# AFS Volume management toolkit: Partition name handling
+# -*- coding: utf-8 -*-
+#
+
+__copyright__ = """
+Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
+Written by David Howells (dhowells@redhat.com)
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public Licence version 2 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public Licence for more details.
+
+You should have received a copy of the GNU General Public Licence
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+def part2id(name):
+ """Convert a partition name or number string into a numeric ID"""
+ if name.isnumeric():
+ n = int(name)
+ else:
+ if name.startswith("/vicep"):
+ name = name[6:]
+ elif name.startswith("vicep"):
+ name = name[5:]
+
+ if len(name) == 1 and name >= "a" and name <= "z":
+ n = int(name, 36) - 10
+ elif (len(name) == 2 and
+ name[0] >= "a" and name[0] <= "z" and
+ name[1] >= "a" and name[1] <= "z"):
+ n = (int(name[0], 36) - 10) * 26 + (int(name[1], 36) - 10)
+ n += 26
+ else:
+ raise RuntimeError("Unparseable partition ID '" + params[0] + "'")
+
+ if n < 0 or n > 255:
+ raise RuntimeError("Partition ID '" + params[0] + "' out of range")
+ return n
+
+def id2part(n):
+ """Convert a numeric ID into a partition name string"""
+ if n < 26:
+ return "/vicep{:c}".format(n + 97)
+ else:
+ n -= 26
+ return "/vicep{:c}{:c}".format(n / 26 + 97, n % 26 + 97)