im.pidgin.pidgin: 8f4d756b61b5688ce770c469c3a7095dbe6c271d

elb at pidgin.im elb at pidgin.im
Thu Nov 8 15:05:48 EST 2007


-----------------------------------------------------------------
Revision: 8f4d756b61b5688ce770c469c3a7095dbe6c271d
Ancestor: 1f57ebe4e6d16159c74db823ecff2ec0f4c46936
Author: elb at pidgin.im
Date: 2007-11-08T19:49:12
Branch: im.pidgin.pidgin

Modified files:
        configure.ac libpurple/util.c

ChangeLog: 

Manually fflush() files written with purple_util_write_data_to_file,
because apparently some filesystems (XFS) can and will leave bogus
file contents if we don't.  For systems which don't have fileno(),
this involves closing and reopening the file.

-------------- next part --------------
============================================================
--- configure.ac	04fa2ed04348a0a3fd118a1f3626f03684302aa2
+++ configure.ac	6f627664714dbc8f3f1e544fa0a0ee7a86be09fa
@@ -233,6 +233,30 @@ AC_CHECK_FUNC(dlopen, LIBDL="", [AC_CHEC
 dnl FreeBSD doesn't have libdl, dlopen is provided by libc
 AC_CHECK_FUNC(dlopen, LIBDL="", [AC_CHECK_LIB(dl, dlopen, LIBDL="-ldl")])
 
+AC_MSG_CHECKING(for fileno())
+AC_TRY_RUN([
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+	int fd;
+
+	fd = fileno(stdout);
+
+	return !(fd > 0);
+}
+], [
+	AC_MSG_RESULT(yes)
+	AC_DEFINE([HAVE_FILENO], [1],
+	          [Define to 1 if your stdio has int fileno(FILE *).])
+], [
+	AC_MSG_RESULT(no)
+], [
+	# Fallback for Cross Compiling...
+	# This will enable the compatibility code.
+	AC_MSG_RESULT(no)
+])
+
 AC_MSG_CHECKING(for the %z format string in strftime())
 AC_TRY_RUN([
 #ifdef HAVE_SYS_TIME_H
============================================================
--- libpurple/util.c	383cfed34f7becdc1014101f5ecf413ba53378f8
+++ libpurple/util.c	4abf7c727e91c922c3279113da230950ffbb9d24
@@ -2561,6 +2561,9 @@ purple_util_write_data_to_file_absolute(
 	FILE *file;
 	size_t real_size, byteswritten;
 	struct stat st;
+#ifndef HAVE_FILENO
+	int fd;
+#endif
 
 	purple_debug_info("util", "Writing file %s\n",
 					filename_full);
@@ -2595,6 +2598,19 @@ purple_util_write_data_to_file_absolute(
 	real_size = (size == -1) ? strlen(data) : (size_t) size;
 	byteswritten = fwrite(data, 1, real_size, file);
 
+#ifdef HAVE_FILENO
+	/* Apparently XFS (and possibly other filesystems) do not
+	 * guarantee that file data is flushed before file metadata,
+	 * so this procedure is insufficient without some flushage. */
+	if (fsync(fileno(file)) < 0) {
+		purple_debug_error("util", "Error syncing file contents for %s: %s\n",
+				   filename_temp, g_strerror(errno));
+		g_free(filename_temp);
+		fclose(file);
+		return FALSE;
+	}
+#endif
+    
 	/* Close file */
 	if (fclose(file) != 0)
 	{
@@ -2604,6 +2620,30 @@ purple_util_write_data_to_file_absolute(
 		return FALSE;
 	}
 
+#ifndef HAVE_FILENO
+	/* This is the same effect (we hope) as the HAVE_FILENO block
+	 * above, but for systems without fileno(). */
+	if ((fd = open(filename_temp, O_RDWR)) < 0) {
+		purple_debug_error("util", "Error opening file %s for flush: %s\n",
+				   filename_temp, g_strerror(errno));
+		g_free(filename_temp);
+		return FALSE;
+	}
+	if (fsync(fd) < 0) {
+		purple_debug_error("util", "Error syncing %s: %s\n",
+				   filename_temp, g_strerror(errno));
+		g_free(filename_temp);
+		close(fd);
+		return FALSE;
+	}
+	if (close(fd) < 0) {
+		purple_debug_error("util", "Error closing %s after sync: %s\n",
+				   filename_temp, g_strerror(errno));
+		g_free(filename_temp);
+		return FALSE;
+	}
+#endif
+
 	/* Ensure the file is the correct size */
 	if (byteswritten != real_size)
 	{


More information about the Commits mailing list