-
-
Notifications
You must be signed in to change notification settings - Fork 25
fix(ADFA-2584): Allow file rename for case-only changes #1359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: stage
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,11 +28,17 @@ class FileManagerViewModel : ViewModel() { | |
|
|
||
| fun renameFile(file: File, newName: String, context: Context? = null, onResult: ((Boolean) -> Unit)? = null) { | ||
| viewModelScope.launch { | ||
| val destFile = File(file.parentFile, newName) | ||
| val renamed = withContext(Dispatchers.IO) { | ||
| newName.length in 1..40 && FileUtils.rename(file, newName) | ||
| if (file.name.equals(newName, ignoreCase = true)) { | ||
| val tempFile = File(file.parentFile, "$newName.tmp") | ||
| file.renameTo(tempFile) && tempFile.renameTo(destFile) | ||
| } else { | ||
| FileUtils.rename(file, newName) | ||
| } | ||
| } | ||
|
|
||
| if (renamed) { | ||
| if (newName.length in 1..40 && renamed) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Validate name before performing rename operation. The validation of Validation must occur before attempting the rename to prevent this inconsistency. 🛡️ Proposed fix to validate before rename fun renameFile(file: File, newName: String, context: Context? = null, onResult: ((Boolean) -> Unit)? = null) {
viewModelScope.launch {
+ if (newName.length !in 1..40) {
+ _operationResult.emit(FileOpResult.Error(R.string.rename_failed))
+ onResult?.invoke(false)
+ return@launch
+ }
val destFile = File(file.parentFile, newName)
val renamed = withContext(Dispatchers.IO) {
if (file.name.equals(newName, ignoreCase = true)) {
val tempFile = File(file.parentFile, "$newName.tmp")
file.renameTo(tempFile) && tempFile.renameTo(destFile)
} else {
FileUtils.rename(file, newName)
}
}
- if (newName.length in 1..40 && renamed) {
+ if (renamed) {
// Notify system of the rename🤖 Prompt for AI Agents |
||
| // Notify system of the rename | ||
| val renameEvent = FileRenameEvent(file, File(file.parent, newName)) | ||
| if (context != null) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Check for temporary file collision before rename.
The temporary file
"$newName.tmp"could already exist in the directory. If it does,renameTowill either fail (leaving the original file unchanged) or potentially overwrite existing data on some file systems, causing data loss.🛡️ Proposed fix to check for temp file existence
if (file.name.equals(newName, ignoreCase = true)) { val tempFile = File(file.parentFile, "$newName.tmp") + if (tempFile.exists()) { + return@withContext false + } file.renameTo(tempFile) && tempFile.renameTo(destFile) } else {📝 Committable suggestion
🤖 Prompt for AI Agents
Major: Add rollback mechanism for failed two-step rename.
If
file.renameTo(tempFile)succeeds buttempFile.renameTo(destFile)fails, the file will be left with the.tmpextension. There is no cleanup or rollback, leaving the file system in an inconsistent state.🔄 Proposed fix to add rollback logic
if (file.name.equals(newName, ignoreCase = true)) { val tempFile = File(file.parentFile, "$newName.tmp") - file.renameTo(tempFile) && tempFile.renameTo(destFile) + val firstRename = file.renameTo(tempFile) + if (firstRename) { + val secondRename = tempFile.renameTo(destFile) + if (!secondRename) { + // Rollback: restore original name + tempFile.renameTo(file) + } + secondRename + } else { + false + } } else {🤖 Prompt for AI Agents