Merge pull request #62719 from m4gr3d/fix_directory_copy_bug_3x

Fix the directory copy bug issue
This commit is contained in:
Rémi Verschelde 2022-07-04 23:49:37 +02:00 committed by GitHub
commit 0cc154b120
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View file

@ -103,6 +103,8 @@ internal abstract class DataAccess(private val filePath: String) {
}
protected abstract val fileChannel: FileChannel
internal var endOfFile = false
private set
fun close() {
try {
@ -123,6 +125,9 @@ internal abstract class DataAccess(private val filePath: String) {
fun seek(position: Long) {
try {
fileChannel.position(position)
if (position <= size()) {
endOfFile = false
}
} catch (e: Exception) {
Log.w(TAG, "Exception when seeking file $filePath.", e)
}
@ -153,11 +158,15 @@ internal abstract class DataAccess(private val filePath: String) {
0L
}
fun isEndOfFile() = position() >= size()
fun read(buffer: ByteBuffer): Int {
return try {
fileChannel.read(buffer)
val readBytes = fileChannel.read(buffer)
if (readBytes == -1) {
endOfFile = true
0
} else {
readBytes
}
} catch (e: IOException) {
Log.w(TAG, "Exception while reading from file $filePath.", e)
0
@ -166,7 +175,10 @@ internal abstract class DataAccess(private val filePath: String) {
fun write(buffer: ByteBuffer) {
try {
fileChannel.write(buffer)
val writtenBytes = fileChannel.write(buffer)
if (writtenBytes > 0) {
endOfFile = false
}
} catch (e: IOException) {
Log.w(TAG, "Exception while writing to file $filePath.", e)
}

View file

@ -186,7 +186,7 @@ class FileAccessHandler(val context: Context) {
return false
}
return files[fileId].isEndOfFile()
return files[fileId].endOfFile
}
fun fileClose(fileId: Int) {