Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- r"""
- ADS are alternative data streams of files in NTFS on Windows.
- This is just a demonstration, that you can use a regular open
- function to write and read this files.
- I got the Idea from: https://www.youtube.com/watch?v=vz15OqiYYXo
- After you run the programm, you can comment out the write of the file.
- Then repeat the process and you'll see the content again.
- Then look on your desktop and look if you see more than one file.txt
- You can show this files with dir. Here some output from my vm:
- C:\Users\andre\Desktop>dir /R
- ... removed ...
- 26.03.2021 18:40 <DIR> .
- 26.03.2021 18:40 <DIR> ..
- 26.03.2021 18:30 632 ads.py
- 26.03.2021 18:30 4 file.txt
- 3 file.txt:bar:$DATA
- 4 file.txt:buzz:$DATA
- 4 file.txt:fizz:$DATA
- 3 file.txt:foo:$DATA
- 7 file.txt:my_data:$DATA
- ... removed ...
- """
- from pathlib import Path
- from typing import Optional
- def open_ads(
- file: Path,
- mode: str = "r",
- stream: Optional[str] = None,
- **kwargs,
- ):
- """
- Open alternate data streams on Windows.
- if stream is None, then the default stream is used,
- which is visible to the user.
- """
- file = str(file)
- if stream:
- file += f":{stream}:$data"
- return open(file, mode, **kwargs)
- # place to put the file is yor Desktop with
- # the current user, who runs the program
- desktop = Path.home() / "Desktop"
- text_file = desktop / "file.txt"
- # name of streams
- # None is a regular file
- STREAMS = (None, "my_data", "foo", "bar", "fizz", "buzz")
- def write():
- for stream in STREAMS:
- print("Writing to stream", stream or "default")
- with open_ads(text_file, "w", stream=stream) as fd:
- fd.write(stream or "None")
- def read():
- for stream in STREAMS:
- print(f"Content of {stream or 'default'} is:", end=" ")
- with open_ads(text_file, stream=stream) as fd:
- print(fd.read())
- if __name__ == "__main__":
- print(f"'{text_file}' will be created.", end="\n\n")
- yesno = input("Am I allowed to do this? [y/n]: ")
- if yesno.lower().strip().startswith("y"):
- write()
- print()
- try:
- read()
- except FileNotFoundError:
- print(f"'{text_file}' does not exsit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement