-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for deleted files in deleted RecycleBin directories #718
Open
DevJoost
wants to merge
3
commits into
fox-it:main
Choose a base branch
from
DevJoost:improvement/638_recyclebin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
("path", "path"), | ||
("filesize", "filesize"), | ||
("path", "deleted_path"), | ||
("string", "source"), | ||
("path", "source"), | ||
], | ||
) | ||
|
||
|
@@ -87,30 +87,34 @@ def recyclebin(self) -> Generator[RecycleBinRecord, None, None]: | |
) | ||
|
||
for recyclebin in recyclebin_paths: | ||
yield from self.read_recycle_bin(recyclebin) | ||
yield from self.read_recycle_file(recyclebin) | ||
|
||
def _is_recycle_file(self, path: TargetPath) -> bool: | ||
def _is_recycle_meta_file(self, path: TargetPath) -> bool: | ||
"""Check wether the path is a recycle bin metadata file""" | ||
return path.name and path.name.lower().startswith("$i") | ||
|
||
def read_recycle_bin(self, bin_path: TargetPath) -> Generator[RecycleBinRecord, None, None]: | ||
if self._is_recycle_file(bin_path): | ||
yield self.read_bin_file(bin_path) | ||
def read_recycle_file(self, path: TargetPath) -> Iterator[RecycleBinRecord]: | ||
if self._is_recycle_meta_file(path): | ||
yield self.read_recycle_meta_file(path) | ||
return | ||
|
||
if bin_path.is_dir(): | ||
for new_file in bin_path.iterdir(): | ||
yield from self.read_recycle_bin(new_file) | ||
if path.is_dir() and path.name.startswith("$R"): | ||
yield from self.read_recycle_deleted_folder(path) | ||
return | ||
|
||
if path.is_dir(): | ||
for new_file in path.iterdir(): | ||
yield from self.read_recycle_file(new_file) | ||
|
||
def read_bin_file(self, bin_path: TargetPath) -> RecycleBinRecord: | ||
def read_recycle_meta_file(self, bin_path: TargetPath) -> RecycleBinRecord: | ||
data = bin_path.read_bytes() | ||
|
||
header = self.select_header(data) | ||
entry = header(data) | ||
|
||
sid = self.find_sid(bin_path) | ||
source_path = str(bin_path).lstrip("/") | ||
deleted_path = str(bin_path.parent / bin_path.name.replace("/$i", "/$r")).lstrip("/") | ||
deleted_path = str(bin_path.parent / bin_path.name.replace("$I", "$R")).lstrip("/") | ||
|
||
user_details = self.target.user_details.find(sid=sid) | ||
user = user_details.user if user_details else None | ||
|
@@ -125,6 +129,42 @@ def read_bin_file(self, bin_path: TargetPath) -> RecycleBinRecord: | |
_user=user, | ||
) | ||
|
||
def read_recycle_deleted_folder(self, folder_path: TargetPath) -> RecycleBinRecord: | ||
# Generally speaking when deleting a file, the $R* file is the actual renamed deleted file. | ||
# This is however also the case for deleted folders. When a folder is deleted, | ||
# it is also renamed and placed here (with original recursive content). | ||
# | ||
# This function will create RecycleBin records for each file in a deleted folder. | ||
|
||
meta_file = self.target.fs.path(str(folder_path.parent / folder_path.name.replace("$R", "$I")).lstrip("/")) | ||
if not meta_file.exists(): | ||
return | ||
|
||
meta_data = meta_file.read_bytes() | ||
header = self.select_header(meta_data) | ||
entry = header(meta_data) | ||
|
||
sid = self.find_sid(folder_path) | ||
original_folder_path = self.target.fs.path(entry.filename.rstrip("\x00")) | ||
|
||
user_details = self.target.user_details.find(sid=sid) | ||
user = user_details.user if user_details else None | ||
for parent_path, _, childs in folder_path.walk(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
for child in childs: | ||
child_path = self.target.fs.path(f"{str(parent_path).lstrip('/')}/{child}") | ||
original_parent_of_child = original_folder_path / str(parent_path).split(folder_path.name)[1].lstrip( | ||
"/" | ||
) | ||
yield RecycleBinRecord( | ||
ts=wintimestamp(entry.timestamp), | ||
path=original_parent_of_child / child, | ||
source=meta_file, | ||
filesize=child_path.stat().st_size, | ||
deleted_path=child_path, | ||
_target=self.target, | ||
_user=user, | ||
) | ||
|
||
def find_sid(self, path: TargetPath) -> str: | ||
parent_path = path.parent | ||
if parent_path.name.lower() == "$recycle.bin": | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And rename accordingly.