aboutsummaryrefslogtreecommitdiff
path: root/sbopkglint.d/50-icons.t.sh
blob: 2296e9dc2bd160f47df15671c073c3374ea9a3f3 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh

# sbopkglint test, must be sourced by sbopkglint (not run standalone).

# PKG, PRGNAM, VERSION, ARCH are set by sbopkglint. also the current
# directory is the root of the installed package tree.

#######################################################################
# check icons in /usr/share/icons/hicolor/ and /usr/share/pixmaps.
# all icons must be world-readable, owned by root:root. the file type
# has to match the filename (e.g. a PNG file that's called "foo.gif"
# is a failure). for /usr/share/icons/hicolor/<size>, the image
# size has to match the directory name (e.g. 64x64).

nonimages=""

check_mime() {
	local file="$1"
	local havemime="$2"
	local wantmime="$3"

	#echo "check_mime $file $havemime $wantmime"

	if [ "$havemime" != "$wantmime" ]; then
		warn "bad filename extension: $file is $havemime, .$ext should be $wantmime"
	fi
}

check_image() {
	local f="$1"
	local size
	local actualsize
	local mime
	local bn="$( basename "$f" )"
	local ext="$( echo "$bn" | sed 's,.*\.\([^.]*\)$,\1,' )"
	[ "$ext" = "$f" ] && ext=""

	[ "$bn" = "theme" -o "$bn" = "icon-theme.cache" -o "$bn" = "index.theme" ] && return

	mime="$( file -L -zS -b --mime-type "$f" )"
	case "$mime" in
		image/*) ;; # OK
		*) ( echo -n "[$mime]: " ; ls -bld "$f" ) >> .nonimages.$$; return ;;
	esac

	# it's not real clear to me whether .bmp or .ico should be allowed
	# as icons. for now, just check the mime types.
	case "$ext" in
		""|".") badextensions+="$f " ;;
		PNG|png)           check_mime "$f" "$mime" image/png ;;
		GIF|gif)           check_mime "$f" "$mime" image/gif ;;
		JPG|jpg|JPEG|jpeg) check_mime "$f" "$mime" image/jpeg ;;
		XPM|xpm)           check_mime "$f" "$mime" image/x-xpmi ;;
		SVG|svg|SVGZ|svgz) check_mime "$f" "$mime" image/svg+xml ;;
		BMP|bmp)           check_mime "$f" "$mime" image/bmp ;;
		ICO|ico)           check_mime "$f" "$mime" image/vnd.microsoft.icon ;;
	esac

	# extract e.g. 64x64 or 128x128 from the path to the file.
	# this works for e.g. /usr/share/icons/hicolor/64x64/apps/blah.png,
	# but it's required to have / before and after the size, so an
	# icon named /usr/share/pixmaps/foo_32x32.png won't have its
	# size checked.
	size="$( echo $f | grep -o '/\([0-9][0-9]*\)x\1/' | cut -d/ -f2 )"

	if [ -n "$size" ]; then
		actualsize="$( identify "$f" | sed -n 's,.* \([0-9][0-9]*x[0-9][0-9]*\) .*,\1,p' )"
		if [ "$size" != "$actualsize" ]; then
			warn "$f: icon is $actualsize, but is in a dir with $size in the name. resize it, or rename the dir."
		fi
	fi
}

for icondir in usr/share/pixmaps usr/share/icons/hicolor; do
	if [ -d "$icondir" ]; then
		find -L "$icondir" -type f | while read i; do
			check_image "$i"
			[ "$( stat -Lc '%a %U %G' "$i" )" = "644 root root" ] || \
				ls -bld "$i" >> .badperms.$$
		done
	fi
done

[ -s .nonimages.$$ ] && warn "non-image files in icon dirs:" && cat .nonimages.$$

if [ -s .badperms.$$ ]; then
	warn "bad permissions/owner on icon(s) (should be 0644 root:root):"
	cat .badperms.$$
fi

rm -f .nonimages.$$ .badperms.$$