bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainensdbox_file_copy_attachments(struct sdbox_file *src_file,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen struct dbox_storage *src_storage = src_file->file.storage;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen struct dbox_storage *dest_storage = dest_file->file.storage;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen const char *extrefs_line, *src, *dest, *dest_relpath;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* no attachments in source storage */
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen strcmp(src_storage->storage.set->mail_attachment_fs,
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen dest_storage->storage.set->mail_attachment_fs) != 0 ||
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen strcmp(src_storage->storage.set->mail_attachment_hash,
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen dest_storage->storage.set->mail_attachment_hash) != 0) {
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen /* different attachment dirs/settings between storages.
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen have to copy the slow way. */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen if ((ret = sdbox_file_get_attachments(&src_file->file,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen pool = pool_alloconly_create("sdbox attachments copy", 1024);
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen if (!index_attachment_parse_extrefs(extrefs_line, pool, &extrefs)) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&dest_file->mbox->box,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen "Can't copy %s with corrupted extref metadata: %s",
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen pool_alloconly_create("sdbox attachment copy paths", 512);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen p_array_init(&dest_file->attachment_paths, dest_file->attachment_pool,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen src = t_strdup_printf("%s/%s", dest_storage->attachment_dir,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen sdbox_file_attachment_relpath(src_file, extref->path));
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen dest_relpath = p_strconcat(dest_file->attachment_pool,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen dest = t_strdup_printf("%s/%s", dest_storage->attachment_dir,
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen /* we verified above that attachment_fs is compatible for
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen src and dest, so it doesn't matter which storage's
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen attachment_fs we use. in any case we need to use the same
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen one or fs_copy() will crash with assert. */
98449946caeaf8a3b413a0d93128315b158cbffbTimo Sirainen src_fsfile = fs_file_init(dest_storage->attachment_fs, src,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen dest_fsfile = fs_file_init(dest_storage->attachment_fs, dest,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&dest_file->mbox->box, "%s",
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainensdbox_copy_hardlink(struct mail_save_context *_ctx, struct mail *mail)
804fa3f03bd9170272168a5ad214053bbe3160c7Josef 'Jeff' Sipek struct dbox_save_context *ctx = DBOX_SAVECTX(_ctx);
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *dest_mbox = SDBOX_MAILBOX(_ctx->transaction->box);
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen if (strcmp(mail->box->storage->name, SDBOX_STORAGE_NAME) == 0)
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen /* Source storage isn't sdbox, can't hard link */
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen src_file = sdbox_file_init(src_mbox, mail->uid);
2a77044395c864cc791cecd34b03002094f4973bTimo Sirainen ret = nfs_safe_link(src_path, dest_path, FALSE);
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen if (ret < 0 && errno == ENOENT && src_file->alt_path != NULL) {
2a77044395c864cc791cecd34b03002094f4973bTimo Sirainen dest_path = dest_file->cur_path = dest_file->alt_path;
2a77044395c864cc791cecd34b03002094f4973bTimo Sirainen ret = nfs_safe_link(src_path, dest_path, FALSE);
e3e5ca6aec3efe6ef7419f411d934a5350f06df9Timo Sirainen /* try if the fallback copying code can still
e3e5ca6aec3efe6ef7419f411d934a5350f06df9Timo Sirainen read the file (the mail could still have the
e3e5ca6aec3efe6ef7419f411d934a5350f06df9Timo Sirainen stream open) */
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mail_set_critical(mail, "link(%s, %s) failed: %m",
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ret = sdbox_file_copy_attachments((struct sdbox_file *)src_file,
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)sdbox_file_unlink_aborted_save((struct sdbox_file *)dest_file);
9fd6f70ed6d4733ae5eabd49822bda280393e403Timo Sirainen ((struct sdbox_file *)dest_file)->written_to_disk = TRUE;
1554bed8d2b4e4286c10f7d6bcf716b246bd5bafTimo Sirainen index_copy_cache_fields(_ctx, mail, ctx->seq);
a825281071af96cc148e49c64ac36d8c5cf26f71Timo Sirainen mail_set_seq_saving(_ctx->dest_mail, ctx->seq);
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainenint sdbox_copy(struct mail_save_context *_ctx, struct mail *mail)
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen struct mailbox_transaction_context *_t = _ctx->transaction;
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen struct sdbox_mailbox *mbox = (struct sdbox_mailbox *)_t->box;
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen i_assert((_t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
90e39174d397567c101dbf694761371af3682928Timo Sirainen if (mail_storage_copy_can_use_hardlink(mail->box, &mbox->box) &&
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen /* non-fatal hardlinking failure, try the slow way */