package androidx.room; import android.content.Context; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; /* loaded from: classes.dex */ class j implements p0.c { /* renamed from: a, reason: collision with root package name */ private final Context f2643a; /* renamed from: b, reason: collision with root package name */ private final String f2644b; /* renamed from: c, reason: collision with root package name */ private final File f2645c; /* renamed from: d, reason: collision with root package name */ private final int f2646d; /* renamed from: e, reason: collision with root package name */ private final p0.c f2647e; /* renamed from: f, reason: collision with root package name */ private a f2648f; /* renamed from: g, reason: collision with root package name */ private boolean f2649g; j(Context context, String str, File file, int i10, p0.c cVar) { this.f2643a = context; this.f2644b = str; this.f2645c = file; this.f2646d = i10; this.f2647e = cVar; } private void G() { String databaseName = getDatabaseName(); File databasePath = this.f2643a.getDatabasePath(databaseName); a aVar = this.f2648f; o0.a aVar2 = new o0.a(databaseName, this.f2643a.getFilesDir(), aVar == null || aVar.f2548j); try { aVar2.b(); if (!databasePath.exists()) { try { c(databasePath); aVar2.c(); return; } catch (IOException e10) { throw new RuntimeException("Unable to copy database file.", e10); } } if (this.f2648f == null) { aVar2.c(); return; } try { int c10 = o0.c.c(databasePath); int i10 = this.f2646d; if (c10 == i10) { aVar2.c(); return; } if (this.f2648f.a(c10, i10)) { aVar2.c(); return; } if (this.f2643a.deleteDatabase(databaseName)) { try { c(databasePath); } catch (IOException e11) { Log.w("ROOM", "Unable to copy database file.", e11); } } else { Log.w("ROOM", "Failed to delete database file (" + databaseName + ") for a copy destructive migration."); } aVar2.c(); return; } catch (IOException e12) { Log.w("ROOM", "Unable to read database version.", e12); aVar2.c(); return; } } catch (Throwable th) { aVar2.c(); throw th; } aVar2.c(); throw th; } private void c(File file) { ReadableByteChannel channel; if (this.f2644b != null) { channel = Channels.newChannel(this.f2643a.getAssets().open(this.f2644b)); } else { if (this.f2645c == null) { throw new IllegalStateException("copyFromAssetPath and copyFromFile == null!"); } channel = new FileInputStream(this.f2645c).getChannel(); } File createTempFile = File.createTempFile("room-copy-helper", ".tmp", this.f2643a.getCacheDir()); createTempFile.deleteOnExit(); o0.d.a(channel, new FileOutputStream(createTempFile).getChannel()); File parentFile = file.getParentFile(); if (parentFile != null && !parentFile.exists() && !parentFile.mkdirs()) { throw new IOException("Failed to create directories for " + file.getAbsolutePath()); } if (createTempFile.renameTo(file)) { return; } throw new IOException("Failed to move intermediate file (" + createTempFile.getAbsolutePath() + ") to destination (" + file.getAbsolutePath() + ")."); } @Override // p0.c public synchronized p0.b b0() { if (!this.f2649g) { G(); this.f2649g = true; } return this.f2647e.b0(); } @Override // p0.c, java.io.Closeable, java.lang.AutoCloseable public synchronized void close() { this.f2647e.close(); this.f2649g = false; } @Override // p0.c public String getDatabaseName() { return this.f2647e.getDatabaseName(); } @Override // p0.c public void setWriteAheadLoggingEnabled(boolean z10) { this.f2647e.setWriteAheadLoggingEnabled(z10); } void z(a aVar) { this.f2648f = aVar; } }