package b.f.b.b; import android.os.Environment; import androidx.annotation.VisibleForTesting; import b.f.b.b.c; import b.f.j.c.h; import com.facebook.common.file.FileUtils$CreateDirectoryException; import com.facebook.common.file.FileUtils$ParentDirNotFoundException; import com.facebook.common.file.FileUtils$RenameException; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.concurrent.TimeUnit; /* compiled from: DefaultDiskStorage */ public class a implements c { public static final long a = TimeUnit.MINUTES.toMillis(30); /* renamed from: b reason: collision with root package name */ public static final /* synthetic */ int f454b = 0; public final File c; public final boolean d; public final File e; public final b.f.b.a.a f; public final b.f.d.k.a g; /* compiled from: DefaultDiskStorage */ public class b implements b.f.d.c.a { public final List a = new ArrayList(); public b(C0057a aVar) { } @Override // b.f.d.c.a public void a(File file) { d h = a.h(a.this, file); if (h != null && h.a == ".cnt") { this.a.add(new c(h.f457b, file, null)); } } @Override // b.f.d.c.a public void b(File file) { } @Override // b.f.d.c.a public void c(File file) { } } /* compiled from: DefaultDiskStorage */ @VisibleForTesting public static class c implements c.a { public final String a; /* renamed from: b reason: collision with root package name */ public final b.f.a.a f456b; public long c = -1; public long d = -1; public c(String str, File file, C0057a aVar) { Objects.requireNonNull(str); this.a = str; this.f456b = new b.f.a.a(file); } @Override // b.f.b.b.c.a public long a() { if (this.d < 0) { this.d = this.f456b.a.lastModified(); } return this.d; } @Override // b.f.b.b.c.a public String getId() { return this.a; } @Override // b.f.b.b.c.a public long getSize() { if (this.c < 0) { this.c = this.f456b.a(); } return this.c; } } /* compiled from: DefaultDiskStorage */ public static class d { public final String a; /* renamed from: b reason: collision with root package name */ public final String f457b; public d(String str, String str2) { this.a = str; this.f457b = str2; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(this.a); sb.append("("); return b.d.b.a.a.H(sb, this.f457b, ")"); } } /* compiled from: DefaultDiskStorage */ public static class e extends IOException { public e(long j, long j2) { super("File was not written completely. Expected: " + j + ", found: " + j2); } } /* compiled from: DefaultDiskStorage */ @VisibleForTesting public class f implements c.b { public final String a; @VisibleForTesting /* renamed from: b reason: collision with root package name */ public final File f458b; public f(String str, File file) { this.a = str; this.f458b = file; } public boolean a() { return !this.f458b.exists() || this.f458b.delete(); } public b.f.a.a b(Object obj) throws IOException { Objects.requireNonNull((b.f.d.k.c) a.this.g); long currentTimeMillis = System.currentTimeMillis(); File j = a.this.j(this.a); try { b.c.a.a0.d.Q1(this.f458b, j); if (j.exists()) { j.setLastModified(currentTimeMillis); } return new b.f.a.a(j); } catch (FileUtils$RenameException e) { Throwable cause = e.getCause(); if (cause != null && !(cause instanceof FileUtils$ParentDirNotFoundException)) { boolean z2 = cause instanceof FileNotFoundException; } b.f.b.a.a aVar = a.this.f; int i = a.f454b; Objects.requireNonNull((b.f.b.a.d) aVar); throw e; } } /* JADX INFO: finally extract failed */ public void c(b.f.b.a.g gVar, Object obj) throws IOException { try { FileOutputStream fileOutputStream = new FileOutputStream(this.f458b); try { b.f.d.d.b bVar = new b.f.d.d.b(fileOutputStream); h hVar = (h) gVar; InputStream e = hVar.a.e(); Objects.requireNonNull(e); hVar.f553b.c.a(e, bVar); bVar.flush(); long j = bVar.j; fileOutputStream.close(); if (this.f458b.length() != j) { throw new e(j, this.f458b.length()); } } catch (Throwable th) { fileOutputStream.close(); throw th; } } catch (FileNotFoundException e2) { b.f.b.a.a aVar = a.this.f; int i = a.f454b; Objects.requireNonNull((b.f.b.a.d) aVar); throw e2; } } } /* compiled from: DefaultDiskStorage */ public class g implements b.f.d.c.a { public boolean a; public g(C0057a aVar) { } /* JADX WARNING: Code restructure failed: missing block: B:7:0x002b, code lost: if (r3 > (java.lang.System.currentTimeMillis() - b.f.b.b.a.a)) goto L_0x0036; */ @Override // b.f.d.c.a public void a(File file) { if (this.a) { d h = a.h(a.this, file); boolean z2 = false; if (h != null) { String str = h.a; if (str == ".tmp") { long lastModified = file.lastModified(); Objects.requireNonNull((b.f.d.k.c) a.this.g); } else { if (str == ".cnt") { z2 = true; } b.c.a.a0.d.B(z2); } z2 = true; } if (z2) { return; } } file.delete(); } @Override // b.f.d.c.a public void b(File file) { if (!this.a && file.equals(a.this.e)) { this.a = true; } } @Override // b.f.d.c.a public void c(File file) { if (!a.this.c.equals(file) && !this.a) { file.delete(); } if (this.a && file.equals(a.this.e)) { this.a = false; } } } /* JADX WARNING: Removed duplicated region for block: B:14:0x005b */ /* JADX WARNING: Removed duplicated region for block: B:19:0x0069 A[SYNTHETIC, Splitter:B:19:0x0069] */ public a(File file, int i, b.f.b.a.a aVar) { boolean z2; this.c = file; boolean z3 = false; try { File externalStorageDirectory = Environment.getExternalStorageDirectory(); if (externalStorageDirectory != null) { try { z2 = file.getCanonicalPath().contains(externalStorageDirectory.toString()); } catch (IOException unused) { Objects.requireNonNull((b.f.b.a.d) aVar); } this.d = z2; File file2 = new File(this.c, String.format(null, "%s.ols%d.%d", "v2", 100, Integer.valueOf(i))); this.e = file2; this.f = aVar; if (this.c.exists()) { if (!file2.exists()) { b.c.a.a0.d.Z(this.c); } if (z3) { try { b.c.a.a0.d.l1(file2); } catch (FileUtils$CreateDirectoryException unused2) { b.f.b.a.a aVar2 = this.f; StringBuilder R = b.d.b.a.a.R("version directory could not be created: "); R.append(this.e); R.toString(); Objects.requireNonNull((b.f.b.a.d) aVar2); } } this.g = b.f.d.k.c.a; } z3 = true; if (z3) { } this.g = b.f.d.k.c.a; } } catch (Exception unused3) { Objects.requireNonNull((b.f.b.a.d) aVar); } z2 = false; this.d = z2; File file2 = new File(this.c, String.format(null, "%s.ols%d.%d", "v2", 100, Integer.valueOf(i))); this.e = file2; this.f = aVar; if (this.c.exists()) { } z3 = true; if (z3) { } this.g = b.f.d.k.c.a; } public static d h(a aVar, File file) { d dVar; Objects.requireNonNull(aVar); String name = file.getName(); int lastIndexOf = name.lastIndexOf(46); if (lastIndexOf > 0) { String substring = name.substring(lastIndexOf); String str = ".cnt"; if (!str.equals(substring)) { str = ".tmp".equals(substring) ? ".tmp" : null; } if (str != null) { String substring2 = name.substring(0, lastIndexOf); if (str.equals(".tmp")) { int lastIndexOf2 = substring2.lastIndexOf(46); if (lastIndexOf2 > 0) { substring2 = substring2.substring(0, lastIndexOf2); } } dVar = new d(str, substring2); if (dVar == null && new File(aVar.k(dVar.f457b)).equals(file.getParentFile())) { return dVar; } return null; } } dVar = null; if (dVar == null) { return null; } return dVar; } @Override // b.f.b.b.c public void a() { b.c.a.a0.d.o2(this.c, new g(null)); } @Override // b.f.b.b.c public c.b b(String str, Object obj) throws IOException { File file = new File(k(str)); if (!file.exists()) { try { b.c.a.a0.d.l1(file); } catch (FileUtils$CreateDirectoryException e2) { Objects.requireNonNull((b.f.b.a.d) this.f); throw e2; } } try { return new f(str, File.createTempFile(str + ".", ".tmp", file)); } catch (IOException e3) { Objects.requireNonNull((b.f.b.a.d) this.f); throw e3; } } @Override // b.f.b.b.c public boolean c(String str, Object obj) { File j = j(str); boolean exists = j.exists(); if (exists) { Objects.requireNonNull((b.f.d.k.c) this.g); j.setLastModified(System.currentTimeMillis()); } return exists; } @Override // b.f.b.b.c public b.f.a.a d(String str, Object obj) { File j = j(str); if (!j.exists()) { return null; } Objects.requireNonNull((b.f.d.k.c) this.g); j.setLastModified(System.currentTimeMillis()); return new b.f.a.a(j); } @Override // b.f.b.b.c public Collection e() throws IOException { b bVar = new b(null); b.c.a.a0.d.o2(this.e, bVar); return Collections.unmodifiableList(bVar.a); } @Override // b.f.b.b.c public long f(String str) { return i(j(str)); } @Override // b.f.b.b.c public long g(c.a aVar) { return i(((c) aVar).f456b.a); } public final long i(File file) { if (!file.exists()) { return 0; } long length = file.length(); if (file.delete()) { return length; } return -1; } @Override // b.f.b.b.c public boolean isExternal() { return this.d; } @VisibleForTesting public File j(String str) { return new File(b.d.b.a.a.J(b.d.b.a.a.R(k(str)), File.separator, str, ".cnt")); } public final String k(String str) { String valueOf = String.valueOf(Math.abs(str.hashCode() % 100)); StringBuilder sb = new StringBuilder(); sb.append(this.e); return b.d.b.a.a.H(sb, File.separator, valueOf); } }