2019-08-10 23:57:26 +08:00
|
|
|
#!/usr/bin/env bash
|
2008-01-09 06:11:00 +08:00
|
|
|
############################################################################
|
|
|
|
# tools/link.sh
|
|
|
|
#
|
2024-09-10 20:15:58 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
2021-02-18 20:48:10 +08: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-09 06:11:00 +08:00
|
|
|
#
|
2021-02-18 20:48:10 +08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2008-01-09 06:11:00 +08:00
|
|
|
#
|
2021-02-18 20:48:10 +08: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-09 06:11:00 +08:00
|
|
|
#
|
|
|
|
############################################################################
|
2008-01-09 08:07:03 +08:00
|
|
|
#set -x
|
2008-01-09 06:11:00 +08:00
|
|
|
|
|
|
|
src=$1
|
|
|
|
dest=$2
|
|
|
|
|
|
|
|
# Verify that arguments were provided
|
|
|
|
|
|
|
|
if [ -z "${src}" -o -z "${dest}" ]; then
|
2020-01-06 00:29:59 +08:00
|
|
|
echo "Missing src and/or dest arguments"
|
|
|
|
exit 1
|
2008-01-09 06:11:00 +08:00
|
|
|
fi
|
|
|
|
|
2008-01-09 07:55:02 +08: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-09 06:11:00 +08:00
|
|
|
|
2008-01-09 07:55:02 +08:00
|
|
|
if [ -h "${dest}" ]; then
|
2014-11-11 21:00:18 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
# If the link is already created (and matches the request) do nothing
|
2014-11-11 21:00:18 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
if [ "$(readlink ${dest})" = "${src}" ]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
2014-11-11 21:00:18 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
# Otherwise, remove the link
|
2014-11-11 21:00:18 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
rm -f "${dest}"
|
2008-01-09 07:55:02 +08:00
|
|
|
else
|
2008-01-09 06:11:00 +08:00
|
|
|
|
2020-01-06 00:29:59 +08: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-09 06:11:00 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
if [ -d "${dest}" -a -f "${dest}/.fakelnk" ]; then
|
|
|
|
rm -rf "${dest}"
|
|
|
|
else
|
2008-01-09 06:11:00 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
# Does anything exist at the destination path?
|
2008-01-09 06:11:00 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
if [ -e "${dest}" ]; then
|
2008-01-09 06:11:00 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
# It is something else (like a file) or directory that does
|
|
|
|
# not contain the "fake link" mark
|
2008-01-09 06:11:00 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
echo "${dest} already exists but is not a symbolic link"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2008-01-09 06:11:00 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Verify that a directory exists at the source path
|
|
|
|
|
2008-01-09 07:55:02 +08:00
|
|
|
if [ ! -d "${src}" ]; then
|
2020-01-06 00:29:59 +08:00
|
|
|
echo "No directory at ${src}"
|
|
|
|
exit 1
|
2008-01-09 06:11:00 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create the soft link
|
|
|
|
|
|
|
|
ln -s "${src}" "${dest}" || \
|
2020-01-06 00:29:59 +08:00
|
|
|
{ echo "Failed to create link: $dest" ; exit 1 ; }
|
2018-06-29 21:08:51 +08:00
|
|
|
|
|
|
|
# Verify that the link was created
|
|
|
|
|
2024-01-08 23:24:48 +08:00
|
|
|
if [ -e ${dest} ] && [ -h ${dest} ]; then
|
2021-12-29 01:21:50 +08:00
|
|
|
# The file exists and is a symlink (i.e. the symlink isn't broken)
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
else
|
2020-01-06 00:29:59 +08:00
|
|
|
# The MSYS 'ln' command actually does a directory copy
|
2018-06-29 21:08:51 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
if [ -d ${dest} ]; then
|
|
|
|
# Create the .fakelnk for unlink.sh
|
2018-06-29 22:55:32 +08:00
|
|
|
|
2020-01-06 00:29:59 +08:00
|
|
|
touch ${dest}/.fakelnk
|
2021-12-29 01:21:50 +08:00
|
|
|
exit 0
|
2020-01-06 00:29:59 +08:00
|
|
|
else
|
2021-12-29 01:21:50 +08:00
|
|
|
echo "Error: link at ${dest} not created."
|
2020-01-06 00:29:59 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2018-06-29 21:08:51 +08:00
|
|
|
fi
|