summaryrefslogtreecommitdiff
path: root/mrklib_package.c
blob: 201b3b2c06c0bf7018b174daf10600e25b687309 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "mrklib_priv.h"

static int
write_file(Eet_File *ef, const char *key, const char *file)
{
   FILE *f;
   void *mem;
   int size;
   char *s;

   mem = eet_read(ef, key, &size);
   if (mem)
     {
        s = ecore_file_dir_get(file);
        if (s)
          {
             ecore_file_mkpath(s);
             free(s);
          }
        f = fopen(file, "wb");
        if (f)
          {
             fwrite(mem, size, 1, f);
             fclose(f);
             return 1;
          }
     }
   return 0;
}

EAPI Eina_Bool
mrk_package_src_extract(const char *file, const char *dst)
{
   Eet_File *ef = NULL;
   char tmp[4096];

   ef = eet_open(file, EET_FILE_MODE_READ);
   if (ef)
     {
        int i, num = 0;
        char **keys = eet_list(ef, "src/*", &num);

        if (keys)
          {
             for (i = 0; i < num; i++)
               {
                  if (!strncmp(keys[i], "src/", 4))
                    {
                       if (!_mrk_util_plain_path_check(&(keys[i][4])))
                         continue;
                       snprintf(tmp, sizeof(tmp), "%s/%s", dst, &(keys[i][4]));
                       if (!write_file(ef, keys[i], tmp))
                         {
                            free(keys);
                            goto error;
                         }
                    }
               }
             free(keys);
          }
        snprintf(tmp, sizeof(tmp), "%s/%s", dst, "Marrakesh.mrk");
        if (!write_file(ef, "buildinfo", tmp)) goto error;
        eet_close(ef);
        return EINA_TRUE;
     }
error:
   if (ef) eet_close(ef);
   return EINA_FALSE;
}

static char *
read_string(Eet_File *ef, const char *key)
{
   int size = 0;
   char *str;
   char *s = eet_read(ef, key, &size);
   if (!s) return NULL;
   str = malloc(size + 1);
   if (!str)
     {
        free(s);
        return NULL;
     }
   memcpy(str, s, size);
   str[size] = 0;
   free(s);
   return str;
}

static void
clean_symlinks(const char *dir)
{
   Eina_List *files;
   char *s, *ss;
   char tmp[4096];

   files = ecore_file_ls(dir);
   EINA_LIST_FREE(files, s)
     {
        snprintf(tmp, sizeof(tmp), "%s/%s", dir, s);
        ss = ecore_file_readlink(tmp);
        if (ss)
          {
             free(ss);
             if (!ecore_file_exists(tmp))
               {
                  printf("clean '%s'\n", tmp);
                  ecore_file_unlink(tmp);
               }
          }
        free(s);
     }
}

static int
field_copy(Eet_File *ef, Eet_File *ef2, const char *key)
{
   int size = 0;
   char *s = eet_read(ef, key, &size);
   if (s)
     {
        eet_write(ef2, key, s, size, EET_COMPRESSION_VERYFAST);
        free(s);
        return 1;
     }
   return 0;
}

static const char *
get_home(void)
{
   static char buf[4096] = "";
   const char *tmps, *home;

   if (buf[0]) return buf;
   tmps = getenv("XDG_DATA_HOME");
   home = getenv("HOME");
   if (!home) home = "/tmp";
   if (tmps) snprintf(buf, sizeof(buf), "%s", tmps);
   else snprintf(buf, sizeof(buf), "%s", home);
   return buf;
}

EAPI Eina_Bool
mrk_package_bin_clean(void)
{
   char tmp[4096];

   snprintf(tmp, sizeof(tmp), "%s/.local/share/icons", get_home());
   clean_symlinks(tmp);
   snprintf(tmp, sizeof(tmp), "%s/.local/share/applications", get_home());
   clean_symlinks(tmp);
   snprintf(tmp, sizeof(tmp), "%s/Applications/.bin", get_home());
   clean_symlinks(tmp);
   return 1;
}

EAPI Eina_Bool
mrk_package_verify(const char *file, const char *key_cert_file)
{
   Eet_File *ef;
   Eina_Bool ok = EINA_FALSE;

   ef = eet_open(file, EET_FILE_MODE_READ);
   if (!ef) return EINA_FALSE;
   if (eet_identity_verify(ef, key_cert_file)) ok = EINA_TRUE;
   eet_close(ef);
   return ok;
}

EAPI Eina_Bool
mrk_package_bin_install(const char *file, const char *os, const char *arch)
{
   Eet_File *ef, *ef2;
#define err(reason) do { fprintf(stderr, "%s\n", reason); goto error; } while (0)

   ef = eet_open(file, EET_FILE_MODE_READ);
   if (ef)
     {
        int i, num = 0;
        char **keys = eet_list(ef, "bin/*", &num);
        char inst[4096];
        char dir[4096];
        char tmp[4096];
        char tmp2[4096];
        char *s;
        Eina_List *files;
        char *name;
        char *str;

        str = read_string(ef, "arch");
        if (!str) err("no arch");
        snprintf(tmp, sizeof(tmp), "%s-%s", os, arch);
        if (strcmp(tmp, str))
          {
             free(str);
             err("arch mismatch\n");
          }
        free(str);
        name = read_string(ef, "name");
        if (!name) err("no name");
        if (!_mrk_util_plain_file_check(name)) err("name failed sanity check");
        snprintf(tmp, sizeof(tmp), "%s/Applications/.bin", get_home());
        ecore_file_mkpath(tmp);
        snprintf(inst, sizeof(inst), "%s/Applications/%s", get_home(), name);
        if (ecore_file_exists(inst)) err("destination already exists");
        ecore_file_mkpath(inst);
        if (keys)
          {
             for (i = 0; i < num; i++)
               {
                  if (!strncmp(keys[i], "bin/", 4))
                    {
                       if (!_mrk_util_plain_path_check(&(keys[i][6]))) continue;
                       snprintf(tmp, sizeof(tmp), "%s/%s", inst, &(keys[i][6]));
                       write_file(ef, keys[i], tmp);
                       if (keys[i][4] == 'X')
                         {
                            printf("+x '%s'\n", &(keys[i][6]));
                            chmod(tmp, 00500);
                         }
                    }
               }
          }
        snprintf(dir, sizeof(dir), "%s/share/icons", inst);
        files = ecore_file_ls(dir);
        EINA_LIST_FREE(files, s)
          {
             if (!strcmp(s, ".")) continue;
             else if (!strcmp(s, "..")) continue;
             snprintf(tmp, sizeof(tmp), "../../../Applications/%s/share/icons/%s", name, s);
             snprintf(tmp2, sizeof(tmp2), "%s/.local/share/icons/%s", get_home(), s);
             if (ecore_file_exists(tmp2)) err("icon file symlink already exists");
             ecore_file_symlink(tmp, tmp2);
             free(s);
          }
        snprintf(dir, sizeof(dir), "%s/share/applications", inst);
        files = ecore_file_ls(dir);
        EINA_LIST_FREE(files, s)
          {
             if (!strcmp(s, ".")) continue;
             else if (!strcmp(s, "..")) continue;
             snprintf(tmp, sizeof(tmp), "../../../Applications/%s/share/applications/%s", name, s);
             snprintf(tmp2, sizeof(tmp2), "%s/.local/share/applications/%s", get_home(), s);
             if (ecore_file_exists(tmp2)) err("desktop file symlink already exists");
             ecore_file_symlink(tmp, tmp2);
             free(s);
          }
        snprintf(dir, sizeof(dir), "%s/bin", inst);
        files = ecore_file_ls(dir);
        EINA_LIST_FREE(files, s)
          {
             if (!strcmp(s, ".")) continue;
             else if (!strcmp(s, "..")) continue;
             snprintf(tmp, sizeof(tmp), "../%s/bin/%s", name, s);
             snprintf(tmp2, sizeof(tmp2), "%s/Applications/.bin/%s", get_home(), s);
             if (ecore_file_exists(tmp2)) err("bin file symlink already exists");
             ecore_file_symlink(tmp, tmp2);
             free(s);
          }
        snprintf(tmp, sizeof(tmp), "%s/Applications/%s/.icon.png", get_home(), name);
        write_file(ef, "icon", tmp);
        snprintf(tmp, sizeof(tmp), "%s/Applications/%s/.splash.png", get_home(), name);
        write_file(ef, "splash", tmp);
        snprintf(tmp, sizeof(tmp), "%s/Applications/%s/.info.mki", get_home(), name);
        ef2 = eet_open(tmp, EET_FILE_MODE_WRITE);
        free(name);
        if (ef2)
          {
             field_copy(ef, ef2, "name");
             field_copy(ef, ef2, "brief");
             field_copy(ef, ef2, "version");
             field_copy(ef, ef2, "license");
             field_copy(ef, ef2, "repo");
             field_copy(ef, ef2, "devrepo");
             field_copy(ef, ef2, "contact");
             field_copy(ef, ef2, "needs");
             for (i = 0; i < 9999; i++)
               {
                  snprintf(tmp, sizeof(tmp), "tag/%i", i);
                  if (!field_copy(ef, ef2, tmp)) break;
               }
             for (i = 0; i < 9999; i++)
               {
                  snprintf(tmp, sizeof(tmp), "category/%i", i);
                  if (!field_copy(ef, ef2, tmp)) break;
               }
             eet_close(ef2);
          }
        eet_close(ef);
        return EINA_TRUE;
     }
error:
   if (ef) eet_close(ef);
   return EINA_FALSE;
}

EAPI Eina_Bool
mrk_package_bin_remove(const char *name)
{
   char tmp[4096];

   snprintf(tmp, sizeof(tmp), "%s/Applications/%s", get_home(), name);
   if (!ecore_file_recursive_rm(tmp)) return EINA_FALSE;
   return EINA_TRUE;
}