summaryrefslogtreecommitdiffstats
path: root/urunlevel/my_linux/getpkg.c
blob: 9127c450d281cd99cf04a5831aeeaa65bef34484 (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
/*
 * Mini getpkg implementation for busybox.
 *
 * Copyright (C) 2004 by David Seikel won_fang@yahoo.com.au
 *
 * 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
 * (at your option) 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
 * 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 <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/utsname.h>		/* for uname(2) */

#include "busybox.h"
#include "lib_init_d.h"
#include "my_linux.h"


#define GETPKG_OPT_ALL		1
#define GETPKG_OPT_FILE		2
#define GETPKG_OPT_KERNEL	4


void init_package(char *temp)
{
    if (stat(temp, &path_stat) == 0)
    {
	doit(0, "chmod a+x %s 2> /dev/null", temp);
	bb_printf("Initializing %s\n", temp);
	doit(0, "%s 2> /dev/null", temp);
    }
}


static char *DEFAULT_PACKAGES = 
"LIB\n"
"system\n"
"baselib\n"
"term\n"
"modutil\n"
"swaputil\n"
"bash2\n"
"pthread\n"
"mc\n"
"ext2tools\n"
"sysutil\n"
"fileutil\n"
"diskutil\n"
"modutils\n"
"portmap\n"
"openssh\n"
"links";

static char *DEFAULT_SERVERS =
"ftp://0.0.0.0/distros/Linux-on-net/Trinux/pkg/\n"
"ftp://192.168.146.25/distros/Linux-on-net/Trinux/pkg/\n"
"http://0.0.0.0/ftp/distros/Linux-on-net/Trinux/pkg/\n"
"http://www.trinux.org/pkg/\n"
"http://www.io.com/~mdfranz/trinux/pkg/";

static char *DEFAULT_PATHS =
"/trinux/pkg\n"
"/trinux/bootpkg\n"
"/trinux/kpkg\n"
"/trinux\n"
"/pkg";

static llist_t *package_head = NULL;   /* growable list of packages */
static llist_t *source_head = NULL;    /* growable list of sources */


int get_package(int opt, char *source, char *package, const char *KERNEL)
{
    int result = 0;
    int incomplete = 0;
    char *PACKAGE = 0;
    char *PRE = 0;
    char *temp = "";

    if (strcmp(&package[bb_strlen(package) - 4], ".tgz") != 0)
	temp = ".tgz";

    bb_xasprintf(&PACKAGE, "%s%s%s", KERNEL, package, temp);
    bb_xasprintf(&PRE,  "%s%s", KERNEL, package);

    if (opt & GETPKG_OPT_FILE)
    {
	temp = concat_path_file(source, PACKAGE);
	free(PACKAGE);
	PACKAGE = temp;
	bb_printf("Installing %s\n", PACKAGE);
    }
    else
    {
	char *URL;

	temp = concat_path_file("/tmp", PACKAGE);
	bb_xasprintf(&URL, "%s%s", source, PACKAGE);
	free(PACKAGE);
	PACKAGE = temp;

//bb_printf("Retrieving %s\n", URL);
	doit(0, "wget -O %s %s", PACKAGE, URL);
	if (errno)
	    incomplete = 1;

	free(URL);
    }

    temp = "";
    if ((incomplete == 0) && (stat(PACKAGE, &path_stat) == 0))
    {
	bb_xasprintf(&temp, doit(0, "gunzip -c %s | tar xvf -", PACKAGE));
	result = 1;
	if (!(opt & GETPKG_OPT_FILE))
	    remove_file(PACKAGE, FILEUTILS_FORCE);
    }
    doit(0, "echo \"%s\" > /var/lib/my_linux/contents/%s", temp, PRE);

    if (result)
    {
	free(temp);
	bb_xasprintf(&temp, "/etc/init.d/%s", PRE);
	init_package(temp);
	temp[10] = 'm';
	init_package(temp);
	free(temp);
	bb_xasprintf(&temp, "/etc/pkg/%s", PRE);
	init_package(temp);
    }

    free(temp);
    free(PACKAGE);
    return result;
}


int getpkg_main(int argc, char **argv)
{
    int i;
    unsigned long opt;
    char *PACKAGE = 0;
    char *KERNEL = "";
    char *SRC = 0;
    char *token;
    char *strtok_temp;

    opt = bb_getopt_ulflags(argc, argv, "afk");

    if (argv[optind])
	PACKAGE = argv[optind++];
    if (argv[optind])
	SRC  = argv[optind++];
    if (PACKAGE == 0)
	opt |= GETPKG_OPT_ALL;
    if (opt & GETPKG_OPT_KERNEL)
    {
	struct utsname name;

	if (uname(&name) == -1)
	    bb_perror_msg_and_die("cannot get system information");
	bb_xasprintf(&KERNEL, "%s/", name.release);
    }

    if (opt & GETPKG_OPT_ALL)
    {
	if (PACKAGE != 0)
	{
	    SRC = PACKAGE;
	    PACKAGE = 0;
	}
	PACKAGE = quick_read("/var/lib/my_linux/config/pkglist");
	if ((PACKAGE == 0) || (PACKAGE[0] == '\0'))
	{
	    PACKAGE = (char *) xmalloc (sizeof (char) * bb_strlen(DEFAULT_PACKAGES) + 1);
	    strcpy(PACKAGE, DEFAULT_PACKAGES);
	}
    }

    if (SRC == 0)
    {
	SRC = quick_read("/var/lib/my_linux/config/server");
	if ((SRC == 0) || (SRC[0] == '\0'))
	{
	    if (opt & GETPKG_OPT_FILE)
		token = DEFAULT_PATHS;
	    else
		token = DEFAULT_SERVERS;
	    SRC = (char *) xmalloc (sizeof (char) * bb_strlen(token) + 1);
	    strcpy(SRC, token);
	}
    }

    token = strtok_r(PACKAGE, " \r\n", &strtok_temp);
    for (i = 0; token != NULL; i++)
    {
	package_head = llist_add_to_end(package_head, token);
	token = strtok_r(NULL, " \r\n", &strtok_temp);
    }

    token = strtok_r(SRC, " \r\n", &strtok_temp);
    for (i = 0; token != NULL; i++)
    {
	source_head = llist_add_to_end(source_head, token);
	token = strtok_r(NULL, " \r\n", &strtok_temp);
    }


    llist_t *package_current = package_head;
    while (package_current) 
    {
	int found = 0;
	llist_t *source_current = source_head;

	while (source_current) 
	{
	    if ((found = get_package(opt, source_current->data, package_current->data, KERNEL)))
		break;
	    source_current = source_current->link;
	}

        package_current = package_current->link;
    }

    return EXIT_SUCCESS;
}