source: mainline/uspace/app/pkg/pkg.c@ cf13b17

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cf13b17 was 9621c7d, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Add simple coastline package installer. Add downloader option to save output to file.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/*
2 * Copyright (c) 2017 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup pkg
30 * @{
31 */
32/** @file Package installer
33 *
34 * Utility to simplify installation of coastline packages
35 */
36
37#include <errno.h>
38#include <macros.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <str_error.h>
42#include <task.h>
43
44#define NAME "pkg"
45
46static void print_syntax(void)
47{
48 fprintf(stderr, "syntax: " NAME " install <package-name>\n");
49}
50
51static int cmd_runl(const char *path, ...)
52{
53 va_list ap;
54 const char *arg;
55 int cnt = 0;
56
57 va_start(ap, path);
58 do {
59 arg = va_arg(ap, const char *);
60 cnt++;
61 } while (arg != NULL);
62 va_end(ap);
63
64 va_start(ap, path);
65 task_id_t id;
66 task_wait_t wait;
67 int rc = task_spawn(&id, &wait, path, cnt, ap);
68 va_end(ap);
69
70 if (rc != EOK) {
71 printf("Error spawning %s (%s)\n", path, str_error(rc));
72 return rc;
73 }
74
75 if (!id) {
76 printf("Error spawning %s (invalid task ID)\n", path);
77 return EINVAL;
78 }
79
80 task_exit_t texit;
81 int retval;
82 rc = task_wait(&wait, &texit, &retval);
83 if (rc != EOK) {
84 printf("Error waiting for %s (%s)\n", path, str_error(rc));
85 return rc;
86 }
87
88 if (texit != TASK_EXIT_NORMAL) {
89 printf("Command %s unexpectedly terminated\n", path);
90 return EINVAL;
91 }
92
93 if (retval != 0) {
94 printf("Command %s returned non-zero exit code %d)\n",
95 path, retval);
96 }
97
98 return retval;
99}
100
101
102static int pkg_install(int argc, char *argv[])
103{
104 char *pkg_name;
105 char *src_uri;
106 char *fname;
107 char *fnunpack;
108 int rc;
109
110 if (argc != 3) {
111 print_syntax();
112 return EINVAL;
113 }
114
115 pkg_name = argv[2];
116
117 rc = asprintf(&src_uri, "http://ci.helenos.org/latest/" STRING(UARCH)
118 "/%s-for-helenos-" STRING(UARCH) ".tar.gz",
119 pkg_name);
120 if (rc < 0) {
121 printf("Out of memory.\n");
122 return ENOMEM;
123 }
124
125 rc = asprintf(&fname, "/tmp/%s-for-helenos-" STRING(UARCH)
126 ".tar.gz", pkg_name);
127 if (rc < 0) {
128 printf("Out of memory.\n");
129 return ENOMEM;
130 }
131
132 rc = asprintf(&fnunpack, "/tmp/%s-for-helenos-" STRING(UARCH) ".tar",
133 pkg_name);
134 if (rc < 0) {
135 printf("Out of memory.\n");
136 return ENOMEM;
137 }
138 /*XXX error cleanup */
139
140 printf("Downloading '%s'.\n", src_uri);
141
142 rc = cmd_runl("/app/download", "/app/download", "-o", fname,
143 src_uri, NULL);
144 if (rc != EOK) {
145 printf("Error downloading package archive.\n");
146 return rc;
147 }
148
149 printf("Extracting package\n");
150
151 rc = cmd_runl("/app/gunzip", "/app/gunzip", fname, fnunpack, NULL);
152 if (rc != EOK) {
153 printf("Error uncompressing package archive.\n");
154 return rc;
155 }
156
157 if (remove(fname) != 0) {
158 printf("Error deleting package archive.\n");
159 return rc;
160 }
161
162
163 rc = cmd_runl("/app/untar", "/app/untar", fnunpack, NULL);
164 if (rc != EOK) {
165 printf("Error extracting package archive.\n");
166 return rc;
167 }
168
169 if (remove(fnunpack) != 0) {
170 printf("Error deleting package archive.\n");
171 return rc;
172 }
173
174 printf("Package '%s' installed.\n", pkg_name);
175
176 return EOK;
177}
178
179int main(int argc, char *argv[])
180{
181 char *cmd;
182 int rc;
183
184 if (argc < 2) {
185 fprintf(stderr, "Arguments missing.\n");
186 print_syntax();
187 return 1;
188 }
189
190 cmd = argv[1];
191
192 if (str_cmp(cmd, "install") == 0) {
193 rc = pkg_install(argc, argv);
194 } else {
195 fprintf(stderr, "Unknown command.\n");
196 print_syntax();
197 return 1;
198 }
199
200 if (rc != EOK)
201 return 1;
202
203 return 0;
204}
205
206/** @}
207 */
Note: See TracBrowser for help on using the repository browser.