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