source: mainline/uspace/app/pkg/pkg.c@ 133ff9d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 133ff9d was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 4.6 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.h>
42#include <str_error.h>
43#include <task.h>
44
45#define NAME "pkg"
46
47static void print_syntax(void)
48{
49 fprintf(stderr, "syntax: " NAME " install <package-name>\n");
50}
51
52static errno_t cmd_runl(const char *path, ...)
53{
54 va_list ap;
55 const char *arg;
56 int cnt = 0;
57
58 va_start(ap, path);
59 do {
60 arg = va_arg(ap, const char *);
61 cnt++;
62 } while (arg != NULL);
63 va_end(ap);
64
65 va_start(ap, path);
66 task_id_t id;
67 task_wait_t wait;
68 errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
69 va_end(ap);
70
71 if (rc != EOK) {
72 printf("Error spawning %s (%s)\n", path, str_error(rc));
73 return rc;
74 }
75
76 if (!id) {
77 printf("Error spawning %s (invalid task ID)\n", path);
78 return EINVAL;
79 }
80
81 task_exit_t texit;
82 int retval;
83 rc = task_wait(&wait, &texit, &retval);
84 if (rc != EOK) {
85 printf("Error waiting for %s (%s)\n", path, str_error(rc));
86 return rc;
87 }
88
89 if (texit != TASK_EXIT_NORMAL) {
90 printf("Command %s unexpectedly terminated\n", path);
91 return EINVAL;
92 }
93
94 if (retval != 0) {
95 printf("Command %s returned non-zero exit code %d)\n",
96 path, retval);
97 }
98
99 return retval == 0 ? EOK : EPARTY;
100}
101
102
103static errno_t pkg_install(int argc, char *argv[])
104{
105 char *pkg_name;
106 char *src_uri;
107 char *fname;
108 char *fnunpack;
109 errno_t rc;
110 int ret;
111
112 if (argc != 3) {
113 print_syntax();
114 return EINVAL;
115 }
116
117 pkg_name = argv[2];
118
119 ret = asprintf(&src_uri, "http://ci.helenos.org/latest/" STRING(UARCH)
120 "/%s-for-helenos-" STRING(UARCH) ".tar.gz",
121 pkg_name);
122 if (ret < 0) {
123 printf("Out of memory.\n");
124 return ENOMEM;
125 }
126
127 ret = asprintf(&fname, "/tmp/%s-for-helenos-" STRING(UARCH)
128 ".tar.gz", pkg_name);
129 if (ret < 0) {
130 printf("Out of memory.\n");
131 return ENOMEM;
132 }
133
134 ret = asprintf(&fnunpack, "/tmp/%s-for-helenos-" STRING(UARCH) ".tar",
135 pkg_name);
136 if (ret < 0) {
137 printf("Out of memory.\n");
138 return ENOMEM;
139 }
140 /*XXX error cleanup */
141
142 printf("Downloading '%s'.\n", src_uri);
143
144 rc = cmd_runl("/app/download", "/app/download", "-o", fname,
145 src_uri, NULL);
146 if (rc != EOK) {
147 printf("Error downloading package archive.\n");
148 return rc;
149 }
150
151 printf("Extracting package\n");
152
153 rc = cmd_runl("/app/gunzip", "/app/gunzip", fname, fnunpack, NULL);
154 if (rc != EOK) {
155 printf("Error uncompressing package archive.\n");
156 return rc;
157 }
158
159 if (remove(fname) != 0) {
160 printf("Error deleting package archive.\n");
161 return rc;
162 }
163
164
165 rc = cmd_runl("/app/untar", "/app/untar", fnunpack, NULL);
166 if (rc != EOK) {
167 printf("Error extracting package archive.\n");
168 return rc;
169 }
170
171 if (remove(fnunpack) != 0) {
172 printf("Error deleting package archive.\n");
173 return rc;
174 }
175
176 printf("Package '%s' installed.\n", pkg_name);
177
178 return EOK;
179}
180
181int main(int argc, char *argv[])
182{
183 char *cmd;
184 errno_t rc;
185
186 if (argc < 2) {
187 fprintf(stderr, "Arguments missing.\n");
188 print_syntax();
189 return 1;
190 }
191
192 cmd = argv[1];
193
194 if (str_cmp(cmd, "install") == 0) {
195 rc = pkg_install(argc, argv);
196 } else {
197 fprintf(stderr, "Unknown command.\n");
198 print_syntax();
199 return 1;
200 }
201
202 if (rc != EOK)
203 return 1;
204
205 return 0;
206}
207
208/** @}
209 */
Note: See TracBrowser for help on using the repository browser.