2019-08-10 17:57:26 +02:00
|
|
|
#!/usr/bin/env bash
|
2008-01-08 23:11:00 +01:00
|
|
|
############################################################################
|
|
|
|
# tools/link.sh
|
|
|
|
#
|
2024-09-10 14:15:58 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
2021-02-18 13:48:10 +01:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership. The
|
|
|
|
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance with the
|
|
|
|
# License. You may obtain a copy of the License at
|
2008-01-08 23:11:00 +01:00
|
|
|
#
|
2021-02-18 13:48:10 +01:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2008-01-08 23:11:00 +01:00
|
|
|
#
|
2021-02-18 13:48:10 +01:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2008-01-08 23:11:00 +01:00
|
|
|
#
|
|
|
|
############################################################################
|
2008-01-09 01:07:03 +01:00
|
|
|
#set -x
|
2008-01-08 23:11:00 +01:00
|
|
|
|
|
|
|
src=$1
|
|
|
|
dest=$2
|
|
|
|
|
|
|
|
# Verify that arguments were provided
|
|
|
|
|
|
|
|
if [ -z "${src}" -o -z "${dest}" ]; then
|
2020-01-05 17:29:59 +01:00
|
|
|
echo "Missing src and/or dest arguments"
|
|
|
|
exit 1
|
2008-01-08 23:11:00 +01:00
|
|
|
fi
|
|
|
|
|
2008-01-09 00:55:02 +01:00
|
|
|
# Check if something already exists at the destination path replace it with
|
|
|
|
# the new link (which might be different). Note that we check for the
|
|
|
|
# the link (-h) before we check for existence (-e) because a bad link will
|
|
|
|
# report that it does not exist.
|
2008-01-08 23:11:00 +01:00
|
|
|
|
2008-01-09 00:55:02 +01:00
|
|
|
if [ -h "${dest}" ]; then
|
2014-11-11 14:00:18 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
# If the link is already created (and matches the request) do nothing
|
2014-11-11 14:00:18 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
if [ "$(readlink ${dest})" = "${src}" ]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
2014-11-11 14:00:18 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
# Otherwise, remove the link
|
2014-11-11 14:00:18 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
rm -f "${dest}"
|
2008-01-09 00:55:02 +01:00
|
|
|
else
|
2008-01-08 23:11:00 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
# If the path exists and is a directory that contains the "fake link"
|
|
|
|
# mark, then treat it like a soft link (i.e., remove the directory)
|
2008-01-08 23:11:00 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
if [ -d "${dest}" -a -f "${dest}/.fakelnk" ]; then
|
|
|
|
rm -rf "${dest}"
|
|
|
|
else
|
2008-01-08 23:11:00 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
# Does anything exist at the destination path?
|
2008-01-08 23:11:00 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
if [ -e "${dest}" ]; then
|
2008-01-08 23:11:00 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
# It is something else (like a file) or directory that does
|
|
|
|
# not contain the "fake link" mark
|
2008-01-08 23:11:00 +01:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
echo "${dest} already exists but is not a symbolic link"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2008-01-08 23:11:00 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Verify that a directory exists at the source path
|
|
|
|
|
2008-01-09 00:55:02 +01:00
|
|
|
if [ ! -d "${src}" ]; then
|
2020-01-05 17:29:59 +01:00
|
|
|
echo "No directory at ${src}"
|
|
|
|
exit 1
|
2008-01-08 23:11:00 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create the soft link
|
|
|
|
|
|
|
|
ln -s "${src}" "${dest}" || \
|
2020-01-05 17:29:59 +01:00
|
|
|
{ echo "Failed to create link: $dest" ; exit 1 ; }
|
2018-06-29 15:08:51 +02:00
|
|
|
|
|
|
|
# Verify that the link was created
|
|
|
|
|
2024-01-08 16:24:48 +01:00
|
|
|
if [ -e ${dest} ] && [ -h ${dest} ]; then
|
2021-12-28 18:21:50 +01:00
|
|
|
# The file exists and is a symlink (i.e. the symlink isn't broken)
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
else
|
2020-01-05 17:29:59 +01:00
|
|
|
# The MSYS 'ln' command actually does a directory copy
|
2018-06-29 15:08:51 +02:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
if [ -d ${dest} ]; then
|
|
|
|
# Create the .fakelnk for unlink.sh
|
2018-06-29 16:55:32 +02:00
|
|
|
|
2020-01-05 17:29:59 +01:00
|
|
|
touch ${dest}/.fakelnk
|
2021-12-28 18:21:50 +01:00
|
|
|
exit 0
|
2020-01-05 17:29:59 +01:00
|
|
|
else
|
2021-12-28 18:21:50 +01:00
|
|
|
echo "Error: link at ${dest} not created."
|
2020-01-05 17:29:59 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
2018-06-29 15:08:51 +02:00
|
|
|
fi
|