static int mdbox_file_create(struct dbox_file *file)
{
bool create_parents;
- int ret;
create_parents = dbox_file_is_in_alt(file);
file->fd = file->storage->v.
file_create_fd(file, file->cur_path, create_parents);
-
- /* even though we don't need it locked while writing to it, by the
- time we rename() it it needs to be locked. so we might as well do
- it here. */
- if ((ret = dbox_file_try_lock(file)) <= 0) {
- if (ret < 0)
- return -1;
- mail_storage_set_critical(&file->storage->storage,
- "dbox: Couldn't lock created file: %s",
- file->cur_path);
- return -1;
- }
- return 0;
+ return file->fd == -1 ? -1 : 0;
}
static struct dbox_file *
uoff_t mail_size, struct ostream **output_r)
{
struct dbox_map *map = ctx->map;
- struct dbox_file_append_context *const *file_appends;
+ struct dbox_file_append_context *const *file_appends, *append;
+ struct mdbox_file *mfile;
unsigned int i, count;
uoff_t append_offset;
/* first try to use files already used in this append */
file_appends = array_get(&ctx->file_appends, &count);
for (i = count; i > ctx->files_nonappendable_count; i--) {
- append_offset = file_appends[i-1]->output->offset;
- if (append_offset + mail_size <= map->set->mdbox_rotate_size &&
- dbox_file_get_append_stream(file_appends[i-1], output_r) > 0)
- return file_appends[i-1];
+ append = file_appends[i-1];
- /* can't append to this file anymore. we could close it
- otherwise, except that would also lose our lock too early. */
+ append_offset = append->output->offset;
+ if (append_offset + mail_size <= map->set->mdbox_rotate_size &&
+ dbox_file_get_append_stream(append, output_r) > 0)
+ return append;
+
+ /* can't append to this file anymore. if we created this file,
+ close it so we don't waste fds. if we didn't, we can't close
+ it without also losing our lock too early. */
+ mfile = (struct mdbox_file *)append->file;
+ if (mfile->file_id == 0 && dbox_file_append_flush(append) == 0)
+ dbox_file_close(append->file);
}
ctx->files_nonappendable_count = count;
return NULL;