Source

kalu / kalu-alpm.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/**
 * kalu - Copyright (C) 2012 Olivier Brunel
 *
 * kalu-alpm.c
 * Copyright (C) 2012 Olivier Brunel <i.am.jack.mail@gmail.com>
 * 
 * This file is part of kalu.
 *
 * kalu is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * kalu is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * kalu. If not, see http://www.gnu.org/licenses/
 */

#include <config.h>

/* C */
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <utime.h>

/* alpm */
#include <alpm.h>
#include <alpm_list.h>

/* glib */
#include <glib-2.0/glib.h>

/* kalu */
#include "kalu.h"
#include "kalu-alpm.h"
#include "util.h"
#include "conf.h"

/* global variable */
unsigned short alpm_verbose;


static kalu_alpm_t *alpm;

static gboolean copy_file (const gchar *from, const gchar *to);
static gboolean create_local_db (const gchar *dbpath, gchar **newpath, GError **error);



static gboolean
copy_file (const gchar *from, const gchar *to)
{
    gchar *contents;
    gsize  length;
    
    debug ("copying %s to %s", from, to);
    
    if (!g_file_get_contents (from, &contents, &length, NULL))
    {
        debug ("cannot read %s", from);
        return FALSE;
    }
    
    if (!g_file_set_contents (to, contents, (gssize) length, NULL))
    {
        debug ("cannot write %s", to);
        g_free (contents);
        return FALSE;
    }
    
    debug ("..done");
    g_free (contents);
    return TRUE;
}

static gboolean
create_local_db (const gchar *_dbpath, gchar **newpath, GError **error)
{
    gchar    buf[MAX_PATH];
    gchar    buf2[MAX_PATH];
    gchar   *dbpath;
    size_t   l;
    gchar   *folder;
    GDir    *dir;
    
    debug ("creating local db");
    
    /* create folder in tmp dir */
    if (NULL == (folder = g_dir_make_tmp ("kalu-XXXXXX", NULL)))
    {
        g_set_error (error, KALU_ERROR, 1, "Unable to create temp folder");
        return FALSE;
    }
    debug ("created tmp folder %s", folder);
    
    /* dbpath will not be slash-terminated */
    dbpath = strdup (_dbpath);
    l = strlen (dbpath) - 1;
    if (dbpath[l] == '/')
    {
        dbpath[l] = '\0';
    }
    
    /* symlink local */
    snprintf (buf, MAX_PATH - 1, "%s/local", dbpath);
    snprintf (buf2, MAX_PATH - 1, "%s/local", folder);
    if (0 != symlink (buf, buf2))
    {
        g_set_error (error, KALU_ERROR, 1, "Unable to create symlink %s", buf2);
        goto error;
    }
    debug ("created symlink %s", buf2);
    
    /* copy databases in sync */
    snprintf (buf, MAX_PATH - 1, "%s/sync", folder);
    if (0 != mkdir (buf, 0700))
    {
        g_set_error (error, KALU_ERROR, 1, "Unable to create folder %s", buf);
        goto error;
    }
    debug ("created folder %s", buf);
    
    snprintf (buf, MAX_PATH - 1, "%s/sync", dbpath);
    if (NULL == (dir = g_dir_open (buf, 0, NULL)))
    {
        g_set_error (error, KALU_ERROR, 1, "Unable to open folder %s", buf);
        goto error;
    }
    
    const gchar    *file;
    struct stat     filestat;
    struct utimbuf  times;
    
    while ((file = g_dir_read_name (dir)))
    {
        snprintf (buf, MAX_PATH - 1, "%s/sync/%s", dbpath, file);
        /* stat so we copy files only. also, we need to preserve modified date,
         * used to determine if DBs are up to date or not by libalpm */
        if (0 == stat (buf, &filestat))
        {
            if (S_ISREG (filestat.st_mode))
            {
                snprintf (buf2, MAX_PATH - 1, "%s/sync/%s", folder, file);
                if (!copy_file (buf, buf2))
                {
                    g_set_error (error, KALU_ERROR, 1, "Copy failed for %s", buf);
                    g_dir_close (dir);
                    goto error;
                }
                /* preserve time */
                times.actime = filestat.st_atime;
                times.modtime = filestat.st_mtime;
                if (0 != utime (buf2, &times))
                {
                    /* sucks, but no fail, we'll just have to download this db */
                    debug ("Unable to change time of %s", buf2);
                }
                else
                {
                    debug ("updated time for %s", buf2);
                }
            }
            else
            {
                debug ("ignoring non-regular file: %s", buf);
            }
        }
        else
        {
            g_set_error (error, KALU_ERROR, 1, "Unable to stat %s\n", buf);
            g_dir_close (dir);
            goto error;
        }
    }
    g_dir_close (dir);
    free (dbpath);
    
    *newpath = folder;
    return TRUE;

error:
    free (dbpath);
    g_free (folder);
    return FALSE;
}

gboolean
kalu_alpm_load (const gchar *conffile, GError **error)
{
    GError             *local_err = NULL;
    gchar              *newpath;
    enum _alpm_errno_t  err;
    pacman_config_t    *pac_conf = NULL;
    
    /* parse pacman.conf */
    debug ("parsing pacman.conf (%s) for options", conffile);
    if (!parse_pacman_conf (conffile, NULL, 0, 0, &pac_conf, &local_err))
    {
        g_propagate_error (error, local_err);
        free_pacman_config (pac_conf);
        return FALSE;
    }
    
    debug ("setting up libalpm");
    if (NULL == (alpm = calloc (1, sizeof (*alpm))))
    {
        g_set_error (error, KALU_ERROR, 1, "Unable to allocate memory");
        free_pacman_config (pac_conf);
        return FALSE;
    }
    
    /* create tmp copy of db (so we can sync w/out being root) */
    if (!create_local_db (pac_conf->dbpath, &newpath, &local_err))
    {
        g_set_error (error, KALU_ERROR, 1, "Unable to create local copy of database: %s",
            local_err->message);
        g_clear_error (&local_err);
        free_pacman_config (pac_conf);
        kalu_alpm_free ();
        return FALSE;
    }
    alpm->dbpath = newpath;
    
    /* init libalpm */
    alpm->handle = alpm_initialize (pac_conf->rootdir, alpm->dbpath, &err);
    if (alpm->handle == NULL)
    {
        g_set_error (error, KALU_ERROR, 1, "Failed to initialize alpm library: %s",
            alpm_strerror (err));
        free_pacman_config (pac_conf);
        kalu_alpm_free ();
        return FALSE;
    }
    
    /* set arch & some options (what to ignore during update) */
    alpm_option_set_arch (alpm->handle, pac_conf->arch);
    alpm_option_set_ignorepkgs (alpm->handle, pac_conf->ignorepkgs);
    alpm_option_set_ignoregroups (alpm->handle, pac_conf->ignoregroups);
    
    /* now we need to add dbs */
    alpm_list_t *i;
    for (i = pac_conf->databases; i; i = alpm_list_next (i))
    {
        database_t *db_conf = i->data;
        alpm_db_t *db;
        
        /* register db */
        debug ("register %s", db_conf->name);
        db = alpm_db_register_sync (alpm->handle, db_conf->name, db_conf->siglevel);
        if (db == NULL)
        {
            g_set_error (error, KALU_ERROR, 1, "Could not register database %s: %s",
                db_conf->name, alpm_strerror (alpm_errno (alpm->handle)));
            free_pacman_config (pac_conf);
            kalu_alpm_free ();
            return FALSE;
        }
        
        /* add servers */
        alpm_list_t *j;
        for (j = db_conf->servers; j; j = alpm_list_next (j))
        {
            char *value = j->data;
            const char *dbname = alpm_db_get_name (db);
            /* let's attempt a replacement for the current repo */
            char *temp = strreplace (value, "$repo", dbname);
            /* let's attempt a replacement for the arch */
            const char *arch = pac_conf->arch;
            char *server;
            
            if (arch)
            {
                server = strreplace (temp, "$arch", arch);
                free (temp);
            }
            else
            {
                if (strstr (temp, "$arch"))
                {
                    g_set_error (error, KALU_ERROR, 1, "Server %s contains the $arch variable, "
                        "but no Architecture was defined", value);
                    free (temp);
                    free (value);
                    free_pacman_config (pac_conf);
                    kalu_alpm_free ();
                    return FALSE;
                }
                server = temp;
            }
            
            debug ("add server %s into %s", server, dbname);
            if (alpm_db_add_server (db, server) != 0)
            {
                /* pm_errno is set by alpm_db_setserver */
                g_set_error (error, KALU_ERROR, 1, "Could not add server %s to database %s: %s",
                    server, dbname, alpm_strerror (alpm_errno (alpm->handle)));
                free (server);
                free (value);
                free_pacman_config (pac_conf);
                kalu_alpm_free ();
                return FALSE;
            }
            
            free (server);
        }
    }
    
    /* set global var */
    alpm_verbose = pac_conf->verbosepkglists;
    
    free_pacman_config (pac_conf);
    return TRUE;
}

gboolean
kalu_alpm_syncdbs (gint *nb_dbs_synced, GError **error)
{
    alpm_list_t     *sync_dbs   = NULL;
    alpm_list_t     *i;
    GError          *local_err  = NULL;
    int             ret;
    
    if (!check_syncdbs (alpm, 1, 0, &local_err))
    {
        g_propagate_error (error, local_err);
		return FALSE;
	}
    
    sync_dbs = alpm_option_get_syncdbs (alpm->handle);
    *nb_dbs_synced = 0;
    for (i = sync_dbs; i; i = alpm_list_next (i))
    {
		alpm_db_t *db = i->data;

        ret = alpm_db_update (0, db);
        if (ret < 0)
        {
            g_set_error (error, KALU_ERROR, 1, "Failed to update %s: %s",
                alpm_db_get_name (db), alpm_strerror (alpm_errno (alpm->handle)));
            return FALSE;
		}
        else if (ret == 1)
        {
			debug ("%s is up to date", alpm_db_get_name (db));
		}
        else
        {
            ++*nb_dbs_synced;
            debug ("%s was updated", alpm_db_get_name (db));
		}
	}
    
    return TRUE;
}

gboolean
kalu_alpm_has_updates (alpm_list_t **packages, GError **error)
{
    alpm_list_t *i;
    alpm_list_t *data       = NULL;
    GError      *local_err  = NULL;
    
    if (!check_syncdbs (alpm, 1, 1, &local_err))
    {
        g_propagate_error (error, local_err);
		return FALSE;
	}
    
    if (!trans_init (alpm, alpm->flags, 1, &local_err) == -1)
    {
        g_propagate_error (error, local_err);
		return FALSE;
	}
    
    if (alpm_sync_sysupgrade (alpm->handle, 0) == -1)
    {
        g_set_error (error, KALU_ERROR, 1, "%s", alpm_strerror (alpm_errno (alpm->handle)));
        goto cleanup;
    }
    
    if (alpm_trans_prepare (alpm->handle, &data) == -1)
    {
        int len = 1024;
        gchar buf[255], err[len--];
        err[0] = '\0';
		switch (alpm_errno (alpm->handle))
        {
			case ALPM_ERR_PKG_INVALID_ARCH:
				for (i = data; i; i = alpm_list_next (i))
                {
					const char *pkg = i->data;
                    len -= snprintf (buf, 255, "- Package %s does not have a valid architecture\n",
                                    pkg);
					if (len >= 0)
                    {
                        strncat (err, buf, (size_t) len);
                    }
				}
				break;
			case ALPM_ERR_UNSATISFIED_DEPS:
				for (i = data; i; i = alpm_list_next (i))
                {
					alpm_depmissing_t *miss = i->data;
					char *depstring = alpm_dep_compute_string (miss->depend);
                    len -= snprintf (buf, 255, "- %s requires %s\n", miss->target, depstring);
					if (len >= 0)
                    {
                        strncat (err, buf, (size_t) len);
                    }
					free (depstring);
				}
				break;
			case ALPM_ERR_CONFLICTING_DEPS:
				for (i = data; i; i = alpm_list_next (i))
                {
					alpm_conflict_t *conflict = i->data;
					/* only print reason if it contains new information */
					if (conflict->reason->mod == ALPM_DEP_MOD_ANY)
                    {
						len -= snprintf (buf, 255, "- %s and %s are in conflict\n",
								 conflict->package1, conflict->package2);
                        if (len >= 0)
                        {
                            strncat (err, buf, (size_t) len);
                        }
					}
                    else
                    {
						char *reason = alpm_dep_compute_string (conflict->reason);
						len -= snprintf (buf, 255, "- %s and %s are in conflict (%s)\n",
								 conflict->package1, conflict->package2, reason);
                        if (len >= 0)
                        {
                            strncat (err, buf, (size_t) len);
                        }
                        free (reason);
					}
				}
				break;
			default:
				break;
		}
        g_set_error (error, KALU_ERROR, 2, "Failed to prepare transaction: %s\n%s",
            alpm_strerror (alpm_errno (alpm->handle)), err);
		goto cleanup;
	}
    
    alpm_db_t  *db_local = alpm_option_get_localdb (alpm->handle);
    for (i = alpm_trans_get_add (alpm->handle); i; i = alpm_list_next (i))
    {
        alpm_pkg_t *pkg = i->data;
        alpm_pkg_t *old = alpm_db_get_pkg (db_local, alpm_pkg_get_name (pkg));
        kalu_package_t *package;
        
        package = calloc (1, sizeof (*package));
        package->name = strdup (alpm_pkg_get_name (pkg));
        package->new_version = strdup (alpm_pkg_get_version (pkg));
        package->dl_size = (guint) alpm_pkg_download_size (pkg);
        package->new_size = (guint) alpm_pkg_get_isize (pkg);
        /* we might not have an old package, when an update requires to
         * install a new package (e.g. after a split) */
        if (old)
        {
            package->old_version = strdup (alpm_pkg_get_version (old));
            package->old_size = (guint) alpm_pkg_get_isize (old);
        }
        else
        {
            package->old_version = strdup ("none");
            package->old_size = 0;
        }
        
        *packages = alpm_list_add (*packages, package);
    }
    
cleanup:
	if (data)
    {
		FREELIST (data);
	}
    trans_release (alpm, NULL);
    
    return (*packages != NULL);
}

gboolean
kalu_alpm_has_updates_watched (alpm_list_t **packages, alpm_list_t *watched, GError **error)
{
    alpm_list_t *sync_dbs = alpm_option_get_syncdbs (alpm->handle);
    alpm_list_t *i, *j;
    GError *local_err = NULL;
    
    if (!check_syncdbs (alpm, 1, 1, &local_err))
    {
        g_propagate_error (error, local_err);
		return FALSE;
	}
    
    for (i = watched; i; i = alpm_list_next (i))
    {
        watched_package_t *w_pkg = i->data;
        for (j = sync_dbs; j; j = alpm_list_next (j))
        {
            alpm_pkg_t *pkg = alpm_db_get_pkg ((alpm_db_t *) j->data, w_pkg->name);
            if (pkg)
            {
                if (alpm_pkg_vercmp (alpm_pkg_get_version (pkg), w_pkg->version) > 0)
                {
                    kalu_package_t *package;
                    package = calloc (1, sizeof (*package));
                    
                    package->name = strdup (alpm_pkg_get_name (pkg));
                    package->old_version = strdup (w_pkg->version);
                    package->new_version = strdup (alpm_pkg_get_version (pkg));
                    package->dl_size = (guint) alpm_pkg_download_size (pkg);
                    package->new_size = (guint) alpm_pkg_get_isize (pkg);
                    
                    *packages = alpm_list_add (*packages, package);
                    debug ("found watched update %s: %s -> %s", package->name,
                           package->old_version, package->new_version);
                }
                break;
            }
        }
    }
    
    return (*packages != NULL);
}

gboolean
kalu_alpm_has_foreign (alpm_list_t **packages, alpm_list_t *ignore, GError **error)
{
    alpm_db_t *dblocal;
    alpm_list_t *sync_dbs, *i, *j;
    gboolean found;
    GError *local_err = NULL;
    
    if (!check_syncdbs (alpm, 1, 1, &local_err))
    {
        g_propagate_error (error, local_err);
		return FALSE;
	}
    
    dblocal  = alpm_option_get_localdb (alpm->handle);
    sync_dbs = alpm_option_get_syncdbs (alpm->handle);
    
    for (i = alpm_db_get_pkgcache (dblocal); i; i = alpm_list_next (i))
    {
        alpm_pkg_t *pkg = i->data;
        const char *pkgname = alpm_pkg_get_name (pkg);
        found = FALSE;
        
        if (NULL != alpm_list_find_str (ignore, pkgname))
        {
            continue;
        }
        
        for (j = sync_dbs; j; j = alpm_list_next (j))
        {
            if (alpm_db_get_pkg ((alpm_db_t *) j->data, pkgname))
            {
                found = TRUE;
                break;
            }
        }
        if (!found)
        {
            *packages = alpm_list_add (*packages, pkg);
        }
    }
    
    return (*packages != NULL);
}

void
kalu_alpm_free (void)
{
    if (alpm == NULL)
    {
        return;
    }
    
    if (alpm->handle != NULL)
    {
        alpm_release (alpm->handle);
    }
    
    /* yes, we remove the dbpath. because we made a tmp copy of it */
    if (alpm->dbpath)
    {
        rmrf (alpm->dbpath);
    }
	free (alpm->dbpath);
    
    g_free (alpm);
    alpm = NULL;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.