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,14 +43,25 @@ class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Co
File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir() File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir()
} }
private fun getNewVolumePath(volumeName: String): File {
return File(
VolumeData(
volumeName,
true,
EncryptedVolume.GOCRYPTFS_VOLUME_TYPE
).getFullPath(context.filesDir.path)
).canonicalFile
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (oldVersion == 3) {
// Adding type column and set it to GOCRYPTFS_VOLUME_TYPE for all existing volumes // Adding type column and set it to GOCRYPTFS_VOLUME_TYPE for all existing volumes
db.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN $COLUMN_TYPE BLOB;") db.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN $COLUMN_TYPE BLOB;")
db.update(TABLE_NAME, ContentValues().apply { db.update(TABLE_NAME, ContentValues().apply {
put(COLUMN_TYPE, byteArrayOf(EncryptedVolume.GOCRYPTFS_VOLUME_TYPE)) put(COLUMN_TYPE, byteArrayOf(EncryptedVolume.GOCRYPTFS_VOLUME_TYPE))
}, null, null) }, null, null)
// Moving hidden volumes to the "volumes" directory // Moving registered hidden volumes to the "volumes" directory
if (File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir()) { if (File(context.filesDir, VolumeData.VOLUMES_DIRECTORY).mkdir()) {
val cursor = db.query( val cursor = db.query(
TABLE_NAME, TABLE_NAME,
@ -62,24 +74,32 @@ class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Co
) )
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
val volumeName = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME)) val volumeName = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME))
File( val success = File(
PathUtils.pathJoin( PathUtils.pathJoin(
context.filesDir.path, context.filesDir.path,
volumeName volumeName
) )
).renameTo( ).renameTo(getNewVolumePath(volumeName))
File( if (!success) {
VolumeData( Log.e(TAG, "Failed to move $volumeName")
volumeName, }
true,
EncryptedVolume.GOCRYPTFS_VOLUME_TYPE
).getFullPath(context.filesDir.path)
).canonicalFile
)
} }
cursor.close() cursor.close()
} else { } else {
Log.e("VolumeDatabase", "Volumes directory creation failed while upgrading") 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)
}
}
}
}
} }
} }