* Replace "img" with the result of applying the binary patch. * The binary patch data itself in patch->fragment is still kept * but the preimage prepared by the caller in "img" is freed here * or in the helper function apply_binary_fragment() this calls. */
| 3274 | * or in the helper function apply_binary_fragment() this calls. |
| 3275 | */ |
| 3276 | static int apply_binary(struct apply_state *state, |
| 3277 | struct image *img, |
| 3278 | struct patch *patch) |
| 3279 | { |
| 3280 | const char *name = patch->old_name ? patch->old_name : patch->new_name; |
| 3281 | struct object_id oid; |
| 3282 | const unsigned hexsz = the_hash_algo->hexsz; |
| 3283 | |
| 3284 | /* |
| 3285 | * For safety, we require patch index line to contain |
| 3286 | * full hex textual object ID for old and new, at least for now. |
| 3287 | */ |
| 3288 | if (strlen(patch->old_oid_prefix) != hexsz || |
| 3289 | strlen(patch->new_oid_prefix) != hexsz || |
| 3290 | get_oid_hex(patch->old_oid_prefix, &oid) || |
| 3291 | get_oid_hex(patch->new_oid_prefix, &oid)) |
| 3292 | return error(_("cannot apply binary patch to '%s' " |
| 3293 | "without full index line"), name); |
| 3294 | |
| 3295 | if (patch->old_name) { |
| 3296 | /* |
| 3297 | * See if the old one matches what the patch |
| 3298 | * applies to. |
| 3299 | */ |
| 3300 | hash_object_file(the_hash_algo, img->buf.buf, img->buf.len, |
| 3301 | OBJ_BLOB, &oid); |
| 3302 | if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix)) |
| 3303 | return error(_("the patch applies to '%s' (%s), " |
| 3304 | "which does not match the " |
| 3305 | "current contents."), |
| 3306 | name, oid_to_hex(&oid)); |
| 3307 | } |
| 3308 | else { |
| 3309 | /* Otherwise, the old one must be empty. */ |
| 3310 | if (img->buf.len) |
| 3311 | return error(_("the patch applies to an empty " |
| 3312 | "'%s' but it is not empty"), name); |
| 3313 | } |
| 3314 | |
| 3315 | get_oid_hex(patch->new_oid_prefix, &oid); |
| 3316 | if (is_null_oid(&oid)) { |
| 3317 | image_clear(img); |
| 3318 | return 0; /* deletion patch */ |
| 3319 | } |
| 3320 | |
| 3321 | if (odb_has_object(the_repository->objects, &oid, 0)) { |
| 3322 | /* We already have the postimage */ |
| 3323 | enum object_type type; |
| 3324 | size_t size; |
| 3325 | char *result; |
| 3326 | |
| 3327 | result = odb_read_object(the_repository->objects, &oid, |
| 3328 | &type, &size); |
| 3329 | if (!result) |
| 3330 | return error(_("the necessary postimage %s for " |
| 3331 | "'%s' cannot be read"), |
| 3332 | patch->new_oid_prefix, name); |
| 3333 | image_clear(img); |
no test coverage detected