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 <afs/param.h>
22 #include <afs/errmap_nt.h>
27 #define PARTITION_NAME_PREFIX "/vicep"
31 * vpt_PartitionNameValid() -- Check partition name syntax.
33 * RETURN CODES: 1 valid, 0 not valid
36 vpt_PartitionNameValid(const char *vpname)
40 /* Only allowing full name, not any of the standard shorthands.
42 * NOTE: yet another incarnation of parsing code; should be consolidated.
45 PARTITION_NAME_PREFIX, sizeof(PARTITION_NAME_PREFIX) - 1)) {
46 /* prefix is correct; check suffix */
47 const char *vpsuffix = vpname + (sizeof(PARTITION_NAME_PREFIX) - 1);
48 size_t vpsuffixLen = strlen(vpsuffix);
50 if (vpsuffixLen == 1) {
51 /* must be 'a' - 'z' (0 .. 25) */
52 if (islower(vpsuffix[0])) {
55 } else if (vpsuffixLen == 2) {
56 /* must be 'aa' - 'zz' and in range (26 .. 255) */
57 if (islower(vpsuffix[0]) && islower(vpsuffix[1])) {
58 int vpsuffixVal = ((vpsuffix[0] - 'a') * 26 +
59 (vpsuffix[1] - 'a') + 26);
60 if (vpsuffixVal <= 255) {
71 * vpt_DeviceNameValid() -- Check device name syntax.
73 * RETURN CODES: 1 valid, 0 not valid
76 vpt_DeviceNameValid(const char *devname)
78 /* assuming C locale */
79 if (isupper(devname[0]) && devname[1] == ':' && devname[2] == '\0') {
88 * vpt_Start() -- Initialize iteration for reading vice partition table.
90 * RETURN CODES: 0 success, -1 failed (errno set)
94 vpt_Start(struct vpt_iter *iterp) /* [out] iteration handle to initialize */
99 memset(iterp, 0, sizeof(*iterp));
101 /* open canonical Afstab key */
102 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
103 KEY_READ, 0, &key, NULL);
105 if (status == ERROR_SUCCESS) {
106 /* enumerate subkeys, each of which represents a vice partition */
107 status = RegEnumKeyAlt(key, &iterp->vpenum);
108 (void)RegCloseKey(key);
112 errno = nterr_nt2unix(status, EIO);
120 * vpt_NextEntry() -- Return next vice partition table entry.
122 * RETURN CODES: 0 success, -1 no more entries (ENOENT) or failed (errno set).
126 vpt_NextEntry(struct vpt_iter *iterp, /* [in] valid iteration handle */
127 struct vptab *vptabp) /* [out] next partiton table entry */
133 if (iterp->vpenum == NULL) {
134 /* no partition entries (or invalid iteration handle) */
139 /* find name of next partition entry to examine in multistring enum */
140 if (iterp->last == NULL) {
142 nextNamep = iterp->last = iterp->vpenum;
144 /* subsequent call */
145 if (*iterp->last != '\0') {
146 /* not at end of multistring; advance to next name */
147 iterp->last += strlen(iterp->last) + 1;
149 nextNamep = iterp->last;
152 if (*nextNamep == '\0') {
153 /* end of multistring; no more entries */
158 if (strlen(nextNamep) >= VPTABSIZE_NAME) {
159 /* invalid partition name entry */
163 strcpy(vptabp->vp_name, nextNamep);
165 /* open canonical Afstab key */
166 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
167 KEY_READ, 0, &tabKey, NULL);
169 if (status == ERROR_SUCCESS) {
170 /* open key representing vice partition */
171 status = RegOpenKeyAlt(tabKey, nextNamep,
172 KEY_READ, 0, &vpKey, NULL);
174 if (status == ERROR_SUCCESS) {
175 /* read partition attributes */
176 DWORD dataType, dataSize = VPTABSIZE_DEV;
178 status = RegQueryValueEx(vpKey,
179 AFSREG_SVR_SVC_AFSTAB_DEVNAME_VALUE,
180 NULL, &dataType, vptabp->vp_dev,
183 if (status == ERROR_SUCCESS && dataType != REG_SZ) {
184 /* invalid device name type */
185 status = ERROR_INVALID_PARAMETER;
187 (void)RegCloseKey(vpKey);
189 (void)RegCloseKey(tabKey);
193 errno = nterr_nt2unix(status, EIO);
201 * vpt_Finish() -- Terminate iteration for vice partition table.
203 * RETURN CODES: 0 success, -1 failed (errno set)
206 vpt_Finish(struct vpt_iter *iterp) /* [in] iteration handle to terminate */
211 memset(iterp, 0, sizeof(*iterp));
217 * vpt_AddEntry() -- Add or update vice partition table entry.
219 * RETURN CODES: 0 success, -1 failed (errno set)
222 vpt_AddEntry(const struct vptab *vptabp)
226 const char *vpName, *vpDev;
228 vpName = vptabp->vp_name;
229 vpDev = vptabp->vp_dev;
231 if (!vpt_PartitionNameValid(vpName) || !vpt_DeviceNameValid(vpDev)) {
236 /* open canonical Afstab key; create if doesn't exist */
237 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
238 KEY_WRITE, 1 /* create */, &tabKey, NULL);
240 if (status == ERROR_SUCCESS) {
241 /* open key representing vice partition; create if doesn't exist */
242 status = RegOpenKeyAlt(tabKey, vpName,
243 KEY_WRITE, 1 /* create */, &vpKey, NULL);
245 if (status == ERROR_SUCCESS) {
246 /* write partition attributes */
247 status = RegSetValueEx(vpKey, AFSREG_SVR_SVC_AFSTAB_DEVNAME_VALUE,
248 0, REG_SZ, vpDev, (DWORD)strlen(vpDev) + 1);
257 errno = nterr_nt2unix(status, EIO);
265 * vpt_RemoveEntry() -- Remove vice partition table entry.
267 * RETURN CODES: 0 success, -1 failed (errno set)
270 vpt_RemoveEntry(const char *vpname)
275 if (!vpt_PartitionNameValid(vpname)) {
280 /* open canonical Afstab key */
281 status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY,
282 KEY_WRITE, 0, &tabKey, NULL);
284 if (status == ERROR_SUCCESS) {
285 /* delete key representing vice partition */
286 status = RegDeleteKey(tabKey, vpname);
290 errno = nterr_nt2unix(status, EIO);