cpw.nader.asynclogging-3: c7898c7a: Removed the PurpleStingref member from P...

morshed.nader at gmail.com morshed.nader at gmail.com
Wed Jan 12 02:48:08 EST 2011


----------------------------------------------------------------------
Revision: c7898c7a1552dafee45b320b0dce5abc03a00c48
Parent:   1cd18905a16f75ffabad7d206b627b85002a9f68
Author:   morshed.nader at gmail.com
Date:     01/12/11 02:28:42
Branch:   im.pidgin.cpw.nader.asynclogging-3
URL: http://d.pidgin.im/viewmtn/revision/info/c7898c7a1552dafee45b320b0dce5abc03a00c48

Changelog: 

Removed the PurpleStingref member from PurpleOldLogPrivate and replaced it with a GFile
Attempted to clean up purple_old_log_list a little, needs work though

Changes against parent 1cd18905a16f75ffabad7d206b627b85002a9f68

  patched  libpurple/log.c
  patched  libpurple/oldlog.c

-------------- next part --------------
============================================================
--- libpurple/log.c	736f1a0f941f986bc5068e5dd2676e7d176d7208
+++ libpurple/log.c	372236f6c30cb002c6734b0ae933924002083cce
@@ -642,7 +642,7 @@ write_update_size_cache(PurpleLog *log, 
 	lu->account = purple_log_get_account(log);
 
 	G_LOCK(logsize_users);
-	if(g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) {
+	if (g_hash_table_lookup_extended(logsize_users, lu, NULL, &ptrsize)) {
 		gchar *tmp = lu->name;
 
 		total = GPOINTER_TO_INT(ptrsize);
@@ -659,7 +659,7 @@ write_update_size_cache(PurpleLog *log, 
 	G_UNLOCK(logsize_users);
 
 	G_LOCK(logsize_users_decayed);
-	if(g_hash_table_lookup_extended(logsize_users_decayed, lu, NULL, &ptrsize)) {
+	if (g_hash_table_lookup_extended(logsize_users_decayed, lu, NULL, &ptrsize)) {
 		total = GPOINTER_TO_INT(ptrsize);
 		total += size;
 		g_hash_table_replace(logsize_users_decayed, lu, GINT_TO_POINTER(total));
============================================================
--- libpurple/oldlog.c	c67947f9736e88df27358dd719f50350324b9898
+++ libpurple/oldlog.c	b4e7ccc34a95ebc34031b35c377037c3dd53d31d
@@ -35,7 +35,7 @@ struct _PurpleOldLogPrivate {
 typedef struct _PurpleOldLogPrivate PurpleOldLogPrivate;
 
 struct _PurpleOldLogPrivate {
-	PurpleStringref *pathref;
+	GFile *file;
 	gssize offset;
 	gssize length;
 };
@@ -77,76 +77,135 @@ purple_old_log_init(PurpleOldLog *old_lo
 {
 }
 
+// Needs testing!!!
 static GList *
 purple_old_log_list(PurpleLog *log, PurpleLogChatType chat_type, const gchar *sn, PurpleAccount *account, GCancellable *cancellable, GError **error)
 {
-	PurpleStringref *pathref;
+	GDataInputStream *data_stream;
+	GError *err = NULL;
+	GFile *file, *file_idx;
+	GFileInfo *info;
+	GFileInputStream *file_stream;
 	GList *list = NULL;
-	FILE *index, *file;
-	time_t log_last_modified, lasttime = 0;
-	struct stat st;
+	guint64 log_last_modified, idx_last_modified;
+	time_t lasttime = 0;
 	struct tm tm;
-	gchar *logfile, *pathstr, *index_tmp, buf[BUF_LONG], month[4], convostart[32], *temp;
-	gint index_fd, logfound = 0, lastoff = 0, newlen, length, offset;
+	gchar *buffer, *logfile, *pathstr, month[4], convostart[32];
+	gssize lastoff = 0, length, newlen, offset = 0;
+	gsize read;
+	gboolean log_found = FALSE;
 	gulong idx_time;
 
+	FILE *index;
+	gchar *index_tmp;
+	gint index_fd;
+
 	logfile = g_strdup_printf("%s.log", purple_normalize(account, sn));
 	pathstr = g_build_filename(purple_user_dir(), "logs", logfile, NULL);
-	pathref = purple_stringref_new(pathstr);
 	g_free(logfile);
 
-	if (g_stat(purple_stringref_value(pathref), &st)) {
-		purple_stringref_unref(pathref);
+	file = g_file_new_for_path(pathstr);
+	info = g_file_query_info(file, G_FILE_ATTRIBUTE_TIME_MODIFIED,
+		G_FILE_QUERY_INFO_NONE, cancellable, error);
+
+	if (info == NULL) {
+		g_object_unref(file);
 		g_free(pathstr);
 
 		return NULL;
-	} else
-		log_last_modified = st.st_mtime;
+	}
 
-	/* Change the .log extension to .idx */
+	log_last_modified = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
+	g_object_unref(info);
+
+	/* Check for the index file, which would save us from having to parse the log itself */
 	strcpy(pathstr + strlen(pathstr) - 3, "idx");
+	file_idx = g_file_new_for_path(pathstr);
 
-	if (g_stat(pathstr, &st) == 0) {
-		if (st.st_mtime < log_last_modified) {
-			purple_debug_warning("log", "Index \"%s\" exists, but is older than the log.\n", pathstr);
-		} else {
-			/* The index file exists and is at least as new as the log, so open it. */
-			if (!(index = g_fopen(pathstr, "rb")))
-			{
-				purple_debug_error("log", "Failed to open index file \"%s\" for reading: %s\n",
-					pathstr, g_strerror(errno));
+	info = g_file_query_info(file, G_FILE_ATTRIBUTE_TIME_MODIFIED,
+		G_FILE_QUERY_INFO_NONE, cancellable, &err);
 
-				/* Fall through so that we'll parse the log file. */
-			} else {
-				purple_debug_info("log", "Using index: %s\n", pathstr);
+	if (info == NULL) {
+		/* No index file, but that's ok as long as it's just not missing */
+		g_object_unref(file_idx);
+
+		if (err->code != G_IO_ERROR_NOT_FOUND) {
+			g_propagate_error(error, err);
+			g_object_unref(file);
+			g_free(pathstr);
+
+			return NULL;
+		}
+
+		g_clear_error(&err);
+	} else {
+		idx_last_modified = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
+		g_object_unref(info);
+
+		if (idx_last_modified >= log_last_modified) {
+			GDataInputStream *data_stream;
+			GFileInputStream *file_stream;
+
+			file_stream = g_file_read(file_idx, cancellable, error);
+
+			if (file_stream == NULL) {
+				g_object_unref(file_idx);
+				g_object_unref(file);
 				g_free(pathstr);
 
-				while (fgets(buf, BUF_LONG, index)) {
-					if (sscanf(buf, "%d\t%d\t%lu", &lastoff, &newlen, &idx_time) == 3) {
-						PurpleLog *log = purple_log_new(PURPLE_TYPE_OLD_LOG, PURPLE_LOG_IM, sn,
-							account, NULL, idx_time, NULL);
-						PurpleOldLogPrivate *priv = PURPLE_OLD_LOG_GET_PRIVATE(log);
+				return NULL;
+			}
 
-						priv->pathref = purple_stringref_ref(pathref);
-						priv->offset = lastoff;
-						priv->length = newlen;
+			data_stream = g_data_input_stream_new(G_INPUT_STREAM(file_stream));
+			g_free(pathstr);
 
-						list = g_list_prepend(list, log);
-					}
+			while ((buffer = g_data_input_stream_read_line(data_stream, NULL,
+				cancellable, &err)) != NULL)
+			{
+				if (sscanf(buffer, "%d\t%d\t%lu", &lastoff, &newlen, &idx_time) == 3) {
+					PurpleLog *log;
+					PurpleOldLogPrivate *priv;
+
+					log = purple_old_log_new(PURPLE_LOG_IM,
+						sn, account, NULL, idx_time, NULL);
+
+					priv = PURPLE_OLD_LOG_GET_PRIVATE(log);
+					priv->file = g_object_ref(file);
+					priv->offset = lastoff;
+					priv->length = newlen;
+
+					list = g_list_prepend(list, log);
 				}
 
-				fclose(index);
-				purple_stringref_unref(pathref);
+				g_free(buffer);
+			}
 
-				return list;
+			/* Make sure the last read didn't have an issue */
+			if (err != NULL) {
+				g_propagate_error(error, err);
+				g_list_foreach(list, (GFunc) g_object_unref, NULL);
+				g_list_free(list);
+				list = NULL;
 			}
+
+			g_object_unref(data_stream);
+			g_object_unref(file_stream);
+			g_object_unref(file_idx);
+			g_object_unref(file);
+			g_free(pathstr);
+
+			return list;
 		}
+
+		// purple_debug_warning("log", "Index \"%s\" exists, but is older than the log.\n", pathstr);
+		g_object_unref(file_idx);
 	}
 
-	if (!(file = g_fopen(purple_stringref_value(pathref), "rb"))) {
-		purple_debug_error("log", "Failed to open log file \"%s\" for reading: %s\n",
-			purple_stringref_value(pathref), g_strerror(errno));
-		purple_stringref_unref(pathref);
+	/* No index file, fun fun! */
+	file_stream = g_file_read(file, cancellable, error);
+
+	if (file_stream == NULL) {
+		g_object_unref(file);
 		g_free(pathstr);
 
 		return NULL;
@@ -155,8 +214,8 @@ purple_old_log_list(PurpleLog *log, Purp
 	index_tmp = g_strdup_printf("%s.XXXXXX", pathstr);
 
 	if ((index_fd = g_mkstemp(index_tmp)) == -1) {
-		purple_debug_error("log", "Failed to open index temp file: %s\n",
-			g_strerror(errno));
+		// purple_debug_error("log", "Failed to open index temp file: %s\n",
+			// g_strerror(errno));
 
 		g_free(pathstr);
 		g_free(index_tmp);
@@ -165,8 +224,8 @@ purple_old_log_list(PurpleLog *log, Purp
 		index = fdopen(index_fd, "wb");
 
 		if (index == NULL) {
-			purple_debug_error("log", "Failed to fdopen() index temp file: %s\n",
-				g_strerror(errno));
+			// purple_debug_error("log", "Failed to fdopen() index temp file: %s\n",
+				// g_strerror(errno));
 			close(index_fd);
 
 			if (index_tmp != NULL) {
@@ -178,30 +237,33 @@ purple_old_log_list(PurpleLog *log, Purp
 		}
 	}
 
-	while (fgets(buf, BUF_LONG, file)) {
-		if (strstr(buf, "---- New C") != NULL) {
-			temp = strchr(buf, '@');
+	data_stream = g_data_input_stream_new(G_INPUT_STREAM(file_stream));
 
+	while ((buffer = g_data_input_stream_read_line(data_stream, &read,
+		cancellable, &err)) != NULL)
+	{
+		offset += read;
+
+		if (strstr(buffer, "---- New C") != NULL) {
+			gchar *temp = strchr(buffer, '@');
+
 			if (temp == NULL || strlen(temp) < 2)
 				continue;
 
 			temp++;
-			length = strcspn(temp, "-");
-			if (length > 31) length = 31;
+			length = MIN(strcspn(temp, "-"), 31);
 
-			offset = ftell(file);
-
-			if (logfound) {
+			if (log_found) {
 				newlen = offset - lastoff - length;
-				if(strstr(buf, "----</H3><BR>")) {
+
+				if (strstr(buffer, "----</H3><BR>"))
 					newlen -=
-						sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ ") +
-						sizeof("----</H3><BR>") - 2;
-				} else {
-					newlen -= sizeof("---- New Conversation @ ") + sizeof("----") - 2;
-				}
+						sizeof("<HR><BR><H3 Align=Center> ---- New Conversation @ "
+							"----</H3><BR>") - 2;
+				else
+					newlen -= sizeof("---- New Conversation @ ----") - 2;
 
-				if(strchr(buf, '\r'))
+				if (strchr(buffer, '\r'))
 					newlen--;
 
 				if (newlen != 0) {
@@ -209,7 +271,7 @@ purple_old_log_list(PurpleLog *log, Purp
 						account, NULL, lasttime, NULL);
 					PurpleOldLogPrivate *priv = PURPLE_OLD_LOG_GET_PRIVATE(log);
 
-					priv->pathref = purple_stringref_ref(pathref);
+					priv->file = g_object_ref(file);
 					priv->offset = lastoff;
 					priv->length = newlen;
 
@@ -222,7 +284,7 @@ purple_old_log_list(PurpleLog *log, Purp
 				}
 			}
 
-			logfound = 1;
+			log_found = TRUE;
 			lastoff = offset;
 
 			g_snprintf(convostart, length, "%s", temp);
@@ -256,18 +318,19 @@ purple_old_log_list(PurpleLog *log, Purp
 			} else if (purple_strequal(month, "Dec")) {
 				tm.tm_mon = 11;
 			}
+
 			tm.tm_year -= 1900;
 			lasttime = mktime(&tm);
 		}
 	}
 
-	if (logfound) {
-		if ((newlen = ftell(file) - lastoff) != 0) {
+	if (log_found) {
+		if ((newlen = offset - lastoff) != 0) {
 			PurpleLog *log = purple_log_new(PURPLE_TYPE_OLD_LOG, PURPLE_LOG_IM, sn,
 				account, NULL, lasttime, NULL);
 			PurpleOldLogPrivate *priv = PURPLE_OLD_LOG_GET_PRIVATE(log);
 
-			priv->pathref = purple_stringref_ref(pathref);
+			priv->file = g_object_ref(file);
 			priv->offset = lastoff;
 			priv->length = newlen;
 
@@ -280,9 +343,6 @@ purple_old_log_list(PurpleLog *log, Purp
 		}
 	}
 
-	purple_stringref_unref(pathref);
-	fclose(file);
-
 	if (index != NULL) {
 		fclose(index);
 
@@ -292,16 +352,19 @@ purple_old_log_list(PurpleLog *log, Purp
 		}
 
 		if (g_rename(index_tmp, pathstr)) {
-			purple_debug_warning("log", "Failed to rename index temp file \"%s\" to \"%s\": %s\n",
-				index_tmp, pathstr, g_strerror(errno));
+			// purple_debug_warning("log", "Failed to rename index temp file \"%s\" to \"%s\": %s\n",
+				// index_tmp, pathstr, g_strerror(errno));
 			g_unlink(index_tmp);
-		} else
-			purple_debug_info("log", "Built index: %s\n", pathstr);
+		} else {
+			; // purple_debug_info("log", "Built index: %s\n", pathstr);
+		}
 
 		g_free(index_tmp);
-		g_free(pathstr);
 	}
 
+	g_object_unref(file);
+	g_free(pathstr);
+
 	return list;
 }
 
@@ -344,28 +407,20 @@ purple_old_log_read(PurpleLog *log, Purp
 purple_old_log_read(PurpleLog *log, PurpleLogReadFlags *flags, GCancellable *cancellable, GError **error)
 {
 	PurpleOldLogPrivate *priv = PURPLE_OLD_LOG_GET_PRIVATE(log);
-	const gchar *path;
-	GFile *file;
 	GFileInputStream *file_stream;
 	GInputStream *stream;
 	gchar *read;
 	gssize result;
 
-	path = purple_stringref_value(priv->pathref);
-	file = g_file_new_for_path(path);
-	file_stream = g_file_read(file, cancellable, error);
+	file_stream = g_file_read(priv->file, cancellable, error);
 
-	if (file_stream == NULL) {
-		g_object_unref(file);
-
+	if (file_stream == NULL)
 		return NULL;
-	}
 
 	stream = G_INPUT_STREAM(file_stream);
 	result = g_input_stream_skip(stream, priv->offset, cancellable, error);
 
 	if (result < 0) {
-		g_object_unref(file);
 		g_object_unref(stream);
 
 		return NULL;
@@ -376,13 +431,11 @@ purple_old_log_read(PurpleLog *log, Purp
 
 	if (result < 0) {
 		g_free(read);
-		g_object_unref(file);
 		g_object_unref(stream);
 
 		return NULL;
 	}
 
-	g_object_unref(file);
 	g_object_unref(stream);
 
 	read[priv->length] = '\0';
@@ -526,7 +579,7 @@ purple_old_log_finalize(GObject *object)
 	PurpleLog *log = PURPLE_LOG(object);
 	PurpleOldLogPrivate *priv = PURPLE_OLD_LOG_GET_PRIVATE(log);
 
-	purple_stringref_unref(priv->pathref);
+	g_object_unref(priv->file);
 
 	G_OBJECT_CLASS(purple_old_log_parent_class)->finalize(object);
 }


More information about the Commits mailing list