package com.bytedance.geckox.utils;

import X.C138081z;
import android.os.Process;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class FileLock {
    public static final Map<String, Integer> LIZ = new HashMap();
    public final int LIZIZ;
    public final String LIZJ;

    public static native int nGetFD(String str);

    public static native void nLockFile(int i);

    public static native void nLockFileSegment(int i, int i2);

    public static native void nRelease(int i);

    public static native boolean nTryLock(int i);

    public static native void nUnlockFile(int i);

    static {
        Covode.recordClassIndex(26639);
        C138081z.LIZ("file_lock");
    }

    public final void LIZ() {
        MethodCollector.i(16735);
        try {
            nUnlockFile(this.LIZIZ);
            MethodCollector.o(16735);
        } catch (Exception unused) {
            RuntimeException runtimeException = new RuntimeException("release lock failed，path:" + this.LIZJ);
            MethodCollector.o(16735);
            throw runtimeException;
        }
    }

    public final void LIZIZ() {
        Integer remove;
        MethodCollector.i(16866);
        Map<String, Integer> map = LIZ;
        synchronized (map) {
            try {
                remove = map.remove(this.LIZJ);
            } catch (Throwable th) {
                MethodCollector.o(16866);
                throw th;
            }
        }
        try {
            nRelease(remove.intValue());
            MethodCollector.o(16866);
        } catch (Exception e) {
            RuntimeException runtimeException = new RuntimeException("release lock failed, file:" + this.LIZJ + " caused by:" + e.getMessage());
            MethodCollector.o(16866);
            throw runtimeException;
        }
    }

    public static int LIZLLL(String str) {
        Integer num;
        MethodCollector.i(14766);
        Map<String, Integer> map = LIZ;
        synchronized (map) {
            try {
                num = map.get(str);
                if (num == null) {
                    new File(str).getParentFile().mkdirs();
                    num = Integer.valueOf(nGetFD(str));
                    map.put(str, num);
                }
            } catch (Throwable th) {
                MethodCollector.o(14766);
                throw th;
            }
        }
        int intValue = num.intValue();
        MethodCollector.o(14766);
        return intValue;
    }

    public static FileLock LIZ(String str) {
        MethodCollector.i(4949);
        try {
            int LIZLLL = LIZLLL(str);
            nLockFile(LIZLLL);
            FileLock fileLock = new FileLock(str, LIZLLL);
            MethodCollector.o(4949);
            return fileLock;
        } catch (Exception e) {
            RuntimeException runtimeException = new RuntimeException("lock failed, file:" + str + ", pid:" + Process.myPid() + " caused by:" + e.getMessage());
            MethodCollector.o(4949);
            throw runtimeException;
        }
    }

    public static FileLock LIZIZ(String str) {
        MethodCollector.i(14143);
        try {
            int LIZLLL = LIZLLL(str);
            if (!nTryLock(LIZLLL)) {
                MethodCollector.o(14143);
                return null;
            }
            FileLock fileLock = new FileLock(str, LIZLLL);
            MethodCollector.o(14143);
            return fileLock;
        } catch (Exception e) {
            RuntimeException runtimeException = new RuntimeException("try lock failed, file:" + str + " caused by:" + e.getMessage());
            MethodCollector.o(14143);
            throw runtimeException;
        }
    }

    public static FileLock LIZJ(String str) {
        MethodCollector.i(14345);
        try {
            int LIZLLL = LIZLLL(str);
            if (!nTryLock(LIZLLL)) {
                new FileLock(str, LIZLLL).LIZIZ();
                MethodCollector.o(14345);
                return null;
            }
            FileLock fileLock = new FileLock(str, LIZLLL);
            MethodCollector.o(14345);
            return fileLock;
        } catch (Exception e) {
            RuntimeException runtimeException = new RuntimeException("try lock failed, file:" + str + " caused by:" + e.getMessage());
            MethodCollector.o(14345);
            throw runtimeException;
        }
    }

    public FileLock(String str, int i) {
        this.LIZJ = str;
        this.LIZIZ = i;
    }

    public static FileLock LIZ(String str, int i) {
        MethodCollector.i(14546);
        try {
            int LIZLLL = LIZLLL(str);
            nLockFileSegment(LIZLLL, i);
            FileLock fileLock = new FileLock(str, LIZLLL);
            MethodCollector.o(14546);
            return fileLock;
        } catch (Exception e) {
            RuntimeException runtimeException = new RuntimeException("lock segment failed, file:" + str + " caused by:" + e.getMessage());
            MethodCollector.o(14546);
            throw runtimeException;
        }
    }
}
