tracker-0.16.2/src/tracker-extract/tracker-extract-text.c

No issues found

  1 /*
  2  * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2.1 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #ifndef _GNU_SOURCE
 23 #define _GNU_SOURCE
 24 #endif
 25 
 26 #include <errno.h>
 27 #include <fcntl.h>
 28 #include <string.h>
 29 #include <sys/types.h>
 30 #include <sys/stat.h>
 31 #include <unistd.h>
 32 #include <sys/mman.h>
 33 
 34 #include <glib.h>
 35 #include <glib/gstdio.h>
 36 
 37 #include <gio/gio.h>
 38 
 39 #include <libtracker-common/tracker-file-utils.h>
 40 
 41 #include <libtracker-extract/tracker-extract.h>
 42 
 43 #include "tracker-main.h"
 44 #include "tracker-read.h"
 45 
 46 static gchar *
 47 get_file_content (GFile *file,
 48                   gsize  n_bytes)
 49 {
 50 	gchar *text, *uri, *path;
 51 	int fd;
 52 
 53 	/* If no content requested, return */
 54 	if (n_bytes == 0) {
 55 		return NULL;
 56 	}
 57 
 58 	uri = g_file_get_uri (file);
 59 
 60 	/* Get filename from URI */
 61 	path = g_file_get_path (file);
 62 
 63 	fd = tracker_file_open_fd (path);
 64 
 65 	if (fd == -1) {
 66 		g_message ("Could not open file '%s': %s",
 67 		           uri,
 68 		           g_strerror (errno));
 69 		g_free (uri);
 70 		g_free (path);
 71 		return NULL;
 72 	}
 73 
 74 	g_debug ("  Starting to read '%s' up to %" G_GSIZE_FORMAT " bytes...",
 75 	         uri, n_bytes);
 76 
 77 	/* Read up to n_bytes from stream. Output is always, always valid UTF-8,
 78 	 * this function closes the FD.
 79 	 */
 80 	text = tracker_read_text_from_fd (fd, n_bytes);
 81 	g_free (uri);
 82 	g_free (path);
 83 
 84 	return text;
 85 }
 86 
 87 G_MODULE_EXPORT gboolean
 88 tracker_extract_module_init (TrackerModuleThreadAwareness  *thread_awareness_ret,
 89                              GError                       **error)
 90 {
 91 	*thread_awareness_ret = TRACKER_MODULE_MULTI_THREAD;
 92 	return TRUE;
 93 }
 94 
 95 G_MODULE_EXPORT gboolean
 96 tracker_extract_get_metadata (TrackerExtractInfo *info)
 97 {
 98 	TrackerSparqlBuilder *metadata;
 99 	TrackerConfig *config;
100 	gchar *content;
101 
102 	config = tracker_main_get_config ();
103 	metadata = tracker_extract_info_get_metadata_builder (info);
104 
105 	content = get_file_content (tracker_extract_info_get_file (info),
106 	                            tracker_config_get_max_bytes (config));
107 
108 	tracker_sparql_builder_predicate (metadata, "a");
109 	tracker_sparql_builder_object (metadata, "nfo:PlainTextDocument");
110 
111 	if (content) {
112 		tracker_sparql_builder_predicate (metadata, "nie:plainTextContent");
113 		tracker_sparql_builder_object_unvalidated (metadata, content);
114 		g_free (content);
115 	}
116 
117 	return TRUE;
118 }