• Hi,

    It seems a few of you have had issues with Storage compaction breaking Storage and losing your data (specifically on Bangle.js 2). I've been trying to reproduce this for ages so I can figure out what happens and fix it, but I just can't reliably cause issues.

    It's been suggested that a Bangle.factoryReset() followed by installing certain apps would cause issues, but I've tried it here - now installing over 50 apps from favourites - and I can't reproduce it.

    Another issue might have been having hugely fragmented storage, and I have come up with this code which can be run in the IDE which should really fragment everything:

    var l = require("Storage").list();
    function frag() {
      var n = 0|(Math.random()*l.length);
      var fn = l[n];
      console.log("Fragment "+fn);
      var d = require("Storage").read(fn);
      require("Storage").write(fn, d);
      if (require("Storage").getFree() > 1000000)
        console.log("Done! Storage almost full");

    But running that for ages (with 50 apps installed) and then running:


    (running from the IDE will at least allow us to see if there are any error messages).

    This doesn't seem to cause any issues immediately after compaction for me either.

    However I did manage to experience something - it's possible that while compact works fine it doesn't clear out the pages of flash storage immediately after the last file, which would cause a "file written with different data" error. I'm going to continue to look but any other input/hints others have would be hugely appreciated.

    You can check what's actually in storage at the last (and subsequent pages) using:

    // Get address of last file's beginning
    var fileName = require("Storage").list().pop();
    var addr = peek32(E.getAddressOf(require("Storage")­.read(fileName)));
    print(fileName, addr.toString(16), require("Storage").read(fileName).length­+" bytes");
    // go back to page containing the header
    addr= (addr-32)&~4095
    // print page contents - note that the last file may go past the end of what's labelled 'PAGE0'
    for (var p=0;p<3;p++) {
      print("PAGE +"+p);
      for (var i=0;i<4096;i+=32) {
        print(addr.toString(16).padStart(8,0), require("Flash").read(32,addr).slice().m­ap(x=>x.toString(16).padStart(2,0)).join­(" "));
        addr += 32;

Avatar for Gordon @Gordon started