Mercurial > dovecot > core-2.2
comparison src/lib-index/mail-index-data.c @ 29:e9375147c0cb HEAD
Added write_full() which is a simple wrapper around write() meant for
writing into files.
When there's too much deleted data in index files, they're now compressed
when the index is being opened.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 26 Aug 2002 02:46:59 +0300 |
parents | 1b34ec11fff8 |
children | 5e9cf9565353 |
comparison
equal
deleted
inserted
replaced
28:6ac07b3fe0ff | 29:e9375147c0cb |
---|---|
1 /* Copyright (C) 2002 Timo Sirainen */ | 1 /* Copyright (C) 2002 Timo Sirainen */ |
2 | 2 |
3 #include "lib.h" | 3 #include "lib.h" |
4 #include "mmap-util.h" | 4 #include "mmap-util.h" |
5 #include "write-full.h" | |
5 #include "mail-index.h" | 6 #include "mail-index.h" |
6 #include "mail-index-data.h" | 7 #include "mail-index-data.h" |
7 #include "mail-index-util.h" | 8 #include "mail-index-util.h" |
8 | 9 |
9 #include <stdio.h> | 10 #include <stdio.h> |
60 MailIndexData *data; | 61 MailIndexData *data; |
61 MailIndexDataHeader *hdr; | 62 MailIndexDataHeader *hdr; |
62 const char *path; | 63 const char *path; |
63 int fd; | 64 int fd; |
64 | 65 |
65 path = t_strconcat(index->filepath, ".data", NULL); | 66 path = t_strconcat(index->filepath, DATA_FILE_PREFIX, NULL); |
66 fd = open(path, O_RDWR); | 67 fd = open(path, O_RDWR); |
67 if (fd == -1) { | 68 if (fd == -1) { |
68 if (errno == ENOENT) { | 69 if (errno == ENOENT) { |
69 /* doesn't exist, rebuild the index */ | 70 /* doesn't exist, rebuild the index */ |
70 INDEX_MARK_CORRUPTED(index); | 71 INDEX_MARK_CORRUPTED(index); |
107 | 108 |
108 /* write header */ | 109 /* write header */ |
109 memset(&hdr, 0, sizeof(hdr)); | 110 memset(&hdr, 0, sizeof(hdr)); |
110 hdr.indexid = index->indexid; | 111 hdr.indexid = index->indexid; |
111 | 112 |
112 if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { | 113 if (write_full(fd, &hdr, sizeof(hdr)) < 0) { |
113 index_set_error(index, "Error writing to temp index data " | 114 index_set_error(index, "Error writing to temp index data " |
114 "%s: %m", temppath); | 115 "%s: %m", temppath); |
115 return NULL; | 116 return NULL; |
116 } | 117 } |
117 | 118 |
118 /* move temp file into .data file, deleting old one | 119 /* move temp file into .data file, deleting old one |
119 if it already exists */ | 120 if it already exists */ |
120 realpath = t_strconcat(index->filepath, ".data", NULL); | 121 realpath = t_strconcat(index->filepath, DATA_FILE_PREFIX, NULL); |
121 if (rename(temppath, realpath) == -1) { | 122 if (rename(temppath, realpath) == -1) { |
122 index_set_error(index, "rename(%s, %s) failed: %m", | 123 index_set_error(index, "rename(%s, %s) failed: %m", |
123 temppath, realpath); | 124 temppath, realpath); |
124 (void)unlink(temppath); | 125 (void)unlink(temppath); |
125 return NULL; | 126 return NULL; |
187 index_set_error(data->index, "lseek() failed for data file " | 188 index_set_error(data->index, "lseek() failed for data file " |
188 "%s: %m", data->filepath); | 189 "%s: %m", data->filepath); |
189 return FALSE; | 190 return FALSE; |
190 } | 191 } |
191 | 192 |
192 if (write(data->fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { | 193 if (write_full(data->fd, &hdr, sizeof(hdr)) < 0) { |
193 index_set_error(data->index, "write() failed for data file " | 194 index_set_error(data->index, "write() failed for data file " |
194 "%s: %m", data->filepath); | 195 "%s: %m", data->filepath); |
195 return FALSE; | 196 return FALSE; |
196 } | 197 } |
197 | 198 |
214 index_set_error(data->index, "lseek() failed with file %s: %m", | 215 index_set_error(data->index, "lseek() failed with file %s: %m", |
215 data->filepath); | 216 data->filepath); |
216 return -1; | 217 return -1; |
217 } | 218 } |
218 | 219 |
219 if ((size_t) write(data->fd, buffer, size) != size) { | 220 if (write_full(data->fd, buffer, size) < 0) { |
220 index_set_error(data->index, "Error appending to file %s: %m", | 221 index_set_error(data->index, "Error appending to file %s: %m", |
221 data->filepath); | 222 data->filepath); |
222 return -1; | 223 return -1; |
223 } | 224 } |
224 | 225 |
374 "Missing \\0 with field %u (%lu)", | 375 "Missing \\0 with field %u (%lu)", |
375 data->filepath, rec->field, | 376 data->filepath, rec->field, |
376 (unsigned long) DATA_FILE_POSITION(data, rec)); | 377 (unsigned long) DATA_FILE_POSITION(data, rec)); |
377 return FALSE; | 378 return FALSE; |
378 } | 379 } |
380 | |
381 void *mail_index_data_get_mmaped(MailIndexData *data, size_t *size) | |
382 { | |
383 if (!mmap_update(data, 0, UINT_MAX)) | |
384 return NULL; | |
385 | |
386 *size = data->mmap_length; | |
387 return data->mmap_base; | |
388 } |