From 9d25dfdfa9c41a59fc67fce3e41e5b6292de6bdd Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Mon, 6 Feb 2012 21:08:59 +0100 Subject: [PATCH] properly handle existing links Signed-off-by: Steven Armstrong --- conf/type/__link/explorer/state | 62 +++++++++++++++++++++++++++++++++ conf/type/__link/gencode-remote | 7 ++++ 2 files changed, 69 insertions(+) create mode 100755 conf/type/__link/explorer/state diff --git a/conf/type/__link/explorer/state b/conf/type/__link/explorer/state new file mode 100755 index 00000000..a9220a3c --- /dev/null +++ b/conf/type/__link/explorer/state @@ -0,0 +1,62 @@ +#!/bin/sh +# +# 2012 Steven Armstrong (steven-cdist at armstrong.cc) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# + +destination="/$__object_id" +type="$(cat "$__object/parameter/type")" +source="$(cat "$__object/parameter/source")" + +# no destination? -> state is absent +if [ ! -e "$destination" ]; then + echo absent + exit 0 +fi + +destination_dir="${destination%/*}" + +case "$type" in + symbolic) + cd "$destination_dir" + source_is=$(ls -l "$destination" | sed 's/.*-> //g') + if [ -h "$destination" -a "$source_is" = "$source" ]; then + echo present + else + echo absent + fi + ;; + hard) + cd "$destination_dir" + # check source relative to destination_dir + if [ ! -e "$source" ]; then + echo sourcemissing + exit 0 + fi + destination_inode=$(ls -i "$destination" | awk '{print $1}') + source_inode=$(ls -i "$source" | awk '{print $1}') + if [ "$destination_inode" -eq "$source_inode" ]; then + echo present + else + echo absent + fi + ;; + *) + echo "Unknown type: $type" >&2 + exit 1 + ;; +esac diff --git a/conf/type/__link/gencode-remote b/conf/type/__link/gencode-remote index 0a367654..8d4cc3d5 100755 --- a/conf/type/__link/gencode-remote +++ b/conf/type/__link/gencode-remote @@ -39,7 +39,14 @@ case "$type" in ;; esac +state_is="$(cat "$__object/explorer/state")" state_should="$(cat "$__object/parameter/state")" + +if [ "$state_should" = "$state_is" ]; then + # nothing to do + exit 0 +fi + case "$state_should" in present) echo ln ${lnopt} -f \"$source\" \"$destination\"