DroidFS/app/src/main/java/sushi/hardcore/droidfs/util/Wiper.kt

92 lines
3.2 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.util
import android.content.Context
import android.net.Uri
import android.provider.OpenableColumns
2021-08-27 19:47:35 +02:00
import androidx.documentfile.provider.DocumentFile
2023-02-06 10:52:51 +01:00
import sushi.hardcore.droidfs.Constants
2020-07-28 22:25:10 +02:00
import sushi.hardcore.droidfs.R
2022-03-05 12:51:02 +01:00
import java.io.File
import java.io.FileOutputStream
2023-04-24 13:37:14 +02:00
import java.nio.ByteBuffer
2020-07-17 16:35:39 +02:00
import java.util.*
import kotlin.math.ceil
object Wiper {
2023-04-24 13:37:14 +02:00
fun wipe(byteBuffer: ByteBuffer) {
if (byteBuffer.hasArray()) {
Arrays.fill(byteBuffer.array(), Byte.MIN_VALUE)
} else {
for (i in 0 until byteBuffer.limit()) {
byteBuffer.put(i, Byte.MIN_VALUE)
}
}
}
2020-07-17 16:35:39 +02:00
private const val buff_size = 4096
2020-07-28 22:25:10 +02:00
fun wipe(context: Context, uri: Uri): String? {
2020-07-17 16:35:39 +02:00
val cursor = context.contentResolver.query(uri, null, null, null, null)
cursor?.let {
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
cursor.moveToFirst()
val size = cursor.getLong(sizeIndex)
cursor.close()
try {
2021-08-27 19:47:35 +02:00
var os = context.contentResolver.openOutputStream(uri)!!
2020-07-17 16:35:39 +02:00
val buff = ByteArray(buff_size)
Arrays.fill(buff, 0.toByte())
val writes = ceil(size.toDouble() / buff_size).toInt()
2023-02-06 10:52:51 +01:00
for (i in 0 until Constants.WIPE_PASSES) {
2020-07-17 16:35:39 +02:00
for (j in 0 until writes) {
2021-08-27 19:47:35 +02:00
os.write(buff)
2020-07-17 16:35:39 +02:00
}
2023-02-06 10:52:51 +01:00
if (i < Constants.WIPE_PASSES - 1) {
2020-07-17 16:35:39 +02:00
//reopening to flush and seek
2021-08-27 19:47:35 +02:00
os.close()
os = context.contentResolver.openOutputStream(uri)!!
2020-07-17 16:35:39 +02:00
}
}
try {
context.contentResolver.delete(uri, null, null)
} catch (e: UnsupportedOperationException){
2021-08-27 19:47:35 +02:00
if (DocumentFile.fromSingleUri(context, uri)?.delete() != true) {
(os as FileOutputStream).channel.truncate(0) //truncate to 0 if cannot delete
}
2020-07-17 16:35:39 +02:00
}
2021-08-27 19:47:35 +02:00
os.close()
2020-07-28 22:25:10 +02:00
return null
2020-07-17 16:35:39 +02:00
} catch (e: Exception) {
2020-07-28 22:25:10 +02:00
return e.message
2020-07-17 16:35:39 +02:00
}
}
2020-07-28 22:25:10 +02:00
return context.getString(R.string.query_cursor_null_error_msg)
2020-07-17 16:35:39 +02:00
}
2020-07-28 22:25:10 +02:00
fun wipe(file: File): String? {
2020-07-17 16:35:39 +02:00
val size = file.length()
try {
var os = FileOutputStream(file)
val buff = ByteArray(buff_size)
Arrays.fill(buff, 0.toByte())
val writes = ceil(size.toDouble() / buff_size).toInt()
2023-02-06 10:52:51 +01:00
for (i in 0 until Constants.WIPE_PASSES) {
2020-07-17 16:35:39 +02:00
for (j in 0 until writes) {
os.write(buff)
}
2023-02-06 10:52:51 +01:00
if (i < Constants.WIPE_PASSES - 1) {
2020-07-17 16:35:39 +02:00
//reopening to flush and seek
os.close()
os = FileOutputStream(file)
}
}
try {
file.delete()
} catch (e: UnsupportedOperationException){
os.channel.truncate(0) //truncate to 0 if cannot delete
}
os.close()
2020-07-28 22:25:10 +02:00
return null
2020-07-17 16:35:39 +02:00
} catch (e: Exception) {
2020-07-28 22:25:10 +02:00
return e.message
2020-07-17 16:35:39 +02:00
}
}
}