summaryrefslogtreecommitdiffstats
path: root/src/ccapi/lib/ccapi_ccache_iterator.c
blob: 1d4dd6c5562c164a9317b4cc1845c9fa9d71a6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * $Header$
 *
 * Copyright 2006 Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 * require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "ccapi_ccache_iterator.h"
#include "ccapi_ccache.h"
#include "ccapi_ipc.h"

/* ------------------------------------------------------------------------ */

typedef struct cci_ccache_iterator_d {
    cc_ccache_iterator_f *functions;
#if TARGET_OS_MAC
    cc_ccache_iterator_f *vector_functions;
#endif
    cci_identifier_t identifier;
    char *saved_ccache_name;
} *cci_ccache_iterator_t;

/* ------------------------------------------------------------------------ */

struct cci_ccache_iterator_d cci_ccache_iterator_initializer = { 
    NULL 
    VECTOR_FUNCTIONS_INITIALIZER, 
    NULL,
    NULL
};

cc_ccache_iterator_f cci_ccache_iterator_f_initializer = { 
    ccapi_ccache_iterator_release,
    ccapi_ccache_iterator_next,
    ccapi_ccache_iterator_clone
};

/* ------------------------------------------------------------------------ */

cc_int32 cci_ccache_iterator_new (cc_ccache_iterator_t *out_ccache_iterator,
                                  cci_identifier_t      in_identifier)
{
    cc_int32 err = ccNoError;
    cci_ccache_iterator_t ccache_iterator = NULL;

    if (!in_identifier      ) { err = cci_check_error (ccErrBadParam); }
    if (!out_ccache_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        ccache_iterator = malloc (sizeof (*ccache_iterator));
        if (ccache_iterator) { 
            *ccache_iterator = cci_ccache_iterator_initializer;
        } else { 
            err = cci_check_error (ccErrNoMem); 
        }
    }
    
    if (!err) {
        ccache_iterator->functions = malloc (sizeof (*ccache_iterator->functions));
        if (ccache_iterator->functions) { 
            *ccache_iterator->functions = cci_ccache_iterator_f_initializer;
        } else { 
            err = cci_check_error (ccErrNoMem); 
        }
    }
    
    if (!err) {
        err = cci_identifier_copy (&ccache_iterator->identifier, in_identifier);
    }
    
    if (!err) {
        *out_ccache_iterator = (cc_ccache_iterator_t) ccache_iterator;
        ccache_iterator = NULL; /* take ownership */
    }
    
    ccapi_ccache_iterator_release ((cc_ccache_iterator_t) ccache_iterator);
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_int32 cci_ccache_iterator_write (cc_ccache_iterator_t in_ccache_iterator,
                                   cci_stream_t          in_stream)
{
    cc_int32 err = ccNoError;
    cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;
    
    if (!in_ccache_iterator) { err = cci_check_error (ccErrBadParam); }
    if (!in_stream         ) { err = cci_check_error (ccErrBadParam); }
        
    if (!err) {
        err =  cci_identifier_write (ccache_iterator->identifier, in_stream);
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_int32 ccapi_ccache_iterator_release (cc_ccache_iterator_t io_ccache_iterator)
{
    cc_int32 err = ccNoError;
    cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) io_ccache_iterator;
    
    if (!io_ccache_iterator) { err = ccErrBadParam; }
    
    if (!err) {
        cc_uint32 initialized = 0;
        
        err = cci_identifier_is_initialized (ccache_iterator->identifier,
                                             &initialized);
        
        if (!err && initialized) {
            err =  cci_ipc_send (cci_ccache_iterator_release_msg_id,
                                 ccache_iterator->identifier,
                                 NULL,
                                 NULL);
            if (err) {
                cci_debug_printf ("%s: cci_ipc_send failed with error %d", 
                                 __FUNCTION__, err);
                err = ccNoError;
            }
        }
    }
    
    if (!err) {
        free ((char *) ccache_iterator->functions);
        cci_identifier_release (ccache_iterator->identifier);
        free (ccache_iterator->saved_ccache_name);
        free (ccache_iterator);
    }
    
    return err;
}

/* ------------------------------------------------------------------------ */

cc_int32 ccapi_ccache_iterator_next (cc_ccache_iterator_t  in_ccache_iterator,
                                     cc_ccache_t          *out_ccache)
{
    cc_int32 err = ccNoError;
    cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;
    cci_stream_t reply = NULL;
    cci_identifier_t identifier = NULL;
    
    if (!in_ccache_iterator) { err = cci_check_error (ccErrBadParam); }
    if (!out_ccache        ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        cc_uint32 initialized = 0;

        err = cci_identifier_is_initialized (ccache_iterator->identifier,
                                             &initialized);

        if (!err && !initialized) {
            /* server doesn't actually exist.  Pretend we're empty. */
            err = cci_check_error (ccIteratorEnd);
        }
    }
    
    if (!err) {
        err =  cci_ipc_send (cci_ccache_iterator_next_msg_id,
                             ccache_iterator->identifier,
                             NULL,
                             &reply);
    }
    
    if (!err) {
        err = cci_identifier_read (&identifier, reply);
    }
    
    if (!err) {
        err = cci_ccache_new (out_ccache, identifier);
    }
    
    cci_stream_release (reply);
    cci_identifier_release (identifier);
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_int32 ccapi_ccache_iterator_clone (cc_ccache_iterator_t  in_ccache_iterator,
                                      cc_ccache_iterator_t *out_ccache_iterator)
{
    cc_int32 err = ccNoError;
    cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;
    cci_stream_t reply = NULL;
    cc_uint32 initialized = 0;
    cci_identifier_t identifier = NULL;
    
    if (!in_ccache_iterator ) { err = cci_check_error (ccErrBadParam); }
    if (!out_ccache_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_identifier_is_initialized (ccache_iterator->identifier,
                                             &initialized);
    }

    if (!err) {
        if (initialized) {
            err =  cci_ipc_send (cci_ccache_iterator_next_msg_id,
                                 ccache_iterator->identifier,
                                 NULL,
                                 &reply);
            
            if (!err) {
                err =  cci_identifier_read (&identifier, reply);
            }

        } else {
            /* server doesn't actually exist.  Make another dummy one. */
            identifier = cci_identifier_uninitialized;
        }
    }
    
    if (!err) {
        err = cci_ccache_iterator_new (out_ccache_iterator, identifier);
    }

    cci_identifier_release (identifier);
    cci_stream_release (reply);
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_int32 cci_ccache_iterator_get_saved_ccache_name (cc_ccache_iterator_t   in_ccache_iterator,
                                                    const char           **out_saved_ccache_name)
{
    cc_int32 err = ccNoError;
    cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;
    
    if (!in_ccache_iterator   ) { err = cci_check_error (ccErrBadParam); }
    if (!out_saved_ccache_name) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        *out_saved_ccache_name = ccache_iterator->saved_ccache_name;
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_int32 cci_ccache_iterator_set_saved_ccache_name (cc_ccache_iterator_t  io_ccache_iterator,
                                                    const char            *in_saved_ccache_name)
{
    cc_int32 err = ccNoError;
    cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) io_ccache_iterator;
    char *new_saved_ccache_name = NULL;
    
    if (!io_ccache_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err && in_saved_ccache_name) {
        new_saved_ccache_name = strdup (in_saved_ccache_name);
        if (!new_saved_ccache_name) { err = ccErrNoMem; }
    }
    
    if (!err) {
        free (ccache_iterator->saved_ccache_name);
        
        ccache_iterator->saved_ccache_name = new_saved_ccache_name;
        new_saved_ccache_name = NULL; /* take ownership */
    }
    
    free (new_saved_ccache_name);
    
    return cci_check_error (err);
}