apache / apache/cordova-plugin-file

Support sync filesystem writing ("flush writes")

Open
#571 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
744
Forks
754
Avg merge
3d 4h
Merged PRs (30d)
1

Description

# Feature Request

## Motivation Behind Feature
I run a distributed ticketing app on a few Android devices, some of which have a built-in printer.
All the app persistence (sessions and tickets) is managed through files.
After years of cluelessly watching random episodes data loss, I finally tracked down a major issue.

For each new ticket, I overwrite files with a FileWriter (I actually use Ionic with Awesome Cordova's File plugin, but this is irrelevant here).
In this moment, with adb I can even watch the files content get seemingly updated, and read the new content with cat.
After the writing callback, the app prints the ticket, and this is my point of failure: printing generates a power consumption peak, and devices on old batteries might shut off abruptly.
When the device reboots after the unmanaged shutdown, recently written files are empty. Not outdated, just empty, 0 bytes.

I know that async writing is an OS feature (and Android volumes are always mounted async as far as I know), so the plugin does work as expected, but I think there can be more.

## Feature Description
Android actually offers a [sync](https://developer.android.com/reference/java/io/FileDescriptor#sync()) method on FileDescriptor since API level 1, which can be used to ensure sync writing.
I implemented support for it in LocalFilesystem.java, and introduced a setSync method in FileWriter.js to set an optional flag to ensure sync writes.
I am currently testing my solution, will start deploying it in production next week, and if it works as expected I will file a PR and add a reference here.

## Alternatives or Workarounds
Again, I know async writing is basically default.
But I believe, if this feature won't be implemented, the documentation should at least warn about potential problems.

Contributor guide

Open the contributing guide

Research direction

Review LocalFilesystem.java and FileWriter.js, the files named in the feature description, and trace the existing write flow first. Verify how an optional sync setting should interact with current callbacks and Android API support. Done means sync writes are exposed while existing asynchronous behavior remains compatible, with the behavior documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java, javascript
Domain
mobile
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.