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

No issues found

  1 /*
  2  * Copyright (C) 2007, Jason Kivlighn <jkivlighn@gmail.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 /* This function is used to find the URI for a file.xmp file. The point here is
 44  * that the URI for file.xmp is not file:///file.xmp but instead for example
 45  * file:///file.jpeg or file:///file.png. The reason is that file.xmp is a
 46  * sidekick, and a sidekick doesn't describe itself, it describes another file. */
 47 
 48 static gchar *
 49 find_orig_uri (const gchar *xmp_filename)
 50 {
 51 	GFile *file;
 52 	GFile *dir;
 53 	GFileEnumerator *iter;
 54 	GFileInfo *orig_info;
 55 	const gchar *filename_a;
 56 	gchar *found_file = NULL;
 57 
 58 	file = g_file_new_for_path (xmp_filename);
 59 	dir = g_file_get_parent (file);
 60 
 61 	orig_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_NAME,
 62 	                               G_FILE_QUERY_INFO_NONE,
 63 	                               NULL, NULL);
 64 
 65 	filename_a = g_file_info_get_name (orig_info);
 66 
 67 	iter = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME,
 68 	                                  G_FILE_QUERY_INFO_NONE,
 69 	                                  NULL, NULL);
 70 
 71 	if (iter) {
 72 		GFileInfo *info;
 73 
 74 		while ((info = g_file_enumerator_next_file (iter, NULL, NULL)) && !found_file) {
 75 			const gchar *filename_b;
 76 			const gchar *ext_a, *ext_b;
 77 			gchar *casefold_a, *casefold_b;
 78 
 79 			/* OK, important:
 80 			 * 1. Files can't be the same.
 81 			 * 2. File names (without extension) must match
 82 			 * 3. Something else? */
 83 
 84 			filename_b = g_file_info_get_name (info);
 85 
 86 			ext_a = g_utf8_strrchr (filename_a, -1, '.');
 87 			ext_b = g_utf8_strrchr (filename_b, -1, '.');
 88 
 89 			/* Look for extension */
 90 			if (!ext_a || !ext_b) {
 91 				g_object_unref (info);
 92 				continue;
 93 			}
 94 
 95 			/* Name part is the same length */
 96 			if ((ext_a - filename_a) != (ext_b - filename_b)) {
 97 				g_object_unref (info);
 98 				continue;
 99 			}
100 
101 			/* Check extensions are not the same (i.e. same len and ext) */
102 			if (g_strcmp0 (ext_a, ext_b) == 0) {
103 				g_object_unref (info);
104 				continue;
105 			}
106 
107 			/* Don't compare the ".xmp" with ".jpeg" and don't match the same file */
108 
109 			/* Now compare name (without ext) and make
110 			 * sure they are the same in a caseless
111 			 * compare. */
112 
113 			casefold_a = g_utf8_casefold (filename_a, (ext_a - filename_a));
114 			casefold_b = g_utf8_casefold (filename_b, (ext_b - filename_b));
115 
116 			if (g_strcmp0 (casefold_a, casefold_b) == 0) {
117 				GFile *found;
118 
119 				found = g_file_get_child (dir, filename_b);
120 				found_file = g_file_get_uri (found);
121 				g_object_unref (found);
122 			}
123 
124 			g_free (casefold_a);
125 			g_free (casefold_b);
126 			g_object_unref (info);
127 		}
128 
129 		g_object_unref (iter);
130 	}
131 
132 	g_object_unref (orig_info);
133 	g_object_unref (file);
134 	g_object_unref (dir);
135 
136 	return found_file;
137 }
138 
139 G_MODULE_EXPORT gboolean
140 tracker_extract_get_metadata (TrackerExtractInfo *info)
141 {
142 	TrackerSparqlBuilder *metadata, *preupdate;
143 	TrackerXmpData *xd = NULL;
144 	gchar *filename, *uri;
145 	gchar *contents;
146 	gsize length = 0;
147 	GFile *file;
148 	const gchar *graph;
149 	int fd;
150 	struct stat st;
151 
152 	file = tracker_extract_info_get_file (info);
153 	filename = g_file_get_path (file);
154 
155 	graph = tracker_extract_info_get_graph (info);
156 	preupdate = tracker_extract_info_get_preupdate_builder (info);
157 	metadata = tracker_extract_info_get_metadata_builder (info);
158 
159 	fd = tracker_file_open_fd (filename);
160 
161 	if (fd == -1) {
162 		g_warning ("Could not open xmp file '%s': %s\n",
163 		           filename,
164 		           g_strerror (errno));
165 		g_free (filename);
166 		return FALSE;
167 	}
168 
169 	if (fstat (fd, &st) == -1) {
170 		g_warning ("Could not fstat xmp file '%s': %s\n",
171 		           filename,
172 		           g_strerror (errno));
173 		close (fd);
174 		g_free (filename);
175 		return FALSE;
176 	}
177 
178 	if (st.st_size == 0) {
179 		contents = NULL;
180 		length = 0;
181 	} else {
182 		contents = (gchar *) mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
183 		if (contents == NULL) {
184 			g_warning ("Could not mmap xmp file '%s': %s\n",
185 			           filename,
186 			           g_strerror (errno));
187 			close (fd);
188 			g_free (filename);
189 			return FALSE;
190 		}
191 		length = st.st_size;
192 	}
193 
194 	uri = g_file_get_uri (file);
195 
196 	if (contents) {
197 		gchar *original_uri;
198 
199 		original_uri = find_orig_uri (filename);
200 
201 		/* If no orig file is found for the sidekick, we use the sidekick to
202 		 * describe itself instead, falling back to uri 
203 		 */
204 		xd = tracker_xmp_new (contents,
205 		                      length,
206 		                      original_uri ? original_uri : uri);
207 
208 		if (xd) {
209 			GString *where;
210 
211 			where = g_string_new ("");
212 			tracker_xmp_apply (preupdate, metadata, graph, where, uri, xd);
213 			tracker_extract_info_set_where_clause (info, where->str);
214 			g_string_free (where, TRUE);
215 		}
216 
217 		g_free (original_uri);
218 		tracker_xmp_free (xd);
219 		g_free (filename);
220 		g_free (uri);
221 
222 		munmap (contents, length);
223 
224 		close (fd);
225 
226 		return TRUE;
227 	}
228 
229 	close (fd);
230 	g_free (filename);
231 	g_free (uri);
232 
233 	return FALSE;
234 }