Recover unregistered hidden volumes

This commit is contained in:
Matéo Duparc 2023-04-25 15:06:20 +02:00
parent e83cfc9794
commit 2bbf003df5
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A

View File

@ -10,8 +10,9 @@ import sushi.hardcore.droidfs.filesystems.EncryptedVolume
import sushi.hardcore.droidfs.util.PathUtils import sushi.hardcore.droidfs.util.PathUtils
import java.io.File import java.io.File
class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Constants.VOLUME_DATABASE_NAME, null, 4) { class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Constants.VOLUME_DATABASE_NAME, null, 5) {
companion object { companion object {
const val TAG = "VolumeDatabase"
const val TABLE_NAME = "Volumes" const val TABLE_NAME = "Volumes"
const val COLUMN_NAME = "name" const val COLUMN_NAME = "name"
const val COLUMN_HIDDEN = "hidden" const val COLUMN_HIDDEN = "hidden"
@ -42,44 +43,63 @@ class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Co
File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir() File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir()
} }
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { private fun getNewVolumePath(volumeName: String): File {
// Adding type column and set it to GOCRYPTFS_VOLUME_TYPE for all existing volumes return File(
db.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN $COLUMN_TYPE BLOB;") VolumeData(
db.update(TABLE_NAME, ContentValues().apply { volumeName,
put(COLUMN_TYPE, byteArrayOf(EncryptedVolume.GOCRYPTFS_VOLUME_TYPE)) true,
}, null, null) EncryptedVolume.GOCRYPTFS_VOLUME_TYPE
).getFullPath(context.filesDir.path)
).canonicalFile
}
// Moving hidden volumes to the "volumes" directory override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir()) { if (oldVersion == 3) {
val cursor = db.query( // Adding type column and set it to GOCRYPTFS_VOLUME_TYPE for all existing volumes
TABLE_NAME, db.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN $COLUMN_TYPE BLOB;")
arrayOf(COLUMN_NAME), db.update(TABLE_NAME, ContentValues().apply {
"$COLUMN_HIDDEN=?", put(COLUMN_TYPE, byteArrayOf(EncryptedVolume.GOCRYPTFS_VOLUME_TYPE))
arrayOf("1"), }, null, null)
null,
null, // Moving registered hidden volumes to the "volumes" directory
null if (File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir()) {
) val cursor = db.query(
while (cursor.moveToNext()) { TABLE_NAME,
val volumeName = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME)) arrayOf(COLUMN_NAME),
File( "$COLUMN_HIDDEN=?",
PathUtils.pathJoin( arrayOf("1"),
context.filesDir.path, null,
volumeName null,
) null
).renameTo(
File(
VolumeData(
volumeName,
true,
EncryptedVolume.GOCRYPTFS_VOLUME_TYPE
).getFullPath(context.filesDir.path)
).canonicalFile
) )
while (cursor.moveToNext()) {
val volumeName = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME))
val success = File(
PathUtils.pathJoin(
context.filesDir.path,
volumeName
)
).renameTo(getNewVolumePath(volumeName))
if (!success) {
Log.e(TAG, "Failed to move $volumeName")
}
}
cursor.close()
} else {
Log.e(TAG, "Volumes directory creation failed while upgrading")
}
}
// Moving unregistered hidden volumes to the "volumes" directory
File(context.filesDir.path).listFiles()?.let {
for (i in it) {
if (i.isDirectory && i.name != Constants.CRYFS_LOCAL_STATE_DIR && i.name != VolumeData.VOLUMES_DIRECTORY) {
if (EncryptedVolume.getVolumeType(i.path) != (-1).toByte()) {
if (!i.renameTo(getNewVolumePath(i.name))) {
Log.e(TAG, "Failed to move "+i.name)
}
}
}
} }
cursor.close()
} else {
Log.e("VolumeDatabase", "Volumes directory creation failed while upgrading")
} }
} }