~ubuntu-branches/ubuntu/trusty/ldb/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/tdb/common/tdb.c

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2012-02-07 16:04:26 UTC
  • mfrom: (1.3.11)
  • Revision ID: package-import@ubuntu.com-20120207160426-hz17vq8gs1epwkf2
Tags: 1:1.1.4+git20120206-1
* New upstream snapshot.
 + Extracts waf source code. Closes: #654482
 + Disable tdb2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
 
125
125
static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
126
126
 
 
127
static int tdb_update_hash_cmp(TDB_DATA key, TDB_DATA data, void *private_data)
 
128
{
 
129
        TDB_DATA *dbuf = (TDB_DATA *)private_data;
 
130
 
 
131
        if (dbuf->dsize != data.dsize) {
 
132
                return -1;
 
133
        }
 
134
        if (memcmp(dbuf->dptr, data.dptr, data.dsize) != 0) {
 
135
                return -1;
 
136
        }
 
137
        return 0;
 
138
}
 
139
 
127
140
/* update an entry in place - this only works if the new data size
128
141
   is <= the old data size and the key exists.
129
142
   on failure return -1.
141
154
         * surprisingly common (eg. with a ldb re-index). */
142
155
        if (rec.key_len == key.dsize && 
143
156
            rec.data_len == dbuf.dsize &&
144
 
            rec.full_hash == hash) {
145
 
                TDB_DATA data = _tdb_fetch(tdb, key);
146
 
                if (data.dsize == dbuf.dsize &&
147
 
                    memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
148
 
                        if (data.dptr) {
149
 
                                free(data.dptr);
150
 
                        }
151
 
                        return 0;
152
 
                }
153
 
                if (data.dptr) {
154
 
                        free(data.dptr);
155
 
                }
 
157
            rec.full_hash == hash &&
 
158
            tdb_parse_record(tdb, key, tdb_update_hash_cmp, &dbuf) == 0) {
 
159
                return 0;
156
160
        }
157
161
 
158
162
        /* must be long enough key, data and tailer */
473
477
{
474
478
        struct tdb_record rec;
475
479
        tdb_off_t rec_ptr;
476
 
        char *p = NULL;
477
480
        int ret = -1;
478
481
 
479
482
        /* check for it existing, on insert. */
503
506
        if (flag != TDB_INSERT)
504
507
                tdb_delete_hash(tdb, key, hash);
505
508
 
506
 
        /* Copy key+value *before* allocating free space in case malloc
507
 
           fails and we are left with a dead spot in the tdb. */
508
 
 
509
 
        if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
510
 
                tdb->ecode = TDB_ERR_OOM;
511
 
                goto fail;
512
 
        }
513
 
 
514
 
        memcpy(p, key.dptr, key.dsize);
515
 
        if (dbuf.dsize)
516
 
                memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
517
 
 
518
509
        if (tdb->max_dead_records != 0) {
519
510
                /*
520
511
                 * Allow for some dead records per hash chain, look if we can
534
525
                        if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
535
526
                            || tdb->methods->tdb_write(
536
527
                                    tdb, rec_ptr + sizeof(rec),
537
 
                                    p, key.dsize + dbuf.dsize) == -1) {
 
528
                                    key.dptr, key.dsize) == -1
 
529
                            || tdb->methods->tdb_write(
 
530
                                    tdb, rec_ptr + sizeof(rec) + key.dsize,
 
531
                                    dbuf.dptr, dbuf.dsize) == -1) {
538
532
                                goto fail;
539
533
                        }
540
534
                        goto done;
577
571
 
578
572
        /* write out and point the top of the hash chain at it */
579
573
        if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
580
 
            || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
 
574
            || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec),
 
575
                                       key.dptr, key.dsize) == -1
 
576
            || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec)+key.dsize,
 
577
                                       dbuf.dptr, dbuf.dsize) == -1
581
578
            || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
582
579
                /* Need to tdb_unallocate() here */
583
580
                goto fail;
589
586
        if (ret == 0) {
590
587
                tdb_increment_seqnum(tdb);
591
588
        }
592
 
 
593
 
        SAFE_FREE(p); 
594
589
        return ret;
595
590
}
596
591
 
1008
1003
#ifdef TDB_TRACE
1009
1004
static void tdb_trace_write(struct tdb_context *tdb, const char *str)
1010
1005
{
1011
 
        if (!tdb_write_alltdb->tracefd, str, strlen(str)) {
 
1006
        if (!tdb_write_all(tdb->tracefd, str, strlen(str))) {
1012
1007
                close(tdb->tracefd);
1013
1008
                tdb->tracefd = -1;
1014
1009
        }