|
|
@@ -1,6 +1,5 @@ |
| 1 |
|
-/* this project is licensed under GNU Public License Version 3 */ |
| 2 |
|
- |
| 3 |
1 |
#include <stdio.h> |
|
2 |
+#include <errno.h> |
| 4 |
3 |
#include <unistd.h> |
| 5 |
4 |
#include <sys/wait.h> |
| 6 |
5 |
#include <stdlib.h> |
|
|
@@ -19,7 +18,7 @@ char bin_dir[512]; |
| 19 |
18 |
char recipe_dir[512]; |
| 20 |
19 |
char usr_dir[512]; |
| 21 |
20 |
char etc_dir[512]; |
| 22 |
|
- |
|
21 |
+char tmp_dir[512]; |
| 23 |
22 |
/* make the fakeroot fs */ |
| 24 |
23 |
int |
| 25 |
24 |
init_fakeroot (void) |
|
|
@@ -32,6 +31,7 @@ init_fakeroot (void) |
| 32 |
31 |
snprintf (bin_dir, sizeof (bin_dir), "%s/usr/bin", root_dir); |
| 33 |
32 |
snprintf (usr_dir, sizeof (usr_dir), "%s/usr", root_dir); |
| 34 |
33 |
snprintf (etc_dir, sizeof (etc_dir), "%s/etc", root_dir); |
|
34 |
+ snprintf (tmp_dir, sizeof (tmp_dir), "%s/tmp", root_dir); |
| 35 |
35 |
snprintf (recipe_dir, sizeof (recipe_dir), "%s/etc/recipes", root_dir); |
| 36 |
36 |
|
| 37 |
37 |
struct stat st; |
|
|
@@ -41,6 +41,7 @@ init_fakeroot (void) |
| 41 |
41 |
mkdir (root_dir, 0755); |
| 42 |
42 |
mkdir (usr_dir, 0755); |
| 43 |
43 |
mkdir (bin_dir, 0755); |
|
44 |
+ mkdir (tmp_dir, 0755); |
| 44 |
45 |
mkdir (etc_dir, 0755); |
| 45 |
46 |
mkdir (recipe_dir, 0755); |
| 46 |
47 |
|
|
|
@@ -48,6 +49,37 @@ init_fakeroot (void) |
| 48 |
49 |
return 0; |
| 49 |
50 |
} |
| 50 |
51 |
|
|
52 |
+ |
|
53 |
+/* helper 1 */ |
|
54 |
+int |
|
55 |
+run_cmd (char *const argv[]) |
|
56 |
+{ |
|
57 |
+ pid_t pid = fork(); |
|
58 |
+ if (pid == 0) |
|
59 |
+ { |
|
60 |
+ execvp(argv[0], argv); |
|
61 |
+ perror("execvp failed"); |
|
62 |
+ _exit(1); |
|
63 |
+ } else if (pid > 0) |
|
64 |
+ { |
|
65 |
+ int status; |
|
66 |
+ waitpid(pid, &status, 0); |
|
67 |
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 0) return 0; |
|
68 |
+ } |
|
69 |
+ return -1; |
|
70 |
+} |
|
71 |
+ |
|
72 |
+/* helper 2 */ |
|
73 |
+int |
|
74 |
+exec_str (const char *cmd) |
|
75 |
+{ |
|
76 |
+ if (cmd == NULL || cmd[0] == '\0') return 0; |
|
77 |
+ char *const argv[] = { "/bin/sh", "-c", (char *)cmd, NULL }; |
|
78 |
+ printf("[EXEC] %s\n", cmd); |
|
79 |
+ return run_cmd (argv); |
|
80 |
+} |
|
81 |
+ |
|
82 |
+ |
| 51 |
83 |
/* recipes parsing */ |
| 52 |
84 |
|
| 53 |
85 |
int |
|
|
@@ -112,35 +144,6 @@ list_recipes (void) |
| 112 |
144 |
closedir (d); |
| 113 |
145 |
} |
| 114 |
146 |
|
| 115 |
|
-/* helper 1 */ |
| 116 |
|
-int |
| 117 |
|
-run_cmd (char *const argv[]) |
| 118 |
|
-{ |
| 119 |
|
- pid_t pid = fork(); |
| 120 |
|
- if (pid == 0) |
| 121 |
|
- { |
| 122 |
|
- execvp(argv[0], argv); |
| 123 |
|
- perror("execvp failed"); |
| 124 |
|
- _exit(1); |
| 125 |
|
- } else if (pid > 0) |
| 126 |
|
- { |
| 127 |
|
- int status; |
| 128 |
|
- waitpid(pid, &status, 0); |
| 129 |
|
- if (WIFEXITED(status) && WEXITSTATUS(status) == 0) return 0; |
| 130 |
|
- } |
| 131 |
|
- return -1; |
| 132 |
|
-} |
| 133 |
|
- |
| 134 |
|
-/* helper 2 */ |
| 135 |
|
-int |
| 136 |
|
-exec_str (const char *cmd) |
| 137 |
|
-{ |
| 138 |
|
- if (cmd == NULL || cmd[0] == '\0') return 0; |
| 139 |
|
- char *const argv[] = { "/bin/sh", "-c", (char *)cmd, NULL }; |
| 140 |
|
- printf("[EXEC] %s\n", cmd); |
| 141 |
|
- return run_cmd (argv); |
| 142 |
|
-} |
| 143 |
|
- |
| 144 |
147 |
/* TODO: add simple dependency res */ |
| 145 |
148 |
|
| 146 |
149 |
/* dependencies, finally. */ |
|
|
@@ -156,7 +159,7 @@ get_deps (const char *pkg_name) |
| 156 |
159 |
return 0; |
| 157 |
160 |
} |
| 158 |
161 |
|
| 159 |
|
- printf ("[DEPS] Loading dependencies for %s\n", pkg_name); |
|
162 |
+ printf ("[DEPS] dependencies for %s\n", pkg_name); |
| 160 |
163 |
char line[1024]; |
| 161 |
164 |
while (fgets (line, sizeof (line), file)) |
| 162 |
165 |
{ |
|
|
@@ -199,15 +202,16 @@ pkg_get (const char *pkg_name) |
| 199 |
202 |
snprintf (build_root, sizeof (build_root), "%s/tmp/build", root_dir); |
| 200 |
203 |
snprintf (build_dir, sizeof (build_dir), "%s/%s", build_root, pkg_name); |
| 201 |
204 |
|
| 202 |
|
- if (mkdir (build_root, 0755) != 0) |
|
205 |
+ |
|
206 |
+ if (mkdir (build_root, 0755) != 0 && errno != EEXIST) |
| 203 |
207 |
{ |
| 204 |
|
- perror("Failed to make directory"); |
|
208 |
+ perror("Failed to make build root directory"); |
| 205 |
209 |
return -1; |
| 206 |
210 |
} |
| 207 |
211 |
|
| 208 |
|
- if (mkdir (build_dir, 0755) != 0) |
|
212 |
+ if (mkdir (build_dir, 0755) != 0 && errno != EEXIST) |
| 209 |
213 |
{ |
| 210 |
|
- perror("Failed to make directory"); |
|
214 |
+ perror("Failed to make package build directory"); |
| 211 |
215 |
return -1; |
| 212 |
216 |
} |
| 213 |
217 |
|
|
|
@@ -216,7 +220,13 @@ pkg_get (const char *pkg_name) |
| 216 |
220 |
perror("Failed to change directory"); |
| 217 |
221 |
return -1; |
| 218 |
222 |
} |
| 219 |
|
- |
|
223 |
+ |
|
224 |
+ if (chdir (build_dir) != 0) |
|
225 |
+ { |
|
226 |
+ perror("Failed to change directory"); |
|
227 |
+ return -1; |
|
228 |
+ } |
|
229 |
+ if (get_deps (pkg_name) != 0) return -1; |
| 220 |
230 |
if (exec_str (r.pkg_fetch) != 0) return -1; |
| 221 |
231 |
if (exec_str (r.pkg_build) != 0) return -1; |
| 222 |
232 |
if (exec_str (r.pkg_install) != 0) return -1; |
|
|
@@ -253,12 +263,12 @@ main (int argc, char **argv) |
| 253 |
263 |
|
| 254 |
264 |
if (argc < 2) |
| 255 |
265 |
{ |
| 256 |
|
- fprintf (stderr, "Usage: %s [list|get|rm|help] <pkg>\n", argv[0]); |
|
266 |
+ fprintf (stderr, "%s: no subcommand provided.\nrun `pkger help` for info.\n", argv[0]); |
| 257 |
267 |
return EXIT_FAILURE; |
| 258 |
268 |
} |
| 259 |
269 |
if (strcmp (argv[1], "help") == 0) |
| 260 |
270 |
{ |
| 261 |
|
- printf ("Usage: %s [list|get|rm|help] <pkg>\n", argv[0]); |
|
271 |
+ printf ("Usage: %s [list|get|rm|help|init] <pkg>\n", argv[0]); |
| 262 |
272 |
} |
| 263 |
273 |
else if (strcmp (argv[1], "list") == 0) |
| 264 |
274 |
{ |
|
|
@@ -268,6 +278,10 @@ main (int argc, char **argv) |
| 268 |
278 |
{ |
| 269 |
279 |
pkg_get (argv[2]); |
| 270 |
280 |
} |
|
281 |
+ else if (strcmp (argv[1], "init") == 0 && argc > 2) |
|
282 |
+ { |
|
283 |
+ if (init_fakeroot () != 0) return EXIT_FAILURE; |
|
284 |
+ } |
| 271 |
285 |
else if (strcmp (argv[1], "rm") == 0 && argc > 2) |
| 272 |
286 |
{ |
| 273 |
287 |
pkg_rm (argv[2]); |