bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic struct mdbox_file *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenmdbox_find_and_move_open_file(struct mdbox_storage *storage, uint32_t file_id)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get(&storage->open_files, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid mdbox_files_free(struct mdbox_storage *storage)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get(&storage->open_files, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid mdbox_files_sync_input(struct mdbox_storage *storage)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get(&storage->open_files, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenmdbox_close_open_files(struct mdbox_storage *storage, unsigned int close_count)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get(&storage->open_files, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count;) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen files = array_get(&storage->open_files, &count);
20d255b566be852826adf63a0e1916d201b7a619Timo Sirainenmdbox_file_init_paths(struct mdbox_file *file, const char *fname, bool alt)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_strdup_printf("%s/%s", file->storage->storage_dir, fname);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_strdup_printf("%s/%s", file->storage->alt_storage_dir,
20d255b566be852826adf63a0e1916d201b7a619Timo Sirainen file->file.cur_path = !alt ? file->file.primary_path :
f6aaada6101dd43cd80fe965ff1ab9bfaf776252Timo Sirainenstatic int mdbox_file_create(struct mdbox_file *file)
f6aaada6101dd43cd80fe965ff1ab9bfaf776252Timo Sirainen file_create_fd(_file, _file->cur_path, create_parents);
f6aaada6101dd43cd80fe965ff1ab9bfaf776252Timo Sirainen } else if (ret == 0) {
f6aaada6101dd43cd80fe965ff1ab9bfaf776252Timo Sirainen /* not supported by filesystem, disable. */
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainenstatic struct dbox_file *
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainenmdbox_file_init_full(struct mdbox_storage *storage,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mdbox_find_and_move_open_file(storage, file_id);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen fname = file_id == 0 ? dbox_generate_tmp_filename() :
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen t_strdup_printf(MDBOX_MAIL_FILE_FORMAT, file_id);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainenmdbox_file_init(struct mdbox_storage *storage, uint32_t file_id)
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen return mdbox_file_init_full(storage, file_id, FALSE);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainenmdbox_file_init_new_alt(struct mdbox_storage *storage)
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen return mdbox_file_init_full(storage, 0, TRUE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_file_assign_file_id(struct mdbox_file *file, uint32_t file_id)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen new_fname = t_strdup_printf(MDBOX_MAIL_FILE_FORMAT, file_id);
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen file->storage->storage_dir : file->storage->alt_storage_dir;
9955f6cba7652469b1d600a3674e8d27dd4e61bdTimo Sirainen new_path = t_strdup_printf("%s/%s", new_dir, new_fname);
dbc6f14323a634ce39f15dd7fef9ee773551dcf0Timo Sirainen mail_storage_set_critical(&file->file.storage->storage,
dbc6f14323a634ce39f15dd7fef9ee773551dcf0Timo Sirainen "mdbox: %s already exists, rebuilding index", new_path);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_storage_set_critical(&file->storage->storage.storage,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen "rename(%s, %s) failed: %m",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen array_append(&file->storage->open_files, &file, 1);
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainenstatic struct mdbox_file *
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainenmdbox_find_oldest_unused_file(struct mdbox_storage *storage,
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen unsigned int *idx_r)
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen struct mdbox_file *const *files, *oldest_file = NULL;
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen files = array_get(&storage->open_files, &count);
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen for (i = 0; i < count; i++) {
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen files[i]->close_time < oldest_file->close_time) {
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainenstatic void mdbox_file_close_timeout(struct mdbox_storage *storage)
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen unsigned int i;
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen time_t close_time = ioloop_time - MDBOX_CLOSE_UNUSED_FILES_TIMEOUT_SECS;
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen while ((oldest = mdbox_find_oldest_unused_file(storage, &i)) != NULL) {
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen timeout_remove(&storage->to_close_unused_files);
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainenstatic void mdbox_file_close_later(struct mdbox_file *mfile)
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen if (mfile->storage->to_close_unused_files == NULL) {
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen timeout_add(MDBOX_CLOSE_UNUSED_FILES_TIMEOUT_SECS*1000,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid mdbox_file_unrefed(struct dbox_file *file)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct mdbox_file *mfile = (struct mdbox_file *)file;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* don't cache metadata seeks while file isn't being referenced */
8bb360f9e5de1c25e4f875205bb06e8bf15dae14Timo Sirainen count = array_count(&mfile->storage->open_files);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* we can leave this file open for now */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* close the oldest file with refcount=0 */
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen oldest_file = mdbox_find_oldest_unused_file(mfile->storage, &i);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen array_delete(&mfile->storage->open_files, i, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* have to close ourself */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_file_create_fd(struct dbox_file *file, const char *path, bool parents)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct mdbox_file *mfile = (struct mdbox_file *)file;
fca7aa88ab19375aba5a8fd9d4100ea885dccb68Timo Sirainen mailbox_list_get_root_permissions(map->root_list, &perm);
fca7aa88ab19375aba5a8fd9d4100ea885dccb68Timo Sirainen old_mask = umask(0666 & ~perm.file_create_mode);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
be110f0a3888a24282f06820ec0b5068d2b8f906Timo Sirainen if (mailbox_list_mkdir_root(map->root_list, dir,
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen mail_storage_copy_list_error(&file->storage->storage,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* try again */
fca7aa88ab19375aba5a8fd9d4100ea885dccb68Timo Sirainen old_mask = umask(0666 & ~perm.file_create_mode);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_storage_set_critical(&file->storage->storage,
fca7aa88ab19375aba5a8fd9d4100ea885dccb68Timo Sirainen } else if (perm.file_create_gid == (gid_t)-1) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* no group change */
fca7aa88ab19375aba5a8fd9d4100ea885dccb68Timo Sirainen } else if (fchown(fd, (uid_t)-1, perm.file_create_gid) < 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_storage_set_critical(&file->storage->storage, "%s",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_storage_set_critical(&file->storage->storage,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen "fchown(%s, -1, %ld) failed: %m",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* continue anyway */