| 4151 | const char *bin_dest; |
| 4152 | } extract_install_args_t; |
| 4153 | static int extract_and_install_binary(extract_install_args_t args) { |
| 4154 | const char *tmp_archive = args.tmp_archive; |
| 4155 | const char *ext = args.ext; |
| 4156 | const char *bin_dest = args.bin_dest; |
| 4157 | FILE *f = fopen(tmp_archive, "rb"); |
| 4158 | if (!f) { |
| 4159 | (void)fprintf(stderr, "error: cannot open %s\n", tmp_archive); |
| 4160 | return CLI_TRUE; |
| 4161 | } |
| 4162 | (void)fseek(f, 0, SEEK_END); |
| 4163 | long fsize = ftell(f); |
| 4164 | (void)fseek(f, 0, SEEK_SET); |
| 4165 | |
| 4166 | unsigned char *data = malloc((size_t)fsize); |
| 4167 | if (!data) { |
| 4168 | (void)fclose(f); |
| 4169 | cbm_unlink(tmp_archive); |
| 4170 | return CLI_TRUE; |
| 4171 | } |
| 4172 | (void)fread(data, CLI_ELEM_SIZE, (size_t)fsize, f); |
| 4173 | (void)fclose(f); |
| 4174 | |
| 4175 | int bin_len = 0; |
| 4176 | unsigned char *bin_data = NULL; |
| 4177 | if (strcmp(ext, "tar.gz") == 0) { |
| 4178 | bin_data = cbm_extract_binary_from_targz(data, (int)fsize, &bin_len); |
| 4179 | } else { |
| 4180 | bin_data = cbm_extract_binary_from_zip(data, (int)fsize, &bin_len); |
| 4181 | } |
| 4182 | free(data); |
| 4183 | cbm_unlink(tmp_archive); |
| 4184 | |
| 4185 | if (!bin_data || bin_len <= 0) { |
| 4186 | (void)fprintf(stderr, "error: binary not found in archive\n"); |
| 4187 | free(bin_data); |
| 4188 | return CLI_TRUE; |
| 4189 | } |
| 4190 | |
| 4191 | if (cbm_replace_binary(bin_dest, bin_data, bin_len, CLI_OCTAL_PERM) != 0) { |
| 4192 | (void)fprintf(stderr, "error: cannot write to %s\n", bin_dest); |
| 4193 | free(bin_data); |
| 4194 | return CLI_TRUE; |
| 4195 | } |
| 4196 | free(bin_data); |
| 4197 | return 0; |
| 4198 | } |
| 4199 | |
| 4200 | /* Build the download URL for the update command. */ |
| 4201 | static void build_update_url(char *url, int url_sz, const char *os, const char *arch, |
no test coverage detected