List the deleted files and access the data of deleted files.
I'm trying to read the file system block by block, but it showing the partition size 0.
Here is my python code
import os
BLOCK_SIZE = 4096
DELETED_FILE_SIGNATURE = b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
def list_deleted_files(partition_path):
deleted_files = []
partition_size = os.stat(partition_path).st_size
with open(partition_path, 'rb') as f:
num_blocks = partition_size // BLOCK_SIZE
print(num_blocks)
for block_number in range(num_blocks):
block_data = f.read(BLOCK_SIZE)
if DELETED_FILE_SIGNATURE in block_data:
file_offset = block_data.index(DELETED_FILE_SIGNATURE)
path_start = block_data.rfind(b'\0', 0, file_offset) + 1
path_end = block_data.index(b'\0', file_offset)
path = block_data[path_start:path_end].decode('utf-8')
deleted_files.append(path)
return deleted_files
partition_path = '/dev/nvme0n1p6'
deleted_files = list_deleted_files(partition_path)
print(deleted_files)
Output showing that number blocks is 0.
How to read partition correctly ?
Another method
import os
partition_path = '/dev/nvme0n1p6'
for root, dirs, files in os.walk(partition_path):
for file in files:
file_path = os.path.join(root, file)
if os.path.islink(file_path) and not os.path.exists(file_path):
print(f"Deleted file found: {file_path}")
But it ended with 0 iterations of for loop
You should not use
os.statbutos.statvfs.You can use
f_blocksandf_bsizeto read the partition.This is the same for command line: