mail-index-compress.c revision 3c7a9aa32ddc1a930a75681a6c3cafb8d10fe810
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (C) 2002 Timo Sirainen */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if (index->header->first_hole_position == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* we don't need to compress after all. shouldn't happen.. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->header->flags &= ~MAIL_INDEX_FLAG_CACHE_FIELDS;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* if we get interrupted, the whole index is probably corrupted.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen so keep rebuild-flag on while doing this */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->header->flags |= MAIL_INDEX_FLAG_REBUILD;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!mail_index_fmsync(index, sizeof(MailIndexHeader)))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* first actually compress the data */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end_rec = (MailIndexRecord *) ((char *) index->mmap_base +
8fa41238067c854435884c459963fde6f8c6436bTimo Sirainen hole_rec = (MailIndexRecord *) ((char *) index->mmap_base +
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen rec = hole_rec + index->header->first_hole_records;
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen memcpy(hole_rec, rec, sizeof(MailIndexRecord));
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* truncate the file to get rid of the extra records */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen fsize = (size_t) ((char *) hole_rec - (char *) index->mmap_base);
5aeb15e5817fbd4b1d8de540aa7673e3819a8030Timo Sirainen if (ftruncate(index->fd, (off_t)fsize) == -1) {
5aeb15e5817fbd4b1d8de540aa7673e3819a8030Timo Sirainen index_set_error(index, "ftruncate() failed for %s: %m",
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen /* update headers */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* make sure the whole file is synced before removing rebuild-flag */
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen index->header->flags &= ~(MAIL_INDEX_FLAG_CACHE_FIELDS |
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic int mail_index_copy_data(MailIndex *index, int fd, const char *path)
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen unsigned char *mmap_data;
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen mmap_data = mail_index_data_get_mmaped(index->data, &mmap_data_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* write data header */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (write_full(fd, &data_hdr, sizeof(data_hdr)) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_set_error(index, "Error writing to temp index data "
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen /* no we'll begin the actual moving. keep rebuild-flag on
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen while doing it. */
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen index->header->flags |= MAIL_INDEX_FLAG_REBUILD;
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen if (!mail_index_fmsync(index, sizeof(MailIndexHeader)))
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen if (rec->data_position + rec->data_size > mmap_data_size) {
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen index_set_error(index, "Error in index file %s: "
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen "data_position+data_size points "
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen if (write_full(fd, mmap_data + rec->data_position,
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen index_set_error(index, "Error writing to temp index "
d161e3c2cde2bd8d5917840f68823a2259ed426eTimo Sirainen /* write the data into temporary file updating the offsets in index
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen while doing it. if we fail (especially if out of disk space/quota)
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen we'll simply fail and index is rebuilt later */
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen fd = mail_index_create_temp_file(index, &temppath);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (!mail_index_copy_data(index, fd, temppath)) {
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen /* now, close the old data file and rename the temp file into
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen new data file */
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen datapath = t_strconcat(index->filepath, DATA_FILE_PREFIX, NULL);
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen index_set_error(index, "rename(%s, %s) failed: %m",
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen /* make sure the whole file is synced before removing rebuild-flag */
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen if (!mail_index_fmsync(index, index->mmap_length))