google / leveldb

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
BSD 3-Clause "New" or "Revised" License
35.71k stars 7.72k forks source link

How to read 000003.log in Default/File System in Chrome using JavaScript? #1191

Closed guest271314 closed 1 month ago

guest271314 commented 1 month ago

When a directories and files are created in Chromium using navigator.storage.getDirectory() the files are written to Default/File System in Chromium configuration folder.

Looking at MANIFEST-000001 in Default/File System/0XX/Paths I see

•|¹Å"�leveldb.BytewiseComparator��

indicating a leveldb database is being used to store "origin private file system" files.

I'm able to get the file names in 00 using the below code

  async function parseChromeDefaultFileSystem(path) {
    var set = new Set([
      32, 45, 46, 47, 48, 49, 50, 51, 52, 53,
      54, 55, 56, 57, 58, 64, 65, 66, 67, 68,
      69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
      79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
      89, 90, 95, 97, 98, 99, 100, 101, 102, 103,
      104, 105, 106, 107, 108, 109, 110, 111,
      112, 113, 114, 115, 116, 117, 118, 119,
      120, 121, 122,
    ]);

    return fetch(
      path,
    ).then((r) => r.text()).then((text) => {
      let str = "";
      for (const char of text) {
        const code = char.codePointAt();
        if (set.has(code)) {
          str += char;
        }
      }
      console.log(str);

      const matches = [
        ...new Set(
          str.replace(/./g, (s) => set.has(s.codePointAt()) ? s : " ").match(
            /00000\d+[A-Za-z-_.0-9\s]+\.crswap/g,
          ),
        ),
      ].map((s) => s.replace(/00000[\d\s]+|\.crswap/g, ""));

      return matches;

    }).catch(console.error);
  }

var paths = await parseChromeDefaultFileSystem("file:///home/user/.config/chromium/Default/File\ System/021/t/Paths/000003.log");
console.log(paths);

What I'm trying to do now is extract the parent directory and subdirectories contained in 000003.log https://gist.github.com/guest271314/c930dd00388aab20ab66528fad86d8c3 so I know which files belong in which directries and subdirectories.

How do I achieve that requirement using JavaScript in the browser?

guest271314 commented 1 month ago

Solved:

async function parseChromeDefaultFileSystem(path) {
  try {
    const set = new Set([
      32, 45, 46, 47, 48, 49, 50, 51, 52, 53,
      54, 55, 56, 57, 58, 64, 65, 66, 67, 68,
      69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
      79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
      89, 90, 95, 97, 98, 99, 100, 101, 102, 
      103, 104, 105, 106, 107, 108, 109, 110,
      111, 112, 113, 114, 115, 116, 117, 118,
      119, 120, 121, 122,
    ]);
    const request = await fetch(path);
    const text = (await request.text()).replace(/./g, (s) => set.has(s.codePointAt()) ? s : "");
    const files = [
      ...new Set(
        text.match(
          /00000\d+[A-Za-z-_.0-9\s]+\.crswap/g,
        ),
      ),
    ].map((s) => {
      const dir = [...new Set(text.slice(0, text.indexOf(s)).match(/(?<=[@\s]|CHILD_OF:0:)([\w-_])+(?=Ux)/g).map((d) =>
        d.split(/\d+|D140/)
      ))].flat().pop();
      const [key] = s.match(/00000[\d\s]+|\.crswap/g);
      return ({
        [key]: s.replace(/00000[\d\s]+|\.crswap/g, ""),
        dir
      })
    });
    return {
      name: files[0].dir,
      files
    }
  } catch (e) {
    console.error(e);
  }
}

let paths = await parseChromeDefaultFileSystem("file:///home/user/.config/chromium/Default/File\ System/021/t/Paths/000003.log");

console.log(JSON.stringify(paths,null,2));

{
  "name": "persistent-serviceworker",
  "files": [
    {
      "00000001": "README.md",
      "dir": "persistent-serviceworker"
    },
    {
      "00000003": "background.js",
      "dir": "chromium_extension_web_accessible_resources_iframe_message_event"
    },
    {
      "00000005": "index.html",
      "dir": "chromium_extension_web_accessible_resources_iframe_message_event"
    },
    {
      "00000007": "index.js",
      "dir": "chromium_extension_web_accessible_resources_iframe_message_event"
    },
    {
      "00000009": "manifest.json",
      "dir": "chromium_extension_web_accessible_resources_iframe_message_event"
    },
    {
      "00000011": "index.html",
      "dir": "docs"
    },
    {
      "00000013": "script.js",
      "dir": "docs"
    },
    {
      "00000015": "sw.js",
      "dir": "docs"
    },
    {
      "00000017": "index.html",
      "dir": "docs"
    },
    {
      "00000019": "script.js",
      "dir": "docs"
    },
    {
      "00000021": "sw.js",
      "dir": "docs"
    },
    {
      "00000023": "index.html",
      "dir": "message-event"
    },
    {
      "00000025": "script.js",
      "dir": "message-event"
    },
    {
      "00000027": "sw.js",
      "dir": "message-event"
    },
    {
      "00000029": "index.html",
      "dir": "readablestream-fetch-respondwith"
    },
    {
      "00000031": "script.js",
      "dir": "readablestream-fetch-respondwith"
    },
    {
      "00000033": "sw.js",
      "dir": "readablestream-fetch-respondwith"
    }
  ]
}