bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen#include "lib.h"
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen#include "fdatasync-path.h"
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen#include <fcntl.h>
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen#include <unistd.h>
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainenint fdatasync_path(const char *path)
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen{
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen int fd, ret = 0;
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen /* Directories need to be opened as read-only.
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen fsync() doesn't appear to care about it. */
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen fd = open(path, O_RDONLY);
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen if (fd == -1)
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen return -1;
79120460f7125f6de695f50d5fabfefd1fc529c5Timo Sirainen if (fdatasync(fd) < 0) {
211c638d81d382517d196ad47565e0d85012c927klemens /* Some OSes/FSes don't allow fsyncing directories. Silently
847db5fdd70f77ba19d887e64a0e91a8306853ceTimo Sirainen ignore the problem. */
79120460f7125f6de695f50d5fabfefd1fc529c5Timo Sirainen if (errno == EBADF) {
847db5fdd70f77ba19d887e64a0e91a8306853ceTimo Sirainen /* e.g. NetBSD */
847db5fdd70f77ba19d887e64a0e91a8306853ceTimo Sirainen } else if (errno == EINVAL) {
847db5fdd70f77ba19d887e64a0e91a8306853ceTimo Sirainen /* e.g. Linux+CIFS */
79120460f7125f6de695f50d5fabfefd1fc529c5Timo Sirainen } else {
79120460f7125f6de695f50d5fabfefd1fc529c5Timo Sirainen ret = -1;
79120460f7125f6de695f50d5fabfefd1fc529c5Timo Sirainen }
79120460f7125f6de695f50d5fabfefd1fc529c5Timo Sirainen }
4307c886579381dbb1897ea1388ae6978c96f560Timo Sirainen i_close_fd(&fd);
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen return ret;
bb41db071dcbc51a6f170d7f44afb74adb00f89aTimo Sirainen}