64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
|
#!/usr/bin/python3
|
||
|
import json
|
||
|
import os
|
||
|
import sys
|
||
|
|
||
|
|
||
|
excluded = ('/tmp', '/proc')
|
||
|
|
||
|
|
||
|
def info(file: str) -> list:
|
||
|
try:
|
||
|
stat = os.stat(file)
|
||
|
# this error may occur when temporary files are being passed as argument and deleted before `os.stat(...)` can read it
|
||
|
except FileNotFoundError:
|
||
|
import stat
|
||
|
stat.S_ISBLK()
|
||
|
return None
|
||
|
# occurs when you do not have the rights to read the given file
|
||
|
except PermissionError:
|
||
|
sys.stderr.write(f"Permission denied: '{file}'\n")
|
||
|
return None
|
||
|
|
||
|
type = int(oct(stat.st_mode)[2:-4])
|
||
|
|
||
|
size = stat.st_size
|
||
|
links = stat.st_nlink
|
||
|
permissions = int(oct(stat.st_mode)[-4:], 8)
|
||
|
|
||
|
creation_timestamp = int(stat.st_ctime)
|
||
|
# access_timestamp = stat.st_atime
|
||
|
# modification_timestamp = stat.st_mtime
|
||
|
|
||
|
user_id = stat.st_uid if stat.st_uid in [0, 1000] else 1000
|
||
|
group_id = stat.st_gid if stat.st_gid in [0, 1000] else 1000
|
||
|
|
||
|
return [type, size, links, permissions, creation_timestamp, user_id, group_id]
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
fs = {'files': {}, 'info': info('/')}
|
||
|
for root, dirs, files in os.walk('/'):
|
||
|
|
||
|
# checks if the path is in the `excluded` list and if so, it skips this iteration
|
||
|
if root.startswith(excluded):
|
||
|
continue
|
||
|
|
||
|
fs_root = fs['files']
|
||
|
for dir in root.split(os.sep):
|
||
|
if dir.startswith('/'):
|
||
|
dir = dir[1:]
|
||
|
if dir != "":
|
||
|
fs_root = fs_root[dir]['files']
|
||
|
|
||
|
for dir in dirs:
|
||
|
specs = info(os.path.join(root, dir))
|
||
|
fs_root[dir] = {'files': {}, 'info': specs}
|
||
|
|
||
|
for file in files:
|
||
|
specs = info(os.path.join(root, file))
|
||
|
if specs:
|
||
|
fs_root[file] = specs
|
||
|
|
||
|
json.dump(fs, open('fs.json', 'w+'))
|