-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfs_ops.py
More file actions
40 lines (31 loc) · 1.07 KB
/
Copy pathfs_ops.py
File metadata and controls
40 lines (31 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Filesystem operations — demonstrates hostfs mount."""
import os
import tempfile
# Write a file, read it back, stat it.
path = "/mnt/host/hello.txt"
with open(path, "w") as f:
f.write("Hello from the guest!\n")
with open(path) as f:
content = f.read()
print(f"Read back: {content.strip()}")
info = os.stat(path)
print(f"Size: {info.st_size} bytes")
# List the mount directory.
entries = os.listdir("/mnt/host")
print(f"Files in /mnt/host: {entries}")
# Create a subdirectory and a file inside it.
os.makedirs("/mnt/host/subdir", exist_ok=True)
with open("/mnt/host/subdir/nested.txt", "w") as f:
f.write("nested content\n")
for root, dirs, files in os.walk("/mnt/host"):
for name in files:
full = os.path.join(root, name)
print(f" {full} ({os.path.getsize(full)} bytes)")
# Write a sentinel that the host can verify.
with open("/mnt/host/sentinel.txt", "w") as f:
f.write("guest-was-here\n")
# Clean up everything except the sentinel.
os.unlink("/mnt/host/subdir/nested.txt")
os.rmdir("/mnt/host/subdir")
os.unlink(path)
print("Cleanup done.")