~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/plugins/fts-squat/squat-uidlist.c

  • Committer: Package Import Robot
  • Author(s): Jaldhar H. Vyas
  • Date: 2013-09-09 00:57:32 UTC
  • mfrom: (1.13.11)
  • mto: (4.8.5 experimental) (1.16.1)
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: package-import@ubuntu.com-20130909005732-dn1eell8srqbhh0e
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2007-2012 Dovecot authors, see the included COPYING file */
 
1
/* Copyright (c) 2007-2013 Dovecot authors, see the included COPYING file */
2
2
 
3
3
#include "lib.h"
4
4
#include "array.h"
65
65
        ARRAY_TYPE(uint32_t) block_offsets;
66
66
        ARRAY_TYPE(uint32_t) block_end_indexes;
67
67
 
68
 
        ARRAY_DEFINE(lists, struct uidlist_list);
 
68
        ARRAY(struct uidlist_list) lists;
69
69
        uint32_t list_start_idx;
70
70
 
71
71
        struct squat_uidlist_file_header build_hdr;
221
221
        if (write_size) {
222
222
                sizebufp = sizebuf;
223
223
                squat_pack_num(&sizebufp, size_value);
224
 
                o_stream_send(output, sizebuf, sizebufp - sizebuf);
 
224
                o_stream_nsend(output, sizebuf, sizebufp - sizebuf);
225
225
        }
226
 
        o_stream_send(output, listbuf, listbufp - listbuf);
227
 
        o_stream_send(output, uidbuf, uid_list_len);
 
226
        o_stream_nsend(output, listbuf, listbufp - listbuf);
 
227
        o_stream_nsend(output, uidbuf, uid_list_len);
228
228
        if (!datastack)
229
229
                i_free(uidbuf);
230
230
 
251
251
                        if (list->uid_count == 1) {
252
252
                                bufp = buf;
253
253
                                squat_pack_num(&bufp, offset);
254
 
                                o_stream_send(output, buf, bufp - buf);
 
254
                                o_stream_nsend(output, buf, bufp - buf);
255
255
                                *size_r = (bufp - buf) << 2 | packed_flags;
256
256
                                return 0;
257
257
                        }
372
372
        }
373
373
        if (uidlist->hdr.indexid != uidlist->trie->hdr.indexid) {
374
374
                /* see if trie was recreated */
375
 
                squat_trie_refresh(uidlist->trie);
 
375
                (void)squat_trie_refresh(uidlist->trie);
376
376
        }
377
377
        if (uidlist->hdr.indexid != uidlist->trie->hdr.indexid) {
378
378
                squat_uidlist_set_corrupted(uidlist, "wrong indexid");
716
716
                struct squat_uidlist_file_header hdr;
717
717
 
718
718
                memset(&hdr, 0, sizeof(hdr));
719
 
                o_stream_send(ctx->output, &hdr, sizeof(hdr));
 
719
                o_stream_nsend(ctx->output, &hdr, sizeof(hdr));
720
720
        }
721
721
        o_stream_cork(ctx->output);
722
722
        i_array_init(&ctx->lists, 10240);
756
756
        if (align != 0) {
757
757
                static char null[sizeof(uint32_t)-1] = { 0, };
758
758
 
759
 
                o_stream_send(output, null, sizeof(uint32_t) - align);
 
759
                o_stream_nsend(output, null, sizeof(uint32_t) - align);
760
760
        }
761
761
        block_list_offset = output->offset;
762
762
 
764
764
        old_block_count = write_old_blocks ? uidlist->cur_block_count : 0;
765
765
 
766
766
        block_offset_count = new_block_count + old_block_count;
767
 
        o_stream_send(output, &block_offset_count, sizeof(block_offset_count));
 
767
        o_stream_nsend(output, &block_offset_count, sizeof(block_offset_count));
768
768
        /* write end indexes */
769
 
        o_stream_send(output, uidlist->cur_block_end_indexes,
770
 
                      old_block_count * sizeof(uint32_t));
771
 
        o_stream_send(output, array_idx(block_end_indexes, 0),
772
 
                      new_block_count * sizeof(uint32_t));
 
769
        o_stream_nsend(output, uidlist->cur_block_end_indexes,
 
770
                       old_block_count * sizeof(uint32_t));
 
771
        o_stream_nsend(output, array_idx(block_end_indexes, 0),
 
772
                       new_block_count * sizeof(uint32_t));
773
773
        /* write offsets */
774
 
        o_stream_send(output, uidlist->cur_block_offsets,
775
 
                      old_block_count * sizeof(uint32_t));
776
 
        o_stream_send(output, array_idx(block_offsets, 0),
777
 
                      new_block_count * sizeof(uint32_t));
778
 
        o_stream_flush(output);
 
774
        o_stream_nsend(output, uidlist->cur_block_offsets,
 
775
                       old_block_count * sizeof(uint32_t));
 
776
        o_stream_nsend(output, array_idx(block_offsets, 0),
 
777
                       new_block_count * sizeof(uint32_t));
 
778
        (void)o_stream_flush(output);
779
779
 
780
780
        /* update header - it's written later when trie is locked */
781
781
        ctx->build_hdr.block_list_offset = block_list_offset;
820
820
                /* write the full size of the uidlists */
821
821
                bufp = buf;
822
822
                squat_pack_num(&bufp, block_offset - start_offset);
823
 
                o_stream_send(ctx->output, buf, bufp - buf);
 
823
                o_stream_nsend(ctx->output, buf, bufp - buf);
824
824
 
825
825
                /* write the sizes/flags */
826
826
                for (j = 0; j < max; j++) {
827
827
                        bufp = buf;
828
828
                        squat_pack_num(&bufp, list_sizes[j]);
829
 
                        o_stream_send(ctx->output, buf, bufp - buf);
 
829
                        o_stream_nsend(ctx->output, buf, bufp - buf);
830
830
                }
831
831
        }
832
832
 
855
855
                return -1;
856
856
 
857
857
        if (!ctx->output->closed) {
858
 
                o_stream_seek(ctx->output, 0);
859
 
                o_stream_send(ctx->output,
860
 
                              &ctx->build_hdr, sizeof(ctx->build_hdr));
861
 
                o_stream_seek(ctx->output, ctx->build_hdr.used_file_size);
862
 
                o_stream_flush(ctx->output);
 
858
                (void)o_stream_seek(ctx->output, 0);
 
859
                o_stream_nsend(ctx->output,
 
860
                               &ctx->build_hdr, sizeof(ctx->build_hdr));
 
861
                (void)o_stream_seek(ctx->output, ctx->build_hdr.used_file_size);
863
862
        }
864
863
 
865
 
        if (ctx->output->last_failed_errno != 0) {
866
 
                errno = ctx->output->last_failed_errno;
 
864
        if (o_stream_nfinish(ctx->output) < 0) {
867
865
                i_error("write() to %s failed: %m", ctx->uidlist->path);
868
866
                return -1;
869
867
        }
891
889
        array_free(&ctx->block_offsets);
892
890
        array_free(&ctx->block_end_indexes);
893
891
        array_free(&ctx->lists);
 
892
        o_stream_ignore_last_errors(ctx->output);
894
893
        o_stream_unref(&ctx->output);
895
894
        i_free(ctx);
896
895
}
933
932
        o_stream_cork(ctx->output);
934
933
 
935
934
        memset(&hdr, 0, sizeof(hdr));
936
 
        o_stream_send(ctx->output, &hdr, sizeof(hdr));
 
935
        o_stream_nsend(ctx->output, &hdr, sizeof(hdr));
937
936
 
938
937
        ctx->cur_block_start_offset = ctx->output->offset;
939
938
        i_array_init(&ctx->new_block_offsets,
962
961
           ended to current offset. write the size of this area. */
963
962
        bufp = buf;
964
963
        squat_pack_num(&bufp, block_offset - ctx->cur_block_start_offset);
965
 
        o_stream_send(ctx->output, buf, bufp - buf);
 
964
        o_stream_nsend(ctx->output, buf, bufp - buf);
966
965
 
967
966
        /* write the sizes/flags */
968
967
        for (i = 0; i < ctx->list_idx; i++) {
969
968
                bufp = buf;
970
969
                squat_pack_num(&bufp, ctx->list_sizes[i]);
971
 
                o_stream_send(ctx->output, buf, bufp - buf);
 
970
                o_stream_nsend(ctx->output, buf, bufp - buf);
972
971
        }
973
972
        ctx->cur_block_start_offset = ctx->output->offset;
974
973
}
1057
1056
                                                    &ctx->new_block_offsets,
1058
1057
                                                    &ctx->new_block_end_indexes,
1059
1058
                                                    FALSE);
1060
 
                o_stream_seek(ctx->output, 0);
1061
 
                o_stream_send(ctx->output, &ctx->build_ctx->build_hdr,
1062
 
                              sizeof(ctx->build_ctx->build_hdr));
1063
 
                o_stream_seek(ctx->output,
1064
 
                              ctx->build_ctx->build_hdr.used_file_size);
1065
 
                o_stream_flush(ctx->output);
 
1059
                (void)o_stream_seek(ctx->output, 0);
 
1060
                o_stream_nsend(ctx->output, &ctx->build_ctx->build_hdr,
 
1061
                               sizeof(ctx->build_ctx->build_hdr));
 
1062
                (void)o_stream_seek(ctx->output,
 
1063
                                    ctx->build_ctx->build_hdr.used_file_size);
1066
1064
 
1067
1065
                if (ctx->uidlist->corrupted)
1068
1066
                        ret = -1;
1069
 
                else if (ctx->output->last_failed_errno != 0) {
1070
 
                        errno = ctx->output->last_failed_errno;
 
1067
                else if (o_stream_nfinish(ctx->output) < 0) {
1071
1068
                        i_error("write() to %s failed: %m", temp_path);
1072
1069
                        ret = -1;
1073
1070
                } else if (rename(temp_path, ctx->uidlist->path) < 0) {
1082
1079
           let it be used for something more useful. */
1083
1080
        squat_uidlist_free_from_memory(ctx->uidlist);
1084
1081
 
 
1082
        o_stream_ignore_last_errors(ctx->output);
1085
1083
        o_stream_unref(&ctx->output);
1086
1084
        if (close(ctx->fd) < 0)
1087
1085
                i_error("close(%s) failed: %m", temp_path);
1178
1176
                }
1179
1177
                for (; mask <= 128; mask <<= 1, idx++) {
1180
1178
                        if ((old_list_idx & mask) != 0) {
1181
 
                                squat_uidlist_build_add_uid(ctx,
1182
 
                                                            uid_list_idx, idx);
 
1179
                                (void)squat_uidlist_build_add_uid(ctx,
 
1180
                                                        uid_list_idx, idx);
1183
1181
                        }
1184
1182
                }
1185
1183
        }
1387
1385
        return 0;
1388
1386
}
1389
1387
 
1390
 
static int uint32_cmp(const void *key, const void *data)
 
1388
static int uint32_cmp(const uint32_t *key, const uint32_t *data)
1391
1389
{
1392
 
        const uint32_t *i1 = key, *i2 = data;
1393
 
 
1394
 
        return (int)*i1 - (int)*i2;
 
1390
        return (int)*key - (int)*data;
1395
1391
}
1396
1392
 
1397
1393
static int
1550
1546
 
1551
1547
        i_array_init(&relative_uids, 128);
1552
1548
        i_array_init(&dest_uids, 128);
1553
 
        squat_uidlist_get(uidlist, uid_list_idx, &relative_uids);
 
1549
        if (squat_uidlist_get(uidlist, uid_list_idx, &relative_uids) < 0)
 
1550
                ret = -1;
1554
1551
 
1555
1552
        parent_idx = 0;
1556
1553
        rel_range = array_get(&relative_uids, &rel_count);
1594
1591
                                ret = -1;
1595
1592
                                break;
1596
1593
                        }
1597
 
                        seq_range_array_add(&dest_uids, 0, parent_uid);
 
1594
                        seq_range_array_add(&dest_uids, parent_uid);
1598
1595
                        for (; parent_idx < parent_count; parent_idx++) {
1599
1596
                                if (parent_range[parent_idx].seq2 <= parent_uid)
1600
1597
                                        continue;