C++ strings: missing space for null terminator
[helenos.git] / uspace / app / pkg / pkg.c
blob91d550dedec99098aadc408e1963cc9b4fdb5779
1 /*
2 * Copyright (c) 2017 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup pkg
30 * @{
32 /** @file Package installer
34 * Utility to simplify installation of coastline packages
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>
45 #define NAME "pkg"
47 static void print_syntax(void)
49 fprintf(stderr, "syntax: " NAME " install <package-name>\n");
52 static errno_t cmd_runl(const char *path, ...)
54 va_list ap;
55 const char *arg;
56 int cnt = 0;
58 va_start(ap, path);
59 do {
60 arg = va_arg(ap, const char *);
61 cnt++;
62 } while (arg != NULL);
63 va_end(ap);
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);
71 if (rc != EOK) {
72 printf("Error spawning %s (%s)\n", path, str_error(rc));
73 return rc;
76 if (!id) {
77 printf("Error spawning %s (invalid task ID)\n", path);
78 return EINVAL;
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;
89 if (texit != TASK_EXIT_NORMAL) {
90 printf("Command %s unexpectedly terminated\n", path);
91 return EINVAL;
94 if (retval != 0) {
95 printf("Command %s returned non-zero exit code %d)\n",
96 path, retval);
99 return retval == 0 ? EOK : EPARTY;
102 static errno_t pkg_install(int argc, char *argv[])
104 char *pkg_name;
105 char *src_uri;
106 char *fname;
107 char *fnunpack;
108 errno_t rc;
109 int ret;
111 if (argc != 3) {
112 print_syntax();
113 return EINVAL;
116 pkg_name = argv[2];
118 ret = asprintf(&src_uri, "http://ci.helenos.org/latest/" STRING(UARCH)
119 "/%s-for-helenos-" STRING(UARCH) ".tar.gz",
120 pkg_name);
121 if (ret < 0) {
122 printf("Out of memory.\n");
123 return ENOMEM;
126 ret = asprintf(&fname, "/tmp/%s-for-helenos-" STRING(UARCH)
127 ".tar.gz", pkg_name);
128 if (ret < 0) {
129 printf("Out of memory.\n");
130 return ENOMEM;
133 ret = asprintf(&fnunpack, "/tmp/%s-for-helenos-" STRING(UARCH) ".tar",
134 pkg_name);
135 if (ret < 0) {
136 printf("Out of memory.\n");
137 return ENOMEM;
139 /* XXX error cleanup */
141 printf("Downloading '%s'.\n", src_uri);
143 rc = cmd_runl("/app/download", "/app/download", "-o", fname,
144 src_uri, NULL);
145 if (rc != EOK) {
146 printf("Error downloading package archive.\n");
147 return rc;
150 printf("Extracting package\n");
152 rc = cmd_runl("/app/gunzip", "/app/gunzip", fname, fnunpack, NULL);
153 if (rc != EOK) {
154 printf("Error uncompressing package archive.\n");
155 return rc;
158 if (remove(fname) != 0) {
159 printf("Error deleting package archive.\n");
160 return rc;
163 rc = cmd_runl("/app/untar", "/app/untar", fnunpack, NULL);
164 if (rc != EOK) {
165 printf("Error extracting package archive.\n");
166 return rc;
169 if (remove(fnunpack) != 0) {
170 printf("Error deleting package archive.\n");
171 return rc;
174 printf("Package '%s' installed.\n", pkg_name);
176 return EOK;
179 int main(int argc, char *argv[])
181 char *cmd;
182 errno_t rc;
184 if (argc < 2) {
185 fprintf(stderr, "Arguments missing.\n");
186 print_syntax();
187 return 1;
190 cmd = argv[1];
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;
200 if (rc != EOK)
201 return 1;
203 return 0;
206 /** @}