-
Notifications
You must be signed in to change notification settings - Fork 740
Fix: Truncate cache files before writing to prevent content corruption #1726
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: main
Are you sure you want to change the base?
Fix: Truncate cache files before writing to prevent content corruption #1726
Conversation
bajrangCoder
left a comment
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.
writeFile now truncates then writes, which fixes stale trailing bytes. However, writer.onwriteend triggers after truncate(0) even when an error occurs, the handler immediately writes new data, causing a second write after a failed truncate and leaving the promise already rejected.
ce15ea1 to
1207f64
Compare
|
This might work but I can't test it right now fileEntry.createWriter((writer) => {
writer.onerror = (e) => reject(e.target.error);
writer.onwrite = () => {
writer.onwrite = () => resolve(filename);
writer.write(data);
};
writer.truncate(0);
});
|
104f0fc to
f22eb85
Compare
Thanks for your input. Unfortunately i cannot test it either, but I changed the PR accordingly. |
This comment has been minimized.
This comment has been minimized.
|
Preview Release for this, has been built. |
This comment was marked as resolved.
This comment was marked as resolved.
Greptile SummaryThis PR attempts to fix a critical file corruption bug where cached remote files (SFTP/FTP) display mixed content from different files when new content is shorter than old cached content. Changes Made
Critical Issue FoundThe implementation uses the non-standard
Root Cause of Original BugThe diagnosis is correct - without truncation, writing shorter content to an existing file leaves trailing garbage data from the previous file content. Fix RequiredChange Confidence Score: 0/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant App as Application
participant SFTP as SFTP/FTP Module
participant IFS as internalFs.writeFile
participant CFS as Cordova FileSystem
participant Writer as FileWriter
App->>SFTP: Request to save remote file
SFTP->>IFS: writeFile(localCache, content, true, false)
IFS->>CFS: resolveLocalFileSystemURL(dirname)
CFS-->>IFS: Directory entry
IFS->>CFS: getFile(name, {create, exclusive})
CFS-->>IFS: File entry
IFS->>Writer: createWriter()
Writer-->>IFS: Writer instance
IFS->>Writer: Set onerror handler
IFS->>Writer: Set onwrite handler (BUG: should be onwriteend)
IFS->>Writer: truncate(0)
Note over Writer: truncate completes, fires onwriteend
Note over Writer: onwrite never fires - wrong event!
Note over IFS: Callbacks never execute
Note over IFS: File write never happens
Note over IFS: Promise never resolves
|
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.
Additional Comments (1)
-
src/fileSystem/internalFs.js, line 53-54 (link)logic:
onwriteis not a standard FileWriter API event. The correct event isonwriteend. This will cause the truncate and write operations to fail silently since the callbacks never fire.
1 file reviewed, 1 comment
This bugfix relates to Issue #1436.
Note: This issue was analyzed and implemented using Claude Code. The solution is coherent to me but I do not have insight on the project.
This implementation is not tested!
Problem
SFTP files (and other cached remote files) sometimes display mixed content from different files.
Specifically:
vars/main.yml)Actual content:

Acode content:

Root Cause
The
internalFs.writeFile()function insrc/fileSystem/internalFs.jsuses the Cordova FileWriter API without truncating the file before writing. When writing new content that is shorter than the existing cached file, the old trailing data remains in the file.Example:
"Long content from project1/vars/main.yml with lots of data..."project2/vars/main.yml(shorter file)"Short YAML""Short YAMLt from project1/vars/main.yml with lots of data..."❌Performance Impact
Affected Areas
internalFs.writeFile()with existing files