Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / butc / list.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
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
8 */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <rx/xdr.h>
16 #include <rx/rx.h>
17 #include <lock.h>
18 #include <lwp.h>
19 #include <afs/tcdata.h>
20
21 #include "error_macros.h"
22
23 /*extern int debugLevel;*/
24 static struct dumpNode *dumpQHeader; /* ptr to head of the dumpNode list */
25 static struct dumpNode headNode; /* the dummy header of the node list */
26 static afs_int32 maxTaskID; /* the largest task Id allotted so far, this is never reused */
27
28 /* allocTaskId
29 * allocate a dump (task) id
30 */
31 afs_int32
32 allocTaskId(void)
33 {
34 return (maxTaskID++);
35 }
36
37
38 /* initialize the node list used to keep track of the active dumps */
39 void
40 InitNodeList(afs_int32 portOffset)
41 {
42 maxTaskID = (portOffset * 1000) + 1; /* this is the first task id alotted */
43 headNode.taskID = -1;
44 headNode.next = NULL;
45 headNode.dumps = (struct tc_dumpDesc *)0;
46 headNode.restores = (struct tc_restoreDesc *)0;
47 dumpQHeader = &headNode; /* noone in the list to start with */
48 }
49
50 /* CreateNode
51 * Create a <newNode> for the dump, put it in the list of active dumps
52 * and return a pointer to it
53 * entry:
54 * newNode - for return ptr
55 * exit:
56 * newNode ptr set to point to a node.
57 */
58
59 void
60 CreateNode(struct dumpNode **newNode)
61 {
62 /* get space */
63 *newNode = calloc(1, sizeof(struct dumpNode));
64
65 (*newNode)->next = dumpQHeader->next;
66 dumpQHeader->next = *newNode;
67 (*newNode)->taskID = allocTaskId();
68
69 /* if (debugLevel) DisplayNode(*newNode); */
70 }
71
72 /* free the space allotted to the node with <taskID> */
73 void
74 FreeNode(afs_int32 taskID)
75 {
76 struct dumpNode *oldPtr, *newPtr, *curPtr;
77 int done;
78
79 curPtr = dumpQHeader;
80 oldPtr = dumpQHeader;
81 if (curPtr)
82 newPtr = dumpQHeader->next;
83 else
84 newPtr = NULL;
85 done = 0;
86 while ((!done) && (curPtr != NULL)) {
87 if (curPtr->taskID == taskID) {
88 done = 1;
89 oldPtr->next = newPtr;
90
91 /* free the node and its structures */
92 if (curPtr->dumpName)
93 free(curPtr->dumpName);
94 if (curPtr->volumeSetName)
95 free(curPtr->volumeSetName);
96 if (curPtr->restores)
97 free(curPtr->restores);
98 if (curPtr->dumps)
99 free(curPtr->dumps);
100 free(curPtr);
101 } else {
102 oldPtr = curPtr;
103 curPtr = newPtr;
104 if (newPtr)
105 newPtr = newPtr->next;
106
107 }
108 }
109 return;
110
111 }
112
113 afs_int32
114 GetNthNode(afs_int32 aindex, afs_int32 *aresult)
115 {
116 struct dumpNode *tn;
117 int i;
118
119 tn = dumpQHeader->next;
120 for (i = 0;; i++) {
121 if (!tn)
122 return ENOENT;
123 /* see if this is the desired node ID */
124 if (i == aindex) {
125 *aresult = tn->taskID;
126 return 0;
127 }
128 /* otherwise, skip to next one and keep looking */
129 tn = tn->next;
130 }
131 }
132
133 /* return the node with <taskID> into <resultNode> */
134 afs_int32
135 GetNode(afs_int32 taskID, struct dumpNode **resultNode)
136 {
137 struct dumpNode *tmpPtr;
138 int done;
139
140 done = 0;
141 tmpPtr = dumpQHeader;
142 while ((!done) && (tmpPtr != NULL)) {
143 if (tmpPtr->taskID == taskID) {
144 *resultNode = tmpPtr;
145 done = 1;
146 } else
147 tmpPtr = tmpPtr->next;
148 }
149 if (done)
150 return 0;
151 else
152 return TC_NODENOTFOUND;
153 }