1#!/bin/sh
2#
3# s3fs - FUSE-based file system backed by Amazon S3
4#
5# Copyright 2007-2008 Randy Rizun <rrizun@gmail.com>
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20#
21
22#
23# Merge old directory object to new.
24# For s3fs after v1.64
25#
26
27###
28### UsageFunction <program name>
29###
30UsageFunction()
31{
32    echo "Usage: $1 [-h] [-y] [-all] <base directory>"
33    echo "  -h   print usage"
34    echo "  -y   no confirm"
35    echo "  -all force all directories"
36    echo "       There is no -all option is only to merge for other S3 client."
37    echo "       If -all is specified, this shell script merge all directory"
38    echo "       for s3fs old version."
39    echo ""
40}
41
42### Check parameters
43WHOAMI=`whoami`
44OWNNAME=`basename $0`
45AUTOYES="no"
46ALLYES="no"
47DIRPARAM=""
48
49while [ "$1" != "" ]; do
50    if [ "X$1" = "X-help" -o "X$1" = "X-h" -o "X$1" = "X-H" ]; then
51        UsageFunction $OWNNAME
52        exit 0
53    elif [ "X$1" = "X-y" -o "X$1" = "X-Y" ]; then
54        AUTOYES="yes"
55    elif [ "X$1" = "X-all" -o "X$1" = "X-ALL" ]; then
56        ALLYES="yes"
57    else
58        if [ "X$DIRPARAM" != "X" ]; then
59            echo "*** Input error."
60            echo ""
61            UsageFunction $OWNNAME
62            exit 1
63        fi
64        DIRPARAM=$1
65    fi
66    shift
67done
68if [ "X$DIRPARAM" = "X" ]; then
69    echo "*** Input error."
70    echo ""
71    UsageFunction $OWNNAME
72    exit 1
73fi
74
75if [ "$WHOAMI" != "root" ]; then
76    echo ""
77    echo "Warning: You run this script by $WHOAMI, should be root."
78    echo ""
79fi
80
81### Caution
82echo "#############################################################################"
83echo "[CAUTION]"
84echo "This program merges a directory made in s3fs which is older than version 1.64."
85echo "And made in other S3 client application."
86echo "This program may be have bugs which are not fixed yet."
87echo "Please execute this program by responsibility of your own."
88echo "#############################################################################"
89echo ""
90
91DATE=`date +'%Y%m%d-%H%M%S'`
92LOGFILE="$OWNNAME-$DATE.log"
93
94echo -n "Start to merge directory object... [$DIRPARAM]"
95echo "# Start to merge directory object... [$DIRPARAM]" >> $LOGFILE
96echo -n "# DATE :        " >> $LOGFILE
97echo `date` >> $LOGFILE
98echo -n "# BASEDIR :     " >> $LOGFILE
99echo `pwd` >> $LOGFILE
100echo -n "# TARGET PATH : " >> $LOGFILE
101echo $DIRPARAM >> $LOGFILE
102echo  "" >> $LOGFILE
103
104if [ "$AUTOYES" = "yes" ]; then
105    echo "(no confirmation)"
106else
107    echo ""
108fi
109echo ""
110
111### Get Directory list
112DIRLIST=`find $DIRPARAM -type d -print | grep -v ^\.$`
113
114#
115# Main loop
116#
117for DIR in $DIRLIST; do
118    ### Skip "." and ".." directories
119    BASENAME=`basename $DIR`
120    if [ "$BASENAME" = "." -o "$BASENAME" = ".." ]; then
121        continue
122    fi
123
124    if [ "$ALLYES" = "no" ]; then
125        ### Skip "d---------" directories.
126        ### Other clients make directory object "dir/" which don't have
127        ### "x-amz-meta-mode" attribute.
128        ### Then these directories is "d---------", it is target directory.
129        DIRPERMIT=`ls -ld --time-style=+'%Y%m%d%H%M' $DIR | awk '{print $1}'`
130        if [ "$DIRPERMIT" != "d---------" ]; then
131            continue
132        fi
133    fi
134
135    ### Confirm
136    ANSWER=""
137    if [ "$AUTOYES" = "yes" ]; then
138        ANSWER="y"
139    fi
140    while [ "X$ANSWER" != "XY" -a "X$ANSWER" != "Xy" -a "X$ANSWER" != "XN" -a "X$ANSWER" != "Xn" ]; do
141        echo -n "Do you merge $DIR? (y/n): "
142        read ANSWER
143    done
144    if [ "X$ANSWER" != "XY" -a "X$ANSWER" != "Xy" ]; then
145        continue
146    fi
147
148    ### Do
149    CHOWN=`ls -ld --time-style=+'%Y%m%d%H%M' $DIR | awk '{print $3":"$4" "$7}'`
150    CHMOD=`ls -ld --time-style=+'%Y%m%d%H%M' $DIR | awk '{print $7}'`
151    TOUCH=`ls -ld --time-style=+'%Y%m%d%H%M' $DIR | awk '{print $6" "$7}'`
152
153    echo -n "*** Merge $DIR :	"
154    echo -n "	$DIR :		" >> $LOGFILE
155
156    chmod 755 $CHMOD > /dev/null 2>&1
157    RESULT=$?
158    if [ $RESULT -ne 0 ]; then
159        echo "Failed(chmod)"
160        echo "Failed(chmod)" >> $LOGFILE
161        continue
162    fi
163    chown $CHOWN > /dev/null 2>&1
164    RESULT=$?
165    if [ $RESULT -ne 0 ]; then
166        echo "Failed(chown)"
167        echo "Failed(chown)" >> $LOGFILE
168        continue
169    fi
170    touch -t $TOUCH > /dev/null 2>&1
171    RESULT=$?
172    if [ $RESULT -ne 0 ]; then
173        echo "Failed(touch)"
174        echo "Failed(touch)" >> $LOGFILE
175        continue
176    fi
177    echo "Succeed"
178    echo "Succeed" >> $LOGFILE
179done
180
181echo ""
182echo "" >> $LOGFILE
183echo "Finished."
184echo -n "# Finished : " >> $LOGFILE
185echo `date` >> $LOGFILE
186
187#
188# Local variables:
189# tab-width: 4
190# c-basic-offset: 4
191# End:
192# vim600: expandtab sw=4 ts=4 fdm=marker
193# vim<600: expandtab sw=4 ts=4
194#
195