/src/tinysparql/src/libtinysparql/tracker-deserializer-directory.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2025, 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: Carlos Garnacho <carlosg@gnome.org> |
20 | | */ |
21 | | |
22 | | #include "config.h" |
23 | | |
24 | | #include "tracker-deserializer-directory.h" |
25 | | |
26 | | struct _TrackerDeserializerDirectory { |
27 | | TrackerDeserializerMerger parent_instance; |
28 | | GFile *directory; |
29 | | GList *files; |
30 | | GList *current_file; |
31 | | }; |
32 | | |
33 | | enum { |
34 | | PROP_0, |
35 | | PROP_DIRECTORY, |
36 | | N_PROPS, |
37 | | }; |
38 | | |
39 | | static GParamSpec *props[N_PROPS] = { 0, }; |
40 | | |
41 | 123k | G_DEFINE_TYPE (TrackerDeserializerDirectory, |
42 | 123k | tracker_deserializer_directory, |
43 | 123k | TRACKER_TYPE_DESERIALIZER_MERGER) |
44 | 123k | |
45 | 123k | static void |
46 | 123k | tracker_deserializer_directory_finalize (GObject *object) |
47 | 123k | { |
48 | 30.9k | TrackerDeserializerDirectory *deserializer = |
49 | 30.9k | TRACKER_DESERIALIZER_DIRECTORY (object); |
50 | | |
51 | 30.9k | g_clear_object (&deserializer->directory); |
52 | 30.9k | g_list_free_full (deserializer->files, g_object_unref); |
53 | 30.9k | deserializer->files = NULL; |
54 | 30.9k | deserializer->current_file = NULL; |
55 | | |
56 | 30.9k | G_OBJECT_CLASS (tracker_deserializer_directory_parent_class)->finalize (object); |
57 | 30.9k | } |
58 | | |
59 | | static void |
60 | | tracker_deserializer_set_property (GObject *object, |
61 | | guint prop_id, |
62 | | const GValue *value, |
63 | | GParamSpec *pspec) |
64 | 30.9k | { |
65 | 30.9k | TrackerDeserializerDirectory *deserializer = |
66 | 30.9k | TRACKER_DESERIALIZER_DIRECTORY (object); |
67 | | |
68 | 30.9k | switch (prop_id) { |
69 | 30.9k | case PROP_DIRECTORY: |
70 | 30.9k | deserializer->directory = g_value_dup_object (value); |
71 | 30.9k | break; |
72 | 0 | default: |
73 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
74 | 0 | break; |
75 | 30.9k | } |
76 | 30.9k | } |
77 | | |
78 | | static int |
79 | | compare_file_names (GFile *file1, |
80 | | GFile *file2) |
81 | 96.2k | { |
82 | 96.2k | char *name1, *name2; |
83 | 96.2k | int ret; |
84 | | |
85 | 96.2k | name1 = g_file_get_basename (file1); |
86 | 96.2k | name2 = g_file_get_basename (file2); |
87 | 96.2k | ret = strcmp (name1, name2); |
88 | 96.2k | g_free (name1); |
89 | 96.2k | g_free (name2); |
90 | | |
91 | 96.2k | return ret; |
92 | 96.2k | } |
93 | | |
94 | | static gboolean |
95 | | get_ontology_files (GFile *directory, |
96 | | GList **ontologies, |
97 | | GError **error) |
98 | 37.8k | { |
99 | 37.8k | GFileEnumerator *enumerator; |
100 | 37.8k | GList *sorted = NULL; |
101 | | |
102 | 37.8k | enumerator = g_file_enumerate_children (directory, |
103 | 37.8k | G_FILE_ATTRIBUTE_STANDARD_NAME, |
104 | 37.8k | G_FILE_QUERY_INFO_NONE, |
105 | 37.8k | NULL, error); |
106 | 37.8k | if (!enumerator) |
107 | 0 | return FALSE; |
108 | | |
109 | 147k | while (TRUE) { |
110 | 147k | GFileInfo *info; |
111 | 147k | GFile *child; |
112 | 147k | const gchar *name; |
113 | | |
114 | 147k | if (!g_file_enumerator_iterate (enumerator, &info, &child, NULL, error)) { |
115 | 0 | g_list_free_full (sorted, g_object_unref); |
116 | 0 | g_object_unref (enumerator); |
117 | 0 | return FALSE; |
118 | 0 | } |
119 | | |
120 | 147k | if (!info) |
121 | 37.8k | break; |
122 | | |
123 | 110k | name = g_file_info_get_name (info); |
124 | 110k | if (g_str_has_suffix (name, ".ontology") || |
125 | 96.2k | tracker_rdf_format_pick_for_file (child, NULL)) { |
126 | 96.2k | sorted = g_list_prepend (sorted, g_object_ref (child)); |
127 | 96.2k | } |
128 | 110k | } |
129 | | |
130 | 37.8k | *ontologies = g_list_sort (sorted, (GCompareFunc) compare_file_names); |
131 | 37.8k | g_object_unref (enumerator); |
132 | | |
133 | 37.8k | return TRUE; |
134 | 37.8k | } |
135 | | |
136 | | static gboolean |
137 | | add_deserializer_for_current_file (TrackerDeserializerDirectory *deserializer, |
138 | | GError **error) |
139 | 134k | { |
140 | 134k | TrackerSparqlCursor *child_cursor; |
141 | | |
142 | 134k | if (!deserializer->current_file) |
143 | 37.8k | return TRUE; |
144 | | |
145 | 96.2k | child_cursor = tracker_deserializer_new_for_file (deserializer->current_file->data, |
146 | 96.2k | NULL, error); |
147 | 96.2k | if (!child_cursor) |
148 | 0 | return FALSE; |
149 | | |
150 | 96.2k | tracker_deserializer_merger_add_child (TRACKER_DESERIALIZER_MERGER (deserializer), |
151 | 96.2k | TRACKER_DESERIALIZER (child_cursor)); |
152 | 96.2k | g_object_unref (child_cursor); |
153 | | |
154 | 96.2k | return TRUE; |
155 | 96.2k | } |
156 | | |
157 | | static gboolean |
158 | | tracker_deserializer_directory_next (TrackerSparqlCursor *cursor, |
159 | | GCancellable *cancellable, |
160 | | GError **error) |
161 | 7.49M | { |
162 | 7.49M | TrackerDeserializerDirectory *deserializer = |
163 | 7.49M | TRACKER_DESERIALIZER_DIRECTORY (cursor); |
164 | 7.49M | gboolean retval = FALSE; |
165 | | |
166 | 7.49M | if (!deserializer->files) { |
167 | 37.8k | if (!get_ontology_files (deserializer->directory, |
168 | 37.8k | &deserializer->files, |
169 | 37.8k | error)) |
170 | 0 | return FALSE; |
171 | | |
172 | 37.8k | deserializer->current_file = deserializer->files; |
173 | | |
174 | 37.8k | if (!add_deserializer_for_current_file (deserializer, error)) |
175 | 0 | return FALSE; |
176 | 37.8k | } |
177 | | |
178 | 15.0M | while (!retval && deserializer->current_file) { |
179 | 7.55M | retval = TRACKER_SPARQL_CURSOR_CLASS (tracker_deserializer_directory_parent_class)->next (cursor, cancellable, error); |
180 | | |
181 | 7.55M | if (!retval) { |
182 | 96.2k | deserializer->current_file = deserializer->current_file->next; |
183 | | |
184 | 96.2k | if (!add_deserializer_for_current_file (deserializer, error)) |
185 | 0 | return FALSE; |
186 | 96.2k | } |
187 | 7.55M | } |
188 | | |
189 | 7.49M | return retval; |
190 | 7.49M | } |
191 | | |
192 | | static void |
193 | | tracker_deserializer_directory_class_init (TrackerDeserializerDirectoryClass *klass) |
194 | 5 | { |
195 | 5 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
196 | 5 | TrackerSparqlCursorClass *cursor_class = TRACKER_SPARQL_CURSOR_CLASS (klass); |
197 | | |
198 | 5 | object_class->finalize = tracker_deserializer_directory_finalize; |
199 | 5 | object_class->set_property = tracker_deserializer_set_property; |
200 | | |
201 | 5 | cursor_class->next = tracker_deserializer_directory_next; |
202 | | |
203 | 5 | props[PROP_DIRECTORY] = |
204 | 5 | g_param_spec_object ("directory", NULL, NULL, |
205 | 5 | G_TYPE_FILE, |
206 | 5 | G_PARAM_CONSTRUCT_ONLY | |
207 | 5 | G_PARAM_STATIC_STRINGS | |
208 | 5 | G_PARAM_WRITABLE); |
209 | | |
210 | 5 | g_object_class_install_properties (object_class, N_PROPS, props); |
211 | 5 | } |
212 | | |
213 | | static void |
214 | | tracker_deserializer_directory_init (TrackerDeserializerDirectory *deserializer) |
215 | 30.9k | { |
216 | 30.9k | } |
217 | | |
218 | | TrackerSparqlCursor * |
219 | | tracker_deserializer_directory_new (GFile *directory, |
220 | | TrackerNamespaceManager *namespaces) |
221 | 30.9k | { |
222 | 30.9k | g_return_val_if_fail (G_IS_FILE (directory), NULL); |
223 | 30.9k | g_return_val_if_fail (!namespaces || TRACKER_IS_NAMESPACE_MANAGER (namespaces), NULL); |
224 | | |
225 | 30.9k | return g_object_new (TRACKER_TYPE_DESERIALIZER_DIRECTORY, |
226 | 30.9k | "namespace-manager", namespaces, |
227 | 30.9k | "directory", directory, |
228 | 30.9k | NULL); |
229 | 30.9k | } |
230 | | |
231 | | void |
232 | | tracker_deserializer_directory_reset (TrackerDeserializerDirectory *deserializer) |
233 | 6.91k | { |
234 | 6.91k | g_list_free_full (deserializer->files, g_object_unref); |
235 | 6.91k | deserializer->files = NULL; |
236 | | deserializer->current_file = NULL; |
237 | 6.91k | } |