forked from dtmateojr/dtmrepo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone
executable file
·124 lines (102 loc) · 2.41 KB
/
clone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
usage() {
echo
echo "Usage: `basename $0` <source> <dest>"
exit 1
}
samefs() {
FS1=`stat -c %d $1`
FS2=`stat -c %d $2`
[ "${FS1}" == "${FS2}" ] && return 0
}
cderror() {
echo "Could not cd into $1."
exit 1
}
toucherror() {
echo "Write test failed on $1."
exit 1
}
mkdirerror() {
echo "Error creating $1."
exit 1
}
copyerror() {
echo "Error during copy."
exit 1
}
docopy() {
SOURCE=$1
DEST=$2
SOURCETYPE=''
DESTTYPE=''
if [ -e ${SOURCE} ]; then
SOURCETYPE=$(stat -c %F ${SOURCE})
else
echo "${SOURCE} does not exist."
exit 1
fi
if [ -e ${DEST} ]; then
DESTTYPE=$(stat -c %F ${DEST})
if [ "${DESTTYPE}" == 'directory' ]; then
if ! samefs ${SOURCE} ${DEST}; then
echo "$(basename $0) will only work if ${SOURCE} and ${DEST} are in the same filesystem."
exit 1
fi
else
echo 'Destination exists. Aborting.'
exit 1
fi
fi
if [ "${SOURCETYPE}" == 'directory' ]; then
[ -e "${DEST}" ] && DEST="${DEST}/$(basename ${SOURCE})"
if mkdir -p ${DEST}; then
if ! samefs ${SOURCE} ${DEST}; then
echo "$(basename $0) will only work if ${SOURCE} and ${DEST} are in the same filesystem."
\rmdir ${DEST}
exit 1
fi
else
mkdirerror ${DEST}
exit 1
fi
TESTFILE=${DEST}/$(basename $(mktemp))
if touch ${TESTFILE}; then
\rm ${TESTFILE}
else
toucherror ${DEST}
fi
CWD=$(pwd)
cd ${DEST} || cderror ${DEST}
DEST=$(pwd)
cd ${CWD}
cd ${SOURCE} || cderror ${SOURCE}
find -type d -exec mkdir -p ${DEST}/{} \;
if [ $? -ne 0 ]; then
cd ${CWD}
copyerror
fi
find ! -type d -exec \ln -f {} ${DEST}/{} \;
if [ $? -ne 0 ]; then
cd ${CWD}
copyerror
fi
cd ${CWD}
else
\ln -f ${SOURCE} ${DEST}
fi
}
[ $# -lt 2 ] && usage
if [ $# -gt 2 ]; then
eval LASTARG=\$$#
if [ "$(stat -c %F ${LASTARG})" != "directory" ]; then
echo "$LASTARG must be a directory."
exit 1
fi
while [ "$#" -gt 1 ]; do
docopy $1 ${LASTARG}
shift
done
else
docopy $1 $2
fi