Skip to content

Commit da78d96

Browse files
authored
Create monkey_playground.py
1 parent 26b2a78 commit da78d96

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed

monkey_playground.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
## nitro monkey -7
2+
## mirrors a path as fuse
3+
# uses vram and tmpfs to make it more faster
4+
# opens in read only
5+
# very fast
6+
7+
## virtual disk - created
8+
## shallow copy - mode
9+
10+
## virtual overlay mode
11+
12+
## creating new directory , sub direcotry , moving files is permitted inside the portal
13+
## wont actually create any direcotry or sub direcotry in the actual
14+
15+
## deletion is allowed
16+
## files will be soft marked as removed , but wont actually delete any file
17+
18+
## any modification is destroyed as soon the program is closed
19+
20+
## think as a virtual file system or sand boxed enviroment or chroot jailed
21+
22+
## safe playground - any changes made here is not reflected back to the source
23+
24+
25+
## nitro playground : activated
26+
import os
27+
import sys
28+
import errno
29+
import threading
30+
import time
31+
import argparse
32+
from collections import deque
33+
from fuse import FUSE, Operations
34+
35+
try:
36+
import psutil
37+
except ImportError:
38+
psutil = None
39+
40+
# --- UI COLORS ---
41+
class Color:
42+
BLUE = '\033[94m' # Loading
43+
PURPLE = '\033[95m' # Loaded / Cached
44+
GREEN = '\033[92m' # Served
45+
RED = '\033[91m' # Error
46+
MAROON = '\033[31m' # Cleared & Destroyed (Virtual)
47+
ORANGE = '\033[33m' # Status/Movement
48+
RESET = '\033[0m'
49+
50+
class NitroZenRelay(Operations):
51+
def __init__(self, root):
52+
self.root = root
53+
self.header_cache = {}
54+
self.path_to_inode = {}
55+
self._io_lock = threading.Lock()
56+
57+
# Virtual Sandbox Layer
58+
self.virtual_deleted = set() # Blacklisted paths
59+
self.virtual_dirs = set() # RAM-only folders
60+
self.virtual_moved = {} # New Path -> Original SSD Path
61+
62+
print(f"\n{Color.ORANGE}{'='*60}")
63+
print(f" NITRO-ZEN RELAY v10 (Pure Virtual Sandbox)")
64+
print(f" NO ORIGINAL FILES WILL BE TOUCHED")
65+
print(f" STATUS: Virtual Environment Active")
66+
print(f" MODE : NITRO virtual playground - remove , move , delete , create direcotry, sub -direcotry without deleting or changing the original source")
67+
print(f"{'='*60}{Color.RESET}\n")
68+
69+
def _full_path(self, partial):
70+
"""Maps virtual portal paths back to the real SSD source."""
71+
if partial in self.virtual_moved:
72+
return self.virtual_moved[partial]
73+
return os.path.join(self.root, partial.lstrip('/'))
74+
75+
def _heartbeat(self, action, path, color=Color.ORANGE, extra=""):
76+
ts = time.strftime("%H:%M:%S")
77+
mem_mb = psutil.Process(os.getpid()).memory_info().rss / 1048576 if psutil else 0
78+
name = os.path.basename(path) if path != "/" else "/"
79+
print(f"[{ts}] {color}{action:20}{Color.RESET} | {color}{name[:30]:30}{Color.RESET} | {mem_mb:.1f}MB {extra}")
80+
81+
# --- VIRTUAL DIRECTORY LOGIC ---
82+
def mkdir(self, path, mode):
83+
"""Allows creating folders in the Nitro environment."""
84+
self.virtual_dirs.add(path)
85+
self._heartbeat("DIR CREATED", path, color=Color.ORANGE, extra="(RAM-ONLY)")
86+
return 0
87+
88+
def create(self, path, mode, fi=None):
89+
"""Blocks actual file creation."""
90+
self._heartbeat("CREATION DENIED", path, color=Color.RED)
91+
raise OSError(errno.EROFS, "Nitro Mode: No File Creation Allowed")
92+
93+
# --- VIRTUAL MOVEMENT & DELETION ---
94+
def rename(self, old, new):
95+
"""Allows moving files/folders inside the portal."""
96+
# Determine the true physical source of what we are moving
97+
phys_source = self._full_path(old)
98+
self.virtual_moved[new] = phys_source
99+
self.virtual_deleted.add(old)
100+
self._heartbeat("MOVED", f"{old} -> {new}", color=Color.ORANGE)
101+
return 0
102+
103+
def unlink(self, path):
104+
"""Virtual deletion: File remains on SSD, disappears from Portal."""
105+
self.virtual_deleted.add(path)
106+
self._heartbeat("CLEARED & DESTROYED", path, color=Color.MAROON, extra="(VIRTUAL)")
107+
return 0
108+
109+
def rmdir(self, path):
110+
"""Virtual folder removal."""
111+
self.virtual_deleted.add(path)
112+
if path in self.virtual_dirs:
113+
self.virtual_dirs.remove(path)
114+
self._heartbeat("CLEARED & DESTROYED", path, color=Color.MAROON, extra="(DIR)")
115+
return 0
116+
117+
# --- READ LOGIC ---
118+
def getattr(self, path, fh=None):
119+
if path in self.virtual_deleted and path not in self.virtual_moved:
120+
raise OSError(errno.ENOENT, "Not found")
121+
122+
if path in self.virtual_dirs:
123+
return dict(st_mode=(0o40755), st_nlink=2, st_size=4096,
124+
st_ctime=time.time(), st_mtime=time.time(), st_atime=time.time())
125+
126+
try:
127+
st = os.lstat(self._full_path(path))
128+
with self._io_lock:
129+
self.path_to_inode[path] = st.st_ino
130+
return {key: getattr(st, key) for key in (
131+
'st_atime', 'st_ctime', 'st_gid', 'st_mode',
132+
'st_mtime', 'st_size', 'st_uid', 'st_nlink'
133+
)}
134+
except FileNotFoundError:
135+
raise OSError(errno.ENOENT, "Not found")
136+
137+
def readdir(self, path, fh):
138+
items = ['.', '..']
139+
140+
# 1. Add physical items from SSD if path isn't a virtual-only folder
141+
if path not in self.virtual_dirs:
142+
full_path = self._full_path(path)
143+
if os.path.exists(full_path):
144+
items += os.listdir(full_path)
145+
146+
# 2. Add virtual folders that belong here
147+
for vdir in self.virtual_dirs:
148+
if os.path.dirname(vdir) == path:
149+
items.append(os.path.basename(vdir))
150+
151+
# 3. Add virtually moved items that belong here
152+
for vmove in self.virtual_moved:
153+
if os.path.dirname(vmove) == path:
154+
items.append(os.path.basename(vmove))
155+
156+
# 4. Filter out everything that was virtually deleted
157+
unique_items = list(set(items))
158+
final_items = [i for i in unique_items if os.path.join(path, i) not in self.virtual_deleted or os.path.join(path, i) in self.virtual_moved]
159+
160+
# Background Warming
161+
for i in final_items[:15]:
162+
if i not in ['.', '..']:
163+
threading.Thread(target=self._lazy_worker, args=(os.path.join(path, i),), daemon=True).start()
164+
165+
self._heartbeat("SCAN_DIR", path, color=Color.ORANGE)
166+
return final_items
167+
168+
def _lazy_worker(self, path):
169+
if path in self.virtual_deleted and path not in self.virtual_moved: return
170+
full_path = self._full_path(path)
171+
try:
172+
if os.path.isfile(full_path):
173+
self._heartbeat("LOADING", path, color=Color.BLUE)
174+
st = os.stat(full_path)
175+
inode = st.st_ino
176+
with self._io_lock:
177+
if inode in self.header_cache:
178+
self._heartbeat("LOADED", path, color=Color.PURPLE, extra="(CACHE)")
179+
return
180+
fd = os.open(full_path, os.O_RDONLY)
181+
self.header_cache[inode] = os.read(fd, 5 * 1024 * 1024)
182+
os.close(fd)
183+
self._heartbeat("LOADED", path, color=Color.PURPLE)
184+
except: pass
185+
186+
def open(self, path, flags):
187+
# Block any write flags
188+
if flags & (os.O_WRONLY | os.O_RDWR | os.O_APPEND | os.O_CREAT):
189+
self._heartbeat("DENIED WRITE", path, color=Color.RED)
190+
raise OSError(errno.EROFS, "Nitro Mode: Read/Move/Destroy Only")
191+
self._heartbeat("SERVED", path, color=Color.GREEN)
192+
return os.open(self._full_path(path), os.O_RDONLY)
193+
194+
def read(self, path, length, offset, fh):
195+
inode = self.path_to_inode.get(path)
196+
if inode in self.header_cache:
197+
buf = self.header_cache[inode]
198+
if offset + length <= len(buf):
199+
self._heartbeat("SERVED (RAM)", path, color=Color.GREEN)
200+
return buf[offset:offset + length]
201+
return os.pread(fh, length, offset)
202+
203+
def release(self, path, fh):
204+
os.close(fh)
205+
return 0
206+
207+
if __name__ == "__main__":
208+
parser = argparse.ArgumentParser()
209+
parser.add_argument("source")
210+
parser.add_argument("mount")
211+
args = parser.parse_args()
212+
try:
213+
FUSE(NitroZenRelay(args.source), args.mount, nothreads=False, foreground=True, allow_other=True)
214+
except KeyboardInterrupt:
215+
pass
216+
finally:
217+
os.system(f"fusermount -u {args.mount} 2>/dev/null")

0 commit comments

Comments
 (0)