aboutsummaryrefslogtreecommitdiffstats
path: root/src/configuration.c
blob: d2a23d45f4989784a214e6be5e9e232f194e65e7 (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
/*
 * 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"


rc_config *read_rc_config(const char *file_name){
	FILE *rc = NULL;
	rc_config *global_config;
	char *getline_buffer = NULL;
	size_t gb_length = 0;
	ssize_t g_size;

	global_config = malloc( sizeof *global_config );
	if( global_config == NULL ){
		fprintf(stderr,"Failed to malloc global_config\n");
		if( errno ){
			perror("global_config malloc");
		}
	}

	rc = fopen(file_name,"r");
	if( rc == NULL ){
		fprintf(stderr,"Please create: %s.\n",file_name);
		if( errno ){
			perror(file_name);
		}
		exit(1);
	}

	while( (g_size = getline(&getline_buffer,&gb_length,rc) ) != EOF ){
		getline_buffer[g_size - 1] = '\0';

		/* check to see if it has our key and value seperated by our token */
		/* and extract them */

		if( getline_buffer[0] == '#' ){
			continue;
		}

		if( strstr(getline_buffer,MIRROR_TOKEN) != NULL ){ /* MIRROR URL */

			if( strlen(getline_buffer) > strlen(MIRROR_TOKEN) ){
				memcpy(
					global_config->mirror_url,
					getline_buffer + strlen(MIRROR_TOKEN),
					(strlen(getline_buffer) - strlen(MIRROR_TOKEN))
				);

				/* make sure our url has a trailing '/' */
				if( global_config->mirror_url[strlen(global_config->mirror_url) - 1] != '/' ){
					strcat(global_config->mirror_url,"/");
				}

			}

		} else if( strstr(getline_buffer,WORKINGDIR_TOKEN) != NULL ){ /* WORKING DIR */

			if( strlen(getline_buffer) > strlen(WORKINGDIR_TOKEN) ){
				memcpy(
					global_config->working_dir,
					getline_buffer + strlen(WORKINGDIR_TOKEN),
					(strlen(getline_buffer) - strlen(WORKINGDIR_TOKEN))
				);
			}

		}else if( strstr(getline_buffer,EXCLUDE_TOKEN) != NULL ){ /* exclude list */
			global_config->exclude_list = parse_exclude(getline_buffer);
		}/* end if/else if */

	}/* end while */
	if( getline_buffer ) free(getline_buffer);

	if( strcmp(global_config->working_dir,"") == 0 ){
		fprintf(stderr,"WORKINGDIR directive not set within %s.\n",file_name);
		exit(1);
	}
	if( strcmp(global_config->mirror_url,"") == 0 ){
		fprintf(stderr,"MIRROR directive not set within %s.\n",file_name);
		exit(1);
	}

	/* initialize */
	global_config->download_only = 0;
	global_config->dist_upgrade = 0;
	global_config->simulate = 0;
	global_config->ignore_excludes = 0;

	return global_config;
}

void working_dir_init(const rc_config *global_config){
	if( access(global_config->working_dir,W_OK) == -1 ){
		if(errno && errno == 2 ){
			if( mkdir(global_config->working_dir,
			S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == -1 ){
				printf("Failed to build working directory [%s]\n",global_config->working_dir);
				if( errno ){
					perror(global_config->working_dir);
					exit(1);
				}
			}else{
				/* we should now have a global_config->working_dir */
				return;
			}
		}else if( errno && errno == 13 ){
			perror(global_config->working_dir);
			fprintf(stderr,"Please update permissions on %s or run with appropriate privileges\n",
				global_config->working_dir);
			exit(1);
		}else{
			fprintf(stderr,"Please update permissions on %s or run with appropriate privileges\n",
				global_config->working_dir);
			exit(1);
		}
	}else{
		return;
	}
}

FILE *open_file(const char *file_name,const char *mode){
	FILE *fh = NULL;
	if( (fh = fopen(file_name,mode)) == NULL ){
		fprintf(stderr,"Failed to open %s\n",file_name);
		if( errno ){
			perror(file_name);
		}
		fprintf(stderr,"Perhaps you want to run --update?\n");
		exit(1);
	}
	return fh;
}

char spinner(void){
	static int spinner_index = 0;
	static char *spinner_parts = "\\|/-";

	if( spinner_index > 3 ){
		spinner_index = 0;
		return spinner_parts[spinner_index];
	}else{
		return spinner_parts[spinner_index++];
	}
}

void clean_pkg_dir(const char *dir_name){
	DIR *tmp;
	struct dirent *file;
	struct stat file_stat;

	if( (tmp = opendir(dir_name)) == NULL ){
		fprintf(stderr,"Failed to opendir %s\n",dir_name);
		return;
	}

	if( chdir(dir_name) == -1 ){
		fprintf(stderr,"Failed to chdir: %s\n",dir_name);
		exit(1);
	}

	while( (file = readdir(tmp)) ){

		/* make sure we don't have . or .. */
		if( (strcmp(file->d_name,"..")) == 0 || (strcmp(file->d_name,".") == 0) )
			continue;

		/* setup file_stat struct */
		if( (stat(file->d_name,&file_stat)) == -1)
			continue;

		/* if its a directory, recurse */
		if( S_ISDIR(file_stat.st_mode) ){
			clean_pkg_dir(file->d_name);
			if( (chdir("..")) == -1 ){
				fprintf(stderr,"Failed to chdir: %s\n",dir_name);
				exit(1);
			}
			continue;
		}
		if( strstr(file->d_name,".tgz") !=NULL ){
#if DEBUG == 1
			printf("unlinking %s\n",file->d_name);
#endif
			unlink(file->d_name);
		}
	}
	closedir(tmp);

}

struct exclude_list *parse_exclude(char *line){
	char *pointer = NULL;
	char *buffer = NULL;
	int position = 0;
	struct exclude_list *list;
	list = malloc( sizeof *list );
	if( list == NULL ){
		fprintf(stderr,"Failed to malloc list\n");
		if( errno ){
			perror("malloc");
		}
		exit(1);
	}
	list->count = 0;

	/* skip ahead past the = */
	line = index(line,'=') + 1;

	while( position < (int) strlen(line) ){
		if( strstr(line + position,",") == NULL ){

			pointer = line + position;
			memcpy(list->excludes[ list->count ], pointer, strlen(pointer) );
			list->excludes[ list->count ][strlen(pointer)] =  '\0';

			list->count++;
			break;
		}else{

			if( line[position] == ',' ){
				position++;
				continue;
			}else{

				pointer = index(line + position,',');
				buffer = calloc( strlen(line + position) - strlen(pointer) + 1, sizeof(char) );
				memcpy(buffer,line + position,strlen(line + position) - strlen(pointer) );
				buffer[ strlen(line + position) - strlen(pointer) ] = '\0';
				memcpy(list->excludes[ list->count ], buffer, strlen(buffer) );

				list->count++;
				position += (strlen(line + position) - strlen(pointer) );
				free(buffer);
			}
			continue;
		}
	}
	
	return list;
}

int is_excluded(const rc_config *global_config,const char *pkg_name){
	int i;

	if( global_config->ignore_excludes == 1 )
		return 0;

	/* maybe EXCLUDE= isn't defined in our rc? */
	if( global_config->exclude_list == NULL )
		return 0;

	for(i = 0; i < global_config->exclude_list->count;i++){
		/*
		 * this is kludgy... global_config->exclude_list->excludes[i] is 1 char longer
		 * than pkg_name
		*/
		if( (strncmp(global_config->exclude_list->excludes[i],pkg_name,strlen(pkg_name)) == 0)
			&& (strlen(global_config->exclude_list->excludes[i]) - strlen(pkg_name) < 2) ){
			return 1;
		}
	}

	return 0;
}

/* recursively create dirs */
void create_dir_structure(const char *dir_name){
	char *pointer = NULL;
	char *cwd = NULL;
	int position = 0;
	char *dir_name_buffer = NULL;

	cwd = getcwd(NULL,0);
	if( cwd == NULL ){
		fprintf(stderr,"Failed to get cwd\n");
		exit(1);
	}

	while( position < (int) strlen(dir_name) ){
		/* if no more directory delim, then this must be last dir */
		if( strstr(dir_name + position,"/" ) == NULL ){

			/* pointer = dir_name + position; */
			dir_name_buffer = calloc( strlen(dir_name + position) + 1 , sizeof(char) );
			memcpy(dir_name_buffer,dir_name + position,strlen(dir_name + position));
			dir_name_buffer[ strlen(dir_name + position) ] = '\0';

			if( strcmp(dir_name_buffer,".") != 0 ){
				if( (mkdir(dir_name_buffer,0755)) == -1){
#if DEBUG == 1
					fprintf(stderr,"Failed to mkdir: %s\n",dir_name_buffer);
#endif
					/* exit(1); */
				}
				if( (chdir(dir_name_buffer)) == -1 ){
					fprintf(stderr,"Failed to chdir to %s\n",dir_name_buffer);
					exit(1);
				}
			}/* don't create . */

			free(dir_name_buffer);
			break;
		}else{
			if( dir_name[position] == '/' ){
				/* move on ahead */
				position++;
			}else{

				/* figure our dir name and mk it */
				pointer = index(dir_name + position,'/');
				dir_name_buffer = calloc(
					strlen(dir_name + position) - strlen(pointer) + 1 , sizeof(char)
				);
				memcpy(dir_name_buffer,dir_name + position, strlen(dir_name + position) - strlen(pointer));
				dir_name_buffer[ (strlen(dir_name + position) - strlen(pointer)) ] = '\0';

				if( strcmp(dir_name_buffer,".") != 0 ){
					if( (mkdir(dir_name_buffer,0755)) == -1 ){
#if DEBUG == 1
						fprintf(stderr,"Failed to mkdir: %s\n",dir_name_buffer);
#endif
						/* exit(1); */
					}
					if( (chdir(dir_name_buffer)) == -1 ){
						fprintf(stderr,"Failed to chdir to %s\n",dir_name_buffer);
						exit(1);
					}
				} /* don't create . */

				free(dir_name_buffer);
				position += (strlen(dir_name + position) - strlen(pointer));
			}
		}
	}/* end while */

	if( (chdir(cwd)) == -1 ){
		fprintf(stderr,"Failed to chdir to %s\n",cwd);
		exit(1);
	}

	free(cwd);
}