|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 SUSE LLC |
| 3 | + * |
| 4 | + * This program is licensed under the BSD license, read LICENSE.BSD |
| 5 | + * for further information |
| 6 | + */ |
| 7 | + |
| 8 | +/* |
| 9 | + * apk2solv - create a solv file from multiple apk packages |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +#include <sys/types.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <unistd.h> |
| 17 | +#include <string.h> |
| 18 | + |
| 19 | +#include "util.h" |
| 20 | +#include "pool.h" |
| 21 | +#include "repo.h" |
| 22 | +#include "repo_apk.h" |
| 23 | +#include "repo_solv.h" |
| 24 | +#include "solv_xfopen.h" |
| 25 | +#include "common_write.h" |
| 26 | + |
| 27 | +static char * |
| 28 | +fgets0(char *s, int size, FILE *stream) |
| 29 | +{ |
| 30 | + char *p = s; |
| 31 | + int c; |
| 32 | + |
| 33 | + while (--size > 0) |
| 34 | + { |
| 35 | + c = getc(stream); |
| 36 | + if (c == EOF) |
| 37 | + { |
| 38 | + if (p == s) |
| 39 | + return 0; |
| 40 | + c = 0; |
| 41 | + } |
| 42 | + *p++ = c; |
| 43 | + if (!c) |
| 44 | + return s; |
| 45 | + } |
| 46 | + *p = 0; |
| 47 | + return s; |
| 48 | +} |
| 49 | + |
| 50 | +int |
| 51 | +main(int argc, char **argv) |
| 52 | +{ |
| 53 | + const char **pkgs = 0; |
| 54 | + char *manifest = 0; |
| 55 | + int manifest0 = 0; |
| 56 | + int isrepo = 0; |
| 57 | + int islocaldb = 0; |
| 58 | + int i, c, res, npkgs = 0; |
| 59 | + Pool *pool = pool_create(); |
| 60 | + Repo *repo; |
| 61 | + FILE *fp; |
| 62 | + char buf[4096], *p; |
| 63 | + int flags = 0; |
| 64 | + |
| 65 | + while ((c = getopt(argc, argv, "0:m:iCrl")) >= 0) |
| 66 | + { |
| 67 | + switch(c) |
| 68 | + { |
| 69 | + case 'm': |
| 70 | + manifest = optarg; |
| 71 | + break; |
| 72 | + case '0': |
| 73 | + manifest0 = 1; |
| 74 | + break; |
| 75 | + case 'r': |
| 76 | + isrepo = 1; |
| 77 | + break; |
| 78 | + case 'l': |
| 79 | + islocaldb = 1; |
| 80 | + isrepo = 1; |
| 81 | + break; |
| 82 | + case 'i': |
| 83 | + flags |= APK_ADD_WITH_PKGID; |
| 84 | + break; |
| 85 | + case 'C': |
| 86 | + flags |= APK_ADD_WITH_HDRID; |
| 87 | + break; |
| 88 | + default: |
| 89 | + exit(1); |
| 90 | + } |
| 91 | + } |
| 92 | + if (manifest) |
| 93 | + { |
| 94 | + if (!strcmp(manifest, "-")) |
| 95 | + fp = stdin; |
| 96 | + else if ((fp = fopen(manifest, "r")) == 0) |
| 97 | + { |
| 98 | + perror(manifest); |
| 99 | + exit(1); |
| 100 | + } |
| 101 | + for (;;) |
| 102 | + { |
| 103 | + if (manifest0) |
| 104 | + { |
| 105 | + if (!fgets0(buf, sizeof(buf), fp)) |
| 106 | + break; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + if (!fgets(buf, sizeof(buf), fp)) |
| 111 | + break; |
| 112 | + if ((p = strchr(buf, '\n')) != 0) |
| 113 | + *p = 0; |
| 114 | + } |
| 115 | + pkgs = solv_extend(pkgs, npkgs, 1, sizeof(char *), 15); |
| 116 | + pkgs[npkgs++] = strdup(buf); |
| 117 | + } |
| 118 | + if (fp != stdin) |
| 119 | + fclose(fp); |
| 120 | + } |
| 121 | + while (optind < argc) |
| 122 | + { |
| 123 | + pkgs = solv_extend(pkgs, npkgs, 1, sizeof(char *), 15); |
| 124 | + pkgs[npkgs++] = solv_strdup(argv[optind++]); |
| 125 | + } |
| 126 | + repo = repo_create(pool, "apk2solv"); |
| 127 | + repo_add_repodata(repo, 0); |
| 128 | + res = 0; |
| 129 | + if (isrepo) |
| 130 | + { |
| 131 | + if (islocaldb) |
| 132 | + flags |= APK_ADD_INDEX; |
| 133 | + if (!npkgs) |
| 134 | + { |
| 135 | + FILE *fp = islocaldb ? stdin : solv_xfopen_fd("stdin.tar.gz", 0, "r");; |
| 136 | + if (repo_add_apk_repo(repo, fp, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|flags) != 0) |
| 137 | + { |
| 138 | + fprintf(stderr, "apk2solv: %s\n", pool_errstr(pool)); |
| 139 | + res = 1; |
| 140 | + } |
| 141 | + if (fp != stdin) |
| 142 | + fclose(fp); |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + for (i = 0; i < npkgs; i++) |
| 147 | + { |
| 148 | + FILE *fp; |
| 149 | + if (!(fp = solv_xfopen(pkgs[i], "r"))) |
| 150 | + { |
| 151 | + perror(pkgs[i]); |
| 152 | + res = 1; |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + if (repo_add_apk_repo(repo, fp, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|flags) != 0) |
| 157 | + { |
| 158 | + fprintf(stderr, "apk2solv: %s\n", pool_errstr(pool)); |
| 159 | + res = 1; |
| 160 | + } |
| 161 | + fclose(fp); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + for (i = 0; i < npkgs; i++) |
| 169 | + if (repo_add_apk_pkg(repo, pkgs[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|flags) == 0) |
| 170 | + { |
| 171 | + fprintf(stderr, "apk2solv: %s\n", pool_errstr(pool)); |
| 172 | + res = 1; |
| 173 | + } |
| 174 | + } |
| 175 | + repo_internalize(repo); |
| 176 | + tool_write(repo, stdout); |
| 177 | + pool_free(pool); |
| 178 | + for (c = 0; c < npkgs; c++) |
| 179 | + solv_free((char *)pkgs[c]); |
| 180 | + solv_free(pkgs); |
| 181 | + exit(res); |
| 182 | +} |
| 183 | + |
0 commit comments