tracker r2910 - in trunk: . src/tracker-extract



Author: mr
Date: Tue Feb 10 14:10:28 2009
New Revision: 2910
URL: http://svn.gnome.org/viewvc/tracker?rev=2910&view=rev

Log:
	* src/tracker-extract/tracker-extract.c: Check file size is
	non-zero before trying to extract metadata from it. This saves us
	time and warnings in the log.

	* src/tracker-extract/tracker-main.c: Added GOption based command
	line argument handling. Also added -m for mime -f for filename and
	-v to set the verbosity to something other than the default config.


Modified:
   trunk/ChangeLog
   trunk/src/tracker-extract/tracker-extract.c
   trunk/src/tracker-extract/tracker-main.c

Modified: trunk/src/tracker-extract/tracker-extract.c
==============================================================================
--- trunk/src/tracker-extract/tracker-extract.c	(original)
+++ trunk/src/tracker-extract/tracker-extract.c	Tue Feb 10 14:10:28 2009
@@ -195,81 +195,114 @@
 		   const gchar    *mime)
 {
 	GHashTable *values;
+	GFile *file;
+	GFileInfo *info;
+	GError *error = NULL;
+	const gchar *attributes = NULL;
 	gchar *path_in_locale;
 	gchar *path_used;
 	gchar *mime_used = NULL;
-
+	goffset size = 0;
+		
 	path_used = g_strdup (path);
 	g_strstrip (path_used);
 
 	path_in_locale = g_filename_from_utf8 (path_used, -1, NULL, NULL, NULL);
+	g_free (path_used);
 
 	if (!path_in_locale) {
-		g_warning ("Could not convert path:'%s' from UTF-8 to locale", path_used);
+		g_warning ("Could not convert path from UTF-8 to locale");
 		g_free (path_used);
 		return NULL;
 	}
 
-	if (!g_file_test (path_in_locale, G_FILE_TEST_EXISTS)) {
+	file = g_file_new_for_path (path_in_locale);
+	if (!file) {
+		g_warning ("Could not create GFile for path:'%s'",
+			   path_in_locale);
+		g_free (path_in_locale);
+		return NULL;
+	}
+		
+	/* Blocks */
+	if (!g_file_query_exists (file, NULL)) {
 		g_warning ("File does not exist '%s'", path_in_locale);
+		g_object_unref (file);
 		g_free (path_in_locale);
 		return NULL;
 	}
 
-	if (mime && *mime) {
-		mime_used = g_strdup (mime);
-		g_strstrip (mime_used);
+	/* Do we get size and mime? or just size? */
+	if (mime && *mime) {	
+		attributes = 
+			G_FILE_ATTRIBUTE_STANDARD_SIZE;
 	} else {
-		GFile *file;
-
-		/* Try to guess mime type */
-		file = g_file_new_for_path (path_in_locale);
-
-		if (file) {
-			GFileInfo *info;
-			GError *error = NULL;
-			const gchar *attributes;
-
-			attributes = 
-				G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
-			
-			info = g_file_query_info (file,
-						  attributes,
-						  G_FILE_QUERY_INFO_NONE,
-						  NULL,
-						  &error);
-
-			if (error) {
-				tracker_dbus_request_comment (request_id,
-							      "  Could not create GFileInfo for path:'%s', %s",
-							      path_in_locale,
-							      error ? error->message : "no error given");
-				g_error_free (error);
-			}
-			
-			if (info) {
-				mime_used = g_strdup (g_file_info_get_content_type (info));
-				g_object_unref (info);
-			}
-
-			tracker_dbus_request_comment (request_id,
-						      "  Guessing mime type as '%s' for path:'%s'",
-						      mime_used,
-						      path_in_locale);
-
-			g_object_unref (file);
-		} else {
-			tracker_dbus_request_comment (request_id,
-						      "  Could not create GFile for path:'%s'",
-						      path_in_locale);
-		}
+		attributes = 
+			G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
+			G_FILE_ATTRIBUTE_STANDARD_SIZE;
 	}
 
+	info = g_file_query_info (file, 
+				  attributes, 
+				  G_FILE_QUERY_INFO_NONE, 
+				  NULL, 
+				  &error);
+	
+	if (error || !info) {
+		tracker_dbus_request_comment (request_id,
+					      "  Could not create GFileInfo for file size check, %s",
+					      error ? error->message : "no error given");
+		g_error_free (error);
+		
+		if (info) {
+			g_object_unref (info);
+		}	
+		
+		g_object_unref (file);
+		g_free (path_in_locale);
+		
+		return NULL;
+	}
+	
+	/* Create hash table to send back */
 	values = g_hash_table_new_full (g_str_hash,
 					g_str_equal,
 					g_free,
 					g_free);
 
+	/* Check the size is actually non-zero */
+	size = g_file_info_get_size (info);
+
+	if (size < 1) {
+		tracker_dbus_request_comment (request_id,
+					      "  File size is 0 bytes, ignoring file");
+		
+		g_object_unref (info);
+		g_object_unref (file);
+		g_free (path_in_locale);
+
+		return values;
+	}
+
+	/* We know the mime */
+	if (mime && *mime) {
+		mime_used = g_strdup (mime);
+		g_strstrip (mime_used);
+	} else {
+		mime_used = g_strdup (g_file_info_get_content_type (info));
+
+		tracker_dbus_request_comment (request_id,
+					      "  Guessing mime type as '%s' for path:'%s'",
+					      mime_used,
+					      path_in_locale);
+	}
+
+	g_object_unref (file);
+
+
+	/* Now we have sanity checked everything, actually get the
+	 * data we need from the extractors.
+	 */
 	if (mime_used) {
 		TrackerExtractPrivate *priv;
 		TrackerExtractData *data;
@@ -292,7 +325,6 @@
 							      g_hash_table_size (values));
 				
 				g_free (path_in_locale);
-				g_free (path_used);
 				g_free (mime_used);
 				
 				return values;
@@ -307,7 +339,6 @@
 	}
 
 	g_free (path_in_locale);
-	g_free (path_used);
 
 	return values;
 }
@@ -358,7 +389,7 @@
 	tracker_dbus_request_debug (request_id,
 				    "  Resetting shutdown timeout");
 	
-	tracker_main_shutdown_timeout_reset ();
+	tracker_main_quit_timeout_reset ();
 
 	values = get_file_metadata (object, request_id, path, mime);
 	if (values) {

Modified: trunk/src/tracker-extract/tracker-main.c
==============================================================================
--- trunk/src/tracker-extract/tracker-main.c	(original)
+++ trunk/src/tracker-extract/tracker-main.c	Tue Feb 10 14:10:28 2009
@@ -41,28 +41,63 @@
 #include "tracker-dbus.h"
 #include "tracker-extract.h"
 
+#define ABOUT								  \
+	"Tracker " PACKAGE_VERSION "\n"					  \
+	"Copyright (c) 2005-2008 Jamie McCracken (jamiemcc gnome org)\n"
+
+#define LICENSE								  \
+	"This program is free software and comes without any warranty.\n" \
+	"It is licensed under version 2 or later of the General Public "  \
+	"License which can be viewed at:\n"				  \
+	"\n"								  \
+	"  http://www.gnu.org/licenses/gpl.txt\n";
+
+#define QUIT_TIMEOUT 30 /* 1/2 minutes worth of seconds */
+
 static GMainLoop *main_loop;
-static guint      shutdown_timeout_id;
+static guint      quit_timeout_id;
+
+static gint       verbosity = -1;
+static gchar     *filename;
+static gchar     *mime_type;
+
+static GOptionEntry  entries[] = {
+	{ "verbosity", 'v', 0,
+	  G_OPTION_ARG_INT, &verbosity,
+	  N_("Logging, 0 = errors only, "
+	     "1 = minimal, 2 = detailed and 3 = debug (default = 0)"),
+	  NULL },
+	{ "file", 'f', 0,
+	  G_OPTION_ARG_STRING, &filename,
+	  N_("File to extract metadata for"),
+	  N_("FILE") },
+	{ "file", 'm', 0,
+	  G_OPTION_ARG_STRING, &mime_type,
+	  N_("MIME type for file (if not provided, this will be guessed)"),
+	  N_("MIME") },
+
+	{ NULL }
+};
 
 static gboolean
-shutdown_timeout_cb (gpointer user_data)
+quit_timeout_cb (gpointer user_data)
 {
-	shutdown_timeout_id = 0;
+	quit_timeout_id = 0;
 	g_main_loop_quit (main_loop);
 
 	return FALSE;
 }
 
 void
-tracker_main_shutdown_timeout_reset (void)
+tracker_main_quit_timeout_reset (void)
 {
-	if (shutdown_timeout_id != 0) {
-		g_source_remove (shutdown_timeout_id);
+	if (quit_timeout_id != 0) {
+		g_source_remove (quit_timeout_id);
 	}
 
-	shutdown_timeout_id = g_timeout_add_seconds (30, 
-						     shutdown_timeout_cb, 
-						     NULL);
+	quit_timeout_id = g_timeout_add_seconds (QUIT_TIMEOUT, 
+						 quit_timeout_cb, 
+						 NULL);
 }
 
 static void
@@ -88,6 +123,7 @@
 main (int argc, char *argv[])
 {
 	GOptionContext *context;
+	GError         *error = NULL;
 	gchar          *summary;
 	TrackerConfig  *config;
 	gchar          *log_filename;
@@ -100,23 +136,26 @@
 	/* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE>	*/
 	context = g_option_context_new (_("- Extract file meta data"));
 
-	/* Translators: this message will appear after the usage string */
-	/* and before the list of options.				*/
-	summary = g_strconcat (_("This command works two ways:"),
-			       "\n",
-			       "\n",
-			       _(" - Calling the DBus API once running"),
-			       "\n",
-			       _(" - Passing arguments:"),
-			       "\n",
-			       "     tracker-extract [filename] [mime-type]\n",
-			       NULL);
+	g_option_context_add_main_entries (context, entries, NULL);
+	g_option_context_parse (context, &argc, &argv, &error);
+
+	if (!filename && mime_type) {
+		gchar *help;
+
+		g_printerr ("%s\n\n",
+			    _("Filename and mime type must be provided together"));
+
+		help = g_option_context_get_help (context, TRUE, NULL);
+		g_option_context_free (context);
+		g_printerr ("%s", help);
+		g_free (help);
+
+		return EXIT_FAILURE;
+	}
 
-	g_option_context_set_summary (context, summary);
-	g_option_context_parse (context, &argc, &argv, NULL);
 	g_option_context_free (context);
 
-	g_free (summary);
+	g_print ("\n" ABOUT "\n" LICENSE "\n");
 
 	tracker_memory_setrlimits ();
 
@@ -132,7 +171,7 @@
 
 	setlocale (LC_ALL, "");
 
-	if (argc >= 2) {
+	if (filename) {
 		TrackerExtract *object;
 
 		object = tracker_extract_new ();
@@ -140,11 +179,7 @@
 			return EXIT_FAILURE;
 		}
 
-		if (argc >= 3) {
-			tracker_extract_get_metadata_by_cmdline (object, argv[1], argv[2]);
-		} else {
-			tracker_extract_get_metadata_by_cmdline (object, argv[1], NULL);
-		}
+		tracker_extract_get_metadata_by_cmdline (object, filename, mime_type);
 
 		g_object_unref (object);
 
@@ -159,6 +194,11 @@
 				  "tracker-extract.log",
 				  NULL);
 
+	/* Extractor command line arguments */
+	if (verbosity > -1) {
+		tracker_config_set_verbosity (config, verbosity);
+	}
+
 	/* Initialize subsystems */
 	initialize_directories ();
 
@@ -178,7 +218,7 @@
 
 	/* Main loop */
 	main_loop = g_main_loop_new (NULL, FALSE);
-	tracker_main_shutdown_timeout_reset ();
+	tracker_main_quit_timeout_reset ();
 	g_main_loop_run (main_loop);
 	g_main_loop_unref (main_loop);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]