Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Auto-backup on Android: when today's backup file belongs to another install (after a reinstall, or the test build beside the real app), the copy is written under the day's second name instead of failing silently, and the second names are pruned with the rest

Duarte Santos92ececb3f053
diff --git a/frontend/src/lib/mobile.autobackup.test.js b/frontend/src/lib/mobile.autobackup.test.js
index e849a8024..bf9c765ed 100644
--- a/frontend/src/lib/mobile.autobackup.test.js
+++ b/frontend/src/lib/mobile.autobackup.test.js
@@ -4,7 +4,7 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { todayISO } from './format.js'
-const h = vi.hoisted(() => ({ files: new Map(), refuseWrite: false, refuseList: false, stuck: new Set(), deleted: [] }))
+const h = vi.hoisted(() => ({ files: new Map(), refuseWrite: false, refuseList: false, stuck: new Set(), deleted: [], foreign: new Set() }))
vi.mock('@capacitor/filesystem', () => ({
Directory: { Data: 'DATA', Documents: 'DOCUMENTS', Cache: 'CACHE' },
@@ -12,6 +12,8 @@ vi.mock('@capacitor/filesystem', () => ({
Filesystem: {
writeFile: async ({ path, directory, data }) => {
if (h.refuseWrite) throw new Error('No space left on device')
+ // Scoped storage: a file another install wrote cannot be written over.
+ if (h.foreign.has(path)) throw new Error(`/storage/emulated/0/Documents/${path}: open failed: EACCES (Permission denied)`)
h.files.set(directory + '/' + path, data)
return { uri: 'file://' + path }
},
@@ -43,7 +45,7 @@ const inFolder = () => [...h.files.keys()].filter(k => k.startsWith('DOCUMENTS/o
const put = (path, data = '{}') => h.files.set('DOCUMENTS/' + path, data)
beforeEach(() => {
- h.files.clear(); h.deleted.length = 0; h.stuck.clear()
+ h.files.clear(); h.deleted.length = 0; h.stuck.clear(); h.foreign.clear()
h.refuseWrite = false; h.refuseList = false
})
@@ -106,6 +108,38 @@ describe('writeAutoBackup', () => {
})
})
+// A reinstall (or the test build beside the real app) left today's name owned by the other install,
+// and every backup that day failed with EACCES and was dropped without a word (Android QA, v1.3.9).
+describe('writeAutoBackup where today\'s name belongs to another install', () => {
+ it('writes the copy under the day\'s second name, and again there on the next write', async () => {
+ const other = 'openGym/' + backup(todayISO())
+ put(other, '{"theirs":true}')
+ h.foreign.add(other)
+ await writeAutoBackup({ n: 1 })
+ const second = 'DOCUMENTS/openGym/' + backup(todayISO()).replace('.json', '-2.json')
+ expect(JSON.parse(h.files.get(second))).toEqual({ n: 1 })
+ await writeAutoBackup({ n: 2 })
+ expect(JSON.parse(h.files.get(second))).toEqual({ n: 2 })
+ expect(JSON.parse(h.files.get('DOCUMENTS/' + other))).toEqual({ theirs: true })
+ })
+
+ it('counts the second names among the fourteen it keeps, and prunes them like any other', async () => {
+ for (let i = 0; i < 20; i++) put('openGym/' + backup(day(i)).replace('.json', i % 2 ? '-2.json' : '.json'))
+ await writeAutoBackup({})
+ expect(inFolder()).toHaveLength(14)
+ expect(inFolder()).toContain(backup(todayISO()))
+ expect(inFolder()).not.toContain(backup(day(0)))
+ expect(inFolder()).not.toContain(backup(day(1)).replace('.json', '-2.json'))
+ })
+
+ it('a disk that takes neither name still deletes nothing', async () => {
+ for (let i = 0; i < 20; i++) put('openGym/' + backup(day(i)))
+ h.refuseWrite = true
+ await expect(writeAutoBackup({})).resolves.toBeUndefined()
+ expect(h.deleted).toEqual([])
+ })
+})
+
// Settings writes the count out in its subtitle rather than importing it (its tests mock this
// module wholesale), so the two are pinned together here.
it('the Settings subtitle names the same number of copies the pruning keeps', async () => {
diff --git a/frontend/src/lib/mobile.js b/frontend/src/lib/mobile.js
index 790709569..9503490c3 100644
--- a/frontend/src/lib/mobile.js
+++ b/frontend/src/lib/mobile.js
@@ -207,23 +207,31 @@ export async function printHtml(html, name) {
// backup still reads either kind).
export const AUTO_BACKUP_DIR = 'openGym'
export const AUTO_BACKUP_KEEP = 14
-// Only the exact name writeAutoBackup gives its files is ever pruned; anything else someone
+// Only the exact names writeAutoBackup gives its files are ever pruned; anything else someone
// keeps in the folder is theirs.
-const AUTO_BACKUP_NAME = /^opengym-backup-\d{4}-\d{2}-\d{2}\.json$/
+const AUTO_BACKUP_NAME = /^opengym-backup-\d{4}-\d{2}-\d{2}(-2)?\.json$/
+// Android's scoped storage lets an install write over, list and delete only the files it wrote
+// itself. After a reinstall, or with the test build beside the real one, today's name can belong
+// to the other install: writing it fails with EACCES, and that day went without a copy, silently.
+// The copy then goes under the day's second name, which this install owns after its first write.
+// The other install's files stay where they are; this one cannot see them to prune them.
export async function writeAutoBackup(state) {
- const name = `opengym-backup-${todayISO()}.json`
+ const day = todayISO()
let fs
- try {
- fs = await import('@capacitor/filesystem')
- await fs.Filesystem.writeFile({
- path: `${AUTO_BACKUP_DIR}/${name}`,
- directory: fs.Directory.Documents,
- data: JSON.stringify(state),
- encoding: fs.Encoding.UTF8,
- recursive: true,
- })
- } catch (e) { return } // best effort — the private mirror in Directory.Data still has the data
+ try { fs = await import('@capacitor/filesystem') } catch (e) { return }
+ const write = name => fs.Filesystem.writeFile({
+ path: `${AUTO_BACKUP_DIR}/${name}`,
+ directory: fs.Directory.Documents,
+ data: JSON.stringify(state),
+ encoding: fs.Encoding.UTF8,
+ recursive: true,
+ })
+ let name = `opengym-backup-${day}.json`
+ try { await write(name) } catch (e) {
+ name = `opengym-backup-${day}-2.json`
+ try { await write(name) } catch (e2) { return } // best effort — the private mirror in Directory.Data still has the data
+ }
// Pruning waits for a successful write: a full disk must never cost the copies already there.
await pruneAutoBackups(fs, name)
}