| [9621c7d] | 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 |
|
|---|
| 46 | static void print_syntax(void)
|
|---|
| 47 | {
|
|---|
| 48 | fprintf(stderr, "syntax: " NAME " install <package-name>\n");
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| [b7fd2a0] | 51 | static errno_t cmd_runl(const char *path, ...)
|
|---|
| [9621c7d] | 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;
|
|---|
| [b7fd2a0] | 67 | errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
|
|---|
| [9621c7d] | 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 |
|
|---|
| [1569a9b] | 98 | return retval == 0 ? EOK : EPARTY;
|
|---|
| [9621c7d] | 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| [b7fd2a0] | 102 | static errno_t pkg_install(int argc, char *argv[])
|
|---|
| [9621c7d] | 103 | {
|
|---|
| 104 | char *pkg_name;
|
|---|
| 105 | char *src_uri;
|
|---|
| 106 | char *fname;
|
|---|
| 107 | char *fnunpack;
|
|---|
| [b7fd2a0] | 108 | errno_t rc;
|
|---|
| [d5c1051] | 109 | int ret;
|
|---|
| [9621c7d] | 110 |
|
|---|
| 111 | if (argc != 3) {
|
|---|
| 112 | print_syntax();
|
|---|
| 113 | return EINVAL;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | pkg_name = argv[2];
|
|---|
| 117 |
|
|---|
| [d5c1051] | 118 | ret = asprintf(&src_uri, "http://ci.helenos.org/latest/" STRING(UARCH)
|
|---|
| [9621c7d] | 119 | "/%s-for-helenos-" STRING(UARCH) ".tar.gz",
|
|---|
| 120 | pkg_name);
|
|---|
| [d5c1051] | 121 | if (ret < 0) {
|
|---|
| [9621c7d] | 122 | printf("Out of memory.\n");
|
|---|
| 123 | return ENOMEM;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| [d5c1051] | 126 | ret = asprintf(&fname, "/tmp/%s-for-helenos-" STRING(UARCH)
|
|---|
| [9621c7d] | 127 | ".tar.gz", pkg_name);
|
|---|
| [d5c1051] | 128 | if (ret < 0) {
|
|---|
| [9621c7d] | 129 | printf("Out of memory.\n");
|
|---|
| 130 | return ENOMEM;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| [d5c1051] | 133 | ret = asprintf(&fnunpack, "/tmp/%s-for-helenos-" STRING(UARCH) ".tar",
|
|---|
| [9621c7d] | 134 | pkg_name);
|
|---|
| [d5c1051] | 135 | if (ret < 0) {
|
|---|
| [9621c7d] | 136 | printf("Out of memory.\n");
|
|---|
| 137 | return ENOMEM;
|
|---|
| 138 | }
|
|---|
| 139 | /*XXX error cleanup */
|
|---|
| 140 |
|
|---|
| 141 | printf("Downloading '%s'.\n", src_uri);
|
|---|
| 142 |
|
|---|
| 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;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | printf("Extracting package\n");
|
|---|
| 151 |
|
|---|
| 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;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | if (remove(fname) != 0) {
|
|---|
| 159 | printf("Error deleting package archive.\n");
|
|---|
| 160 | return rc;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | rc = cmd_runl("/app/untar", "/app/untar", fnunpack, NULL);
|
|---|
| 165 | if (rc != EOK) {
|
|---|
| 166 | printf("Error extracting package archive.\n");
|
|---|
| 167 | return rc;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | if (remove(fnunpack) != 0) {
|
|---|
| 171 | printf("Error deleting package archive.\n");
|
|---|
| 172 | return rc;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | printf("Package '%s' installed.\n", pkg_name);
|
|---|
| 176 |
|
|---|
| 177 | return EOK;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | int main(int argc, char *argv[])
|
|---|
| 181 | {
|
|---|
| 182 | char *cmd;
|
|---|
| [b7fd2a0] | 183 | errno_t rc;
|
|---|
| [9621c7d] | 184 |
|
|---|
| 185 | if (argc < 2) {
|
|---|
| 186 | fprintf(stderr, "Arguments missing.\n");
|
|---|
| 187 | print_syntax();
|
|---|
| 188 | return 1;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | cmd = argv[1];
|
|---|
| 192 |
|
|---|
| 193 | if (str_cmp(cmd, "install") == 0) {
|
|---|
| 194 | rc = pkg_install(argc, argv);
|
|---|
| 195 | } else {
|
|---|
| 196 | fprintf(stderr, "Unknown command.\n");
|
|---|
| 197 | print_syntax();
|
|---|
| 198 | return 1;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | if (rc != EOK)
|
|---|
| 202 | return 1;
|
|---|
| 203 |
|
|---|
| 204 | return 0;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /** @}
|
|---|
| 208 | */
|
|---|