Skip to content

Commit f436ecc

Browse files
fix macOS junk file cleanup (rewrite in Python)
Old shell script was broken — find -exec can't call shell functions, so nothing was actually deleted. Rewrote as Python with the shell script as a thin wrapper. Also adds __MACOSX and .apdisk to the junk list.
1 parent b68769c commit f436ecc

2 files changed

Lines changed: 71 additions & 24 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
"""Delete macOS junk files from the SD card."""
3+
4+
import os
5+
import shutil
6+
7+
SDCARD = "/mnt/SDCARD"
8+
9+
JUNK_NAMES = {
10+
".DS_Store",
11+
".DocumentRevisions-V100",
12+
".Spotlight-V100",
13+
".TemporaryItems",
14+
".Trashes",
15+
".fseventsd",
16+
".VolumeIcon.icns",
17+
"__MACOSX",
18+
".apdisk",
19+
}
20+
21+
# AppleDouble magic bytes: 00 05 16 07
22+
APPLE_DOUBLE_MAGIC = b"\x00\x05\x16\x07"
23+
24+
25+
def is_apple_double(path):
26+
"""Check if a file has the AppleDouble header."""
27+
try:
28+
with open(path, "rb") as f:
29+
return f.read(4) == APPLE_DOUBLE_MAGIC
30+
except (OSError, IOError):
31+
return False
32+
33+
34+
def should_delete(name, path):
35+
if name in JUNK_NAMES:
36+
return True
37+
if name.startswith("._"):
38+
return True
39+
if os.path.isfile(path) and is_apple_double(path):
40+
return True
41+
return False
42+
43+
44+
def clean(root):
45+
deleted = 0
46+
for dirpath, dirnames, filenames in os.walk(root, topdown=True):
47+
# Check directories (iterate copy since we may modify dirnames)
48+
for d in list(dirnames):
49+
full = os.path.join(dirpath, d)
50+
if should_delete(d, full):
51+
shutil.rmtree(full, ignore_errors=True)
52+
dirnames.remove(d)
53+
deleted += 1
54+
55+
for f in filenames:
56+
full = os.path.join(dirpath, f)
57+
if should_delete(f, full):
58+
try:
59+
os.remove(full)
60+
except OSError:
61+
pass
62+
deleted += 1
63+
64+
return deleted
65+
66+
67+
if __name__ == "__main__":
68+
count = clean(SDCARD)
69+
print(f"Deleted {count} macOS junk file(s).")
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
#!/bin/sh
22

3-
check_and_delete() {
4-
BASENAME="$(basename "$1")"
5-
if [ "$BASENAME" = ".DS_Store" ] || \
6-
[ "$BASENAME" = ".DocumentRevisions-V100" ] || \
7-
[ "$BASENAME" = ".Spotlight-V100" ] || \
8-
[ "$BASENAME" = ".TemporaryItems" ] || \
9-
[ "$BASENAME" = ".Trashes" ] || \
10-
[ "$BASENAME" = ".fseventsd" ] || \
11-
[ "$BASENAME" = ".VolumeIcon.icns" ] || \
12-
[ "$(xxd -p -g2 -c16 "$1" | head -n1)" = "00051607000200004d6163204f532058" ]; then
13-
rm -rf "$1"
14-
fi
15-
}
16-
173
log_message "Cleaning up macOS junk files..."
18-
find "/mnt/SDCARD" \( \
19-
-name '._*' \
20-
-or -name '*DS_Store' \
21-
-or -name '.VolumeIcon.icns' \
22-
-or -name '.fseventsd' \
23-
-or -name '.Trashes' \
24-
-or -name '.TemporaryItems' \
25-
-or -name '.Spotlight-V100' \
26-
-or -name '.DocumentRevisions-V100' \) \
27-
-exec check_and_delete {} +
4+
result="$(python3 /mnt/SDCARD/spruce/scripts/tasks/deleteMacFiles.py)"
5+
log_message "$result"

0 commit comments

Comments
 (0)