#!/usr/bin/env python3
import asyncio, sys
sys.stdout.reconfigure(encoding='utf-8')
from pymobiledevice3.lockdown import create_using_usbmux
from pymobiledevice3.services.house_arrest import HouseArrestService

BID = 'vn.gsm.md'
UDID = '00008130-001E084434DB803A'


async def walk(afc, root, depth=0):
    try:
        names = await afc.listdir(root)
    except Exception as e:
        print('  ' * depth + 'ERR ' + str(e))
        return
    for n in names:
        p = root.rstrip('/') + '/' + n
        try:
            st = await afc.stat(p)
        except Exception as e:
            print('  ' * depth + '%s stat ERR' % n)
            continue
        if st.get('st_ifmt') == 'S_IFDIR':
            print('  ' * depth + '[D] %s  mtime=%s' % (p, st.get('st_mtime')))
            if depth < 4:
                await walk(afc, p, depth + 1)
        else:
            print('  ' * depth + '[F] %s  %s B  mtime=%s' % (p, st.get('st_size'), st.get('st_mtime')))


async def main():
    ld = await create_using_usbmux(serial=UDID)
    async with await HouseArrestService.create(ld, BID, documents_only=False) as afc:
        for root in ('/Library/Caches', '/Library/HTTPStorages', '/Library/Cookies',
                     '/Library/Saved Application State', '/Library/SplashBoard', '/Documents', '/tmp'):
            print('==', root)
            await walk(afc, root, 1)


asyncio.run(main())
