cache.c 4.21 KB
Newer Older
Timo Teräs's avatar
Timo Teräs committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/* cache.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <dirent.h>
#include <unistd.h>
14
#include <stdio.h>
Timo Teräs's avatar
Timo Teräs committed
15 16 17 18

#include "apk_defines.h"
#include "apk_applet.h"
#include "apk_database.h"
19
#include "apk_state.h"
20
#include "apk_package.h"
Timo Teräs's avatar
Timo Teräs committed
21 22 23 24 25 26

#define CACHE_CLEAN	BIT(0)
#define CACHE_DOWNLOAD	BIT(1)

static int cache_download(struct apk_database *db)
{
27 28
	struct apk_state *state;
	struct apk_change *change;
Timo Teräs's avatar
Timo Teräs committed
29 30 31 32
	struct apk_package *pkg;
	char pkgfile[256];
	int i, r;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	if (db->world == NULL)
		return 0;

	state = apk_state_new(db);
	for (i = 0; i < db->world->num; i++) {
		r = apk_state_lock_dependency(state, &db->world->item[i]);
		if (r != 0) {
			apk_error("Unable to select version for '%s'",
				  db->world->item[i].name->name);
			goto err;
		}
	}

	list_for_each_entry(change, &state->change_list_head, change_list) {
		pkg = change->newpkg;
Timo Teräs's avatar
Timo Teräs committed
48 49
		snprintf(pkgfile, sizeof(pkgfile), "%s-%s.apk",
			 pkg->name->name, pkg->version);
Timo Teräs's avatar
Timo Teräs committed
50
		if (apk_cache_exists(db, &pkg->csum, pkgfile))
Timo Teräs's avatar
Timo Teräs committed
51 52 53 54 55
			continue;
		for (i = 0; i < db->num_repos; i++) {
			if (!(pkg->repos & BIT(i)))
				continue;

Timo Teräs's avatar
Timo Teräs committed
56
			r = apk_cache_download(db, &pkg->csum, db->repos[i].url,
Timo Teräs's avatar
Timo Teräs committed
57 58 59 60 61
					       pkgfile);
			if (r != 0)
				return r;
		}
	}
62 63 64 65

err:
	apk_state_unref(state);
	return r;
Timo Teräs's avatar
Timo Teräs committed
66 67 68 69 70 71
}

static int cache_clean(struct apk_database *db)
{
	DIR *dir;
	struct dirent *de;
Timo Teräs's avatar
Timo Teräs committed
72
	char path[256], csum[APK_CACHE_CSUM_BYTES];
Timo Teräs's avatar
Timo Teräs committed
73
	int delete, i;
74 75
	apk_blob_t b, bname, bver;
	struct apk_name *name;
Timo Teräs's avatar
Timo Teräs committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89

	snprintf(path, sizeof(path), "%s/%s", db->root, db->cache_dir);
	if (chdir(path) != 0)
		return -1;

	dir = opendir(path);
	if (dir == NULL)
		return -1;

	while ((de = readdir(dir)) != NULL) {
		if (de->d_name[0] == '.')
			continue;
		delete = TRUE;
		do {
90
			b = APK_BLOB_STR(de->d_name);
91
			apk_blob_pull_hexdump(&b, APK_BLOB_BUF(csum));
92 93 94
			apk_blob_pull_char(&b, '.');

			if (apk_blob_compare(b, APK_BLOB_STR(apk_index_gz)) == 0) {
Timo Teräs's avatar
Timo Teräs committed
95
				/* Index - check for matching repository */
96
				for (i = 0; i < db->num_repos; i++) {
Timo Teräs's avatar
Timo Teräs committed
97
					if (memcmp(db->repos[i].csum.data,
98 99 100 101 102 103 104
						   csum, APK_CACHE_CSUM_BYTES) != 0)
						continue;
					delete = 0;
					break;
				}
			} else if (b.len > 4 &&
				   memcmp(b.ptr+b.len-4, ".apk", 4) == 0) {
Timo Teräs's avatar
Timo Teräs committed
105
				/* Package - search for it */
106
				if (apk_pkg_parse_name(b, &bname, &bver) < 0)
107 108
					break;

109 110 111 112 113 114 115 116 117 118 119
				name = apk_db_get_name(db, bname);
				if (name == NULL || name->pkgs == NULL)
					break;

				for (i = 0; i < name->pkgs->num; i++) {
					struct apk_package *pkg = name->pkgs->item[i];
					if (memcmp(pkg->csum.data, csum, APK_CACHE_CSUM_BYTES) != 0)
						continue;
					delete = 0;
					break;
				}
Timo Teräs's avatar
Timo Teräs committed
120 121 122 123 124
			}
		} while (0);

		if (delete) {
			if (apk_verbosity >= 2)
125
				apk_message("deleting %s", de->d_name);
Timo Teräs's avatar
Timo Teräs committed
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
			if (!(apk_flags & APK_SIMULATE))
				unlink(de->d_name);
		}
	}

	closedir(dir);
	return 0;
}

static int cache_main(void *ctx, int argc, char **argv)
{
	struct apk_database db;
	int actions = 0;
	int r;

	if (argc != 1)
		return -100;

	if (strcmp(argv[0], "sync") == 0)
		actions = CACHE_CLEAN | CACHE_DOWNLOAD;
	else if (strcmp(argv[0], "clean") == 0)
		actions = CACHE_CLEAN;
	else if (strcmp(argv[0], "download") == 0)
		actions = CACHE_DOWNLOAD;
	else
		return -100;

	r = apk_db_open(&db, apk_root,
154
			APK_OPENF_NO_SCRIPTS | APK_OPENF_NO_INSTALLED);
Timo Teräs's avatar
Timo Teräs committed
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
	if (r != 0)
		return r;

	if (!apk_db_cache_active(&db)) {
		apk_error("Package cache is not enabled.\n");
		r = 2;
		goto err;
	}

	if (r == 0 && (actions & CACHE_CLEAN))
		r = cache_clean(&db);
	if (r == 0 && (actions & CACHE_DOWNLOAD))
		r = cache_download(&db);

err:
	apk_db_close(&db);
	return r;
}

static struct apk_applet apk_cache = {
	.name = "cache",
	.help = "Download missing PACKAGEs to cache directory, or delete "
		"files no longer required. Package caching is enabled by "
		"making /etc/apk/cache a symlink to the directory (on boot "
		"media) that will be used as package cache.",
	.arguments = "sync | clean | download",
	.main = cache_main,
};

APK_DEFINE_APPLET(apk_cache);