~ubuntu-branches/ubuntu/utopic/389-ds-base/utopic-proposed

« back to all changes in this revision

Viewing changes to ldap/servers/plugins/retrocl/retrocl_trim.c

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2014-09-01 13:32:59 UTC
  • mfrom: (0.2.4)
  • Revision ID: package-import@ubuntu.com-20140901133259-kfw30bd5t1g19qaq
Tags: 1.3.2.23-1
* New bugfix release.
* watch: Update the url.
* control: Update Vcs-Browser url to use cgit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
129
129
}
130
130
 
131
131
/*
132
 
 * Function: handle_getchangerecord_result
 
132
 * Function: handle_getchangetime_result
133
133
 * Arguments: op - pointer to Operation struct for this operation
134
134
 *            err - error code returned from search
135
135
 * Returns: nothing
136
 
 * Description: result handler for get_changerecord().  Sets the crt_err
 
136
 * Description: result handler for get_changetime().  Sets the crt_err
137
137
 *              field of the cnum_result_t struct to the error returned
138
138
 *              from the backend.
139
139
 */
140
140
static void
141
 
handle_getchangerecord_result( int err, void *callback_data )
 
141
handle_getchangetime_result( int err, void *callback_data )
142
142
{
143
143
    cnum_result_t *crt = callback_data;
144
144
 
145
145
    if ( crt == NULL ) {
146
146
        slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
147
 
                "handle_getchangerecord_result: callback_data NULL\n" );
 
147
                "handle_getchangetime_result: callback_data NULL\n" );
148
148
    } else {
149
149
        crt->crt_err = err;
150
150
    }
151
151
}
152
152
 
153
153
/*
154
 
 * Function: handle_getchangerecord_search
 
154
 * Function: handle_getchangetime_search
155
155
 * Arguments: op - pointer to Operation struct for this operation
156
156
 *            e - entry returned by backend
157
157
 * Returns: 0 in all cases
158
 
 * Description: Search result operation handler for get_changerecord().
 
158
 * Description: Search result operation handler for get_changetime().
159
159
 *              Sets fields in the cnum_result_t struct pointed to by
160
160
 *              op->o_handler_data.
161
161
 */
162
162
static int
163
 
handle_getchangerecord_search( Slapi_Entry *e, void *callback_data)
 
163
handle_getchangetime_search( Slapi_Entry *e, void *callback_data)
164
164
{
165
165
    cnum_result_t *crt = callback_data;
 
166
    int rc;
 
167
    Slapi_Attr  *attr;
166
168
 
167
 
    if ( crt == NULL ) {
168
 
        slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
169
 
                "handle_getchangerecord_search: op->o_handler_data NULL\n" );
170
 
    } else if ( crt->crt_nentries > 0 ) {
171
 
        /* only return the first entry, I guess */
172
 
        slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
173
 
                "handle_getchangerecord_search: multiple entries returned\n" );
 
169
    if (crt == NULL) {
 
170
        slapi_log_error(SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
 
171
                "handle_getchangetime_search: op->o_handler_data NULL\n");
 
172
    } else if (crt->crt_nentries > 0) {
 
173
        /* only return the first entry, I guess */
 
174
        slapi_log_error(SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
 
175
                "handle_getchangetime_search: multiple entries returned\n");
174
176
    } else {
175
 
        crt->crt_nentries++;
176
 
        crt->crt_entry = e;
 
177
        crt->crt_nentries++;
 
178
        crt->crt_time = 0;
 
179
 
 
180
        if (NULL != e) {
 
181
            Slapi_Value *sval = NULL;
 
182
            const struct berval *val = NULL;
 
183
            rc = slapi_entry_attr_find(e, attr_changetime, &attr);
 
184
            /* Bug 624442: Logic checking for lack of timestamp was
 
185
               reversed. */
 
186
            if (0 != rc || slapi_attr_first_value(attr, &sval) == -1 ||
 
187
                    (val = slapi_value_get_berval(sval)) == NULL ||
 
188
                    NULL == val->bv_val) {
 
189
                crt->crt_time = 0;
 
190
            } else {
 
191
                crt->crt_time = parse_localTime(val->bv_val);
 
192
            }
 
193
        }
177
194
    }
178
195
 
179
196
    return 0;
181
198
 
182
199
 
183
200
/*
184
 
 * Function: get_changerecord
 
201
 * Function: get_changetime
185
202
 * Arguments: cnum - number of change record to retrieve
186
 
 * Returns: Pointer to an entry structure.  The caller must free the entry.
187
 
 *          If "err" is non-NULL, an error code is returned in the memory
188
 
 *          location it points to.
189
 
 * Description: Retrieve the change record entry whose number is "cnum".
 
203
 * Returns: Taking the attr_changetime of the 'cnum' entry,
 
204
 * it converts it into time_t (parse_localTime) and returns this time value.
 
205
 * It returns 0 in the following cases:
 
206
 *  - changerecord entry has not attr_changetime
 
207
 *  - attr_changetime attribute has no value
 
208
 *  - attr_changetime attribute value is empty
 
209
 * 
 
210
 * Description: Retrieve attr_changetime ("changetime") from a changerecord whose number is "cnum".
190
211
 */
191
 
static Slapi_Entry *get_changerecord( changeNumber cnum, int *err )
 
212
static time_t get_changetime( changeNumber cnum, int *err ) 
192
213
{
193
 
    cnum_result_t       crt, *crtp = &crt;
194
 
    char                fstr[ 16 + CNUMSTR_LEN + 2 ];
 
214
    cnum_result_t crt, *crtp = &crt;
 
215
    char fstr[ 16 + CNUMSTR_LEN + 2 ];
195
216
    Slapi_PBlock *pb;
196
217
 
197
 
    if ( cnum == 0UL ) {
198
 
        if ( err != NULL ) {
199
 
            *err = LDAP_PARAM_ERROR;
200
 
        }
201
 
        return NULL;
202
 
    }
203
 
    crtp->crt_nentries = crtp->crt_err = 0; crtp->crt_entry = NULL;
204
 
    PR_snprintf( fstr, sizeof(fstr), "%s=%ld", attr_changenumber, cnum );
205
 
    
206
 
    pb = slapi_pblock_new ();
207
 
    slapi_search_internal_set_pb (pb, RETROCL_CHANGELOG_DN, 
208
 
                                  LDAP_SCOPE_SUBTREE, fstr,
209
 
                                  (char **)get_cleattrs(),  /* cast const */
210
 
                                  0 /* attrsonly */,
211
 
                                  NULL /* controls */, NULL /* uniqueid */,
212
 
                                  g_plg_identity[PLUGIN_RETROCL], 
213
 
                                  0 /* actions */);
214
 
 
215
 
    slapi_search_internal_callback_pb (pb, crtp, 
216
 
                                       handle_getchangerecord_result, 
217
 
                                       handle_getchangerecord_search, NULL );
218
 
    if ( err != NULL ) {
219
 
        *err = crtp->crt_err;
220
 
    }
221
 
 
222
 
    slapi_pblock_destroy (pb);
223
 
 
224
 
    return( crtp->crt_entry );
 
218
    if (cnum == 0UL) {
 
219
        if (err != NULL) {
 
220
            *err = LDAP_PARAM_ERROR;
 
221
        }
 
222
        return 0;
 
223
    }
 
224
    crtp->crt_nentries = crtp->crt_err = 0;
 
225
    crtp->crt_time = 0;
 
226
    PR_snprintf(fstr, sizeof (fstr), "%s=%ld", attr_changenumber, cnum);
 
227
 
 
228
    pb = slapi_pblock_new();
 
229
    slapi_search_internal_set_pb(pb, RETROCL_CHANGELOG_DN,
 
230
            LDAP_SCOPE_SUBTREE, fstr,
 
231
            (char **) get_cleattrs(), /* cast const */
 
232
            0 /* attrsonly */,
 
233
            NULL /* controls */, NULL /* uniqueid */,
 
234
            g_plg_identity[PLUGIN_RETROCL],
 
235
            0 /* actions */);
 
236
 
 
237
    slapi_search_internal_callback_pb(pb, crtp,
 
238
            handle_getchangetime_result,
 
239
            handle_getchangetime_search, NULL);
 
240
    if (err != NULL) {
 
241
        *err = crtp->crt_err;
 
242
    }
 
243
 
 
244
    slapi_pblock_destroy(pb);
 
245
 
 
246
    return ( crtp->crt_time);
225
247
}
226
248
 
227
249
/*
239
261
    int                 rc = 0, ldrc, done;
240
262
    time_t              now;
241
263
    changeNumber        first_in_log = 0, last_in_log = 0;
242
 
    Slapi_Entry         *e = NULL;
243
264
    int                 num_deleted = 0;
244
265
    int me,lt;
245
266
    
262
283
 
263
284
        while ( !done && retrocl_trimming == 1 ) {
264
285
            int         did_delete;
265
 
            Slapi_Attr  *attr;
266
286
 
267
287
            did_delete = 0;
268
288
            first_in_log = retrocl_get_first_changenumber();
280
300
                break;
281
301
            }
282
302
            if ( me > 0L ) {
283
 
                e = get_changerecord( first_in_log, &ldrc );
284
 
                if ( NULL != e ) {
285
 
                    Slapi_Value *sval = NULL;
286
 
                    const struct berval *val = NULL;
287
 
                    rc = slapi_entry_attr_find( e, attr_changetime, &attr );
288
 
                    /* Bug 624442: Logic checking for lack of timestamp was
289
 
                       reversed. */
290
 
                    if ( 0 != rc  || slapi_attr_first_value( attr,&sval ) == -1 ||
291
 
                            (val = slapi_value_get_berval ( sval )) == NULL ||
292
 
                                NULL == val->bv_val ) {
293
 
                        /* What to do if there's no timestamp? Just delete it. */
294
 
                      retrocl_set_first_changenumber( first_in_log + 1 );
295
 
                      ldrc = delete_changerecord( first_in_log );
296
 
                      num_deleted++;
297
 
                      did_delete = 1;
298
 
                    } else {
299
 
                        time_t change_time = parse_localTime( val->bv_val );
300
 
                        if ( change_time + me < now ) {
301
 
                            retrocl_set_first_changenumber( first_in_log + 1 );
302
 
                            ldrc = delete_changerecord( first_in_log );
303
 
                            num_deleted++;
304
 
                            did_delete = 1;
305
 
                        }
306
 
                    /* slapi_entry_free( e ); */ /* XXXggood should we be freeing this? */
307
 
                    }
308
 
                }
 
303
            time_t change_time = get_changetime(first_in_log, &ldrc);
 
304
            if (change_time) {
 
305
                if ((change_time + me) < now) {
 
306
                    retrocl_set_first_changenumber(first_in_log + 1);
 
307
                    ldrc = delete_changerecord(first_in_log);
 
308
                    num_deleted++;
 
309
                    did_delete = 1;
 
310
                }
 
311
            } else {
 
312
                /* What to do if there's no timestamp? Just delete it. */
 
313
                retrocl_set_first_changenumber(first_in_log + 1);
 
314
                ldrc = delete_changerecord(first_in_log);
 
315
                num_deleted++;
 
316
                did_delete = 1;
 
317
             }   
309
318
            }
310
319
            if ( !did_delete ) {
311
320
                done = 1;