2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Functions and definitions for accessing the vice partition table */
12 #include <afsconfig.h>
13 #include <afs/param.h>
20 #include <afs/errmap_nt.h>
25 #define PARTITION_NAME_PREFIX "/vicep"
29 * vpt_PartitionNameValid() -- Check partition name syntax.
31 * RETURN CODES: 1 valid, 0 not valid
34 vpt_PartitionNameValid(const char *vpname)
38 /* Only allowing full name, not any of the standard shorthands.
40 * NOTE: yet another incarnation of parsing code; should be consolidated.
43 PARTITION_NAME_PREFIX, sizeof(PARTITION_NAME_PREFIX) - 1)) {
44 /* prefix is correct; check suffix */
45 const char *vpsuffix = vpname + (sizeof(PARTITION_NAME_PREFIX) - 1);
46 size_t vpsuffixLen = strlen(vpsuffix);
48 if (vpsuffixLen == 1) {
49 /* must be 'a' - 'z' (0 .. 25) */
50 if (islower(vpsuffix[0])) {
53 } else if (vpsuffixLen == 2) {
54 /* must be 'aa' - 'zz' and in range (26 .. 255) */
55 if (islower(vpsuffix[0]) && islower(vpsuffix[1])) {
56 int vpsuffixVal = ((vpsuffix[0] - 'a') * 26 +
57 (vpsuffix[1] - 'a') + 26);
58 if (vpsuffixVal <= 255) {
69 * vpt_DeviceNameValid() -- Check device name syntax.
71 * RETURN CODES: 1 valid, 0 not valid
74 vpt_DeviceNameValid(const char *devname)
76 /* assuming C locale */
77 if (isupper(devname[0]) && devname[1] == ':' && devname[2] == '\0') {
86 * vpt_Start() -- Initialize iteration for reading vice partition table.
88 * RETURN CODES: 0 success, -1 failed (errno set)
92 vpt_Start(struct vpt_iter *iterp) /* [out] iteration handle to initialize */
97 memset(iterp, 0, sizeof(*iterp));
99 /* open canonical Afstab key */
100 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
101 KEY_READ, 0, &key, NULL);
103 if (status == ERROR_SUCCESS) {
104 /* enumerate subkeys, each of which represents a vice partition */
105 status = RegEnumKeyAlt(key, &iterp->vpenum);
106 (void)RegCloseKey(key);
110 errno = nterr_nt2unix(status, EIO);
118 * vpt_NextEntry() -- Return next vice partition table entry.
120 * RETURN CODES: 0 success, -1 no more entries (ENOENT) or failed (errno set).
124 vpt_NextEntry(struct vpt_iter *iterp, /* [in] valid iteration handle */
125 struct vptab *vptabp) /* [out] next partiton table entry */
131 if (iterp->vpenum == NULL) {
132 /* no partition entries (or invalid iteration handle) */
137 /* find name of next partition entry to examine in multistring enum */
138 if (iterp->last == NULL) {
140 nextNamep = iterp->last = iterp->vpenum;
142 /* subsequent call */
143 if (*iterp->last != '\0') {
144 /* not at end of multistring; advance to next name */
145 iterp->last += strlen(iterp->last) + 1;
147 nextNamep = iterp->last;
150 if (*nextNamep == '\0') {
151 /* end of multistring; no more entries */
156 if (strlen(nextNamep) >= VPTABSIZE_NAME) {
157 /* invalid partition name entry */
161 strcpy(vptabp->vp_name, nextNamep);
163 /* open canonical Afstab key */
164 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
165 KEY_READ, 0, &tabKey, NULL);
167 if (status == ERROR_SUCCESS) {
168 /* open key representing vice partition */
169 status = RegOpenKeyAlt(tabKey, nextNamep,
170 KEY_READ, 0, &vpKey, NULL);
172 if (status == ERROR_SUCCESS) {
173 /* read partition attributes */
174 DWORD dataType, dataSize = VPTABSIZE_DEV;
176 status = RegQueryValueEx(vpKey,
177 AFSREG_SVR_SVC_AFSTAB_DEVNAME_VALUE,
178 NULL, &dataType, vptabp->vp_dev,
181 if (status == ERROR_SUCCESS && dataType != REG_SZ) {
182 /* invalid device name type */
183 status = ERROR_INVALID_PARAMETER;
185 (void)RegCloseKey(vpKey);
187 (void)RegCloseKey(tabKey);
191 errno = nterr_nt2unix(status, EIO);
199 * vpt_Finish() -- Terminate iteration for vice partition table.
201 * RETURN CODES: 0 success, -1 failed (errno set)
204 vpt_Finish(struct vpt_iter *iterp) /* [in] iteration handle to terminate */
209 memset(iterp, 0, sizeof(*iterp));
215 * vpt_AddEntry() -- Add or update vice partition table entry.
217 * RETURN CODES: 0 success, -1 failed (errno set)
220 vpt_AddEntry(const struct vptab *vptabp)
224 const char *vpName, *vpDev;
226 vpName = vptabp->vp_name;
227 vpDev = vptabp->vp_dev;
229 if (!vpt_PartitionNameValid(vpName) || !vpt_DeviceNameValid(vpDev)) {
234 /* open canonical Afstab key; create if doesn't exist */
235 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
236 KEY_WRITE, 1 /* create */, &tabKey, NULL);
238 if (status == ERROR_SUCCESS) {
239 /* open key representing vice partition; create if doesn't exist */
240 status = RegOpenKeyAlt(tabKey, vpName,
241 KEY_WRITE, 1 /* create */, &vpKey, NULL);
243 if (status == ERROR_SUCCESS) {
244 /* write partition attributes */
245 status = RegSetValueEx(vpKey, AFSREG_SVR_SVC_AFSTAB_DEVNAME_VALUE,
246 0, REG_SZ, vpDev, (DWORD)strlen(vpDev) + 1);
255 errno = nterr_nt2unix(status, EIO);
263 * vpt_RemoveEntry() -- Remove vice partition table entry.
265 * RETURN CODES: 0 success, -1 failed (errno set)
268 vpt_RemoveEntry(const char *vpname)
273 if (!vpt_PartitionNameValid(vpname)) {
278 /* open canonical Afstab key */
279 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
280 KEY_WRITE, 0, &tabKey, NULL);
282 if (status == ERROR_SUCCESS) {
283 /* delete key representing vice partition */
284 status = RegDeleteKey(tabKey, vpname);
288 errno = nterr_nt2unix(status, EIO);