Exemple #1
0
int mdbox_file_assign_file_id(struct mdbox_file *file, uint32_t file_id)
{
	struct stat st;
	const char *old_path;
	const char *new_dir, *new_fname, *new_path;

	i_assert(file->file_id == 0);
	i_assert(file_id != 0);

	old_path = file->file.cur_path;
	new_fname = t_strdup_printf(MDBOX_MAIL_FILE_FORMAT, file_id);
	new_dir = !dbox_file_is_in_alt(&file->file) ?
		file->storage->storage_dir : file->storage->alt_storage_dir;
	new_path = t_strdup_printf("%s/%s", new_dir, new_fname);

	if (stat(new_path, &st) == 0) {
		mail_storage_set_critical(&file->file.storage->storage,
			"mdbox: %s already exists, rebuilding index", new_path);
		mdbox_storage_set_corrupted(file->storage);
		return -1;
	}
	if (rename(old_path, new_path) < 0) {
		mail_storage_set_critical(&file->storage->storage.storage,
					  "rename(%s, %s) failed: %m",
					  old_path, new_path);
		return -1;
	}
	mdbox_file_init_paths(file, new_fname,
			      dbox_file_is_in_alt(&file->file));
	file->file_id = file_id;
	array_append(&file->storage->open_files, &file, 1);
	return 0;
}
Exemple #2
0
static int mdbox_file_create(struct mdbox_file *file)
{
	struct dbox_file *_file = &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);
	if (_file->fd == -1)
		return -1;

	if (file->storage->preallocate_space) {
		ret = file_preallocate(_file->fd,
				       file->storage->set->mdbox_rotate_size);
		if (ret < 0) {
			switch (errno) {
			case ENOSPC:
			case EDQUOT:
				/* ignore */
				break;
			default:
				i_error("file_preallocate(%s) failed: %m",
					_file->cur_path);
				break;
			}
		} else if (ret == 0) {
			/* not supported by filesystem, disable. */
			file->storage->preallocate_space = FALSE;
		}
	}
	return 0;
}