diff options
Diffstat (limited to 'src/lib-storage/index/dbox-single')
-rw-r--r-- | src/lib-storage/index/dbox-single/Makefile.am | 30 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/Makefile.in | 848 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-copy.c | 185 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-file.c | 447 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-file.h | 43 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-mail.c | 182 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-save.c | 359 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-storage.c | 535 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-storage.h | 69 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c | 218 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-sync.c | 326 | ||||
-rw-r--r-- | src/lib-storage/index/dbox-single/sdbox-sync.h | 38 |
12 files changed, 3280 insertions, 0 deletions
diff --git a/src/lib-storage/index/dbox-single/Makefile.am b/src/lib-storage/index/dbox-single/Makefile.am new file mode 100644 index 0000000..b6f59bd --- /dev/null +++ b/src/lib-storage/index/dbox-single/Makefile.am @@ -0,0 +1,30 @@ +noinst_LTLIBRARIES = libstorage_dbox_single.la + +AM_CPPFLAGS = \ + -I$(top_srcdir)/src/lib \ + -I$(top_srcdir)/src/lib-settings \ + -I$(top_srcdir)/src/lib-master \ + -I$(top_srcdir)/src/lib-fs \ + -I$(top_srcdir)/src/lib-mail \ + -I$(top_srcdir)/src/lib-imap \ + -I$(top_srcdir)/src/lib-index \ + -I$(top_srcdir)/src/lib-storage \ + -I$(top_srcdir)/src/lib-storage/index \ + -I$(top_srcdir)/src/lib-storage/index/dbox-common + +libstorage_dbox_single_la_SOURCES = \ + sdbox-copy.c \ + sdbox-file.c \ + sdbox-mail.c \ + sdbox-save.c \ + sdbox-sync.c \ + sdbox-sync-rebuild.c \ + sdbox-storage.c + +headers = \ + sdbox-file.h \ + sdbox-storage.h \ + sdbox-sync.h + +pkginc_libdir=$(pkgincludedir) +pkginc_lib_HEADERS = $(headers) diff --git a/src/lib-storage/index/dbox-single/Makefile.in b/src/lib-storage/index/dbox-single/Makefile.in new file mode 100644 index 0000000..03b6f53 --- /dev/null +++ b/src/lib-storage/index/dbox-single/Makefile.in @@ -0,0 +1,848 @@ +# Makefile.in generated by automake 1.16.3 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2020 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + + +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +subdir = src/lib-storage/index/dbox-single +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/ac_checktype2.m4 \ + $(top_srcdir)/m4/ac_typeof.m4 $(top_srcdir)/m4/arc4random.m4 \ + $(top_srcdir)/m4/blockdev.m4 $(top_srcdir)/m4/c99_vsnprintf.m4 \ + $(top_srcdir)/m4/clock_gettime.m4 $(top_srcdir)/m4/crypt.m4 \ + $(top_srcdir)/m4/crypt_xpg6.m4 $(top_srcdir)/m4/dbqlk.m4 \ + $(top_srcdir)/m4/dirent_dtype.m4 $(top_srcdir)/m4/dovecot.m4 \ + $(top_srcdir)/m4/fd_passing.m4 $(top_srcdir)/m4/fdatasync.m4 \ + $(top_srcdir)/m4/flexible_array_member.m4 \ + $(top_srcdir)/m4/glibc.m4 $(top_srcdir)/m4/gmtime_max.m4 \ + $(top_srcdir)/m4/gmtime_tm_gmtoff.m4 \ + $(top_srcdir)/m4/ioloop.m4 $(top_srcdir)/m4/iovec.m4 \ + $(top_srcdir)/m4/ipv6.m4 $(top_srcdir)/m4/libcap.m4 \ + $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/libwrap.m4 \ + $(top_srcdir)/m4/linux_mremap.m4 $(top_srcdir)/m4/ltoptions.m4 \ + $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ + $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/mmap_write.m4 \ + $(top_srcdir)/m4/mntctl.m4 $(top_srcdir)/m4/modules.m4 \ + $(top_srcdir)/m4/notify.m4 $(top_srcdir)/m4/nsl.m4 \ + $(top_srcdir)/m4/off_t_max.m4 $(top_srcdir)/m4/pkg.m4 \ + $(top_srcdir)/m4/pr_set_dumpable.m4 \ + $(top_srcdir)/m4/q_quotactl.m4 $(top_srcdir)/m4/quota.m4 \ + $(top_srcdir)/m4/random.m4 $(top_srcdir)/m4/rlimit.m4 \ + $(top_srcdir)/m4/sendfile.m4 $(top_srcdir)/m4/size_t_signed.m4 \ + $(top_srcdir)/m4/sockpeercred.m4 $(top_srcdir)/m4/sql.m4 \ + $(top_srcdir)/m4/ssl.m4 $(top_srcdir)/m4/st_tim.m4 \ + $(top_srcdir)/m4/static_array.m4 $(top_srcdir)/m4/test_with.m4 \ + $(top_srcdir)/m4/time_t.m4 $(top_srcdir)/m4/typeof.m4 \ + $(top_srcdir)/m4/typeof_dev_t.m4 \ + $(top_srcdir)/m4/uoff_t_max.m4 $(top_srcdir)/m4/vararg.m4 \ + $(top_srcdir)/m4/want_apparmor.m4 \ + $(top_srcdir)/m4/want_bsdauth.m4 \ + $(top_srcdir)/m4/want_bzlib.m4 \ + $(top_srcdir)/m4/want_cassandra.m4 \ + $(top_srcdir)/m4/want_cdb.m4 \ + $(top_srcdir)/m4/want_checkpassword.m4 \ + $(top_srcdir)/m4/want_clucene.m4 $(top_srcdir)/m4/want_db.m4 \ + $(top_srcdir)/m4/want_gssapi.m4 $(top_srcdir)/m4/want_icu.m4 \ + $(top_srcdir)/m4/want_ldap.m4 $(top_srcdir)/m4/want_lua.m4 \ + $(top_srcdir)/m4/want_lz4.m4 $(top_srcdir)/m4/want_lzma.m4 \ + $(top_srcdir)/m4/want_mysql.m4 $(top_srcdir)/m4/want_pam.m4 \ + $(top_srcdir)/m4/want_passwd.m4 $(top_srcdir)/m4/want_pgsql.m4 \ + $(top_srcdir)/m4/want_prefetch.m4 \ + $(top_srcdir)/m4/want_shadow.m4 \ + $(top_srcdir)/m4/want_sodium.m4 $(top_srcdir)/m4/want_solr.m4 \ + $(top_srcdir)/m4/want_sqlite.m4 \ + $(top_srcdir)/m4/want_stemmer.m4 \ + $(top_srcdir)/m4/want_systemd.m4 \ + $(top_srcdir)/m4/want_textcat.m4 \ + $(top_srcdir)/m4/want_unwind.m4 $(top_srcdir)/m4/want_zlib.m4 \ + $(top_srcdir)/m4/want_zstd.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(pkginc_lib_HEADERS) \ + $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +LTLIBRARIES = $(noinst_LTLIBRARIES) +libstorage_dbox_single_la_LIBADD = +am_libstorage_dbox_single_la_OBJECTS = sdbox-copy.lo sdbox-file.lo \ + sdbox-mail.lo sdbox-save.lo sdbox-sync.lo \ + sdbox-sync-rebuild.lo sdbox-storage.lo +libstorage_dbox_single_la_OBJECTS = \ + $(am_libstorage_dbox_single_la_OBJECTS) +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__maybe_remake_depfiles = depfiles +am__depfiles_remade = ./$(DEPDIR)/sdbox-copy.Plo \ + ./$(DEPDIR)/sdbox-file.Plo ./$(DEPDIR)/sdbox-mail.Plo \ + ./$(DEPDIR)/sdbox-save.Plo ./$(DEPDIR)/sdbox-storage.Plo \ + ./$(DEPDIR)/sdbox-sync-rebuild.Plo ./$(DEPDIR)/sdbox-sync.Plo +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(libstorage_dbox_single_la_SOURCES) +DIST_SOURCES = $(libstorage_dbox_single_la_SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = f=`echo $$p | sed -e 's|^.*/||'`; +am__install_max = 40 +am__nobase_strip_setup = \ + srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*|]/\\\\&/g'` +am__nobase_strip = \ + for p in $$list; do echo "$$p"; done | sed -e "s|$$srcdirstrip/||" +am__nobase_list = $(am__nobase_strip_setup); \ + for p in $$list; do echo "$$p $$p"; done | \ + sed "s| $$srcdirstrip/| |;"' / .*\//!s/ .*/ ./; s,\( .*\)/[^/]*$$,\1,' | \ + $(AWK) 'BEGIN { files["."] = "" } { files[$$2] = files[$$2] " " $$1; \ + if (++n[$$2] == $(am__install_max)) \ + { print $$2, files[$$2]; n[$$2] = 0; files[$$2] = "" } } \ + END { for (dir in files) print dir, files[dir] }' +am__base_list = \ + sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \ + sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g' +am__uninstall_files_from_dir = { \ + test -z "$$files" \ + || { test ! -d "$$dir" && test ! -f "$$dir" && test ! -r "$$dir"; } \ + || { echo " ( cd '$$dir' && rm -f" $$files ")"; \ + $(am__cd) "$$dir" && rm -f $$files; }; \ + } +am__installdirs = "$(DESTDIR)$(pkginc_libdir)" +HEADERS = $(pkginc_lib_HEADERS) +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +ETAGS = etags +CTAGS = ctags +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +ACLOCAL_AMFLAGS = @ACLOCAL_AMFLAGS@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +APPARMOR_LIBS = @APPARMOR_LIBS@ +AR = @AR@ +AUTH_CFLAGS = @AUTH_CFLAGS@ +AUTH_LIBS = @AUTH_LIBS@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +BINARY_CFLAGS = @BINARY_CFLAGS@ +BINARY_LDFLAGS = @BINARY_LDFLAGS@ +BISON = @BISON@ +CASSANDRA_CFLAGS = @CASSANDRA_CFLAGS@ +CASSANDRA_LIBS = @CASSANDRA_LIBS@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CDB_LIBS = @CDB_LIBS@ +CFLAGS = @CFLAGS@ +CLUCENE_CFLAGS = @CLUCENE_CFLAGS@ +CLUCENE_LIBS = @CLUCENE_LIBS@ +COMPRESS_LIBS = @COMPRESS_LIBS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CRYPT_LIBS = @CRYPT_LIBS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DICT_LIBS = @DICT_LIBS@ +DLLIB = @DLLIB@ +DLLTOOL = @DLLTOOL@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +FLEX = @FLEX@ +FUZZER_CPPFLAGS = @FUZZER_CPPFLAGS@ +FUZZER_LDFLAGS = @FUZZER_LDFLAGS@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +KRB5CONFIG = @KRB5CONFIG@ +KRB5_CFLAGS = @KRB5_CFLAGS@ +KRB5_LIBS = @KRB5_LIBS@ +LD = @LD@ +LDAP_LIBS = @LDAP_LIBS@ +LDFLAGS = @LDFLAGS@ +LD_NO_WHOLE_ARCHIVE = @LD_NO_WHOLE_ARCHIVE@ +LD_WHOLE_ARCHIVE = @LD_WHOLE_ARCHIVE@ +LIBCAP = @LIBCAP@ +LIBDOVECOT = @LIBDOVECOT@ +LIBDOVECOT_COMPRESS = @LIBDOVECOT_COMPRESS@ +LIBDOVECOT_DEPS = @LIBDOVECOT_DEPS@ +LIBDOVECOT_DSYNC = @LIBDOVECOT_DSYNC@ +LIBDOVECOT_LA_LIBS = @LIBDOVECOT_LA_LIBS@ +LIBDOVECOT_LDA = @LIBDOVECOT_LDA@ +LIBDOVECOT_LDAP = @LIBDOVECOT_LDAP@ +LIBDOVECOT_LIBFTS = @LIBDOVECOT_LIBFTS@ +LIBDOVECOT_LIBFTS_DEPS = @LIBDOVECOT_LIBFTS_DEPS@ +LIBDOVECOT_LOGIN = @LIBDOVECOT_LOGIN@ +LIBDOVECOT_LUA = @LIBDOVECOT_LUA@ +LIBDOVECOT_LUA_DEPS = @LIBDOVECOT_LUA_DEPS@ +LIBDOVECOT_SQL = @LIBDOVECOT_SQL@ +LIBDOVECOT_STORAGE = @LIBDOVECOT_STORAGE@ +LIBDOVECOT_STORAGE_DEPS = @LIBDOVECOT_STORAGE_DEPS@ +LIBEXTTEXTCAT_CFLAGS = @LIBEXTTEXTCAT_CFLAGS@ +LIBEXTTEXTCAT_LIBS = @LIBEXTTEXTCAT_LIBS@ +LIBICONV = @LIBICONV@ +LIBICU_CFLAGS = @LIBICU_CFLAGS@ +LIBICU_LIBS = @LIBICU_LIBS@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBSODIUM_CFLAGS = @LIBSODIUM_CFLAGS@ +LIBSODIUM_LIBS = @LIBSODIUM_LIBS@ +LIBTIRPC_CFLAGS = @LIBTIRPC_CFLAGS@ +LIBTIRPC_LIBS = @LIBTIRPC_LIBS@ +LIBTOOL = @LIBTOOL@ +LIBUNWIND_CFLAGS = @LIBUNWIND_CFLAGS@ +LIBUNWIND_LIBS = @LIBUNWIND_LIBS@ +LIBWRAP_LIBS = @LIBWRAP_LIBS@ +LINKED_STORAGE_LDADD = @LINKED_STORAGE_LDADD@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBICONV = @LTLIBICONV@ +LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ +LUA_CFLAGS = @LUA_CFLAGS@ +LUA_LIBS = @LUA_LIBS@ +MAINT = @MAINT@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +MODULE_LIBS = @MODULE_LIBS@ +MODULE_SUFFIX = @MODULE_SUFFIX@ +MYSQL_CFLAGS = @MYSQL_CFLAGS@ +MYSQL_CONFIG = @MYSQL_CONFIG@ +MYSQL_LIBS = @MYSQL_LIBS@ +NM = @NM@ +NMEDIT = @NMEDIT@ +NOPLUGIN_LDFLAGS = @NOPLUGIN_LDFLAGS@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PANDOC = @PANDOC@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +PGSQL_CFLAGS = @PGSQL_CFLAGS@ +PGSQL_LIBS = @PGSQL_LIBS@ +PG_CONFIG = @PG_CONFIG@ +PIE_CFLAGS = @PIE_CFLAGS@ +PIE_LDFLAGS = @PIE_LDFLAGS@ +PKG_CONFIG = @PKG_CONFIG@ +PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ +PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ +QUOTA_LIBS = @QUOTA_LIBS@ +RANLIB = @RANLIB@ +RELRO_LDFLAGS = @RELRO_LDFLAGS@ +RPCGEN = @RPCGEN@ +RUN_TEST = @RUN_TEST@ +SED = @SED@ +SETTING_FILES = @SETTING_FILES@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +SQLITE_CFLAGS = @SQLITE_CFLAGS@ +SQLITE_LIBS = @SQLITE_LIBS@ +SQL_CFLAGS = @SQL_CFLAGS@ +SQL_LIBS = @SQL_LIBS@ +SSL_CFLAGS = @SSL_CFLAGS@ +SSL_LIBS = @SSL_LIBS@ +STRIP = @STRIP@ +SYSTEMD_CFLAGS = @SYSTEMD_CFLAGS@ +SYSTEMD_LIBS = @SYSTEMD_LIBS@ +VALGRIND = @VALGRIND@ +VERSION = @VERSION@ +ZSTD_CFLAGS = @ZSTD_CFLAGS@ +ZSTD_LIBS = @ZSTD_LIBS@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_CXX = @ac_ct_CXX@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +dict_drivers = @dict_drivers@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +moduledir = @moduledir@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +rundir = @rundir@ +runstatedir = @runstatedir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +sql_drivers = @sql_drivers@ +srcdir = @srcdir@ +ssldir = @ssldir@ +statedir = @statedir@ +sysconfdir = @sysconfdir@ +systemdservicetype = @systemdservicetype@ +systemdsystemunitdir = @systemdsystemunitdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +noinst_LTLIBRARIES = libstorage_dbox_single.la +AM_CPPFLAGS = \ + -I$(top_srcdir)/src/lib \ + -I$(top_srcdir)/src/lib-settings \ + -I$(top_srcdir)/src/lib-master \ + -I$(top_srcdir)/src/lib-fs \ + -I$(top_srcdir)/src/lib-mail \ + -I$(top_srcdir)/src/lib-imap \ + -I$(top_srcdir)/src/lib-index \ + -I$(top_srcdir)/src/lib-storage \ + -I$(top_srcdir)/src/lib-storage/index \ + -I$(top_srcdir)/src/lib-storage/index/dbox-common + +libstorage_dbox_single_la_SOURCES = \ + sdbox-copy.c \ + sdbox-file.c \ + sdbox-mail.c \ + sdbox-save.c \ + sdbox-sync.c \ + sdbox-sync-rebuild.c \ + sdbox-storage.c + +headers = \ + sdbox-file.h \ + sdbox-storage.h \ + sdbox-sync.h + +pkginc_libdir = $(pkgincludedir) +pkginc_lib_HEADERS = $(headers) +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/lib-storage/index/dbox-single/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/lib-storage/index/dbox-single/Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-noinstLTLIBRARIES: + -test -z "$(noinst_LTLIBRARIES)" || rm -f $(noinst_LTLIBRARIES) + @list='$(noinst_LTLIBRARIES)'; \ + locs=`for p in $$list; do echo $$p; done | \ + sed 's|^[^/]*$$|.|; s|/[^/]*$$||; s|$$|/so_locations|' | \ + sort -u`; \ + test -z "$$locs" || { \ + echo rm -f $${locs}; \ + rm -f $${locs}; \ + } + +libstorage_dbox_single.la: $(libstorage_dbox_single_la_OBJECTS) $(libstorage_dbox_single_la_DEPENDENCIES) $(EXTRA_libstorage_dbox_single_la_DEPENDENCIES) + $(AM_V_CCLD)$(LINK) $(libstorage_dbox_single_la_OBJECTS) $(libstorage_dbox_single_la_LIBADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sdbox-copy.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sdbox-file.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sdbox-mail.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sdbox-save.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sdbox-storage.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sdbox-sync-rebuild.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sdbox-sync.Plo@am__quote@ # am--include-marker + +$(am__depfiles_remade): + @$(MKDIR_P) $(@D) + @echo '# dummy' >$@-t && $(am__mv) $@-t $@ + +am--depfiles: $(am__depfiles_remade) + +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs +install-pkginc_libHEADERS: $(pkginc_lib_HEADERS) + @$(NORMAL_INSTALL) + @list='$(pkginc_lib_HEADERS)'; test -n "$(pkginc_libdir)" || list=; \ + if test -n "$$list"; then \ + echo " $(MKDIR_P) '$(DESTDIR)$(pkginc_libdir)'"; \ + $(MKDIR_P) "$(DESTDIR)$(pkginc_libdir)" || exit 1; \ + fi; \ + for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; \ + done | $(am__base_list) | \ + while read files; do \ + echo " $(INSTALL_HEADER) $$files '$(DESTDIR)$(pkginc_libdir)'"; \ + $(INSTALL_HEADER) $$files "$(DESTDIR)$(pkginc_libdir)" || exit $$?; \ + done + +uninstall-pkginc_libHEADERS: + @$(NORMAL_UNINSTALL) + @list='$(pkginc_lib_HEADERS)'; test -n "$(pkginc_libdir)" || list=; \ + files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \ + dir='$(DESTDIR)$(pkginc_libdir)'; $(am__uninstall_files_from_dir) + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) distdir-am + +distdir-am: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(LTLIBRARIES) $(HEADERS) +installdirs: + for dir in "$(DESTDIR)$(pkginc_libdir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-libtool clean-noinstLTLIBRARIES \ + mostlyclean-am + +distclean: distclean-am + -rm -f ./$(DEPDIR)/sdbox-copy.Plo + -rm -f ./$(DEPDIR)/sdbox-file.Plo + -rm -f ./$(DEPDIR)/sdbox-mail.Plo + -rm -f ./$(DEPDIR)/sdbox-save.Plo + -rm -f ./$(DEPDIR)/sdbox-storage.Plo + -rm -f ./$(DEPDIR)/sdbox-sync-rebuild.Plo + -rm -f ./$(DEPDIR)/sdbox-sync.Plo + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: install-pkginc_libHEADERS + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f ./$(DEPDIR)/sdbox-copy.Plo + -rm -f ./$(DEPDIR)/sdbox-file.Plo + -rm -f ./$(DEPDIR)/sdbox-mail.Plo + -rm -f ./$(DEPDIR)/sdbox-save.Plo + -rm -f ./$(DEPDIR)/sdbox-storage.Plo + -rm -f ./$(DEPDIR)/sdbox-sync-rebuild.Plo + -rm -f ./$(DEPDIR)/sdbox-sync.Plo + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-pkginc_libHEADERS + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \ + clean-generic clean-libtool clean-noinstLTLIBRARIES \ + cscopelist-am ctags ctags-am distclean distclean-compile \ + distclean-generic distclean-libtool distclean-tags distdir dvi \ + dvi-am html html-am info info-am install install-am \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-pkginc_libHEADERS install-ps \ + install-ps-am install-strip installcheck installcheck-am \ + installdirs maintainer-clean maintainer-clean-generic \ + mostlyclean mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool pdf pdf-am ps ps-am tags tags-am uninstall \ + uninstall-am uninstall-pkginc_libHEADERS + +.PRECIOUS: Makefile + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/src/lib-storage/index/dbox-single/sdbox-copy.c b/src/lib-storage/index/dbox-single/sdbox-copy.c new file mode 100644 index 0000000..48a3f59 --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-copy.c @@ -0,0 +1,185 @@ +/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "nfs-workarounds.h" +#include "fs-api.h" +#include "dbox-save.h" +#include "dbox-attachment.h" +#include "sdbox-storage.h" +#include "sdbox-file.h" +#include "mail-copy.h" + +static int +sdbox_file_copy_attachments(struct sdbox_file *src_file, + struct sdbox_file *dest_file) +{ + struct dbox_storage *src_storage = src_file->file.storage; + struct dbox_storage *dest_storage = dest_file->file.storage; + struct fs_file *src_fsfile, *dest_fsfile; + ARRAY_TYPE(mail_attachment_extref) extrefs; + const struct mail_attachment_extref *extref; + const char *extrefs_line, *src, *dest, *dest_relpath; + pool_t pool; + int ret; + + if (src_storage->attachment_dir == NULL) { + /* no attachments in source storage */ + return 1; + } + if (dest_storage->attachment_dir == NULL || + strcmp(src_storage->attachment_dir, + dest_storage->attachment_dir) != 0 || + strcmp(src_storage->storage.set->mail_attachment_fs, + dest_storage->storage.set->mail_attachment_fs) != 0 || + strcmp(src_storage->storage.set->mail_attachment_hash, + dest_storage->storage.set->mail_attachment_hash) != 0) { + /* different attachment dirs/settings between storages. + have to copy the slow way. */ + return 0; + } + + if ((ret = sdbox_file_get_attachments(&src_file->file, + &extrefs_line)) <= 0) + return ret < 0 ? -1 : 1; + + pool = pool_alloconly_create("sdbox attachments copy", 1024); + p_array_init(&extrefs, pool, 16); + if (!index_attachment_parse_extrefs(extrefs_line, pool, &extrefs)) { + mailbox_set_critical(&dest_file->mbox->box, + "Can't copy %s with corrupted extref metadata: %s", + src_file->file.cur_path, extrefs_line); + pool_unref(&pool); + return -1; + } + + dest_file->attachment_pool = + pool_alloconly_create("sdbox attachment copy paths", 512); + p_array_init(&dest_file->attachment_paths, dest_file->attachment_pool, + array_count(&extrefs)); + + ret = 1; + array_foreach(&extrefs, extref) T_BEGIN { + src = t_strdup_printf("%s/%s", dest_storage->attachment_dir, + sdbox_file_attachment_relpath(src_file, extref->path)); + dest_relpath = p_strconcat(dest_file->attachment_pool, + extref->path, "-", + guid_generate(), NULL); + dest = t_strdup_printf("%s/%s", dest_storage->attachment_dir, + dest_relpath); + /* we verified above that attachment_fs is compatible for + src and dest, so it doesn't matter which storage's + attachment_fs we use. in any case we need to use the same + one or fs_copy() will crash with assert. */ + src_fsfile = fs_file_init(dest_storage->attachment_fs, src, + FS_OPEN_MODE_READONLY); + dest_fsfile = fs_file_init(dest_storage->attachment_fs, dest, + FS_OPEN_MODE_READONLY); + if (fs_copy(src_fsfile, dest_fsfile) < 0) { + mailbox_set_critical(&dest_file->mbox->box, "%s", + fs_file_last_error(dest_fsfile)); + ret = -1; + } else { + array_push_back(&dest_file->attachment_paths, + &dest_relpath); + } + fs_file_deinit(&src_fsfile); + fs_file_deinit(&dest_fsfile); + } T_END; + pool_unref(&pool); + return ret; +} + +static int +sdbox_copy_hardlink(struct mail_save_context *_ctx, struct mail *mail) +{ + struct dbox_save_context *ctx = DBOX_SAVECTX(_ctx); + struct sdbox_mailbox *dest_mbox = SDBOX_MAILBOX(_ctx->transaction->box); + struct sdbox_mailbox *src_mbox; + struct dbox_file *src_file, *dest_file; + const char *src_path, *dest_path; + int ret; + + if (strcmp(mail->box->storage->name, SDBOX_STORAGE_NAME) == 0) + src_mbox = SDBOX_MAILBOX(mail->box); + else { + /* Source storage isn't sdbox, can't hard link */ + return 0; + } + + src_file = sdbox_file_init(src_mbox, mail->uid); + dest_file = sdbox_file_init(dest_mbox, 0); + + ctx->ctx.data.flags &= ENUM_NEGATE(DBOX_INDEX_FLAG_ALT); + + src_path = src_file->primary_path; + dest_path = dest_file->primary_path; + ret = nfs_safe_link(src_path, dest_path, FALSE); + if (ret < 0 && errno == ENOENT && src_file->alt_path != NULL) { + src_path = src_file->alt_path; + if (dest_file->alt_path != NULL) { + dest_path = dest_file->cur_path = dest_file->alt_path; + ctx->ctx.data.flags |= DBOX_INDEX_FLAG_ALT; + } + ret = nfs_safe_link(src_path, dest_path, FALSE); + } + if (ret < 0) { + if (ECANTLINK(errno)) + ret = 0; + else if (errno == ENOENT) { + /* try if the fallback copying code can still + read the file (the mail could still have the + stream open) */ + ret = 0; + } else { + mail_set_critical(mail, "link(%s, %s) failed: %m", + src_path, dest_path); + } + dbox_file_unref(&src_file); + dbox_file_unref(&dest_file); + return ret; + } + + ret = sdbox_file_copy_attachments((struct sdbox_file *)src_file, + (struct sdbox_file *)dest_file); + if (ret <= 0) { + (void)sdbox_file_unlink_aborted_save((struct sdbox_file *)dest_file); + dbox_file_unref(&src_file); + dbox_file_unref(&dest_file); + return ret; + } + ((struct sdbox_file *)dest_file)->written_to_disk = TRUE; + + dbox_save_add_to_index(ctx); + index_copy_cache_fields(_ctx, mail, ctx->seq); + + sdbox_save_add_file(_ctx, dest_file); + mail_set_seq_saving(_ctx->dest_mail, ctx->seq); + dbox_file_unref(&src_file); + return 1; +} + +int sdbox_copy(struct mail_save_context *_ctx, struct mail *mail) +{ + struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx; + struct mailbox_transaction_context *_t = _ctx->transaction; + struct sdbox_mailbox *mbox = (struct sdbox_mailbox *)_t->box; + int ret; + + i_assert((_t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0); + + ctx->finished = TRUE; + if (mail_storage_copy_can_use_hardlink(mail->box, &mbox->box) && + _ctx->data.guid == NULL) { + T_BEGIN { + ret = sdbox_copy_hardlink(_ctx, mail); + } T_END; + + if (ret != 0) { + index_save_context_free(_ctx); + return ret > 0 ? 0 : -1; + } + + /* non-fatal hardlinking failure, try the slow way */ + } + return mail_storage_copy(_ctx, mail); +} diff --git a/src/lib-storage/index/dbox-single/sdbox-file.c b/src/lib-storage/index/dbox-single/sdbox-file.c new file mode 100644 index 0000000..fed111a --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-file.c @@ -0,0 +1,447 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "eacces-error.h" +#include "fdatasync-path.h" +#include "mkdir-parents.h" +#include "istream.h" +#include "ostream.h" +#include "str.h" +#include "fs-api.h" +#include "dbox-attachment.h" +#include "sdbox-storage.h" +#include "sdbox-file.h" + +#include <stdio.h> +#include <utime.h> + +static void sdbox_file_init_paths(struct sdbox_file *file, const char *fname) +{ + struct mailbox *box = &file->mbox->box; + const char *alt_path; + + i_free(file->file.primary_path); + i_free(file->file.alt_path); + file->file.primary_path = + i_strdup_printf("%s/%s", mailbox_get_path(box), fname); + file->file.cur_path = file->file.primary_path; + + if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_ALT_MAILBOX, + &alt_path) > 0) + file->file.alt_path = i_strdup_printf("%s/%s", alt_path, fname); +} + +struct dbox_file *sdbox_file_init(struct sdbox_mailbox *mbox, uint32_t uid) +{ + struct sdbox_file *file; + const char *fname; + + file = i_new(struct sdbox_file, 1); + file->file.storage = &mbox->storage->storage; + file->mbox = mbox; + T_BEGIN { + if (uid != 0) { + fname = t_strdup_printf(SDBOX_MAIL_FILE_FORMAT, uid); + sdbox_file_init_paths(file, fname); + file->uid = uid; + } else { + sdbox_file_init_paths(file, dbox_generate_tmp_filename()); + } + } T_END; + dbox_file_init(&file->file); + return &file->file; +} + +struct dbox_file *sdbox_file_create(struct sdbox_mailbox *mbox) +{ + struct dbox_file *file; + + file = sdbox_file_init(mbox, 0); + file->fd = file->storage->v. + file_create_fd(file, file->primary_path, FALSE); + return file; +} + +void sdbox_file_free(struct dbox_file *file) +{ + struct sdbox_file *sfile = (struct sdbox_file *)file; + + pool_unref(&sfile->attachment_pool); + dbox_file_free(file); +} + +int sdbox_file_get_attachments(struct dbox_file *file, const char **extrefs_r) +{ + const char *line; + bool deleted; + int ret; + + *extrefs_r = NULL; + + /* read the metadata */ + ret = dbox_file_open(file, &deleted); + if (ret > 0) { + if (deleted) + return 0; + if ((ret = dbox_file_seek(file, 0)) > 0) + ret = dbox_file_metadata_read(file); + } + if (ret <= 0) { + if (ret < 0) + return -1; + /* corrupted file. we're deleting it anyway. */ + line = NULL; + } else { + line = dbox_file_metadata_get(file, DBOX_METADATA_EXT_REF); + } + if (line == NULL) { + /* no attachments */ + return 0; + } + *extrefs_r = line; + return 1; +} + +const char * +sdbox_file_attachment_relpath(struct sdbox_file *file, const char *srcpath) +{ + const char *p; + + p = strchr(srcpath, '-'); + if (p == NULL) { + mailbox_set_critical(&file->mbox->box, + "sdbox attachment path in invalid format: %s", srcpath); + } else { + p = strchr(p+1, '-'); + } + return t_strdup_printf("%s-%s-%u", + p == NULL ? srcpath : t_strdup_until(srcpath, p), + guid_128_to_string(file->mbox->mailbox_guid), + file->uid); +} + +static int sdbox_file_rename_attachments(struct sdbox_file *file) +{ + struct dbox_storage *storage = file->file.storage; + struct fs_file *src_file, *dest_file; + const char *path, *src, *dest; + int ret = 0; + + array_foreach_elem(&file->attachment_paths, path) T_BEGIN { + src = t_strdup_printf("%s/%s", storage->attachment_dir, path); + dest = t_strdup_printf("%s/%s", storage->attachment_dir, + sdbox_file_attachment_relpath(file, path)); + src_file = fs_file_init(storage->attachment_fs, src, + FS_OPEN_MODE_READONLY); + dest_file = fs_file_init(storage->attachment_fs, dest, + FS_OPEN_MODE_READONLY); + if (fs_rename(src_file, dest_file) < 0) { + mailbox_set_critical(&file->mbox->box, "%s", + fs_file_last_error(dest_file)); + ret = -1; + } + fs_file_deinit(&src_file); + fs_file_deinit(&dest_file); + } T_END; + return ret; +} + +int sdbox_file_assign_uid(struct sdbox_file *file, uint32_t uid) +{ + const char *p, *old_path, *dir, *new_fname, *new_path; + struct stat st; + + i_assert(file->uid == 0); + i_assert(uid != 0); + + old_path = file->file.cur_path; + p = strrchr(old_path, '/'); + i_assert(p != NULL); + dir = t_strdup_until(old_path, p); + + new_fname = t_strdup_printf(SDBOX_MAIL_FILE_FORMAT, uid); + new_path = t_strdup_printf("%s/%s", dir, new_fname); + + if (stat(new_path, &st) == 0) { + mailbox_set_critical(&file->mbox->box, + "sdbox: %s already exists, rebuilding index", new_path); + sdbox_set_mailbox_corrupted(&file->mbox->box); + return -1; + } + if (rename(old_path, new_path) < 0) { + mailbox_set_critical(&file->mbox->box, + "rename(%s, %s) failed: %m", + old_path, new_path); + return -1; + } + sdbox_file_init_paths(file, new_fname); + file->uid = uid; + + if (array_is_created(&file->attachment_paths)) { + if (sdbox_file_rename_attachments(file) < 0) + return -1; + } + return 0; +} + +static int sdbox_file_unlink_aborted_save_attachments(struct sdbox_file *file) +{ + struct dbox_storage *storage = file->file.storage; + struct fs *fs = storage->attachment_fs; + struct fs_file *fs_file; + const char *path, *att_path; + int ret = 0; + + array_foreach_elem(&file->attachment_paths, att_path) T_BEGIN { + /* we don't know if we aborted before renaming this attachment, + so try deleting both source and dest path. the source paths + point to temporary files (not to source messages' + attachment paths), so it's safe to delete them. */ + path = t_strdup_printf("%s/%s", storage->attachment_dir, + att_path); + fs_file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY); + if (fs_delete(fs_file) < 0 && + errno != ENOENT) { + mailbox_set_critical(&file->mbox->box, "%s", + fs_file_last_error(fs_file)); + ret = -1; + } + fs_file_deinit(&fs_file); + + path = t_strdup_printf("%s/%s", storage->attachment_dir, + sdbox_file_attachment_relpath(file, att_path)); + fs_file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY); + if (fs_delete(fs_file) < 0 && + errno != ENOENT) { + mailbox_set_critical(&file->mbox->box, "%s", + fs_file_last_error(fs_file)); + ret = -1; + } + fs_file_deinit(&fs_file); + } T_END; + return ret; +} + +int sdbox_file_unlink_aborted_save(struct sdbox_file *file) +{ + int ret = 0; + + if (unlink(file->file.cur_path) < 0) { + mailbox_set_critical(&file->mbox->box, + "unlink(%s) failed: %m", file->file.cur_path); + ret = -1; + } + if (array_is_created(&file->attachment_paths)) { + if (sdbox_file_unlink_aborted_save_attachments(file) < 0) + ret = -1; + } + return ret; +} + +int sdbox_file_create_fd(struct dbox_file *file, const char *path, bool parents) +{ + struct sdbox_file *sfile = (struct sdbox_file *)file; + struct mailbox *box = &sfile->mbox->box; + const struct mailbox_permissions *perm = mailbox_get_permissions(box); + const char *p, *dir; + mode_t old_mask; + int fd; + + old_mask = umask(0666 & ~perm->file_create_mode); + fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); + umask(old_mask); + if (fd == -1 && errno == ENOENT && parents && + (p = strrchr(path, '/')) != NULL) { + dir = t_strdup_until(path, p); + if (mkdir_parents_chgrp(dir, perm->dir_create_mode, + perm->file_create_gid, + perm->file_create_gid_origin) < 0 && + errno != EEXIST) { + mailbox_set_critical(box, + "mkdir_parents(%s) failed: %m", dir); + return -1; + } + /* try again */ + old_mask = umask(0666 & ~perm->file_create_mode); + fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); + umask(old_mask); + } + if (fd == -1) { + mailbox_set_critical(box, "open(%s, O_CREAT) failed: %m", path); + } else if (perm->file_create_gid == (gid_t)-1) { + /* no group change */ + } else if (fchown(fd, (uid_t)-1, perm->file_create_gid) < 0) { + if (errno == EPERM) { + mailbox_set_critical(box, "%s", + eperm_error_get_chgrp("fchown", path, + perm->file_create_gid, + perm->file_create_gid_origin)); + } else { + mailbox_set_critical(box, + "fchown(%s, -1, %ld) failed: %m", + path, (long)perm->file_create_gid); + } + /* continue anyway */ + } + return fd; +} + +int sdbox_file_move(struct dbox_file *file, bool alt_path) +{ + struct mail_storage *storage = &file->storage->storage; + struct ostream *output; + const char *dest_dir, *temp_path, *dest_path, *p; + struct stat st; + struct utimbuf ut; + bool deleted; + int out_fd, ret = 0; + + i_assert(file->input != NULL); + + if (dbox_file_is_in_alt(file) == alt_path) + return 0; + if (file->alt_path == NULL) + return 0; + + if (stat(file->cur_path, &st) < 0 && errno == ENOENT) { + /* already expunged/moved by another session */ + return 0; + } + + dest_path = !alt_path ? file->primary_path : file->alt_path; + + i_assert(dest_path != NULL); + + p = strrchr(dest_path, '/'); + i_assert(p != NULL); + dest_dir = t_strdup_until(dest_path, p); + temp_path = t_strdup_printf("%s/%s", dest_dir, + dbox_generate_tmp_filename()); + + /* first copy the file. make sure to catch every possible error + since we really don't want to break the file. */ + out_fd = file->storage->v.file_create_fd(file, temp_path, TRUE); + if (out_fd == -1) + return -1; + + output = o_stream_create_fd_file(out_fd, 0, FALSE); + i_stream_seek(file->input, 0); + o_stream_nsend_istream(output, file->input); + if (o_stream_finish(output) < 0) { + mail_storage_set_critical(storage, "write(%s) failed: %s", + temp_path, o_stream_get_error(output)); + ret = -1; + } + o_stream_unref(&output); + + if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER && ret == 0) { + if (fsync(out_fd) < 0) { + mail_storage_set_critical(storage, + "fsync(%s) failed: %m", temp_path); + ret = -1; + } + } + if (close(out_fd) < 0) { + mail_storage_set_critical(storage, + "close(%s) failed: %m", temp_path); + ret = -1; + } + if (ret < 0) { + i_unlink(temp_path); + return -1; + } + /* preserve the original atime/mtime. this isn't necessary for Dovecot, + but could be useful for external reasons. */ + ut.actime = st.st_atime; + ut.modtime = st.st_mtime; + if (utime(temp_path, &ut) < 0) { + mail_storage_set_critical(storage, + "utime(%s) failed: %m", temp_path); + } + + /* the temp file was successfully written. rename it now to the + destination file. the destination shouldn't exist, but if it does + its contents should be the same (except for maybe older metadata) */ + if (rename(temp_path, dest_path) < 0) { + mail_storage_set_critical(storage, + "rename(%s, %s) failed: %m", temp_path, dest_path); + i_unlink_if_exists(temp_path); + return -1; + } + if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER) { + if (fdatasync_path(dest_dir) < 0) { + mail_storage_set_critical(storage, + "fdatasync(%s) failed: %m", dest_dir); + i_unlink(dest_path); + return -1; + } + } + if (unlink(file->cur_path) < 0) { + dbox_file_set_syscall_error(file, "unlink()"); + if (errno == EACCES) { + /* configuration problem? revert the write */ + i_unlink(dest_path); + } + /* who knows what happened to the file. keep both just to be + sure both won't get deleted. */ + return -1; + } + + /* file was successfully moved - reopen it */ + dbox_file_close(file); + if (dbox_file_open(file, &deleted) <= 0) { + mail_storage_set_critical(storage, + "dbox_file_move(%s): reopening file failed", dest_path); + return -1; + } + return 0; +} + +static int +sdbox_unlink_attachments(struct sdbox_file *sfile, + const ARRAY_TYPE(mail_attachment_extref) *extrefs) +{ + struct dbox_storage *storage = sfile->file.storage; + const struct mail_attachment_extref *extref; + const char *path; + int ret = 0; + + array_foreach(extrefs, extref) T_BEGIN { + path = sdbox_file_attachment_relpath(sfile, extref->path); + if (index_attachment_delete(&storage->storage, + storage->attachment_fs, path) < 0) + ret = -1; + } T_END; + return ret; +} + +int sdbox_file_unlink_with_attachments(struct sdbox_file *sfile) +{ + ARRAY_TYPE(mail_attachment_extref) extrefs; + const char *extrefs_line; + pool_t pool; + int ret; + + ret = sdbox_file_get_attachments(&sfile->file, &extrefs_line); + if (ret < 0) + return -1; + if (ret == 0) { + /* no attachments */ + return dbox_file_unlink(&sfile->file); + } + + pool = pool_alloconly_create("sdbox attachments unlink", 1024); + p_array_init(&extrefs, pool, 16); + if (!index_attachment_parse_extrefs(extrefs_line, pool, &extrefs)) { + i_warning("%s: Ignoring corrupted extref: %s", + sfile->file.cur_path, extrefs_line); + array_clear(&extrefs); + } + + /* try to delete the file first, so if it fails we don't have + missing attachments */ + if ((ret = dbox_file_unlink(&sfile->file)) >= 0) + (void)sdbox_unlink_attachments(sfile, &extrefs); + pool_unref(&pool); + return ret; +} diff --git a/src/lib-storage/index/dbox-single/sdbox-file.h b/src/lib-storage/index/dbox-single/sdbox-file.h new file mode 100644 index 0000000..ba5a7f9 --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-file.h @@ -0,0 +1,43 @@ +#ifndef SDBOX_FILE_H +#define SDBOX_FILE_H + +#include "dbox-file.h" + +struct sdbox_file { + struct dbox_file file; + struct sdbox_mailbox *mbox; + + /* 0 while file is being created */ + uint32_t uid; + + /* list of attachment paths while saving/copying message */ + pool_t attachment_pool; + ARRAY_TYPE(const_string) attachment_paths; + bool written_to_disk; +}; + +struct dbox_file *sdbox_file_init(struct sdbox_mailbox *mbox, uint32_t uid); +struct dbox_file *sdbox_file_create(struct sdbox_mailbox *mbox); +void sdbox_file_free(struct dbox_file *file); + +/* Get file's extrefs metadata. */ +int sdbox_file_get_attachments(struct dbox_file *file, const char **extrefs_r); +/* Returns attachment path for this file, given the source path. The result is + always <hash>-<guid>-<mailbox_guid>-<uid>. The source path is expected to + contain <hash>-<guid>[-*]. */ +const char * +sdbox_file_attachment_relpath(struct sdbox_file *file, const char *srcpath); + +/* Assign UID for a newly created file (by renaming it) */ +int sdbox_file_assign_uid(struct sdbox_file *file, uint32_t uid); + +int sdbox_file_create_fd(struct dbox_file *file, const char *path, + bool parents); +/* Move the file to alt path or back. */ +int sdbox_file_move(struct dbox_file *file, bool alt_path); +/* Unlink file and all of its referenced attachments. */ +int sdbox_file_unlink_with_attachments(struct sdbox_file *sfile); +/* Unlink file and its attachments when rolling back a saved message. */ +int sdbox_file_unlink_aborted_save(struct sdbox_file *file); + +#endif diff --git a/src/lib-storage/index/dbox-single/sdbox-mail.c b/src/lib-storage/index/dbox-single/sdbox-mail.c new file mode 100644 index 0000000..3b0352c --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-mail.c @@ -0,0 +1,182 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "ioloop.h" +#include "istream.h" +#include "str.h" +#include "index-mail.h" +#include "dbox-mail.h" +#include "sdbox-storage.h" +#include "sdbox-file.h" + +#include <sys/stat.h> + +static void sdbox_mail_set_expunged(struct dbox_mail *mail) +{ + struct mail *_mail = &mail->imail.mail.mail; + + mail_index_refresh(_mail->box->index); + if (mail_index_is_expunged(_mail->transaction->view, _mail->seq)) { + mail_set_expunged(_mail); + return; + } + + mail_set_critical(_mail, "dbox: Unexpectedly lost uid"); + sdbox_set_mailbox_corrupted(_mail->box); +} + +static int sdbox_mail_file_set(struct dbox_mail *mail) +{ + struct mail *_mail = &mail->imail.mail.mail; + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(_mail->box); + bool deleted; + int ret; + + if (mail->open_file != NULL) { + /* already set */ + return 0; + } else if (!_mail->saving) { + mail->open_file = sdbox_file_init(mbox, _mail->uid); + return 0; + } else { + /* mail is being saved in this transaction */ + mail->open_file = + sdbox_save_file_get_file(_mail->transaction, + _mail->seq); + mail->open_file->refcount++; + + /* it doesn't have input stream yet */ + ret = dbox_file_open(mail->open_file, &deleted); + if (ret <= 0) { + mail_set_critical(_mail, + "dbox: Unexpectedly lost mail being saved"); + sdbox_set_mailbox_corrupted(_mail->box); + return -1; + } + return 1; + } +} + +static int +sdbox_mail_get_special(struct mail *_mail, enum mail_fetch_field field, + const char **value_r) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(_mail->box); + struct dbox_mail *mail = DBOX_MAIL(_mail); + struct stat st; + + switch (field) { + case MAIL_FETCH_REFCOUNT: + if (sdbox_mail_file_set(mail) < 0) + return -1; + + _mail->transaction->stats.fstat_lookup_count++; + if (dbox_file_stat(mail->open_file, &st) < 0) { + if (errno == ENOENT) + mail_set_expunged(_mail); + return -1; + } + *value_r = p_strdup_printf(mail->imail.mail.data_pool, "%lu", + (unsigned long)st.st_nlink); + return 0; + case MAIL_FETCH_REFCOUNT_ID: + if (sdbox_mail_file_set(mail) < 0) + return -1; + + _mail->transaction->stats.fstat_lookup_count++; + if (dbox_file_stat(mail->open_file, &st) < 0) { + if (errno == ENOENT) + mail_set_expunged(_mail); + return -1; + } + *value_r = p_strdup_printf(mail->imail.mail.data_pool, "%llu", + (unsigned long long)st.st_ino); + return 0; + case MAIL_FETCH_UIDL_BACKEND: + if (!dbox_header_have_flag(&mbox->box, mbox->hdr_ext_id, + offsetof(struct sdbox_index_header, flags), + DBOX_INDEX_HEADER_FLAG_HAVE_POP3_UIDLS)) { + *value_r = ""; + return 0; + } + break; + case MAIL_FETCH_POP3_ORDER: + if (!dbox_header_have_flag(&mbox->box, mbox->hdr_ext_id, + offsetof(struct sdbox_index_header, flags), + DBOX_INDEX_HEADER_FLAG_HAVE_POP3_ORDERS)) { + *value_r = ""; + return 0; + } + break; + default: + break; + } + return dbox_mail_get_special(_mail, field, value_r); +} + +int sdbox_mail_open(struct dbox_mail *mail, uoff_t *offset_r, + struct dbox_file **file_r) +{ + struct mail *_mail = &mail->imail.mail.mail; + bool deleted; + int ret; + + if (!mail_stream_access_start(_mail)) + return -1; + + ret = sdbox_mail_file_set(mail); + if (ret < 0) + return -1; + if (ret == 0) { + if (!dbox_file_is_open(mail->open_file)) + _mail->transaction->stats.open_lookup_count++; + if (dbox_file_open(mail->open_file, &deleted) <= 0) + return -1; + if (deleted) { + sdbox_mail_set_expunged(mail); + return -1; + } + } + + *file_r = mail->open_file; + *offset_r = 0; + return 0; +} + +struct mail_vfuncs sdbox_mail_vfuncs = { + dbox_mail_close, + index_mail_free, + index_mail_set_seq, + index_mail_set_uid, + index_mail_set_uid_cache_updates, + index_mail_prefetch, + index_mail_precache, + index_mail_add_temp_wanted_fields, + + index_mail_get_flags, + index_mail_get_keywords, + index_mail_get_keyword_indexes, + index_mail_get_modseq, + index_mail_get_pvt_modseq, + index_mail_get_parts, + index_mail_get_date, + dbox_mail_get_received_date, + dbox_mail_get_save_date, + dbox_mail_get_virtual_size, + dbox_mail_get_physical_size, + index_mail_get_first_header, + index_mail_get_headers, + index_mail_get_header_stream, + dbox_mail_get_stream, + index_mail_get_binary_stream, + sdbox_mail_get_special, + index_mail_get_backend_mail, + index_mail_update_flags, + index_mail_update_keywords, + index_mail_update_modseq, + index_mail_update_pvt_modseq, + NULL, + index_mail_expunge, + index_mail_set_cache_corrupted, + index_mail_opened, +}; diff --git a/src/lib-storage/index/dbox-single/sdbox-save.c b/src/lib-storage/index/dbox-single/sdbox-save.c new file mode 100644 index 0000000..03e24ab --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-save.c @@ -0,0 +1,359 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "array.h" +#include "fdatasync-path.h" +#include "hex-binary.h" +#include "hex-dec.h" +#include "str.h" +#include "istream.h" +#include "istream-crlf.h" +#include "ostream.h" +#include "write-full.h" +#include "index-mail.h" +#include "mail-copy.h" +#include "index-pop3-uidl.h" +#include "dbox-attachment.h" +#include "dbox-save.h" +#include "sdbox-storage.h" +#include "sdbox-file.h" +#include "sdbox-sync.h" + + +struct sdbox_save_context { + struct dbox_save_context ctx; + + struct sdbox_mailbox *mbox; + struct sdbox_sync_context *sync_ctx; + + struct dbox_file *cur_file; + struct dbox_file_append_context *append_ctx; + + uint32_t first_saved_seq; + ARRAY(struct dbox_file *) files; +}; + +#define SDBOX_SAVECTX(s) container_of(DBOX_SAVECTX(s), struct sdbox_save_context, ctx) + +struct dbox_file * +sdbox_save_file_get_file(struct mailbox_transaction_context *t, uint32_t seq) +{ + struct sdbox_save_context *ctx = SDBOX_SAVECTX(t->save_ctx); + struct dbox_file *const *files, *file; + unsigned int count; + + i_assert(seq >= ctx->first_saved_seq); + + files = array_get(&ctx->files, &count); + i_assert(count > 0); + i_assert(seq - ctx->first_saved_seq < count); + + file = files[seq - ctx->first_saved_seq]; + i_assert(((struct sdbox_file *)file)->written_to_disk); + return file; +} + +struct mail_save_context * +sdbox_save_alloc(struct mailbox_transaction_context *t) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(t->box); + struct sdbox_save_context *ctx; + + i_assert((t->flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0); + + if (t->save_ctx != NULL) { + /* use the existing allocated structure */ + ctx = SDBOX_SAVECTX(t->save_ctx); + ctx->cur_file = NULL; + ctx->ctx.failed = FALSE; + ctx->ctx.finished = FALSE; + ctx->ctx.dbox_output = NULL; + return &ctx->ctx.ctx; + } + + ctx = i_new(struct sdbox_save_context, 1); + ctx->ctx.ctx.transaction = t; + ctx->ctx.trans = t->itrans; + ctx->mbox = mbox; + i_array_init(&ctx->files, 32); + t->save_ctx = &ctx->ctx.ctx; + return t->save_ctx; +} + +void sdbox_save_add_file(struct mail_save_context *_ctx, struct dbox_file *file) +{ + struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx); + struct dbox_file *const *files; + unsigned int count; + + if (ctx->first_saved_seq == 0) + ctx->first_saved_seq = ctx->ctx.seq; + + files = array_get(&ctx->files, &count); + if (count > 0) { + /* a plugin may leave a previously saved file open. + we'll close it here to avoid eating too many fds. */ + dbox_file_close(files[count-1]); + } + array_push_back(&ctx->files, &file); +} + +int sdbox_save_begin(struct mail_save_context *_ctx, struct istream *input) +{ + struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx); + struct dbox_file *file; + int ret; + + file = sdbox_file_create(ctx->mbox); + ctx->append_ctx = dbox_file_append_init(file); + ret = dbox_file_get_append_stream(ctx->append_ctx, + &ctx->ctx.dbox_output); + if (ret <= 0) { + i_assert(ret != 0); + dbox_file_append_rollback(&ctx->append_ctx); + dbox_file_unref(&file); + ctx->ctx.failed = TRUE; + return -1; + } + ctx->cur_file = file; + dbox_save_begin(&ctx->ctx, input); + + sdbox_save_add_file(_ctx, file); + return ctx->ctx.failed ? -1 : 0; +} + +static int dbox_save_mail_write_metadata(struct dbox_save_context *ctx, + struct dbox_file *file) +{ + struct sdbox_file *sfile = (struct sdbox_file *)file; + const ARRAY_TYPE(mail_attachment_extref) *extrefs_arr; + const struct mail_attachment_extref *extrefs; + struct dbox_message_header dbox_msg_hdr; + uoff_t message_size; + guid_128_t guid_128; + unsigned int i, count; + + i_assert(file->msg_header_size == sizeof(dbox_msg_hdr)); + + message_size = ctx->dbox_output->offset - + file->msg_header_size - file->file_header_size; + + dbox_save_write_metadata(&ctx->ctx, ctx->dbox_output, + message_size, NULL, guid_128); + dbox_msg_header_fill(&dbox_msg_hdr, message_size); + if (o_stream_pwrite(ctx->dbox_output, &dbox_msg_hdr, + sizeof(dbox_msg_hdr), + file->file_header_size) < 0) { + dbox_file_set_syscall_error(file, "pwrite()"); + return -1; + } + sfile->written_to_disk = TRUE; + + /* remember the attachment paths until commit time */ + extrefs_arr = index_attachment_save_get_extrefs(&ctx->ctx); + if (extrefs_arr != NULL) + extrefs = array_get(extrefs_arr, &count); + else { + extrefs = NULL; + count = 0; + } + if (count > 0) { + sfile->attachment_pool = + pool_alloconly_create("sdbox attachment paths", 512); + p_array_init(&sfile->attachment_paths, + sfile->attachment_pool, count); + for (i = 0; i < count; i++) { + const char *path = p_strdup(sfile->attachment_pool, + extrefs[i].path); + array_push_back(&sfile->attachment_paths, &path); + } + } + return 0; +} + +static int dbox_save_finish_write(struct mail_save_context *_ctx) +{ + struct sdbox_save_context *ctx = (struct sdbox_save_context *)_ctx; + struct dbox_file **files; + + ctx->ctx.finished = TRUE; + if (ctx->ctx.dbox_output == NULL) + return -1; + + if (_ctx->data.save_date != (time_t)-1) { + /* we can't change ctime, but we can add the date to cache */ + struct index_mail *mail = (struct index_mail *)_ctx->dest_mail; + uint32_t t = _ctx->data.save_date; + + index_mail_cache_add(mail, MAIL_CACHE_SAVE_DATE, &t, sizeof(t)); + } + dbox_save_end(&ctx->ctx); + + files = array_back_modifiable(&ctx->files); + if (!ctx->ctx.failed) T_BEGIN { + if (dbox_save_mail_write_metadata(&ctx->ctx, *files) < 0) + ctx->ctx.failed = TRUE; + } T_END; + + if (ctx->ctx.failed) { + index_storage_save_abort_last(&ctx->ctx.ctx, ctx->ctx.seq); + dbox_file_append_rollback(&ctx->append_ctx); + dbox_file_unlink(*files); + dbox_file_unref(files); + array_pop_back(&ctx->files); + } else { + dbox_file_append_checkpoint(ctx->append_ctx); + if (dbox_file_append_commit(&ctx->append_ctx) < 0) + ctx->ctx.failed = TRUE; + dbox_file_close(*files); + } + + i_stream_unref(&ctx->ctx.input); + ctx->ctx.dbox_output = NULL; + + return ctx->ctx.failed ? -1 : 0; +} + +int sdbox_save_finish(struct mail_save_context *ctx) +{ + int ret; + + ret = dbox_save_finish_write(ctx); + index_save_context_free(ctx); + return ret; +} + +void sdbox_save_cancel(struct mail_save_context *_ctx) +{ + struct dbox_save_context *ctx = DBOX_SAVECTX(_ctx); + + ctx->failed = TRUE; + (void)sdbox_save_finish(_ctx); +} + +static int dbox_save_assign_uids(struct sdbox_save_context *ctx, + const ARRAY_TYPE(seq_range) *uids) +{ + struct dbox_file *const *files; + struct seq_range_iter iter; + unsigned int i, count, n = 0; + uint32_t uid; + bool ret; + + seq_range_array_iter_init(&iter, uids); + files = array_get(&ctx->files, &count); + for (i = 0; i < count; i++) { + struct sdbox_file *sfile = (struct sdbox_file *)files[i]; + + ret = seq_range_array_iter_nth(&iter, n++, &uid); + i_assert(ret); + if (sdbox_file_assign_uid(sfile, uid) < 0) + return -1; + if (ctx->ctx.highest_pop3_uidl_seq == i+1) { + index_pop3_uidl_set_max_uid(&ctx->mbox->box, + ctx->ctx.trans, uid); + } + } + i_assert(!seq_range_array_iter_nth(&iter, n, &uid)); + return 0; +} + +static void dbox_save_unref_files(struct sdbox_save_context *ctx) +{ + struct dbox_file **files; + unsigned int i, count; + + files = array_get_modifiable(&ctx->files, &count); + for (i = 0; i < count; i++) { + if (ctx->ctx.failed) { + struct sdbox_file *sfile = + (struct sdbox_file *)files[i]; + + (void)sdbox_file_unlink_aborted_save(sfile); + } + dbox_file_unref(&files[i]); + } + array_free(&ctx->files); +} + +int sdbox_transaction_save_commit_pre(struct mail_save_context *_ctx) +{ + struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx); + struct mailbox_transaction_context *_t = _ctx->transaction; + const struct mail_index_header *hdr; + + i_assert(ctx->ctx.finished); + + if (array_count(&ctx->files) == 0) { + /* the mail must be freed in the commit_pre() */ + return 0; + } + + if (sdbox_sync_begin(ctx->mbox, SDBOX_SYNC_FLAG_FORCE | + SDBOX_SYNC_FLAG_FSYNC, &ctx->sync_ctx) < 0) { + sdbox_transaction_save_rollback(_ctx); + return -1; + } + + /* update dbox header flags */ + dbox_save_update_header_flags(&ctx->ctx, ctx->sync_ctx->sync_view, + ctx->mbox->hdr_ext_id, offsetof(struct sdbox_index_header, flags)); + + /* assign UIDs for new messages */ + hdr = mail_index_get_header(ctx->sync_ctx->sync_view); + mail_index_append_finish_uids(ctx->ctx.trans, hdr->next_uid, + &_t->changes->saved_uids); + if (dbox_save_assign_uids(ctx, &_t->changes->saved_uids) < 0) { + sdbox_transaction_save_rollback(_ctx); + return -1; + } + + _t->changes->uid_validity = hdr->uid_validity; + return 0; +} + +void sdbox_transaction_save_commit_post(struct mail_save_context *_ctx, + struct mail_index_transaction_commit_result *result) +{ + struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx); + struct mail_storage *storage = _ctx->transaction->box->storage; + + _ctx->transaction = NULL; /* transaction is already freed */ + + if (array_count(&ctx->files) == 0) { + sdbox_transaction_save_rollback(_ctx); + return; + } + + mail_index_sync_set_commit_result(ctx->sync_ctx->index_sync_ctx, + result); + + if (sdbox_sync_finish(&ctx->sync_ctx, TRUE) < 0) + ctx->ctx.failed = TRUE; + + if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER) { + const char *box_path = mailbox_get_path(&ctx->mbox->box); + + if (fdatasync_path(box_path) < 0) { + mail_set_critical(_ctx->dest_mail, + "fdatasync_path(%s) failed: %m", box_path); + } + } + i_assert(ctx->ctx.finished); + dbox_save_unref_files(ctx); + i_free(ctx); +} + +void sdbox_transaction_save_rollback(struct mail_save_context *_ctx) +{ + struct sdbox_save_context *ctx = SDBOX_SAVECTX(_ctx); + + ctx->ctx.failed = TRUE; + if (!ctx->ctx.finished) + sdbox_save_cancel(_ctx); + dbox_save_unref_files(ctx); + + if (ctx->sync_ctx != NULL) + (void)sdbox_sync_finish(&ctx->sync_ctx, FALSE); + i_free(ctx); +} diff --git a/src/lib-storage/index/dbox-single/sdbox-storage.c b/src/lib-storage/index/dbox-single/sdbox-storage.c new file mode 100644 index 0000000..a25c325 --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-storage.c @@ -0,0 +1,535 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "fs-api.h" +#include "master-service.h" +#include "mail-index-modseq.h" +#include "mail-search-build.h" +#include "mailbox-list-private.h" +#include "index-pop3-uidl.h" +#include "dbox-mail.h" +#include "dbox-save.h" +#include "sdbox-file.h" +#include "sdbox-sync.h" +#include "sdbox-storage.h" + +extern struct mail_storage dbox_storage, sdbox_storage; +extern struct mailbox sdbox_mailbox; +extern struct dbox_storage_vfuncs sdbox_dbox_storage_vfuncs; + +static struct event_category event_category_sdbox = { + .name = "sdbox", + .parent = &event_category_storage, +}; + +static struct mail_storage *sdbox_storage_alloc(void) +{ + struct sdbox_storage *storage; + pool_t pool; + + pool = pool_alloconly_create("sdbox storage", 512+256); + storage = p_new(pool, struct sdbox_storage, 1); + storage->storage.v = sdbox_dbox_storage_vfuncs; + storage->storage.storage = sdbox_storage; + storage->storage.storage.pool = pool; + return &storage->storage.storage; +} + +static int sdbox_storage_create(struct mail_storage *_storage, + struct mail_namespace *ns, + const char **error_r) +{ + struct dbox_storage *storage = DBOX_STORAGE(_storage); + enum fs_properties props; + + if (dbox_storage_create(_storage, ns, error_r) < 0) + return -1; + + if (storage->attachment_fs != NULL) { + props = fs_get_properties(storage->attachment_fs); + if ((props & FS_PROPERTY_RENAME) == 0) { + *error_r = "mail_attachment_fs: " + "Backend doesn't support renaming"; + return -1; + } + } + return 0; +} + +static const char * +sdbox_storage_find_root_dir(const struct mail_namespace *ns) +{ + bool debug = ns->mail_set->mail_debug; + const char *home, *path; + + if (ns->owner != NULL && + mail_user_get_home(ns->owner, &home) > 0) { + path = t_strconcat(home, "/sdbox", NULL); + if (access(path, R_OK|W_OK|X_OK) == 0) { + if (debug) + i_debug("sdbox: root exists (%s)", path); + return path; + } + if (debug) + i_debug("sdbox: access(%s, rwx): failed: %m", path); + } + return NULL; +} + +static bool sdbox_storage_autodetect(const struct mail_namespace *ns, + struct mailbox_list_settings *set) +{ + bool debug = ns->mail_set->mail_debug; + struct stat st; + const char *path, *root_dir; + + if (set->root_dir != NULL) + root_dir = set->root_dir; + else { + root_dir = sdbox_storage_find_root_dir(ns); + if (root_dir == NULL) { + if (debug) + i_debug("sdbox: couldn't find root dir"); + return FALSE; + } + } + + /* NOTE: this check works for mdbox as well. we'll rely on the + autodetect ordering to catch mdbox before we get here. */ + path = t_strconcat(root_dir, "/"DBOX_MAILBOX_DIR_NAME, NULL); + if (stat(path, &st) < 0) { + if (debug) + i_debug("sdbox autodetect: stat(%s) failed: %m", path); + return FALSE; + } + + if (!S_ISDIR(st.st_mode)) { + if (debug) + i_debug("sdbox autodetect: %s not a directory", path); + return FALSE; + } + + set->root_dir = root_dir; + dbox_storage_get_list_settings(ns, set); + return TRUE; +} + +static struct mailbox * +sdbox_mailbox_alloc(struct mail_storage *storage, struct mailbox_list *list, + const char *vname, enum mailbox_flags flags) +{ + struct sdbox_mailbox *mbox; + struct index_mailbox_context *ibox; + pool_t pool; + + /* dbox can't work without index files */ + flags &= ENUM_NEGATE(MAILBOX_FLAG_NO_INDEX_FILES); + + pool = pool_alloconly_create("sdbox mailbox", 1024*3); + mbox = p_new(pool, struct sdbox_mailbox, 1); + mbox->box = sdbox_mailbox; + mbox->box.pool = pool; + mbox->box.storage = storage; + mbox->box.list = list; + mbox->box.mail_vfuncs = &sdbox_mail_vfuncs; + + index_storage_mailbox_alloc(&mbox->box, vname, flags, MAIL_INDEX_PREFIX); + + ibox = INDEX_STORAGE_CONTEXT(&mbox->box); + ibox->index_flags |= MAIL_INDEX_OPEN_FLAG_KEEP_BACKUPS | + MAIL_INDEX_OPEN_FLAG_NEVER_IN_MEMORY; + + mbox->storage = SDBOX_STORAGE(storage); + return &mbox->box; +} + +int sdbox_read_header(struct sdbox_mailbox *mbox, + struct sdbox_index_header *hdr, bool log_error, + bool *need_resize_r) +{ + struct mail_index_view *view; + const void *data; + size_t data_size; + int ret = 0; + + i_assert(mbox->box.opened); + + view = mail_index_view_open(mbox->box.index); + mail_index_get_header_ext(view, mbox->hdr_ext_id, + &data, &data_size); + if (data_size < SDBOX_INDEX_HEADER_MIN_SIZE && + (!mbox->box.creating || data_size != 0)) { + if (log_error) { + mailbox_set_critical(&mbox->box, + "sdbox: Invalid dbox header size"); + } + ret = -1; + } else { + i_zero(hdr); + memcpy(hdr, data, I_MIN(data_size, sizeof(*hdr))); + if (guid_128_is_empty(hdr->mailbox_guid)) + ret = -1; + else { + /* data is valid. remember it in case mailbox + is being reset */ + mail_index_set_ext_init_data(mbox->box.index, + mbox->hdr_ext_id, + hdr, sizeof(*hdr)); + } + } + mail_index_view_close(&view); + *need_resize_r = data_size < sizeof(*hdr); + return ret; +} + +static void sdbox_update_header(struct sdbox_mailbox *mbox, + struct mail_index_transaction *trans, + const struct mailbox_update *update) +{ + struct sdbox_index_header hdr, new_hdr; + bool need_resize; + + if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0) { + i_zero(&hdr); + need_resize = TRUE; + } + + new_hdr = hdr; + + if (update != NULL && !guid_128_is_empty(update->mailbox_guid)) { + memcpy(new_hdr.mailbox_guid, update->mailbox_guid, + sizeof(new_hdr.mailbox_guid)); + } else if (guid_128_is_empty(new_hdr.mailbox_guid)) { + guid_128_generate(new_hdr.mailbox_guid); + } + + if (need_resize) { + mail_index_ext_resize_hdr(trans, mbox->hdr_ext_id, + sizeof(new_hdr)); + } + if (memcmp(&hdr, &new_hdr, sizeof(hdr)) != 0) { + mail_index_update_header_ext(trans, mbox->hdr_ext_id, 0, + &new_hdr, sizeof(new_hdr)); + } + memcpy(mbox->mailbox_guid, new_hdr.mailbox_guid, + sizeof(mbox->mailbox_guid)); +} + +int sdbox_mailbox_create_indexes(struct mailbox *box, + const struct mailbox_update *update, + struct mail_index_transaction *trans) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box); + struct mail_index_transaction *new_trans = NULL; + const struct mail_index_header *hdr; + uint32_t uid_validity, uid_next; + + if (trans == NULL) { + new_trans = mail_index_transaction_begin(box->view, 0); + trans = new_trans; + } + + hdr = mail_index_get_header(box->view); + if (update != NULL && update->uid_validity != 0) + uid_validity = update->uid_validity; + else if (hdr->uid_validity != 0) + uid_validity = hdr->uid_validity; + else { + /* set uidvalidity */ + uid_validity = dbox_get_uidvalidity_next(box->list); + } + + if (hdr->uid_validity != uid_validity) { + mail_index_update_header(trans, + offsetof(struct mail_index_header, uid_validity), + &uid_validity, sizeof(uid_validity), TRUE); + } + if (update != NULL && hdr->next_uid < update->min_next_uid) { + uid_next = update->min_next_uid; + mail_index_update_header(trans, + offsetof(struct mail_index_header, next_uid), + &uid_next, sizeof(uid_next), TRUE); + } + if (update != NULL && update->min_first_recent_uid != 0 && + hdr->first_recent_uid < update->min_first_recent_uid) { + uint32_t first_recent_uid = update->min_first_recent_uid; + + mail_index_update_header(trans, + offsetof(struct mail_index_header, first_recent_uid), + &first_recent_uid, sizeof(first_recent_uid), FALSE); + } + if (update != NULL && update->min_highest_modseq != 0 && + mail_index_modseq_get_highest(box->view) < + update->min_highest_modseq) { + mail_index_modseq_enable(box->index); + mail_index_update_highest_modseq(trans, + update->min_highest_modseq); + } + + if (box->inbox_user && box->creating) { + /* initialize pop3-uidl header when creating mailbox + (not on mailbox_update()) */ + index_pop3_uidl_set_max_uid(box, trans, 0); + } + + sdbox_update_header(mbox, trans, update); + if (new_trans != NULL) { + if (mail_index_transaction_commit(&new_trans) < 0) { + mailbox_set_index_error(box); + return -1; + } + } + return 0; +} + +static const char * +sdbox_get_attachment_path_suffix(struct dbox_file *_file) +{ + struct sdbox_file *file = (struct sdbox_file *)_file; + + return t_strdup_printf("-%s-%u", + guid_128_to_string(file->mbox->mailbox_guid), + file->uid); +} + +void sdbox_set_mailbox_corrupted(struct mailbox *box) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box); + struct sdbox_index_header hdr; + bool need_resize; + + if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0 || + hdr.rebuild_count == 0) + mbox->corrupted_rebuild_count = 1; + else + mbox->corrupted_rebuild_count = hdr.rebuild_count; +} + +static void sdbox_set_file_corrupted(struct dbox_file *_file) +{ + struct sdbox_file *file = (struct sdbox_file *)_file; + + sdbox_set_mailbox_corrupted(&file->mbox->box); +} + +static int sdbox_mailbox_alloc_index(struct sdbox_mailbox *mbox) +{ + struct sdbox_index_header hdr; + + if (index_storage_mailbox_alloc_index(&mbox->box) < 0) + return -1; + + mbox->hdr_ext_id = + mail_index_ext_register(mbox->box.index, "dbox-hdr", + sizeof(struct sdbox_index_header), 0, 0); + /* set the initialization data in case the mailbox is created */ + i_zero(&hdr); + guid_128_generate(hdr.mailbox_guid); + mail_index_set_ext_init_data(mbox->box.index, mbox->hdr_ext_id, + &hdr, sizeof(hdr)); + return 0; +} + +static int sdbox_mailbox_open(struct mailbox *box) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box); + struct sdbox_index_header hdr; + bool need_resize; + + if (dbox_mailbox_check_existence(box) < 0) + return -1; + + if (sdbox_mailbox_alloc_index(mbox) < 0) + return -1; + + if (dbox_mailbox_open(box) < 0) + return -1; + + if (box->creating) { + /* wait for mailbox creation to initialize the index */ + return 0; + } + + /* get/generate mailbox guid */ + if (sdbox_read_header(mbox, &hdr, FALSE, &need_resize) < 0) { + /* looks like the mailbox is corrupted */ + (void)sdbox_sync(mbox, SDBOX_SYNC_FLAG_FORCE); + if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0) + i_zero(&hdr); + } + + if (guid_128_is_empty(hdr.mailbox_guid)) { + /* regenerate it */ + if (sdbox_mailbox_create_indexes(box, NULL, NULL) < 0 || + sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0) + return -1; + } + memcpy(mbox->mailbox_guid, hdr.mailbox_guid, + sizeof(mbox->mailbox_guid)); + return 0; +} + +static void sdbox_mailbox_close(struct mailbox *box) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box); + + if (mbox->corrupted_rebuild_count != 0) + (void)sdbox_sync(mbox, 0); + + dbox_mailbox_close_cleanup(box); + dbox_mailbox_close(box); +} + +static int +sdbox_mailbox_create(struct mailbox *box, + const struct mailbox_update *update, bool directory) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box); + struct sdbox_index_header hdr; + bool need_resize; + + if (dbox_mailbox_create(box, update, directory) < 0) + return -1; + if (directory || !guid_128_is_empty(mbox->mailbox_guid)) + return 0; + + /* another process just created the mailbox. read the mailbox_guid. */ + if (sdbox_read_header(mbox, &hdr, FALSE, &need_resize) < 0) { + mailbox_set_critical(box, + "sdbox: Failed to read newly created dbox header"); + return -1; + } + memcpy(mbox->mailbox_guid, hdr.mailbox_guid, + sizeof(mbox->mailbox_guid)); + i_assert(!guid_128_is_empty(mbox->mailbox_guid)); + return 0; +} + +static int +sdbox_mailbox_get_metadata(struct mailbox *box, + enum mailbox_metadata_items items, + struct mailbox_metadata *metadata_r) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box); + + if (index_mailbox_get_metadata(box, items, metadata_r) < 0) + return -1; + if ((items & MAILBOX_METADATA_GUID) != 0) { + memcpy(metadata_r->guid, mbox->mailbox_guid, + sizeof(metadata_r->guid)); + } + return 0; +} + +static int +dbox_mailbox_update(struct mailbox *box, const struct mailbox_update *update) +{ + if (!box->opened) { + if (mailbox_open(box) < 0) + return -1; + } + if (sdbox_mailbox_create_indexes(box, update, NULL) < 0) + return -1; + return index_storage_mailbox_update_common(box, update); +} + +struct mail_storage sdbox_storage = { + .name = SDBOX_STORAGE_NAME, + .class_flags = MAIL_STORAGE_CLASS_FLAG_FILE_PER_MSG | + MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_GUIDS | + MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_SAVE_GUIDS | + MAIL_STORAGE_CLASS_FLAG_BINARY_DATA, + .event_category = &event_category_sdbox, + + .v = { + NULL, + sdbox_storage_alloc, + sdbox_storage_create, + dbox_storage_destroy, + NULL, + dbox_storage_get_list_settings, + sdbox_storage_autodetect, + sdbox_mailbox_alloc, + NULL, + mail_storage_list_index_rebuild, + } +}; + +struct mail_storage dbox_storage = { + .name = "dbox", /* alias */ + .class_flags = MAIL_STORAGE_CLASS_FLAG_FILE_PER_MSG, + .event_category = &event_category_sdbox, + + .v = { + NULL, + sdbox_storage_alloc, + sdbox_storage_create, + dbox_storage_destroy, + NULL, + dbox_storage_get_list_settings, + sdbox_storage_autodetect, + sdbox_mailbox_alloc, + NULL, + mail_storage_list_index_rebuild, + } +}; + +struct mailbox sdbox_mailbox = { + .v = { + index_storage_is_readonly, + index_storage_mailbox_enable, + index_storage_mailbox_exists, + sdbox_mailbox_open, + sdbox_mailbox_close, + index_storage_mailbox_free, + sdbox_mailbox_create, + dbox_mailbox_update, + index_storage_mailbox_delete, + index_storage_mailbox_rename, + index_storage_get_status, + sdbox_mailbox_get_metadata, + index_storage_set_subscribed, + index_storage_attribute_set, + index_storage_attribute_get, + index_storage_attribute_iter_init, + index_storage_attribute_iter_next, + index_storage_attribute_iter_deinit, + index_storage_list_index_has_changed, + index_storage_list_index_update_sync, + sdbox_storage_sync_init, + index_mailbox_sync_next, + index_mailbox_sync_deinit, + NULL, + dbox_notify_changes, + index_transaction_begin, + index_transaction_commit, + index_transaction_rollback, + NULL, + dbox_mail_alloc, + index_storage_search_init, + index_storage_search_deinit, + index_storage_search_next_nonblock, + index_storage_search_next_update_seq, + index_storage_search_next_match_mail, + sdbox_save_alloc, + sdbox_save_begin, + dbox_save_continue, + sdbox_save_finish, + sdbox_save_cancel, + sdbox_copy, + sdbox_transaction_save_commit_pre, + sdbox_transaction_save_commit_post, + sdbox_transaction_save_rollback, + index_storage_is_inconsistent + } +}; + +struct dbox_storage_vfuncs sdbox_dbox_storage_vfuncs = { + sdbox_file_free, + sdbox_file_create_fd, + sdbox_mail_open, + sdbox_mailbox_create_indexes, + sdbox_get_attachment_path_suffix, + sdbox_set_mailbox_corrupted, + sdbox_set_file_corrupted +}; diff --git a/src/lib-storage/index/dbox-single/sdbox-storage.h b/src/lib-storage/index/dbox-single/sdbox-storage.h new file mode 100644 index 0000000..66d08da --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-storage.h @@ -0,0 +1,69 @@ +#ifndef SDBOX_STORAGE_H +#define SDBOX_STORAGE_H + +#include "index-storage.h" +#include "dbox-storage.h" + +#define SDBOX_STORAGE_NAME "sdbox" +#define SDBOX_MAIL_FILE_PREFIX "u." +#define SDBOX_MAIL_FILE_FORMAT SDBOX_MAIL_FILE_PREFIX"%u" + +#define SDBOX_INDEX_HEADER_MIN_SIZE (sizeof(uint32_t)) +struct sdbox_index_header { + /* increased every time a full mailbox rebuild is done */ + uint32_t rebuild_count; + guid_128_t mailbox_guid; + uint8_t flags; /* enum dbox_index_header_flags */ + uint8_t unused[3]; +}; + +struct sdbox_storage { + struct dbox_storage storage; +}; + +struct sdbox_mailbox { + struct mailbox box; + struct sdbox_storage *storage; + + uint32_t hdr_ext_id; + /* if non-zero, storage should be rebuilt (except if rebuild_count + has changed from this value) */ + uint32_t corrupted_rebuild_count; + + guid_128_t mailbox_guid; +}; + +#define SDBOX_STORAGE(s) container_of(DBOX_STORAGE(s), struct sdbox_storage, storage) +#define SDBOX_MAILBOX(s) container_of(s, struct sdbox_mailbox, box) + +extern struct mail_vfuncs sdbox_mail_vfuncs; + +int sdbox_mail_open(struct dbox_mail *mail, uoff_t *offset_r, + struct dbox_file **file_r); + +int sdbox_read_header(struct sdbox_mailbox *mbox, + struct sdbox_index_header *hdr, bool log_error, + bool *need_resize_r); +int sdbox_mailbox_create_indexes(struct mailbox *box, + const struct mailbox_update *update, + struct mail_index_transaction *trans); +void sdbox_set_mailbox_corrupted(struct mailbox *box); + +struct mail_save_context * +sdbox_save_alloc(struct mailbox_transaction_context *_t); +int sdbox_save_begin(struct mail_save_context *ctx, struct istream *input); +int sdbox_save_finish(struct mail_save_context *ctx); +void sdbox_save_cancel(struct mail_save_context *ctx); + +struct dbox_file * +sdbox_save_file_get_file(struct mailbox_transaction_context *t, uint32_t seq); +void sdbox_save_add_file(struct mail_save_context *ctx, struct dbox_file *file); + +int sdbox_transaction_save_commit_pre(struct mail_save_context *ctx); +void sdbox_transaction_save_commit_post(struct mail_save_context *ctx, + struct mail_index_transaction_commit_result *result); +void sdbox_transaction_save_rollback(struct mail_save_context *ctx); + +int sdbox_copy(struct mail_save_context *ctx, struct mail *mail); + +#endif diff --git a/src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c b/src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c new file mode 100644 index 0000000..aa4832f --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c @@ -0,0 +1,218 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "array.h" +#include "index-rebuild.h" +#include "mail-cache.h" +#include "sdbox-storage.h" +#include "sdbox-file.h" +#include "sdbox-sync.h" + +#include <dirent.h> + +static void sdbox_sync_set_uidvalidity(struct index_rebuild_context *ctx) +{ + uint32_t uid_validity; + + /* if uidvalidity is set in the old index, use it */ + uid_validity = mail_index_get_header(ctx->view)->uid_validity; + if (uid_validity == 0) + uid_validity = dbox_get_uidvalidity_next(ctx->box->list); + + mail_index_update_header(ctx->trans, + offsetof(struct mail_index_header, uid_validity), + &uid_validity, sizeof(uid_validity), TRUE); +} + +static int +sdbox_sync_add_file_index(struct index_rebuild_context *ctx, + struct dbox_file *file, uint32_t uid, bool primary) +{ + uint32_t seq; + bool deleted; + int ret; + + if ((ret = dbox_file_open(file, &deleted)) > 0) { + if (deleted) + return 0; + ret = dbox_file_seek(file, 0); + } + if (ret == 0) { + if ((ret = dbox_file_fix(file, 0)) > 0) + ret = dbox_file_seek(file, 0); + } + + if (ret <= 0) { + if (ret < 0) + return -1; + + i_warning("sdbox: Skipping unfixable file: %s", file->cur_path); + return 0; + } + + if (!dbox_file_is_in_alt(file) && !primary) { + /* we were supposed to open the file in alt storage, but it + exists in primary storage as well. skip it to avoid adding + it twice. */ + return 0; + } + + mail_index_append(ctx->trans, uid, &seq); + T_BEGIN { + index_rebuild_index_metadata(ctx, seq, uid); + } T_END; + return 0; +} + +static int +sdbox_sync_add_file(struct index_rebuild_context *ctx, + const char *fname, bool primary) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(ctx->box); + struct dbox_file *file; + uint32_t uid; + int ret; + + if (!str_begins(fname, SDBOX_MAIL_FILE_PREFIX)) + return 0; + fname += strlen(SDBOX_MAIL_FILE_PREFIX); + + if (str_to_uint32(fname, &uid) < 0 || uid == 0) { + i_warning("sdbox %s: Ignoring invalid filename %s", + mailbox_get_path(ctx->box), fname); + return 0; + } + + file = sdbox_file_init(mbox, uid); + if (!primary) + file->cur_path = file->alt_path; + ret = sdbox_sync_add_file_index(ctx, file, uid, primary); + dbox_file_unref(&file); + return ret; +} + +static int sdbox_sync_index_rebuild_dir(struct index_rebuild_context *ctx, + const char *path, bool primary) +{ + DIR *dir; + struct dirent *d; + int ret = 0; + + dir = opendir(path); + if (dir == NULL) { + if (errno == ENOENT) { + if (!primary) { + /* alt directory doesn't exist, ignore */ + return 0; + } + return index_mailbox_fix_inconsistent_existence(ctx->box, path); + } + mailbox_set_critical(ctx->box, "opendir(%s) failed: %m", path); + return -1; + } + do { + errno = 0; + if ((d = readdir(dir)) == NULL) + break; + + ret = sdbox_sync_add_file(ctx, d->d_name, primary); + } while (ret >= 0); + if (errno != 0) { + mailbox_set_critical(ctx->box, "readdir(%s) failed: %m", path); + ret = -1; + } + + if (closedir(dir) < 0) { + mailbox_set_critical(ctx->box, "closedir(%s) failed: %m", path); + ret = -1; + } + return ret; +} + +static void sdbox_sync_update_header(struct index_rebuild_context *ctx) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(ctx->box); + struct sdbox_index_header hdr; + bool need_resize; + + if (sdbox_read_header(mbox, &hdr, FALSE, &need_resize) < 0) + i_zero(&hdr); + if (guid_128_is_empty(hdr.mailbox_guid)) + guid_128_generate(hdr.mailbox_guid); + if (++hdr.rebuild_count == 0) + hdr.rebuild_count = 1; + /* mailbox is being reset. this gets written directly there */ + mail_index_set_ext_init_data(ctx->box->index, mbox->hdr_ext_id, + &hdr, sizeof(hdr)); +} + +static int +sdbox_sync_index_rebuild_singles(struct index_rebuild_context *ctx) +{ + const char *path, *alt_path; + int ret = 0; + + path = mailbox_get_path(ctx->box); + if (mailbox_get_path_to(ctx->box, MAILBOX_LIST_PATH_TYPE_ALT_MAILBOX, + &alt_path) < 0) + return -1; + + sdbox_sync_set_uidvalidity(ctx); + if (sdbox_sync_index_rebuild_dir(ctx, path, TRUE) < 0) { + mailbox_set_critical(ctx->box, "sdbox: Rebuilding failed"); + ret = -1; + } else if (alt_path != NULL) { + if (sdbox_sync_index_rebuild_dir(ctx, alt_path, FALSE) < 0) { + mailbox_set_critical(ctx->box, + "sdbox: Rebuilding failed on alt path %s", + alt_path); + ret = -1; + } + } + sdbox_sync_update_header(ctx); + return ret; +} + +int sdbox_sync_index_rebuild(struct sdbox_mailbox *mbox, bool force) +{ + struct index_rebuild_context *ctx; + struct mail_index_view *view; + struct mail_index_transaction *trans; + struct sdbox_index_header hdr; + bool need_resize; + int ret; + + if (!force && sdbox_read_header(mbox, &hdr, FALSE, &need_resize) == 0) { + if (hdr.rebuild_count != mbox->corrupted_rebuild_count && + hdr.rebuild_count != 0) { + /* already rebuilt by someone else */ + return 0; + } + } + i_warning("sdbox %s: Rebuilding index", mailbox_get_path(&mbox->box)); + + if (dbox_verify_alt_storage(mbox->box.list) < 0) { + mailbox_set_critical(&mbox->box, + "sdbox: Alt storage not mounted, " + "aborting index rebuild"); + return -1; + } + + view = mail_index_view_open(mbox->box.index); + trans = mail_index_transaction_begin(view, + MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL); + + ctx = index_index_rebuild_init(&mbox->box, view, trans); + ret = sdbox_sync_index_rebuild_singles(ctx); + index_index_rebuild_deinit(&ctx, dbox_get_uidvalidity_next); + + if (ret < 0) + mail_index_transaction_rollback(&trans); + else { + mail_index_unset_fscked(trans); + ret = mail_index_transaction_commit(&trans); + } + mail_index_view_close(&view); + mbox->corrupted_rebuild_count = 0; + return ret; +} diff --git a/src/lib-storage/index/dbox-single/sdbox-sync.c b/src/lib-storage/index/dbox-single/sdbox-sync.c new file mode 100644 index 0000000..2698ea9 --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-sync.c @@ -0,0 +1,326 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "dbox-attachment.h" +#include "sdbox-storage.h" +#include "sdbox-file.h" +#include "sdbox-sync.h" +#include "mailbox-recent-flags.h" + +#define SDBOX_REBUILD_COUNT 3 + +static void +dbox_sync_file_move_if_needed(struct dbox_file *file, + enum sdbox_sync_entry_type type) +{ + struct stat st; + bool move_to_alt = type == SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT; + bool deleted; + + if (move_to_alt == dbox_file_is_in_alt(file) && + !move_to_alt) { + /* unopened dbox files default to primary dir. + stat the file to update its location. */ + (void)dbox_file_stat(file, &st); + + } + if (move_to_alt != dbox_file_is_in_alt(file)) { + /* move the file. if it fails, nothing broke so + don't worry about it. */ + if (dbox_file_open(file, &deleted) > 0 && !deleted) + (void)sdbox_file_move(file, move_to_alt); + } +} + +static void sdbox_sync_file(struct sdbox_sync_context *ctx, + uint32_t seq, uint32_t uid, + enum sdbox_sync_entry_type type) +{ + struct dbox_file *file; + enum modify_type modify_type; + + switch (type) { + case SDBOX_SYNC_ENTRY_TYPE_EXPUNGE: + if (!mail_index_transaction_is_expunged(ctx->trans, seq)) { + mail_index_expunge(ctx->trans, seq); + array_push_back(&ctx->expunged_uids, &uid); + } + break; + case SDBOX_SYNC_ENTRY_TYPE_MOVE_FROM_ALT: + case SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT: + /* update flags in the sync transaction, mainly to make + sure that these alt changes get marked as synced + and won't be retried */ + modify_type = type == SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT ? + MODIFY_ADD : MODIFY_REMOVE; + mail_index_update_flags(ctx->trans, seq, modify_type, + (enum mail_flags)DBOX_INDEX_FLAG_ALT); + file = sdbox_file_init(ctx->mbox, uid); + dbox_sync_file_move_if_needed(file, type); + dbox_file_unref(&file); + break; + } +} + +static void sdbox_sync_add(struct sdbox_sync_context *ctx, + const struct mail_index_sync_rec *sync_rec) +{ + uint32_t uid; + enum sdbox_sync_entry_type type; + uint32_t seq, seq1, seq2; + + if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE) { + /* we're interested */ + type = SDBOX_SYNC_ENTRY_TYPE_EXPUNGE; + } else if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_FLAGS) { + /* we care only about alt flag changes */ + if ((sync_rec->add_flags & DBOX_INDEX_FLAG_ALT) != 0) + type = SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT; + else if ((sync_rec->remove_flags & DBOX_INDEX_FLAG_ALT) != 0) + type = SDBOX_SYNC_ENTRY_TYPE_MOVE_FROM_ALT; + else + return; + } else { + /* not interested */ + return; + } + + if (!mail_index_lookup_seq_range(ctx->sync_view, + sync_rec->uid1, sync_rec->uid2, + &seq1, &seq2)) { + /* already expunged everything. nothing to do. */ + return; + } + + for (seq = seq1; seq <= seq2; seq++) { + mail_index_lookup_uid(ctx->sync_view, seq, &uid); + sdbox_sync_file(ctx, seq, uid, type); + } +} + +static int sdbox_sync_index(struct sdbox_sync_context *ctx) +{ + struct mailbox *box = &ctx->mbox->box; + const struct mail_index_header *hdr; + struct mail_index_sync_rec sync_rec; + uint32_t seq1, seq2; + + hdr = mail_index_get_header(ctx->sync_view); + if (hdr->uid_validity == 0) { + /* newly created index file */ + if (hdr->next_uid == 1) { + /* could be just a race condition where we opened the + mailbox between mkdir and index creation. fix this + silently. */ + if (sdbox_mailbox_create_indexes(box, NULL, ctx->trans) < 0) + return -1; + return 1; + } + mailbox_set_critical(box, + "sdbox: Broken index: missing UIDVALIDITY"); + sdbox_set_mailbox_corrupted(box); + return 0; + } + + /* mark the newly seen messages as recent */ + if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid, + hdr->next_uid, &seq1, &seq2)) + mailbox_recent_flags_set_seqs(box, ctx->sync_view, seq1, seq2); + + while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) + sdbox_sync_add(ctx, &sync_rec); + return 1; +} + +static void dbox_sync_file_expunge(struct sdbox_sync_context *ctx, + uint32_t uid) +{ + struct mailbox *box = &ctx->mbox->box; + struct dbox_file *file; + struct sdbox_file *sfile; + int ret; + + file = sdbox_file_init(ctx->mbox, uid); + sfile = (struct sdbox_file *)file; + if (file->storage->attachment_dir != NULL) + ret = sdbox_file_unlink_with_attachments(sfile); + else + ret = dbox_file_unlink(file); + + /* do sync_notify only when the file was unlinked by us */ + if (ret > 0) + mailbox_sync_notify(box, uid, MAILBOX_SYNC_TYPE_EXPUNGE); + dbox_file_unref(&file); +} + +static void dbox_sync_expunge_files(struct sdbox_sync_context *ctx) +{ + uint32_t uid; + + /* NOTE: Index is no longer locked. Multiple processes may be unlinking + the files at the same time. */ + ctx->mbox->box.tmp_sync_view = ctx->sync_view; + array_foreach_elem(&ctx->expunged_uids, uid) T_BEGIN { + dbox_sync_file_expunge(ctx, uid); + } T_END; + mailbox_sync_notify(&ctx->mbox->box, 0, 0); + ctx->mbox->box.tmp_sync_view = NULL; +} + +static int +sdbox_refresh_header(struct sdbox_mailbox *mbox, bool retry, bool log_error) +{ + struct mail_index_view *view; + struct sdbox_index_header hdr; + bool need_resize; + int ret; + + view = mail_index_view_open(mbox->box.index); + ret = sdbox_read_header(mbox, &hdr, log_error, &need_resize); + mail_index_view_close(&view); + + if (ret < 0 && retry) { + mail_index_refresh(mbox->box.index); + return sdbox_refresh_header(mbox, FALSE, log_error); + } + return ret; +} + +int sdbox_sync_begin(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags, + struct sdbox_sync_context **ctx_r) +{ + const struct mail_index_header *hdr = + mail_index_get_header(mbox->box.view); + struct sdbox_sync_context *ctx; + enum mail_index_sync_flags sync_flags; + unsigned int i; + int ret; + bool rebuild, force_rebuild; + + force_rebuild = (flags & SDBOX_SYNC_FLAG_FORCE_REBUILD) != 0; + rebuild = force_rebuild || + (hdr->flags & MAIL_INDEX_HDR_FLAG_FSCKD) != 0 || + mbox->corrupted_rebuild_count != 0 || + sdbox_refresh_header(mbox, TRUE, FALSE) < 0; + + ctx = i_new(struct sdbox_sync_context, 1); + ctx->mbox = mbox; + ctx->flags = flags; + i_array_init(&ctx->expunged_uids, 32); + + sync_flags = index_storage_get_sync_flags(&mbox->box); + if (!rebuild && (flags & SDBOX_SYNC_FLAG_FORCE) == 0) + sync_flags |= MAIL_INDEX_SYNC_FLAG_REQUIRE_CHANGES; + if ((flags & SDBOX_SYNC_FLAG_FSYNC) != 0) + sync_flags |= MAIL_INDEX_SYNC_FLAG_FSYNC; + /* don't write unnecessary dirty flag updates */ + sync_flags |= MAIL_INDEX_SYNC_FLAG_AVOID_FLAG_UPDATES; + + for (i = 0;; i++) { + ret = index_storage_expunged_sync_begin(&mbox->box, + &ctx->index_sync_ctx, &ctx->sync_view, + &ctx->trans, sync_flags); + if (mail_index_reset_fscked(mbox->box.index)) + sdbox_set_mailbox_corrupted(&mbox->box); + if (ret <= 0) { + array_free(&ctx->expunged_uids); + i_free(ctx); + *ctx_r = NULL; + return ret; + } + + if (rebuild) + ret = 0; + else { + if ((ret = sdbox_sync_index(ctx)) > 0) + break; + } + + /* failure. keep the index locked while we're doing a + rebuild. */ + if (ret == 0) { + if (i >= SDBOX_REBUILD_COUNT) { + mailbox_set_critical(&ctx->mbox->box, + "sdbox: Index keeps breaking"); + ret = -1; + } else { + /* do a full resync and try again. */ + rebuild = FALSE; + ret = sdbox_sync_index_rebuild(mbox, + force_rebuild); + } + } + mail_index_sync_rollback(&ctx->index_sync_ctx); + if (ret < 0) { + index_storage_expunging_deinit(&ctx->mbox->box); + array_free(&ctx->expunged_uids); + i_free(ctx); + return -1; + } + } + + *ctx_r = ctx; + return 0; +} + +int sdbox_sync_finish(struct sdbox_sync_context **_ctx, bool success) +{ + struct sdbox_sync_context *ctx = *_ctx; + struct mail_storage *storage = &ctx->mbox->storage->storage.storage; + int ret = success ? 0 : -1; + + *_ctx = NULL; + + if (success) { + mail_index_view_ref(ctx->sync_view); + + if (mail_index_sync_commit(&ctx->index_sync_ctx) < 0) { + mailbox_set_index_error(&ctx->mbox->box); + ret = -1; + } else { + dbox_sync_expunge_files(ctx); + } + mail_index_view_close(&ctx->sync_view); + } else { + mail_index_sync_rollback(&ctx->index_sync_ctx); + } + + if (storage->rebuild_list_index) + ret = mail_storage_list_index_rebuild_and_set_uncorrupted(storage); + + index_storage_expunging_deinit(&ctx->mbox->box); + array_free(&ctx->expunged_uids); + i_free(ctx); + return ret; +} + +int sdbox_sync(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags) +{ + struct sdbox_sync_context *sync_ctx; + + if (sdbox_sync_begin(mbox, flags, &sync_ctx) < 0) + return -1; + + if (sync_ctx == NULL) + return 0; + return sdbox_sync_finish(&sync_ctx, TRUE); +} + +struct mailbox_sync_context * +sdbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags) +{ + struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box); + enum sdbox_sync_flags sdbox_sync_flags = 0; + int ret = 0; + + if (mail_index_reset_fscked(box->index)) + sdbox_set_mailbox_corrupted(box); + if (index_mailbox_want_full_sync(&mbox->box, flags) || + mbox->corrupted_rebuild_count != 0) { + if ((flags & MAILBOX_SYNC_FLAG_FORCE_RESYNC) != 0) + sdbox_sync_flags |= SDBOX_SYNC_FLAG_FORCE_REBUILD; + ret = sdbox_sync(mbox, sdbox_sync_flags); + } + + return index_mailbox_sync_init(box, flags, ret < 0); +} diff --git a/src/lib-storage/index/dbox-single/sdbox-sync.h b/src/lib-storage/index/dbox-single/sdbox-sync.h new file mode 100644 index 0000000..70306c9 --- /dev/null +++ b/src/lib-storage/index/dbox-single/sdbox-sync.h @@ -0,0 +1,38 @@ +#ifndef SDBOX_SYNC_H +#define SDBOX_SYNC_H + +struct mailbox; +struct sdbox_mailbox; + +enum sdbox_sync_flags { + SDBOX_SYNC_FLAG_FORCE = 0x01, + SDBOX_SYNC_FLAG_FSYNC = 0x02, + SDBOX_SYNC_FLAG_FORCE_REBUILD = 0x04 +}; + +enum sdbox_sync_entry_type { + SDBOX_SYNC_ENTRY_TYPE_EXPUNGE, + SDBOX_SYNC_ENTRY_TYPE_MOVE_FROM_ALT, + SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT +}; + +struct sdbox_sync_context { + struct sdbox_mailbox *mbox; + struct mail_index_sync_ctx *index_sync_ctx; + struct mail_index_view *sync_view; + struct mail_index_transaction *trans; + enum sdbox_sync_flags flags; + ARRAY_TYPE(uint32_t) expunged_uids; +}; + +int sdbox_sync_begin(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags, + struct sdbox_sync_context **ctx_r); +int sdbox_sync_finish(struct sdbox_sync_context **ctx, bool success); +int sdbox_sync(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags); + +int sdbox_sync_index_rebuild(struct sdbox_mailbox *mbox, bool force); + +struct mailbox_sync_context * +sdbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags); + +#endif |