~ccheney/ubuntu/lucid/eucalyptus/lucid-sru

« back to all changes in this revision

Viewing changes to util/data.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2009-02-11 02:45:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090211024539-0jhzbpg3hk6nu1yg
Tags: upstream-1.5~bzr139
ImportĀ upstreamĀ versionĀ 1.5~bzr139

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h> 
 
4
#include "data.h"
 
5
 
 
6
/* metadata is present in every type of nc request */
 
7
ncMetadata * allocate_metadata (char *correlationId, char *userId)
 
8
{
 
9
    ncMetadata * meta;
 
10
 
 
11
    if (!(meta = malloc(sizeof(ncMetadata)))) return NULL;
 
12
    if (correlationId) meta->correlationId = strdup(correlationId);
 
13
    if (userId)        meta->userId        = strdup(userId);
 
14
 
 
15
    return meta;
 
16
}
 
17
 
 
18
void free_metadata (ncMetadata ** metap)
 
19
{
 
20
    ncMetadata * meta = * metap;
 
21
    if (meta->correlationId) free(meta->correlationId);
 
22
    if (meta->userId)        free(meta->userId);
 
23
    * metap = NULL;
 
24
}
 
25
 
 
26
/* instances are present in instance-related requests */
 
27
ncInstance * allocate_instance (char *instanceId, char *reservationId, 
 
28
                                ncInstParams *params, 
 
29
                                char *imageId, char *imageURL, 
 
30
                                char *kernelId, char *kernelURL, 
 
31
                                char *ramdiskId, char *ramdiskURL,
 
32
                                char *stateName, int stateCode, char *userId, 
 
33
                                ncNetConf *ncnet, char *keyName,
 
34
                                char *userData, char *launchIndex, char **groupNames, int groupNamesSize)
 
35
{
 
36
    ncInstance * inst;
 
37
 
 
38
    /* zeroed out for cleaner-looking checkpoints and
 
39
     * strings that are empty unless set */
 
40
    if (!(inst = calloc(1, sizeof(ncInstance)))) return NULL; 
 
41
 
 
42
    if (userData) {
 
43
        strncpy(inst->userData, userData, CHAR_BUFFER_SIZE*10);
 
44
    }
 
45
 
 
46
    if (launchIndex) {
 
47
        strncpy(inst->launchIndex, launchIndex, CHAR_BUFFER_SIZE);
 
48
    }
 
49
 
 
50
    inst->groupNamesSize = groupNamesSize;
 
51
    if (groupNames && groupNamesSize) {
 
52
        int i;
 
53
        for (i=0; groupNames[i] && i<groupNamesSize; i++) {
 
54
            strncpy(inst->groupNames[i], groupNames[i], CHAR_BUFFER_SIZE);
 
55
        }
 
56
    }
 
57
    inst->volumesSize = 0;
 
58
    
 
59
    if (ncnet != NULL) {
 
60
      memcpy(&(inst->ncnet), ncnet, sizeof(ncNetConf));
 
61
    }
 
62
 
 
63
    if (instanceId) {
 
64
      strncpy(inst->instanceId, instanceId, CHAR_BUFFER_SIZE);
 
65
    }
 
66
 
 
67
    if (keyName) {
 
68
      strncpy(inst->keyName, keyName, CHAR_BUFFER_SIZE*4);
 
69
    }
 
70
 
 
71
    if (reservationId) {
 
72
      strncpy(inst->reservationId, reservationId, CHAR_BUFFER_SIZE);
 
73
    }
 
74
 
 
75
    if (imageId) {
 
76
      strncpy(inst->imageId, imageId, CHAR_BUFFER_SIZE);
 
77
    }
 
78
    if (imageURL) {
 
79
      strncpy(inst->imageURL, imageURL, CHAR_BUFFER_SIZE);
 
80
    }
 
81
    if (kernelId) {
 
82
      strncpy(inst->kernelId, kernelId, CHAR_BUFFER_SIZE);
 
83
    }
 
84
    if (kernelURL) {
 
85
      strncpy(inst->kernelURL, kernelURL, CHAR_BUFFER_SIZE);
 
86
    }
 
87
    if (ramdiskId) {
 
88
      strncpy(inst->ramdiskId, ramdiskId, CHAR_BUFFER_SIZE);
 
89
    }
 
90
    if (ramdiskURL) {
 
91
      strncpy(inst->ramdiskURL, ramdiskURL, CHAR_BUFFER_SIZE);
 
92
    }
 
93
 
 
94
    if (stateName) {
 
95
      strncpy(inst->stateName, stateName, CHAR_BUFFER_SIZE);
 
96
    }
 
97
    if (userId) {
 
98
      strncpy(inst->userId, userId, CHAR_BUFFER_SIZE);
 
99
    }
 
100
    if (params) {
 
101
        inst->params.memorySize = params->memorySize;
 
102
        inst->params.diskSize = params->diskSize;
 
103
        inst->params.numberOfCores = params->numberOfCores;
 
104
    }
 
105
    inst->stateCode = stateCode;
 
106
    
 
107
    return inst;
 
108
}
 
109
 
 
110
void free_instance (ncInstance ** instp) 
 
111
{
 
112
    ncInstance * inst;
 
113
 
 
114
    if (!instp) return;
 
115
    inst = * instp;
 
116
    * instp = NULL;
 
117
    if (!inst) return;
 
118
 
 
119
    free (inst);
 
120
}
 
121
 
 
122
/* resource is used to return information about resources */
 
123
ncResource * allocate_resource (char *nodeStatus,
 
124
                                int memorySizeMax, int memorySizeAvailable, 
 
125
                                int diskSizeMax, int diskSizeAvailable,
 
126
                                int numberOfCoresMax, int numberOfCoresAvailable,
 
127
                                char *publicSubnets)
 
128
{
 
129
    ncResource * res;
 
130
    
 
131
    if (!(res = malloc(sizeof(ncResource)))) return NULL;
 
132
    if (nodeStatus) {
 
133
      strncpy(res->nodeStatus, nodeStatus, CHAR_BUFFER_SIZE);
 
134
    }
 
135
    if (publicSubnets) {
 
136
      strncpy(res->publicSubnets, publicSubnets, CHAR_BUFFER_SIZE);
 
137
    }
 
138
    res->memorySizeMax = memorySizeMax;
 
139
    res->memorySizeAvailable = memorySizeAvailable;
 
140
    res->diskSizeMax = diskSizeMax;
 
141
    res->diskSizeAvailable = diskSizeAvailable;
 
142
    res->numberOfCoresMax = numberOfCoresMax;
 
143
    res->numberOfCoresAvailable = numberOfCoresAvailable;
 
144
    
 
145
    if (!res->nodeStatus ) free_resource(&res);
 
146
    return res;
 
147
}
 
148
 
 
149
void free_resource (ncResource ** resp)
 
150
{
 
151
    ncResource * res;
 
152
 
 
153
    if (!resp) return;
 
154
    res = * resp;
 
155
    * resp = NULL;
 
156
    if (!res) return;
 
157
 
 
158
    free (res);
 
159
}
 
160
 
 
161
instance_error_codes add_instance (bunchOfInstances ** headp, ncInstance * instance)
 
162
{
 
163
    bunchOfInstances * new = malloc (sizeof(bunchOfInstances));
 
164
 
 
165
    if ( new == NULL ) return OUT_OF_MEMORY;
 
166
    new->instance = instance;
 
167
    new->next = NULL;
 
168
 
 
169
    if ( * headp == NULL) {
 
170
        * headp = new;
 
171
        (* headp)->count = 1;
 
172
 
 
173
    } else {
 
174
        bunchOfInstances * last = * headp;
 
175
        do {
 
176
            if ( !strcmp(last->instance->instanceId, instance->instanceId) ) {
 
177
                free (new);
 
178
                return DUPLICATE;
 
179
            }
 
180
        } while ( last->next && (last = last->next) );
 
181
        last->next = new;
 
182
        (* headp)->count++;
 
183
    }        
 
184
 
 
185
    return OK;
 
186
}
 
187
 
 
188
instance_error_codes remove_instance (bunchOfInstances ** headp, ncInstance * instance)
 
189
{
 
190
    bunchOfInstances * head, * prev = NULL;
 
191
 
 
192
    for ( head = * headp; head; ) {
 
193
        int count = (* headp)->count;
 
194
 
 
195
        if ( !strcmp(head->instance->instanceId, instance->instanceId) ) {
 
196
            if ( prev ) {
 
197
                prev->next = head->next;
 
198
            } else {
 
199
                * headp = head->next;
 
200
            }
 
201
            if (* headp) {
 
202
                (* headp)->count = count - 1;
 
203
            }
 
204
            free (head);
 
205
            return OK;
 
206
        }
 
207
        prev = head;
 
208
        head = head->next;
 
209
    }
 
210
    return NOT_FOUND;
 
211
}
 
212
 
 
213
instance_error_codes for_each_instance (bunchOfInstances ** headp, void (* function)(bunchOfInstances **, ncInstance *, void *), void *param) 
 
214
{
 
215
    bunchOfInstances * head;
 
216
    
 
217
    for ( head = * headp; head; head = head->next ) {
 
218
        function ( headp, head->instance, param );
 
219
    }
 
220
    
 
221
    return OK;
 
222
}
 
223
 
 
224
ncInstance * find_instance (bunchOfInstances **headp, char * instanceId)
 
225
{
 
226
    bunchOfInstances * head;
 
227
    
 
228
    for ( head = * headp; head; head = head->next ) {
 
229
        if (!strcmp(head->instance->instanceId, instanceId)) {
 
230
            return head->instance;
 
231
        }
 
232
    }
 
233
    
 
234
    return NULL;
 
235
}
 
236
 
 
237
ncInstance * get_instance (bunchOfInstances **headp)
 
238
{
 
239
    static bunchOfInstances * current = NULL;
 
240
    
 
241
    /* advance static variable, wrapping to head if at the end */
 
242
    if ( current == NULL ) current = * headp;
 
243
    else current = current->next;
 
244
    
 
245
    /* return the new value, if any */
 
246
    if ( current == NULL ) return NULL;
 
247
    else return current->instance;
 
248
}
 
249
 
 
250
int total_instances (bunchOfInstances **headp)
 
251
{
 
252
    if ( * headp ) return (* headp)->count;
 
253
    else return 0;
 
254
}
 
255
 
 
256
/* 
 
257
 * finds a matching volume 
 
258
 * OR returns a pointer to the next empty volume slot 
 
259
 * OR if full, returns NULL
 
260
 */
 
261
static ncVolume * find_volume (ncInstance * instance, char *volumeId) 
 
262
{
 
263
    ncVolume * v = instance->volumes;
 
264
 
 
265
    int i;
 
266
    for (i=0; i<EUCA_MAX_VOLUMES; i++,v++) {
 
267
        if ( ! strncmp (v->volumeId, volumeId, CHAR_BUFFER_SIZE) )
 
268
            break;
 
269
        if ( ! strnlen (v->volumeId, CHAR_BUFFER_SIZE) )
 
270
            break;
 
271
    }
 
272
    if (i==EUCA_MAX_VOLUMES)
 
273
        v = NULL;
 
274
 
 
275
    return v;
 
276
}
 
277
 
 
278
ncVolume * add_volume (ncInstance * instance, char *volumeId, char *remoteDev, char *localDev)
 
279
{
 
280
    ncVolume * v = find_volume (instance, volumeId);
 
281
 
 
282
    if ( v == NULL) {
 
283
        return NULL; /* out of room */
 
284
    }
 
285
 
 
286
    if ( ! strncmp (v->volumeId, volumeId, CHAR_BUFFER_SIZE) ) {
 
287
        return NULL; /* already there */
 
288
    } else {
 
289
        strncpy (v->volumeId, volumeId, CHAR_BUFFER_SIZE);
 
290
        strncpy (v->remoteDev, remoteDev, CHAR_BUFFER_SIZE);
 
291
        strncpy (v->localDev, localDev , CHAR_BUFFER_SIZE);
 
292
        instance->volumesSize++;
 
293
    }
 
294
 
 
295
    return v;
 
296
}
 
297
 
 
298
ncVolume * free_volume (ncInstance * instance, char *volumeId, char *remoteDev, char *localDev)
 
299
{
 
300
    ncVolume * v = find_volume (instance, volumeId);
 
301
    
 
302
    if ( v == NULL) {
 
303
        return NULL; /* not there (and out of room) */
 
304
    }
 
305
 
 
306
    if ( strncmp (v->volumeId, volumeId, CHAR_BUFFER_SIZE) ) {
 
307
        return NULL; /* not there */
 
308
        
 
309
    } else {
 
310
        ncVolume * last_v = instance->volumes+(EUCA_MAX_VOLUMES-1);
 
311
        int slots_left = last_v - v;
 
312
        /* shift the remaining entries up, empty or not */
 
313
        if (slots_left)
 
314
            memmove (v, v+1, slots_left*sizeof(ncVolume));
 
315
        
 
316
        /* empty the last one */
 
317
        bzero (last_v, sizeof(ncVolume));
 
318
        instance->volumesSize--;
 
319
    }
 
320
    
 
321
    return v;
 
322
}