diff libpurple/util.c @ 21284:6de09629f091

propagate from branch 'im.pidgin.pidgin.next.minor' (head 0c9637482b845cc65e95a26e144697391c51133f) to branch 'im.pidgin.pidgin' (head e3a6991e78dac328f13804950fee54dfb8afc3c5)
author Ka-Hing Cheung <khc@hxbc.us>
date Sat, 10 Nov 2007 04:52:20 +0000
parents b3c6d817d68d 4a0f841e6f26
children 39231317310a
line wrap: on
line diff
--- a/libpurple/util.c	Sat Nov 10 01:18:15 2007 +0000
+++ b/libpurple/util.c	Sat Nov 10 04:52:20 2007 +0000
@@ -1537,8 +1537,8 @@
 							plain = g_string_append(plain, alt->str);
 						if(!src && xhtml)
 							xhtml = g_string_append(xhtml, alt->str);
+						g_string_free(alt, TRUE);
 					}
-					g_string_free(alt, TRUE);
 					g_string_free(src, TRUE);
 					continue;
 				}
@@ -1571,7 +1571,7 @@
 					pt->dest_tag = "a";
 					tags = g_list_prepend(tags, pt);
 					if(xhtml)
-						g_string_append_printf(xhtml, "<a href='%s'>", g_strstrip(url->str));
+						g_string_append_printf(xhtml, "<a href='%s'>", url ? g_strstrip(url->str) : "");
 					continue;
 				}
 				if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
@@ -1664,7 +1664,7 @@
 					pt->src_tag = "font";
 					pt->dest_tag = "span";
 					tags = g_list_prepend(tags, pt);
-					if(style->len)
+					if(style->len && xhtml)
 						g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str));
 					else
 						pt->ignore = TRUE;
@@ -2531,7 +2531,7 @@
 		}
 
 		if (g_mkdir(dir, mode) < 0) {
-			purple_debug_warning("build_dir", "mkdir: %s\n", strerror(errno));
+			purple_debug_warning("build_dir", "mkdir: %s\n", g_strerror(errno));
 			g_strfreev(components);
 			g_free(dir);
 			return -1;
@@ -2567,7 +2567,7 @@
 		if (g_mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
 		{
 			purple_debug_error("util", "Error creating directory %s: %s\n",
-							 user_dir, strerror(errno));
+							 user_dir, g_strerror(errno));
 			return FALSE;
 		}
 	}
@@ -2587,6 +2587,9 @@
 	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);
@@ -2602,7 +2605,7 @@
 		{
 			purple_debug_error("util", "Error removing old file "
 					   "%s: %s\n",
-					   filename_temp, strerror(errno));
+					   filename_temp, g_strerror(errno));
 		}
 	}
 
@@ -2612,7 +2615,7 @@
 	{
 		purple_debug_error("util", "Error opening file %s for "
 				   "writing: %s\n",
-				   filename_temp, strerror(errno));
+				   filename_temp, g_strerror(errno));
 		g_free(filename_temp);
 		return FALSE;
 	}
@@ -2621,14 +2624,58 @@
 	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 (fflush(file) < 0) {
+		purple_debug_error("util", "Error flushing %s: %s\n",
+				   filename_temp, g_strerror(errno));
+		g_free(filename_temp);
+		fclose(file);
+		return FALSE;
+	}
+	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)
 	{
 		purple_debug_error("util", "Error closing file %s: %s\n",
-				   filename_temp, strerror(errno));
+				   filename_temp, g_strerror(errno));
+		g_free(filename_temp);
+		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)
@@ -2657,7 +2704,7 @@
 	if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1)
 	{
 		purple_debug_error("util", "Error setting permissions of file %s: %s\n",
-						 filename_temp, strerror(errno));
+						 filename_temp, g_strerror(errno));
 	}
 #endif
 
@@ -2666,7 +2713,7 @@
 	{
 		purple_debug_error("util", "Error renaming %s to %s: %s\n",
 				   filename_temp, filename_full,
-				   strerror(errno));
+				   g_strerror(errno));
 	}
 
 	g_free(filename_temp);
@@ -3093,9 +3140,6 @@
 		ret[j++] = text[i];
 	}
 
-	purple_debug_misc("purple_str_add_cr", "got: %s, leaving with %s\n",
-					text, ret);
-
 	return ret;
 }
 
@@ -3225,7 +3269,7 @@
 char *
 purple_str_size_to_units(size_t size)
 {
-	static const char *size_str[4] = { "bytes", "KiB", "MiB", "GiB" };
+	static const char * const size_str[] = { "bytes", "KiB", "MiB", "GiB" };
 	float size_mag;
 	int size_index = 0;
 
@@ -3418,11 +3462,11 @@
 	char host[256], path[256], user[256], passwd[256];
 	int port = 0;
 	/* hyphen at end includes it in control set */
-	static char addr_ctrl[] = "A-Za-z0-9.-";
-	static char port_ctrl[] = "0-9";
-	static char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-";
-	static char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
-	static char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
+	static const char addr_ctrl[] = "A-Za-z0-9.-";
+	static const char port_ctrl[] = "0-9";
+	static const char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-";
+	static const char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
+	static const char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
 
 	g_return_val_if_fail(url != NULL, FALSE);
 
@@ -3519,7 +3563,7 @@
 	gboolean full;
 	int len;
 
-	if ((s = g_strstr_len(data, data_len, "Location: ")) == NULL)
+	if ((s = g_strstr_len(data, data_len, "\nLocation: ")) == NULL)
 		/* We're not being redirected */
 		return FALSE;
 
@@ -3717,7 +3761,7 @@
 					if(new_data == NULL) {
 						purple_debug_error("util",
 								"Failed to allocate %u bytes: %s\n",
-								content_len, strerror(errno));
+								content_len, g_strerror(errno));
 						purple_util_fetch_url_error(gfud,
 								_("Unable to allocate enough memory to hold "
 								  "the contents from %s.  The web server may "
@@ -3755,7 +3799,7 @@
 			return;
 		} else {
 			purple_util_fetch_url_error(gfud, _("Error reading from %s: %s"),
-					gfud->website.address, strerror(errno));
+					gfud->website.address, g_strerror(errno));
 			return;
 		}
 	}
@@ -3786,7 +3830,7 @@
 		return;
 	else if (len < 0) {
 		purple_util_fetch_url_error(gfud, _("Error writing to %s: %s"),
-				gfud->website.address, strerror(errno));
+				gfud->website.address, g_strerror(errno));
 		return;
 	}
 	gfud->request_written += len;