package okio.internal;

import com.google.android.gms.measurement.internal.u1;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import kd.e;
import kotlin.Metadata;
import kotlin.a;
import kotlin.collections.EmptyList;
import kotlin.collections.h;
import kotlin.collections.m;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.RestrictedSuspendLambda;
import kotlin.jvm.JvmName;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.SourceDebugExtension;
import kotlin.sequences.f;
import kotlin.sequences.g;
import okio.BufferedSink;
import okio.FileMetadata;
import okio.Okio;
import okio.Path;
import okio.Source;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/* JADX INFO: renamed from: okio.internal.-FileSystem, reason: invalid class name */
/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u00004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0006\u001a\u0014\u0010\u0004\u001a\u00020\u0003*\u00020\u00002\u0006\u0010\u0002\u001a\u00020\u0001H\u0000\u001a\u0014\u0010\u0006\u001a\u00020\u0005*\u00020\u00002\u0006\u0010\u0002\u001a\u00020\u0001H\u0000\u001a\u001c\u0010\n\u001a\u00020\t*\u00020\u00002\u0006\u0010\u0007\u001a\u00020\u00012\u0006\u0010\b\u001a\u00020\u0005H\u0000\u001a\u001c\u0010\r\u001a\u00020\t*\u00020\u00002\u0006\u0010\u000b\u001a\u00020\u00012\u0006\u0010\f\u001a\u00020\u0001H\u0000\u001a\u001c\u0010\u0010\u001a\u00020\t*\u00020\u00002\u0006\u0010\u000e\u001a\u00020\u00012\u0006\u0010\u000f\u001a\u00020\u0005H\u0000\u001a\"\u0010\u0013\u001a\b\u0012\u0004\u0012\u00020\u00010\u0012*\u00020\u00002\u0006\u0010\u0007\u001a\u00020\u00012\u0006\u0010\u0011\u001a\u00020\u0005H\u0000\u001aK\u0010\u0019\u001a\u00020\t*\b\u0012\u0004\u0012\u00020\u00010\u00142\u0006\u0010\u0015\u001a\u00020\u00002\f\u0010\u0017\u001a\b\u0012\u0004\u0012\u00020\u00010\u00162\u0006\u0010\u0002\u001a\u00020\u00012\u0006\u0010\u0011\u001a\u00020\u00052\u0006\u0010\u0018\u001a\u00020\u0005H\u0080@ø\u0001\u0000¢\u0006\u0004\b\u0019\u0010\u001a\u001a\u0016\u0010\u001b\u001a\u0004\u0018\u00010\u0001*\u00020\u00002\u0006\u0010\u0002\u001a\u00020\u0001H\u0000\u0082\u0002\u0004\n\u0002\b\u0019¨\u0006\u001c"}, d2 = {"Lokio/FileSystem;", "Lokio/Path;", "path", "Lokio/FileMetadata;", "commonMetadata", "", "commonExists", "dir", "mustCreate", "Lkd/e;", "commonCreateDirectories", "source", "target", "commonCopy", "fileOrDirectory", "mustExist", "commonDeleteRecursively", "followSymlinks", "Lkotlin/sequences/e;", "commonListRecursively", "Lkotlin/sequences/g;", "fileSystem", "Lkotlin/collections/h;", "stack", "postorder", "collectRecursively", "(Lkotlin/sequences/g;Lokio/FileSystem;Lkotlin/collections/h;Lokio/Path;ZZLkotlin/coroutines/Continuation;)Ljava/lang/Object;", "symlinkTarget", "okio"}, k = 2, mv = {1, 9, 0})
@JvmName(name = "-FileSystem")
@SourceDebugExtension({"SMAP\nFileSystem.kt\nKotlin\n*S Kotlin\n*F\n+ 1 FileSystem.kt\nokio/internal/-FileSystem\n+ 2 Okio.kt\nokio/Okio__OkioKt\n*L\n1#1,155:1\n52#2,5:156\n52#2,21:161\n60#2,10:182\n57#2,2:192\n71#2,2:194\n*S KotlinDebug\n*F\n+ 1 FileSystem.kt\nokio/internal/-FileSystem\n*L\n65#1:156,5\n66#1:161,21\n65#1:182,10\n65#1:192,2\n65#1:194,2\n*E\n"})
public final class FileSystem {

    /* JADX INFO: renamed from: okio.internal.-FileSystem$collectRecursively$1, reason: invalid class name */
    @Metadata(k = 3, mv = {1, 9, 0}, xi = 48)
    @DebugMetadata(c = "okio.internal.-FileSystem", f = "FileSystem.kt", i = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, l = {116, 135, 145}, m = "collectRecursively", n = {"$this$collectRecursively", "fileSystem", "stack", "path", "followSymlinks", "postorder", "$this$collectRecursively", "fileSystem", "stack", "path", "followSymlinks", "postorder"}, s = {"L$0", "L$1", "L$2", "L$3", "Z$0", "Z$1", "L$0", "L$1", "L$2", "L$3", "Z$0", "Z$1"})
    public static final class AnonymousClass1 extends ContinuationImpl {
        Object L$0;
        Object L$1;
        Object L$2;
        Object L$3;
        Object L$4;
        boolean Z$0;
        boolean Z$1;
        int label;
        /* synthetic */ Object result;

        public AnonymousClass1(Continuation continuation) {
            super(continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @Nullable
        public final Object invokeSuspend(@NotNull Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return FileSystem.collectRecursively(null, null, null, null, false, false, this);
        }
    }

    /* JADX INFO: renamed from: okio.internal.-FileSystem$commonListRecursively$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(d1 = {"\u0000\u000e\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\u0010\u0003\u001a\u00020\u0002*\b\u0012\u0004\u0012\u00020\u00010\u0000H\u008a@"}, d2 = {"Lkotlin/sequences/g;", "Lokio/Path;", "Lkd/e;", "<anonymous>"}, k = 3, mv = {1, 9, 0})
    @DebugMetadata(c = "okio.internal.-FileSystem$commonListRecursively$1", f = "FileSystem.kt", i = {0, 0}, l = {96}, m = "invokeSuspend", n = {"$this$sequence", "stack"}, s = {"L$0", "L$1"})
    public static final class C01321 extends RestrictedSuspendLambda implements Function2 {
        final /* synthetic */ Path $dir;
        final /* synthetic */ boolean $followSymlinks;
        final /* synthetic */ okio.FileSystem $this_commonListRecursively;
        private /* synthetic */ Object L$0;
        Object L$1;
        Object L$2;
        int label;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C01321(Path path, okio.FileSystem fileSystem, boolean z10, Continuation continuation) {
            super(continuation);
            this.$dir = path;
            this.$this_commonListRecursively = fileSystem;
            this.$followSymlinks = z10;
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @NotNull
        public final Continuation create(@Nullable Object obj, @NotNull Continuation continuation) {
            C01321 c01321 = new C01321(this.$dir, this.$this_commonListRecursively, this.$followSymlinks, continuation);
            c01321.L$0 = obj;
            return c01321;
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @Nullable
        public final Object invokeSuspend(@NotNull Object obj) {
            C01321 c01321;
            g gVar;
            h hVar;
            Iterator<Path> it;
            CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
            int i10 = this.label;
            if (i10 == 0) {
                a.d(obj);
                g gVar2 = (g) this.L$0;
                h hVar2 = new h();
                hVar2.addLast(this.$dir);
                c01321 = this;
                gVar = gVar2;
                hVar = hVar2;
                it = this.$this_commonListRecursively.list(this.$dir).iterator();
            } else {
                if (i10 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                it = (Iterator) this.L$2;
                h hVar3 = (h) this.L$1;
                g gVar3 = (g) this.L$0;
                a.d(obj);
                c01321 = this;
                hVar = hVar3;
                gVar = gVar3;
            }
            while (it.hasNext()) {
                Path next = it.next();
                okio.FileSystem fileSystem = c01321.$this_commonListRecursively;
                boolean z10 = c01321.$followSymlinks;
                c01321.L$0 = gVar;
                c01321.L$1 = hVar;
                c01321.L$2 = it;
                c01321.label = 1;
                if (FileSystem.collectRecursively(gVar, fileSystem, hVar, next, z10, false, c01321) == coroutineSingletons) {
                    return coroutineSingletons;
                }
            }
            return e.f29109a;
        }

        @Override // kotlin.jvm.functions.Function2
        @Nullable
        public final Object invoke(@NotNull g gVar, @Nullable Continuation continuation) {
            return ((C01321) create(gVar, continuation)).invokeSuspend(e.f29109a);
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:41:0x00d6, code lost:
    
        if (r0 != false) goto L43;
     */
    /* JADX WARN: Code restructure failed: missing block: B:42:0x00d8, code lost:
    
        if (r8 != 0) goto L58;
     */
    /* JADX WARN: Code restructure failed: missing block: B:43:0x00da, code lost:
    
        r6.addLast(r13);
     */
    /* JADX WARN: Code restructure failed: missing block: B:45:0x00e1, code lost:
    
        r8 = r6;
        r6 = r1;
        r1 = r0;
        r0 = r2;
        r2 = r3.iterator();
     */
    /* JADX WARN: Code restructure failed: missing block: B:53:0x011e, code lost:
    
        r0 = th;
     */
    /* JADX WARN: Code restructure failed: missing block: B:54:0x011f, code lost:
    
        r8 = r6;
     */
    /* JADX WARN: Removed duplicated region for block: B:60:0x012b  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x001b  */
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object collectRecursively(@NotNull g gVar, @NotNull okio.FileSystem fileSystem, @NotNull h hVar, @NotNull Path path, boolean z10, boolean z11, @NotNull Continuation continuation) throws Throwable {
        AnonymousClass1 anonymousClass1;
        h hVar2;
        g gVar2;
        boolean z12;
        okio.FileSystem fileSystem2;
        CoroutineSingletons coroutineSingletons;
        Path path2 = path;
        boolean z13 = z11;
        if (continuation instanceof AnonymousClass1) {
            anonymousClass1 = (AnonymousClass1) continuation;
            int i10 = anonymousClass1.label;
            if ((i10 & Integer.MIN_VALUE) != 0) {
                anonymousClass1.label = i10 - Integer.MIN_VALUE;
            } else {
                anonymousClass1 = new AnonymousClass1(continuation);
            }
        }
        Object obj = anonymousClass1.result;
        CoroutineSingletons coroutineSingletons2 = CoroutineSingletons.f29226a;
        int i11 = anonymousClass1.label;
        e eVar = e.f29109a;
        if (i11 == 0) {
            a.d(obj);
            if (!z13) {
                anonymousClass1.L$0 = gVar;
                anonymousClass1.L$1 = fileSystem;
                anonymousClass1.L$2 = hVar;
                anonymousClass1.L$3 = path2;
                anonymousClass1.Z$0 = z10;
                anonymousClass1.Z$1 = z13;
                anonymousClass1.label = 1;
                f fVar = (f) gVar;
                fVar.f29252b = path2;
                fVar.f29251a = 3;
                fVar.f29254d = anonymousClass1;
                return coroutineSingletons2;
            }
            hVar2 = hVar;
            gVar2 = gVar;
            z12 = z10;
            fileSystem2 = fileSystem;
        } else {
            if (i11 != 1) {
                if (i11 != 2) {
                    if (i11 != 3) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    a.d(obj);
                    return eVar;
                }
                boolean z14 = anonymousClass1.Z$1;
                boolean z15 = anonymousClass1.Z$0;
                Iterator<Path> it = (Iterator) anonymousClass1.L$4;
                Path path3 = (Path) anonymousClass1.L$3;
                h hVar3 = (h) anonymousClass1.L$2;
                fileSystem2 = (okio.FileSystem) anonymousClass1.L$1;
                gVar2 = (g) anonymousClass1.L$0;
                try {
                    a.d(obj);
                    coroutineSingletons = coroutineSingletons2;
                    while (it.hasNext()) {
                        Path next = it.next();
                        anonymousClass1.L$0 = gVar2;
                        anonymousClass1.L$1 = fileSystem2;
                        anonymousClass1.L$2 = hVar3;
                        anonymousClass1.L$3 = path3;
                        anonymousClass1.L$4 = it;
                        anonymousClass1.Z$0 = z15;
                        anonymousClass1.Z$1 = z14;
                        anonymousClass1.label = 2;
                        if (collectRecursively(gVar2, fileSystem2, hVar3, next, z15, z14, anonymousClass1) == coroutineSingletons) {
                            return coroutineSingletons;
                        }
                    }
                    hVar3.removeLast();
                    z13 = z14;
                    path2 = path3;
                    if (z13) {
                        anonymousClass1.L$0 = null;
                        anonymousClass1.L$1 = null;
                        anonymousClass1.L$2 = null;
                        anonymousClass1.L$3 = null;
                        anonymousClass1.L$4 = null;
                        anonymousClass1.label = 3;
                        f fVar2 = (f) gVar2;
                        fVar2.f29252b = path2;
                        fVar2.f29251a = 3;
                        fVar2.f29254d = anonymousClass1;
                        if (coroutineSingletons2 == coroutineSingletons) {
                            return coroutineSingletons;
                        }
                    }
                    return eVar;
                } catch (Throwable th) {
                    th = th;
                    hVar3.removeLast();
                    throw th;
                }
            }
            boolean z16 = anonymousClass1.Z$1;
            boolean z17 = anonymousClass1.Z$0;
            Path path4 = (Path) anonymousClass1.L$3;
            hVar2 = (h) anonymousClass1.L$2;
            fileSystem2 = (okio.FileSystem) anonymousClass1.L$1;
            gVar2 = (g) anonymousClass1.L$0;
            a.d(obj);
            z13 = z16;
            z12 = z17;
            path2 = path4;
        }
        List<Path> listListOrNull = fileSystem2.listOrNull(path2);
        if (listListOrNull == null) {
            listListOrNull = EmptyList.f29203a;
        }
        if (true ^ listListOrNull.isEmpty()) {
            int i12 = 0;
            Path path5 = path2;
            while (true) {
                if (z12 && hVar2.contains(path5)) {
                    throw new IOException(u1.l("symlink cycle at ", path2));
                }
                Path pathSymlinkTarget = symlinkTarget(fileSystem2, path5);
                if (pathSymlinkTarget == null) {
                    break;
                }
                i12++;
                path5 = pathSymlinkTarget;
            }
        }
        coroutineSingletons = coroutineSingletons2;
        if (z13) {
        }
        return eVar;
    }

    /* JADX WARN: Removed duplicated region for block: B:21:0x0040 A[Catch: all -> 0x003a, TRY_LEAVE, TryCatch #4 {all -> 0x003a, blocks: (B:3:0x0014, B:21:0x0040, B:27:0x0053, B:16:0x0036, B:13:0x0031, B:4:0x001c), top: B:46:0x0014, inners: #1, #5 }] */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0053 A[Catch: all -> 0x003a, TRY_ENTER, TRY_LEAVE, TryCatch #4 {all -> 0x003a, blocks: (B:3:0x0014, B:21:0x0040, B:27:0x0053, B:16:0x0036, B:13:0x0031, B:4:0x001c), top: B:46:0x0014, inners: #1, #5 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final void commonCopy(@NotNull okio.FileSystem fileSystem, @NotNull Path path, @NotNull Path path2) throws IOException {
        Long lValueOf;
        Throwable th;
        Long lValueOf2;
        nb.g.i(fileSystem, "<this>");
        nb.g.i(path, "source");
        nb.g.i(path2, "target");
        Source source = fileSystem.source(path);
        Throwable th2 = null;
        try {
            BufferedSink bufferedSinkBuffer = Okio.buffer(fileSystem.sink(path2));
            try {
                lValueOf2 = Long.valueOf(bufferedSinkBuffer.writeAll(source));
            } catch (Throwable th3) {
                if (bufferedSinkBuffer != null) {
                    try {
                        bufferedSinkBuffer.close();
                    } catch (Throwable th4) {
                        a.a(th3, th4);
                    }
                }
                th = th3;
                lValueOf2 = null;
            }
            if (bufferedSinkBuffer != null) {
                try {
                    bufferedSinkBuffer.close();
                    th = null;
                } catch (Throwable th5) {
                    th = th5;
                }
                if (th == null) {
                    throw th;
                }
                nb.g.f(lValueOf2);
                lValueOf = Long.valueOf(lValueOf2.longValue());
                if (source != null) {
                    try {
                        source.close();
                    } catch (Throwable th6) {
                        th2 = th6;
                    }
                }
            } else {
                th = null;
                if (th == null) {
                }
            }
        } catch (Throwable th7) {
            if (source != null) {
                try {
                    source.close();
                } catch (Throwable th8) {
                    a.a(th7, th8);
                }
            }
            th2 = th7;
            lValueOf = null;
        }
        if (th2 != null) {
            throw th2;
        }
        nb.g.f(lValueOf);
    }

    public static final void commonCreateDirectories(@NotNull okio.FileSystem fileSystem, @NotNull Path path, boolean z10) throws IOException {
        nb.g.i(fileSystem, "<this>");
        nb.g.i(path, "dir");
        h hVar = new h();
        for (Path pathParent = path; pathParent != null && !fileSystem.exists(pathParent); pathParent = pathParent.parent()) {
            hVar.addFirst(pathParent);
        }
        if (z10 && hVar.isEmpty()) {
            throw new IOException(path + " already exists.");
        }
        Iterator<E> it = hVar.iterator();
        while (it.hasNext()) {
            fileSystem.createDirectory((Path) it.next());
        }
    }

    public static final void commonDeleteRecursively(@NotNull okio.FileSystem fileSystem, @NotNull Path path, boolean z10) throws IOException {
        nb.g.i(fileSystem, "<this>");
        nb.g.i(path, "fileOrDirectory");
        Iterator it = new m(new FileSystem$commonDeleteRecursively$sequence$1(fileSystem, path, null), 1).iterator();
        while (it.hasNext()) {
            fileSystem.delete((Path) it.next(), z10 && !it.hasNext());
        }
    }

    public static final boolean commonExists(@NotNull okio.FileSystem fileSystem, @NotNull Path path) throws IOException {
        nb.g.i(fileSystem, "<this>");
        nb.g.i(path, "path");
        return fileSystem.metadataOrNull(path) != null;
    }

    @NotNull
    public static final kotlin.sequences.e commonListRecursively(@NotNull okio.FileSystem fileSystem, @NotNull Path path, boolean z10) throws IOException {
        nb.g.i(fileSystem, "<this>");
        nb.g.i(path, "dir");
        return new m(new C01321(path, fileSystem, z10, null), 1);
    }

    @NotNull
    public static final FileMetadata commonMetadata(@NotNull okio.FileSystem fileSystem, @NotNull Path path) throws IOException {
        nb.g.i(fileSystem, "<this>");
        nb.g.i(path, "path");
        FileMetadata fileMetadataMetadataOrNull = fileSystem.metadataOrNull(path);
        if (fileMetadataMetadataOrNull != null) {
            return fileMetadataMetadataOrNull;
        }
        throw new FileNotFoundException(u1.l("no such file: ", path));
    }

    @Nullable
    public static final Path symlinkTarget(@NotNull okio.FileSystem fileSystem, @NotNull Path path) throws IOException {
        nb.g.i(fileSystem, "<this>");
        nb.g.i(path, "path");
        Path symlinkTarget = fileSystem.metadata(path).getSymlinkTarget();
        if (symlinkTarget == null) {
            return null;
        }
        Path pathParent = path.parent();
        nb.g.f(pathParent);
        return pathParent.resolve(symlinkTarget);
    }
}
