forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdg-app-builtins-list.c
218 lines (166 loc) · 5.83 KB
/
xdg-app-builtins-list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "libgsystem.h"
#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
static gboolean opt_show_details;
static gboolean opt_user;
static gboolean opt_system;
static GOptionEntry options[] = {
{ "user", 0, 0, G_OPTION_ARG_NONE, &opt_user, "Show user installations", NULL },
{ "system", 0, 0, G_OPTION_ARG_NONE, &opt_system, "Show system-wide installations", NULL },
{ "show-details", 0, 0, G_OPTION_ARG_NONE, &opt_show_details, "Show arches and branches", NULL },
{ NULL }
};
static gboolean
print_installed_refs (XdgAppDir *dir, const char *kind, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
gs_unref_object GFile *base;
gs_unref_object GFileEnumerator *dir_enum = NULL;
gs_unref_object GFileInfo *child_info = NULL;
GError *temp_error = NULL;
gs_unref_ptrarray GPtrArray *refs = NULL;
int i;
base = g_file_get_child (xdg_app_dir_get_path (dir), kind);
if (!g_file_query_exists (base, cancellable))
return TRUE;
refs = g_ptr_array_new ();
dir_enum = g_file_enumerate_children (base, G_FILE_ATTRIBUTE_STANDARD_NAME,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, error);
if (!dir_enum)
goto out;
while ((child_info = g_file_enumerator_next_file (dir_enum, cancellable, &temp_error)))
{
gs_unref_object GFile *child = NULL;
gs_unref_object GFileEnumerator *dir_enum2 = NULL;
gs_unref_object GFileInfo *child_info2 = NULL;
const char *name;
name = g_file_info_get_name (child_info);
child = g_file_get_child (base, name);
g_clear_object (&dir_enum2);
dir_enum2 = g_file_enumerate_children (child, G_FILE_ATTRIBUTE_STANDARD_NAME,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, error);
if (!dir_enum2)
goto out;
while ((child_info2 = g_file_enumerator_next_file (dir_enum2, cancellable, &temp_error)))
{
gs_unref_object GFile *child2 = NULL;
gs_unref_object GFileEnumerator *dir_enum3 = NULL;
gs_unref_object GFileInfo *child_info3 = NULL;
const char *arch;
arch = g_file_info_get_name (child_info2);
if (strcmp (arch, "data") == 0)
continue;
child2 = g_file_get_child (child, arch);
g_clear_object (&dir_enum3);
dir_enum3 = g_file_enumerate_children (child2, G_FILE_ATTRIBUTE_STANDARD_NAME,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, error);
if (!dir_enum3)
goto out;
while ((child_info3 = g_file_enumerator_next_file (dir_enum3, cancellable, &temp_error)))
{
const char *branch;
char *ref;
gboolean found;
branch = g_file_info_get_name (child_info3);
if (opt_show_details)
ref = g_strdup_printf ("%s/%s/%s", name, arch, branch);
else
ref = g_strdup (name);
found = FALSE;
for (i = 0; i < refs->len; i++)
{
if (strcmp (ref, g_ptr_array_index (refs, i)) == 0)
{
found = TRUE;
break;
}
}
if (found)
g_free (refs);
else
g_ptr_array_add (refs, ref);
g_clear_object (&child_info3);
}
g_ptr_array_sort (refs, (GCompareFunc)strcmp);
if (temp_error != NULL)
goto out;
g_clear_object (&child_info2);
}
if (temp_error != NULL)
goto out;
g_clear_object (&child_info);
}
if (temp_error != NULL)
goto out;
for (i = 0; i < refs->len; i++)
g_print ("%s\n", (char *)g_ptr_array_index (refs, i));
ret = TRUE;
out:
if (temp_error != NULL)
g_propagate_error (error, temp_error);
return ret;
}
gboolean
xdg_app_builtin_list_runtimes (int argc, char **argv, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
GOptionContext *context;
context = g_option_context_new (" - List installed runtimes");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
goto out;
if (opt_user || (!opt_user && !opt_system))
{
gs_unref_object XdgAppDir *dir = NULL;
dir = xdg_app_dir_get (TRUE);
if (!print_installed_refs (dir, "runtime", cancellable, error))
goto out;
}
if (opt_system || (!opt_user && !opt_system))
{
gs_unref_object XdgAppDir *dir = NULL;
dir = xdg_app_dir_get (FALSE);
if (!print_installed_refs (dir, "runtime", cancellable, error))
goto out;
}
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}
gboolean
xdg_app_builtin_list_apps (int argc, char **argv, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
GOptionContext *context;
context = g_option_context_new (" - List installed applications");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
goto out;
if (opt_user || (!opt_user && !opt_system))
{
gs_unref_object XdgAppDir *dir = NULL;
dir = xdg_app_dir_get (TRUE);
if (!print_installed_refs (dir, "app", cancellable, error))
goto out;
}
if (opt_system || (!opt_user && !opt_system))
{
gs_unref_object XdgAppDir *dir = NULL;
dir = xdg_app_dir_get (FALSE);
if (!print_installed_refs (dir, "app", cancellable, error))
goto out;
}
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}