1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Merge pull request #83538 from bruvzg/size_and_at

[FileAccess] Implement `get_size` and `get_access_time` methods.
This commit is contained in:
Thaddeus Crews
2025-03-11 14:00:58 -05:00
25 changed files with 359 additions and 9 deletions

View File

@@ -66,6 +66,8 @@ internal class AssetData(context: Context, private val filePath: String, accessF
}
fun fileLastModified(path: String) = 0L
fun fileLastAccessed(path: String) = 0L
fun fileSize(path: String) = -1L
fun delete(path: String) = false

View File

@@ -132,6 +132,24 @@ internal abstract class DataAccess {
}
}
fun fileLastAccessed(storageScope: StorageScope, context: Context, path: String): Long {
return when(storageScope) {
StorageScope.APP -> FileData.fileLastAccessed(path)
StorageScope.ASSETS -> AssetData.fileLastAccessed(path)
StorageScope.SHARED -> MediaStoreData.fileLastAccessed(context, path)
StorageScope.UNKNOWN -> 0L
}
}
fun fileSize(storageScope: StorageScope, context: Context, path: String): Long {
return when(storageScope) {
StorageScope.APP -> FileData.fileSize(path)
StorageScope.ASSETS -> AssetData.fileSize(path)
StorageScope.SHARED -> MediaStoreData.fileSize(context, path)
StorageScope.UNKNOWN -> -1L
}
}
fun removeFile(storageScope: StorageScope, context: Context, path: String): Boolean {
return when(storageScope) {
StorageScope.APP -> FileData.delete(path)

View File

@@ -228,6 +228,21 @@ class FileAccessHandler(val context: Context) {
}
}
fun fileLastAccessed(filepath: String?): Long {
val storageScope = storageScopeIdentifier.identifyStorageScope(filepath)
if (storageScope == StorageScope.UNKNOWN) {
return 0L
}
return try {
filepath?.let {
DataAccess.fileLastAccessed(storageScope, context, it)
} ?: 0L
} catch (e: SecurityException) {
0L
}
}
fun fileResize(fileId: Int, length: Long): Int {
if (!hasFileId(fileId)) {
return Error.FAILED.toNativeValue()
@@ -236,6 +251,21 @@ class FileAccessHandler(val context: Context) {
return files[fileId].resize(length).toNativeValue()
}
fun fileSize(filepath: String?): Long {
val storageScope = storageScopeIdentifier.identifyStorageScope(filepath)
if (storageScope == StorageScope.UNKNOWN) {
return -1L
}
return try {
filepath?.let {
DataAccess.fileSize(storageScope, context, it)
} ?: -1L
} catch (e: SecurityException) {
-1L
}
}
fun fileGetPosition(fileId: Int): Long {
if (!hasFileId(fileId)) {
return 0L

View File

@@ -30,10 +30,16 @@
package org.godotengine.godot.io.file
import android.os.*
import java.io.File
import java.io.FileOutputStream
import java.io.RandomAccessFile
import java.nio.channels.FileChannel
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.Files
import java.nio.file.FileSystems
import java.nio.file.Path
import java.util.concurrent.TimeUnit
/**
* Implementation of [DataAccess] which handles regular (not scoped) file access and interactions.
@@ -59,6 +65,30 @@ internal class FileData(filePath: String, accessFlag: FileAccessFlags) : DataAcc
}
}
fun fileLastAccessed(filepath: String): Long {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.readAttributes<BasicFileAttributes>(FileSystems.getDefault().getPath(filepath), BasicFileAttributes::class.java).lastAccessTime().to(TimeUnit.SECONDS)
} else {
0L
}
} catch (e: SecurityException) {
0L
}
}
fun fileSize(filepath: String): Long {
return try {
if (File(filepath).isFile) {
File(filepath).length()
} else {
-1L
}
} catch (e: SecurityException) {
-1L
}
}
fun delete(filepath: String): Boolean {
return try {
File(filepath).delete()

View File

@@ -212,6 +212,20 @@ internal class MediaStoreData(context: Context, filePath: String, accessFlag: Fi
return dataItem.dateModified.toLong() / 1000L
}
fun fileLastAccessed(@Suppress("UNUSED_PARAMETER") context: Context, @Suppress("UNUSED_PARAMETER") path: String): Long {
return 0L
}
fun fileSize(context: Context, path: String): Long {
val result = queryByPath(context, path)
if (result.isEmpty()) {
return -1L
}
val dataItem = result[0]
return dataItem.size.toLong()
}
fun rename(context: Context, from: String, to: String): Boolean {
// Ensure the source exists.
val sources = queryByPath(context, from)