package com.google.android.play.core.assetpacks;

import java.io.File;
import java.io.IOException;

/* loaded from: classes3.dex */
final class i2 {

    /* renamed from: b, reason: collision with root package name */
    private static final rj.h0 f13943b = new rj.h0("MergeSliceTaskHandler");

    /* renamed from: a, reason: collision with root package name */
    private final e0 f13944a;

    /* JADX INFO: Access modifiers changed from: package-private */
    public i2(e0 e0Var) {
        this.f13944a = e0Var;
    }

    private static void b(File file, File file2) {
        if (!file.isDirectory()) {
            if (file2.exists()) {
                throw new d1("File clashing with existing file from other slice: ".concat(file2.toString()));
            }
            if (!file.renameTo(file2)) {
                throw new d1("Unable to move file: ".concat(String.valueOf(file)));
            }
            return;
        }
        file2.mkdirs();
        for (File file3 : file.listFiles()) {
            b(file3, new File(file2, file3.getName()));
        }
        if (!file.delete()) {
            throw new d1("Unable to delete directory: ".concat(String.valueOf(file)));
        }
    }

    public final void a(h2 h2Var) {
        File C = this.f13944a.C(h2Var.f14187b, h2Var.f13930c, h2Var.f13931d, h2Var.f13932e);
        if (!C.exists()) {
            throw new d1(String.format("Cannot find verified files for slice %s.", h2Var.f13932e), h2Var.f14186a);
        }
        File v10 = this.f13944a.v(h2Var.f14187b, h2Var.f13930c, h2Var.f13931d);
        if (!v10.exists()) {
            v10.mkdirs();
        }
        b(C, v10);
        try {
            this.f13944a.a(h2Var.f14187b, h2Var.f13930c, h2Var.f13931d, this.f13944a.p(h2Var.f14187b, h2Var.f13930c, h2Var.f13931d) + 1);
        } catch (IOException e10) {
            f13943b.b("Writing merge checkpoint failed with %s.", e10.getMessage());
            throw new d1("Writing merge checkpoint failed.", e10, h2Var.f14186a);
        }
    }
}
