only delete links; delete existing destination before creating links
Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
		
					parent
					
						
							
								a132adbb03
							
						
					
				
			
			
				commit
				
					
						bba68b6e40
					
				
			
		
					 3 changed files with 53 additions and 4 deletions
				
			
		
							
								
								
									
										26
									
								
								cdist/conf/type/__link/explorer/type
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										26
									
								
								cdist/conf/type/__link/explorer/type
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
#!/bin/sh
 | 
			
		||||
# 2013 Steven Armstrong (steven-cdist armstrong.cc)
 | 
			
		||||
 | 
			
		||||
destination="/$__object_id"
 | 
			
		||||
 | 
			
		||||
if [ ! -e "$destination" ]; then
 | 
			
		||||
   echo none
 | 
			
		||||
elif [ -h "$destination" ]; then
 | 
			
		||||
   echo symlink
 | 
			
		||||
elif [ -f "$destination" ]; then
 | 
			
		||||
   type="$(cat "$__object/parameter/type")"
 | 
			
		||||
   case "$type" in
 | 
			
		||||
      hard)
 | 
			
		||||
         link_count=$(ls -l "$destination" | awk '{ print $2 }')
 | 
			
		||||
         if [ $link_count -gt 1 ]; then
 | 
			
		||||
            echo hardlink
 | 
			
		||||
            exit 0
 | 
			
		||||
         fi
 | 
			
		||||
      ;;
 | 
			
		||||
   esac
 | 
			
		||||
   echo file
 | 
			
		||||
elif [ -d "$destination" ]; then
 | 
			
		||||
   echo directory
 | 
			
		||||
else
 | 
			
		||||
   echo unknown
 | 
			
		||||
fi
 | 
			
		||||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
#!/bin/sh
 | 
			
		||||
#
 | 
			
		||||
# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
 | 
			
		||||
# 2013 Steven Armstrong (steven-cdist at armstrong.cc)
 | 
			
		||||
#
 | 
			
		||||
# This file is part of cdist.
 | 
			
		||||
#
 | 
			
		||||
| 
						 | 
				
			
			@ -40,17 +41,38 @@ case "$type" in
 | 
			
		|||
esac
 | 
			
		||||
 | 
			
		||||
state_is="$(cat "$__object/explorer/state")"
 | 
			
		||||
state_should=present
 | 
			
		||||
[ -f "$__object/parameter/state" ] && state_should="$(cat "$__object/parameter/state")"
 | 
			
		||||
state_should="$(cat "$__object/parameter/state")"
 | 
			
		||||
 | 
			
		||||
[ "$state_should" = "$state_is" ] && exit 0
 | 
			
		||||
 | 
			
		||||
file_type="$(cat "$__object/explorer/type")"
 | 
			
		||||
case "$state_should" in
 | 
			
		||||
    present)
 | 
			
		||||
        echo ln ${lnopt} -f \"$source\" \"$destination\"
 | 
			
		||||
        if [ "$file_type" = "directory" ]; then
 | 
			
		||||
            # our destination is currently a directory, move it out of the way
 | 
			
		||||
            cat << DONE
 | 
			
		||||
destination_old="\$(mktemp "${destination}.cdist.XXXXXXXXXX")"
 | 
			
		||||
mv "$destination" "\$destination_old"
 | 
			
		||||
DONE
 | 
			
		||||
        fi
 | 
			
		||||
 | 
			
		||||
        # create our link
 | 
			
		||||
        cat << DONE
 | 
			
		||||
ln ${lnopt} -f "$source" "$destination"
 | 
			
		||||
DONE
 | 
			
		||||
 | 
			
		||||
        if [ "$file_type" = "directory" ]; then
 | 
			
		||||
            # delete the legacy directory
 | 
			
		||||
            cat << DONE
 | 
			
		||||
rm -rf "\$destination_old"
 | 
			
		||||
DONE
 | 
			
		||||
        fi
 | 
			
		||||
    ;;
 | 
			
		||||
    absent)
 | 
			
		||||
        # only delete if it is a sym/hard link
 | 
			
		||||
        if [ "$file_type" = "symlink" -o "$file_type" = "hardlink" ]; then
 | 
			
		||||
            echo rm -f \"$destination\"
 | 
			
		||||
        fi
 | 
			
		||||
    ;;
 | 
			
		||||
    *)
 | 
			
		||||
        echo "Unknown state: $state_should" >&2
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										1
									
								
								cdist/conf/type/__link/parameter/default/state
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								cdist/conf/type/__link/parameter/default/state
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
present
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue