sdbox-copy.c revision 2a77044395c864cc791cecd34b03002094f4973b
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2002-2015 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainensdbox_file_copy_attachments(struct sdbox_file *src_file,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct dbox_storage *src_storage = src_file->file.storage;
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen struct dbox_storage *dest_storage = dest_file->file.storage;
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen const char *extrefs_line, *src, *dest, *dest_relpath;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* no attachments in source storage */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* different attachment dirs between storages.
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen have to copy the slow way. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((ret = sdbox_file_get_attachments(&src_file->file,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen pool = pool_alloconly_create("sdbox attachments copy", 1024);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!index_attachment_parse_extrefs(extrefs_line, pool, &extrefs)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(&dest_storage->storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Can't copy %s with corrupted extref metadata: %s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen pool_alloconly_create("sdbox attachment copy paths", 512);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen p_array_init(&dest_file->attachment_paths, dest_file->attachment_pool,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen src = t_strdup_printf("%s/%s", dest_storage->attachment_dir,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen sdbox_file_attachment_relpath(src_file, extref->path));
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen dest_relpath = p_strconcat(dest_file->attachment_pool,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen dest = t_strdup_printf("%s/%s", dest_storage->attachment_dir,
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen src_fsfile = fs_file_init(src_storage->attachment_fs, src,
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen dest_fsfile = fs_file_init(dest_storage->attachment_fs, dest,
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen mail_storage_set_critical(&dest_storage->storage, "%s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainensdbox_copy_hardlink(struct mail_save_context *_ctx, struct mail *mail)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (struct sdbox_mailbox *)_ctx->transaction->box;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (strcmp(mail->box->storage->name, SDBOX_STORAGE_NAME) == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* Source storage isn't sdbox, can't hard link */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen src_file = sdbox_file_init(src_mbox, mail->uid);
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen ret = nfs_safe_link(src_path, dest_path, FALSE);
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen if (ret < 0 && errno == ENOENT && src_file->alt_path != NULL) {
8a3d609fdd84f5938c82e8e7eeb84a24ab41b317Timo Sirainen dest_path = dest_file->cur_path = dest_file->alt_path;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = nfs_safe_link(src_path, dest_path, FALSE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* try if the fallback copying code can still
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen read the file (the mail could still have the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen stream open) */
return ret;
if (ret <= 0) {
return ret;
int ret;
T_BEGIN {
} T_END;
if (ret != 0) {