Location | Tool | Test ID | Function | Issue |
---|---|---|---|---|
tracker-extract-ps.c:174:5 | clang-analyzer | Value stored to 'pageno_atend' is never read | ||
tracker-extract-ps.c:183:4 | clang-analyzer | Value stored to 'header_finished' is never read |
1 /*
2 * Copyright (C) 2007, Jamie McCracken <jamiemcc@gnome.org>
3 * Copyright (C) 2008-2009, Nokia <ivan.frade@nokia.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25 #endif
26
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include <glib.h>
34 #include <glib/gstdio.h>
35
36 #include <libtracker-common/tracker-os-dependant.h>
37 #include <libtracker-common/tracker-file-utils.h>
38
39 #include <libtracker-extract/tracker-extract.h>
40
41 static gchar *
42 hour_day_str_day (const gchar *date)
43 {
44 /* From: ex. date: "(18:07 Tuesday 22 May 2007)"
45 * To : ex. ISO8601 date: "2007-05-22T18:07:10-0600"
46 */
47 return tracker_date_format_to_iso8601 (date, "(%H:%M %A %d %B %Y)");
48 }
49
50 static gchar *
51 day_str_month_day (const gchar *date)
52 {
53 /* From: ex. date: "Tue May 22 18:07:10 2007"
54 * To : ex. ISO8601 date: "2007-05-22T18:07:10-0600"
55 */
56 return tracker_date_format_to_iso8601 (date, "%A %B %d %H:%M:%S %Y");
57 }
58
59 static gchar *
60 day_month_year_date (const gchar *date)
61 {
62 /* From: ex. date: "22 May 1997 18:07:10 -0600"
63 * To : ex. ISO8601 date: "2007-05-22T18:07:10-0600"
64 */
65 return tracker_date_format_to_iso8601 (date, "%d %B %Y %H:%M:%S %z");
66 }
67
68 static gchar *
69 hour_month_day_date (const gchar *date)
70 {
71 /* From: ex. date: "6:07 PM May 22, 2007"
72 * To : ex. ISO8601 date: "2007-05-22T18:07:10-0600"
73 */
74 return tracker_date_format_to_iso8601 (date, "%I:%M %p %B %d, %Y");
75 }
76
77 static gchar *
78 date_to_iso8601 (const gchar *date)
79 {
80 if (date && date[1] && date[2]) {
81 if (date[0] == '(') {
82 /* we have probably a date like
83 * "(18:07 Tuesday 22 May 2007)"
84 */
85 return hour_day_str_day (date);
86 } else if (g_ascii_isalpha (date[0])) {
87 /* we have probably a date like
88 * "Tue May 22 18:07:10 2007"
89 */
90 return day_str_month_day (date);
91
92 } else if (date[1] == ' ' || date[2] == ' ') {
93 /* we have probably a date like
94 * "22 May 1997 18:07:10 -0600"
95 */
96 return day_month_year_date (date);
97
98 } else if (date[1] == ':' || date[2] == ':') {
99 /* we have probably a date like
100 * "6:07 PM May 22, 2007"
101 */
102 return hour_month_day_date (date);
103 }
104 }
105
106 return NULL;
107 }
108
109 static void
110 extract_ps_from_filestream (FILE *f,
111 TrackerSparqlBuilder *preupdate,
112 TrackerSparqlBuilder *metadata)
113 {
114 gchar *line;
115 gsize length;
116 gssize read_char;
117 gsize accum;
118 gsize max_bytes;
119
120 line = NULL;
121 length = 0;
122
123 tracker_sparql_builder_predicate (metadata, "a");
124 tracker_sparql_builder_object (metadata, "nfo:PaginatedTextDocument");
125
126 /* 20 MiB should be enough! (original safe limit) */
127 accum = 0;
128 max_bytes = 20u << 20;
129
130 /* Reuse the same buffer for all lines. Must be dynamically allocated with
131 * malloc family methods as getline() may re-size it with realloc() */
132 length = 1024;
133 line = g_malloc (length);
134
135 /* Halt the whole when one of these conditions is met:
136 * a) Reached max bytes to read
137 * b) No more lines to read
138 */
139 while ((accum < max_bytes) &&
140 (read_char = tracker_getline (&line, &length, f)) != -1) {
141 gboolean pageno_atend = FALSE;
142 gboolean header_finished = FALSE;
143
144 /* Update accumulated bytes read */
145 accum += read_char;
146
147 line[read_char - 1] = '\0'; /* overwrite '\n' char */
148
149 if (!header_finished && strncmp (line, "%%Copyright:", 12) == 0) {
150 tracker_sparql_builder_predicate (metadata, "nie:copyright");
151 tracker_sparql_builder_object_unvalidated (metadata, line + 13);
152 } else if (!header_finished && strncmp (line, "%%Title:", 8) == 0) {
153 tracker_sparql_builder_predicate (metadata, "nie:title");
154 tracker_sparql_builder_object_unvalidated (metadata, line + 9);
155 } else if (!header_finished && strncmp (line, "%%Creator:", 10) == 0) {
156 tracker_sparql_builder_predicate (metadata, "nco:creator");
157 tracker_sparql_builder_object_blank_open (metadata);
158 tracker_sparql_builder_predicate (metadata, "a");
159 tracker_sparql_builder_object (metadata, "nco:Contact");
160 tracker_sparql_builder_predicate (metadata, "nco:fullname");
161 tracker_sparql_builder_object_unvalidated (metadata, line + 11);
162 tracker_sparql_builder_object_blank_close (metadata);
163 } else if (!header_finished && strncmp (line, "%%CreationDate:", 15) == 0) {
164 gchar *date;
165
166 date = date_to_iso8601 (line + 16);
167 if (date) {
168 tracker_sparql_builder_predicate (metadata, "nie:contentCreated");
169 tracker_sparql_builder_object_unvalidated (metadata, date);
170 g_free (date);
171 }
172 } else if (strncmp (line, "%%Pages:", 8) == 0) {
173 if (strcmp (line + 9, "(atend)") == 0) {
174 pageno_atend = TRUE;
(emitted by clang-analyzer)TODO: a detailed trace is available in the data model (not yet rendered in this report)
175 } else {
176 gint64 page_count;
177
178 page_count = g_ascii_strtoll (line + 9, NULL, 10);
179 tracker_sparql_builder_predicate (metadata, "nfo:pageCount");
180 tracker_sparql_builder_object_int64 (metadata, page_count);
181 }
182 } else if (strncmp (line, "%%EndComments", 14) == 0) {
183 header_finished = TRUE;
(emitted by clang-analyzer)TODO: a detailed trace is available in the data model (not yet rendered in this report)
184
185 if (!pageno_atend) {
186 break;
187 }
188 }
189 }
190
191 /* Deallocate the buffer */
192 if (line) {
193 g_free (line);
194 }
195 }
196
197
198
199 static void
200 extract_ps (const gchar *uri,
201 TrackerSparqlBuilder *preupdate,
202 TrackerSparqlBuilder *metadata)
203 {
204 FILE *f;
205 gchar *filename;
206
207 filename = g_filename_from_uri (uri, NULL, NULL);
208 f = tracker_file_open (filename);
209 g_free (filename);
210
211 if (!f) {
212 return;
213 }
214
215 /* Extract from filestream! */
216 g_debug ("Extracting PS '%s'...", uri);
217 extract_ps_from_filestream (f, preupdate, metadata);
218
219 tracker_file_close (f, FALSE);
220 }
221
222 #ifdef USING_UNZIPPSFILES
223
224 static void
225 extract_ps_gz (const gchar *uri,
226 TrackerSparqlBuilder *preupdate,
227 TrackerSparqlBuilder *metadata)
228 {
229 FILE *fz;
230 gint fdz;
231 const gchar *argv[4];
232 gchar *filename;
233 GError *error = NULL;
234
235 filename = g_filename_from_uri (uri, NULL, NULL);
236
237 /* TODO: we should be using libz for this instead */
238
239 argv[0] = "gunzip";
240 argv[1] = "-c";
241 argv[2] = filename;
242 argv[3] = NULL;
243
244 /* Fork & spawn to gunzip the file */
245 if (!g_spawn_async_with_pipes (g_get_tmp_dir (),
246 (gchar **) argv,
247 NULL,
248 G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
249 tracker_spawn_child_func,
250 GINT_TO_POINTER (10),
251 NULL,
252 NULL,
253 &fdz,
254 NULL,
255 &error)) {
256 g_warning ("Couldn't fork & spawn to gunzip '%s': %s",
257 uri, error ? error->message : NULL);
258 g_clear_error (&error);
259 }
260 /* Get FILE from FD */
261 else if ((fz = fdopen (fdz, "r")) == NULL) {
262 g_warning ("Couldn't open FILE from FD (%s)...", uri);
263 close (fdz);
264 }
265 /* Extract from filestream! */
266 else
267 {
268 g_debug ("Extracting compressed PS '%s'...", uri);
269 extract_ps_from_filestream (fz, preupdate, metadata);
270 #ifdef HAVE_POSIX_FADVISE
271 posix_fadvise (fdz, 0, 0, POSIX_FADV_DONTNEED);
272 #endif /* HAVE_POSIX_FADVISE */
273 fclose (fz);
274 }
275
276 g_free (filename);
277 }
278
279 #endif /* USING_UNZIPPSFILES */
280
281 G_MODULE_EXPORT gboolean
282 tracker_extract_get_metadata (TrackerExtractInfo *info)
283 {
284 TrackerSparqlBuilder *preupdate, *metadata;
285 const gchar *mimetype;
286 GFile *file;
287 gchar *uri;
288
289 preupdate = tracker_extract_info_get_preupdate_builder (info);
290 metadata = tracker_extract_info_get_metadata_builder (info);
291 mimetype = tracker_extract_info_get_mimetype (info);
292
293 file = tracker_extract_info_get_file (info);
294 uri = g_file_get_uri (file);
295
296 #ifdef USING_UNZIPPSFILES
297 if (strcmp (mimetype, "application/x-gzpostscript") == 0) {
298 extract_ps_gz (uri, preupdate, metadata);
299 } else
300 #endif /* USING_UNZIPPSFILES */
301 {
302 extract_ps (uri, preupdate, metadata);
303 }
304
305 g_free (uri);
306
307 return TRUE;
308 }