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