aboutsummaryrefslogtreecommitdiffstats
path: root/src/action.c
blob: 0e48599de0e97108365042edb6ec304d4dbfb640 (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
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
/*
 * Copyright (C) 2003 Jason Woodward <woodwardj at jaos dot org>
 *
 * This program 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 2 of the License, or
 * any later version.
 *
 * This program 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 Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <main.h>

/* clean out local cache */
void pkg_action_clean(const rc_config *global_config){
	clean_pkg_dir(global_config->working_dir);
}

/* install pkg */
void pkg_action_install(const rc_config *global_config,const pkg_action_args_t *action_args){
	int i;
	pkg_info_t *installed_pkg;
	pkg_info_t *pkg;
	transaction tran;

	struct pkg_list *installed;
	struct pkg_list *all;

	printf("Reading Package Lists... ");
	installed = get_installed_pkgs();
	all = get_available_pkgs();
	printf("Done\n");

	init_transaction(&tran);

	for(i = 0; i < action_args->count; i++){

		/* make sure there is a package called action_args->pkgs[i] */
		if( (pkg = get_newest_pkg(all->pkgs,action_args->pkgs[i],all->pkg_count)) == NULL ){
			fprintf(stderr,"No Such package: %s\n",action_args->pkgs[i]);
			continue;
		}

		/* if it's not already installed, install it */
		if(
			(installed_pkg
				= get_newest_pkg(installed->pkgs,action_args->pkgs[i],installed->pkg_count)
			) == NULL
		){

			/* this way we install the most up to date pkg */
			add_install_to_transaction(&tran,pkg);

		}else{ /* else we upgrade or reinstall */

			/* it's already installed, attempt an upgrade */
			if(
				((cmp_pkg_versions(installed_pkg->version,pkg->version)) < 0)
				|| (global_config->re_install == 1)
			){
				add_upgrade_to_transaction(&tran,installed_pkg,pkg);
			}else{
				printf("%s is up to date.\n",installed_pkg->name);
			}
	
		}

	}

	handle_transaction(global_config,&tran);

	free_pkg_list(installed);
	free_pkg_list(all);
	free_transaction(&tran);
	return;
}

/* list pkgs */
void pkg_action_list(void){
	struct pkg_list *pkgs = NULL;
	int iterator;

	pkgs = get_available_pkgs();

	for(iterator = 0; iterator < pkgs->pkg_count; iterator++ ){
		/* this should eliminate the printing of updates */
		if( strstr(pkgs->pkgs[iterator]->description,"no description") == NULL ){
			char *short_description = gen_short_pkg_description(pkgs->pkgs[iterator]);
			printf("%s - %s\n",pkgs->pkgs[iterator]->name,short_description);
			free(short_description);
		}
	}

	free_pkg_list(pkgs);

}

/* list installed pkgs */
void pkg_action_list_installed(void){
	int iterator;
	struct pkg_list *installed_pkgs = NULL;

	installed_pkgs = get_installed_pkgs();

	for(iterator = 0; iterator < installed_pkgs->pkg_count; iterator++ ){
		printf("%s - %s\n",
			installed_pkgs->pkgs[iterator]->name,
			installed_pkgs->pkgs[iterator]->version
		);
	}

	free_pkg_list(installed_pkgs);
}

/* remove/uninstall pkg */
void pkg_action_remove(const rc_config *global_config,const pkg_action_args_t *action_args){
	int i;
	pkg_info_t *pkg;
	struct pkg_list *installed;
	transaction tran;

	installed = get_installed_pkgs();
	init_transaction(&tran);

	for(i = 0; i < action_args->count; i++){
		if(
			(pkg = get_newest_pkg(installed->pkgs,action_args->pkgs[i],installed->pkg_count)) != NULL
		){
			add_remove_to_transaction(&tran,pkg);
		}else{
			printf("%s is not installed.\n",action_args->pkgs[i]);
		}
	}

	handle_transaction(global_config,&tran);

	free_pkg_list(installed);
	free_transaction(&tran);
}

/* search for a pkg (support extended POSIX regex) */
void pkg_action_search(const char *pattern){
	struct pkg_list *pkgs = NULL;
	int iterator;
	sg_regex search_regex;
	search_regex.nmatch = MAX_REGEX_PARTS;

	/* compile our regex */
	search_regex.reg_return = regcomp(&search_regex.regex, pattern, REG_EXTENDED|REG_NEWLINE);
	if( search_regex.reg_return != 0 ){
		size_t regerror_size;
		char errbuf[1024];
		size_t errbuf_size = 1024;
		fprintf(stderr, "Failed to compile regex\n");

		regerror_size = regerror(search_regex.reg_return, &search_regex.regex,errbuf,errbuf_size);
		if( regerror_size != 0 ){
			printf("Regex Error: %s\n",errbuf);
		}
		exit(1);
	}

	/* read in pkg data */
	pkgs = get_available_pkgs();

	for(iterator = 0; iterator < pkgs->pkg_count; iterator++ ){
		if(
			( regexec(
				&search_regex.regex,
				pkgs->pkgs[iterator]->name,
				search_regex.nmatch,
				search_regex.pmatch,
				0
			) == 0)
			||
			( regexec(
				&search_regex.regex,
				pkgs->pkgs[iterator]->description,
				search_regex.nmatch,
				search_regex.pmatch,
				0
			) == 0)
		){
				char *short_description = gen_short_pkg_description(pkgs->pkgs[iterator]);
				printf("%s - %s\n",pkgs->pkgs[iterator]->name,short_description);
				free(short_description);
		}
	}
	regfree(&search_regex.regex);
	free_pkg_list(pkgs);

}/* end search */

/* show the details for a specific package */
void pkg_action_show(const char *pkg_name){
	pkg_info_t *pkg;
	struct pkg_list *available_pkgs;

	available_pkgs = get_available_pkgs();

	pkg = get_newest_pkg(available_pkgs->pkgs,pkg_name,available_pkgs->pkg_count);

	if( pkg != NULL ){
		printf("Package Name: %s\n",pkg->name);
		printf("Package Mirror: %s\n",pkg->mirror);
		printf("Package Location: %s\n",pkg->location);
		printf("Package Version: %s\n",pkg->version);
		printf("Package Description:\n");
		printf("%s",pkg->description);
	}else{
		printf("No such package: %s\n",pkg_name);
	}

	free_pkg_list(available_pkgs);
}

/* update package data from mirror url */
void pkg_action_update(const rc_config *global_config){
	int i;
	FILE *pkg_list_fh;
	FILE *patches_list_fh;
	FILE *checksum_list_fh;
	FILE *tmp_file;
	struct pkg_list *available_pkgs = NULL;

	/* download our PKG_LIST */
	#if USE_CURL_PROGRESS == 0
	printf("Retrieving package data...");
	#else
	printf("Retrieving package data...\n");
	#endif
	pkg_list_fh = open_file(PKG_LIST_L,"w+");
	for(i = 0; i < global_config->sources.count; i++){
		tmp_file = tmpfile();
		if( get_mirror_data_from_source(tmp_file,global_config->sources.url[i],PKG_LIST) == 0 ){
			rewind(tmp_file); /* make sure we are back at the front of the file */
			available_pkgs = parse_packages_txt(tmp_file);
			write_pkg_data(global_config->sources.url[i],pkg_list_fh,available_pkgs);
			free_pkg_list(available_pkgs);
		}
		fclose(tmp_file);
	}
	#if USE_CURL_PROGRESS == 0
	printf("Done\n");
	#endif

	/* download EXTRAS_LIST */
	#if USE_CURL_PROGRESS == 0
	printf("Retrieving extras list...");
	#else
	printf("Retrieving extras list...\n");
	#endif
	for(i = 0; i < global_config->sources.count; i++){
		tmp_file = tmpfile();
		if( get_mirror_data_from_source(tmp_file,global_config->sources.url[i],EXTRAS_LIST) == 0 ){
			rewind(tmp_file); /* make sure we are back at the front of the file */
			available_pkgs = parse_packages_txt(tmp_file);
			write_pkg_data(global_config->sources.url[i],pkg_list_fh,available_pkgs);
			free_pkg_list(available_pkgs);
		}
		fclose(tmp_file);
	}
	#if USE_CURL_PROGRESS == 0
	printf("Done\n");
	#endif

	/* download PATCHES_LIST */
	#if USE_CURL_PROGRESS == 0
	printf("Retrieving patch list...");
	#else
	printf("Retrieving patch list...\n");
	#endif
	for(i = 0; i < global_config->sources.count; i++){
		patches_list_fh = tmpfile();
		if( get_mirror_data_from_source(patches_list_fh,global_config->sources.url[i],PATCHES_LIST) == 0 ){
			rewind(patches_list_fh); /* make sure we are back at the front of the file */
			available_pkgs = parse_packages_txt(patches_list_fh);
			write_pkg_data(global_config->sources.url[i],pkg_list_fh,available_pkgs);
			free_pkg_list(available_pkgs);
		}
		fclose(patches_list_fh);
	}
	#if USE_CURL_PROGRESS == 0
	printf("Done\n");
	#endif
	fclose(pkg_list_fh);

	/* download checksum file */
	#if USE_CURL_PROGRESS == 0
	printf("Retrieving checksum list...");
	#else
	printf("Retrieving checksum list...\n");
	#endif
	checksum_list_fh = open_file(CHECKSUM_FILE,"w+");
	for(i = 0; i < global_config->sources.count; i++){
		get_mirror_data_from_source(checksum_list_fh,global_config->sources.url[i],CHECKSUM_FILE);
	}
	#if USE_CURL_PROGRESS == 0
	printf("Done\n");
	#endif
	fclose(checksum_list_fh);

	/* source listing to go here */

	return;
}

/* upgrade all installed pkgs with available updates */
void pkg_action_upgrade_all(const rc_config *global_config){
	int iterator;
	struct pkg_list *installed_pkgs;
	struct pkg_list *all_pkgs;
	pkg_info_t *update_pkg;
	transaction tran;

	printf("Reading Package Lists... ");
	installed_pkgs = get_installed_pkgs();
	all_pkgs = get_available_pkgs();
	printf("Done\n");
	init_transaction(&tran);

	for(iterator = 0; iterator < installed_pkgs->pkg_count;iterator++){

		/* see if we have an available update for the pkg */
		update_pkg = get_newest_pkg(
			all_pkgs->pkgs,
			installed_pkgs->pkgs[iterator]->name,
			all_pkgs->pkg_count
		);
		if( update_pkg != NULL ){

			if( is_excluded(global_config,update_pkg->name) == 1 ){
				add_exclude_to_transaction(&tran,update_pkg);
				continue;
			}

			/* if the update has a newer version */
			if( (cmp_pkg_versions(installed_pkgs->pkgs[iterator]->version,update_pkg->version)) < 0 ){

				/* attempt to upgrade */
				add_upgrade_to_transaction(&tran,installed_pkgs->pkgs[iterator],update_pkg);

			} /* end version check */

		}/* end upgrade pkg found */

	}/* end for */

	handle_transaction(global_config,&tran);

	free_pkg_list(installed_pkgs);
	free_pkg_list(all_pkgs);
	free_transaction(&tran);
}