Files we don't need, including symbolic links that might result
problematic to make dist, are removed from the copied data.
---
tests/nodeinfodata/copy-from-host.sh | 170 +++++++++++++++++++++++++++++++++++
1 file changed, 170 insertions(+)
create mode 100755 tests/nodeinfodata/copy-from-host.sh
diff --git a/tests/nodeinfodata/copy-from-host.sh b/tests/nodeinfodata/copy-from-host.sh
new file mode 100755
index 0000000..b5c5e8e
--- /dev/null
+++ b/tests/nodeinfodata/copy-from-host.sh
@@ -0,0 +1,170 @@
+#!/bin/sh
+
+# copy-from-host.sh - Copy nodeinfo test data from a running host
+
+# Copyright (C) 2015 Red Hat, Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see
+# <
http://www.gnu.org/licenses/>.
+
+SYSFS_PATH="/sys/devices/system"
+KEEP_LINKS="cpu[0-9]*"
+KEEP_DIRECTORIES="cpu
+ cpu[0-9]*
+ node
+ node[0-9]*
+ topology"
+KEEP_FILES="core_id
+ core_siblings*
+ kernel_max
+ meminfo
+ offline
+ online
+ physical_package_id
+ possible
+ present
+ thread_siblings*"
+
+die() {
+ typeset message=$1
+
+ echo "$message" >&2
+
+ exit 1
+}
+
+keep() {
+ typeset tag=$1
+ typeset path=$2
+
+ echo " $tag $path"
+
+ return 0
+}
+
+delete() {
+ typeset tag=$1
+ typeset path=$2
+
+ rm -rf "$path"
+
+ return $?
+}
+
+is_valid_name() {
+ typeset name=$1
+ typeset ret=0
+
+ case "$name" in
+ */*)
+ # We don't want to have to deal with subdirectories, so
+ # names containing slashes are rejected
+ ret=1
+ ;;
+ esac
+
+ return $ret
+}
+
+matches_any_pattern() {
+ typeset name=$1
+ typeset patterns=$2
+ typeset ret=1
+
+ for pattern in $patterns; do
+ case "$name" in
+ $pattern)
+ ret=0
+ ;;
+ esac
+ done
+
+ return $ret
+}
+
+main() {
+ typeset name=$1
+
+ if ! test "$name"; then
+ die "Usage: $SELF NAME"
+ fi
+
+ if ! is_valid_name "$name"; then
+ die "Name '$name' is not valid"
+ fi
+
+ # Create directory
+ if test -e "$name"; then
+ die "$name: File or directory already exists"
+ fi
+
+ if ! mkdir "$name" >/dev/null 2>&1; then
+ die "$name: Unable to create directory"
+ fi
+
+ echo "Copying host data..."
+
+ # Copy data from host. Errors are ignored because some files we don't
+ # care about always give input/output error when read
+ cp -r "$SYSFS_PATH/cpu" "$name/" >/dev/null 2>&1
+ cp -r "$SYSFS_PATH/node" "$name/" >/dev/null 2>&1
+
+ if ! test -d "$name/cpu" || ! test -d "$name/node"; then
+ die "Error while copying host data"
+ fi
+
+ echo "Cleaning up data..."
+
+ # Delete symbolic links
+ find "$name" -type l | while read l; do
+ b=${l##*/}
+ if matches_any_pattern "$b" "$KEEP_LINKS"; then
+ keep "l" "$l"
+ else
+ delete "l" "$l"
+ fi
+ done
+
+ # Delete directories
+ find "$name" -type d | while read d; do
+ b=${d##*/}
+ # We don't want to delete the directory we just created :)
+ if test "$b" = "$name"; then
+ continue
+ fi
+ if matches_any_pattern "$b" "$KEEP_DIRECTORIES"; then
+ keep "d" "$d"
+ else
+ delete "d" "$d"
+ fi
+ done
+
+ # Delete files
+ find "$name" -type f | while read f; do
+ b=${f##*/}
+ if matches_any_pattern "$b" "$KEEP_FILES"; then
+ keep "f" "$f"
+ else
+ delete "f" "$f"
+ fi
+ done
+
+ echo "All done"
+
+ return 0
+}
+
+SELF=$0
+main "$@"
+exit $?
--
2.4.3