1 /*
2 * Copyright (C) 2011, Red Hat, Inc
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 * Author: Christophe Fergeau <cfergeau@redhat.com>
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25
26 #include <osinfo/osinfo.h>
27
28 #include <gio/gio.h>
29
30 #include <libtracker-extract/tracker-extract.h>
31 #include <libtracker-sparql/tracker-sparql.h>
32
33 G_MODULE_EXPORT gboolean
34 tracker_extract_get_metadata (TrackerExtractInfo *info_)
35 {
36 /* NOTE: This function has to exist, tracker-extract checks
37 * the symbole table for this function and if it doesn't
38 * exist, the module is not loaded to be used as an extractor.
39 */
40
41 /* File information */
42 GFile *file;
43 GError *error = NULL;
44 gchar *filename;
45 OsinfoLoader *loader = NULL;
46 OsinfoMedia *media;
47 OsinfoMedia *matched_media;
48 OsinfoDb *db;
49 OsinfoOs *os;
50
51 /* Data input */
52 gboolean bootable;
53 const gchar *id;
54 const gchar *name;
55 TrackerSparqlBuilder *metadata;
56
57 metadata = tracker_extract_info_get_metadata_builder (info_);
58
59 file = tracker_extract_info_get_file (info_);
60 filename = g_file_get_path (file);
61
62 media = osinfo_media_create_from_location (filename, NULL, &error);
63 if (error != NULL) {
64 if (error->code != OSINFO_MEDIA_ERROR_NOT_BOOTABLE) {
65 g_message ("Could not extract iso info from '%s': %s",
66 filename, error->message);
67 g_free (filename);
68 g_error_free (error);
69 return FALSE;
70 }
71 bootable = FALSE;
72 goto no_os;
73 } else {
74 bootable = TRUE;
75 }
76 g_free (filename);
77
78 loader = osinfo_loader_new ();
79 osinfo_loader_process_default_path (loader, &error);
80 if (error != NULL) {
81 g_message ("Error loading libosinfo OS data: %s",
82 error->message);
83 g_error_free (error);
84 goto no_os;
85 }
86 g_warn_if_fail (media != NULL);
87 g_warn_if_fail (loader != NULL);
88
89 db = osinfo_loader_get_db (loader);
90 os = osinfo_db_guess_os_from_media (db, media, &matched_media);
'osinfo_db_guess_os_from_media' is deprecated (declared at /usr/include/libosinfo-1.0/osinfo/osinfo_db.h:104): Use 'osinfo_db_identify_media' instead
(emitted by gcc)
91
92 if (os == NULL)
93 goto unknown_os;
94
95 tracker_sparql_builder_predicate (metadata, "a");
96 tracker_sparql_builder_object (metadata, "nfo:FilesystemImage");
97
98 name = osinfo_product_get_name (OSINFO_PRODUCT (os));
99 if (name != NULL) {
100 tracker_sparql_builder_predicate (metadata, "nie:title");
101 tracker_sparql_builder_object_string (metadata, name);
102 }
103
104 if (osinfo_media_get_live (matched_media)) {
105 tracker_sparql_builder_predicate (metadata, "a");
106 tracker_sparql_builder_object (metadata, "nfo:OperatingSystem");
107 }
108
109 if (osinfo_media_get_installer (matched_media)) {
110 tracker_sparql_builder_predicate (metadata, "a");
111 tracker_sparql_builder_object (metadata, "osinfo:Installer");
112 }
113
114 tracker_sparql_builder_predicate (metadata, "nfo:isBootable");
115 tracker_sparql_builder_object_boolean (metadata, bootable);
116
117 id = osinfo_entity_get_id (OSINFO_ENTITY (os));
118 if (id != NULL) {
119 tracker_sparql_builder_predicate (metadata, "osinfo:id");
120 tracker_sparql_builder_object_string (metadata, id);
121 }
122
123 id = osinfo_entity_get_id (OSINFO_ENTITY (matched_media));
124 if (id != NULL) {
125 tracker_sparql_builder_predicate (metadata, "osinfo:mediaId");
126 tracker_sparql_builder_object_string (metadata, id);
127 }
128
129 g_object_unref (G_OBJECT (media));
130 g_object_unref (G_OBJECT (loader));
131
132 return TRUE;
133
134 unknown_os:
135 name = osinfo_media_get_volume_id (media);
136 if (name != NULL) {
137 gchar *stripped = g_strdup (name);
138
139 g_strstrip (stripped);
140 tracker_sparql_builder_predicate (metadata, "nie:title");
141 tracker_sparql_builder_object_string (metadata, stripped);
142 g_free (stripped);
143 }
144
145 no_os:
146 if (media != NULL) {
147 g_object_unref (G_OBJECT (media));
148 }
149 if (loader != NULL) {
150 g_object_unref (G_OBJECT (loader));
151 }
152
153 tracker_sparql_builder_predicate (metadata, "a");
154 tracker_sparql_builder_object (metadata, "nfo:FilesystemImage");
155
156 tracker_sparql_builder_predicate (metadata, "nfo:isBootable");
157 tracker_sparql_builder_object_boolean (metadata, bootable);
158
159 return TRUE;
160 }