Hui Zhou wrote:
On Tue, Sep 20, 2005 at 11:45:13AM -0400, Jesse Guardiani wrote:
Tonight I plan to write a simple shell script to convert an entire
directory automatically.
I can use "Dumping" and "The stream seems ok to me" and the exit code
(looks like 0
is success and 1 is failure) as my reference for what the packfix has
done to the current file.
Thanks again!
I am glad the program is useful for you. I guess you can use the
messages the way you described. Let me know if you enounter glitches.
Please see attached for a script capable of converting all files given
on the command line.
Place it in the same directory as packfix, cd to that dir, then run it
like this:
./packfix_dir.sh `find /share/tv -name '*.nuv' -and -mtime '+8'
-print0 | xargs -0 echo`
(The above would convert everything in /share/tv/*.nuv with an mtime
equal to or older than 8 days.)
My shell code tends to be a bit verbose and some of this stuff could be
normalized/functionalized, but safety was the primary design goal and I
think it's fairly safe. By default it doesn't delete the original file;
It moves it to
the current directory. If you need to delete the file AND you trust my
code &
packfix, then simply uncomment line 55 of packfix_dir.sh (You're braver
than I!).
It uses `mv` internally, so it should be quite fast, but the consequence
is that
it's not atomic. Each of your files will blink out of existence for a
second on
line 47. :)
I've converted 61 files (and counting) successfully (well, I don't know
if they work yet,
but packfix didn't return an error) with this script. YMMV. Don't blame
me if it deletes
your filesystem. :)
--
Jesse Guardiani
Programmer/Sys Admin
[EMAIL PROTECTED]
#!/bin/sh
# files this script appends to
:>packfix.log
:>packfix_dir.log
:>packfix_dir_error.files
:>packfix_dir_fixed.files
:>packfix_dir_already_ok.files
echo "<<< DATETIME:" `date` | tee -a packfix_dir.log
for file in "$@"; do
echo "<<< STARTING '$file'" | tee -a packfix_dir.log
./packfix "$file" | tee packfix.log
PACKFIX_EXIT_CODE=$?
ALREADY_OK=`grep 'stream seems ok' packfix.log`
DUMPING=`grep 'Dumping fixed' packfix.log`
if [ "$PACKFIX_EXIT_CODE" -eq "1" ]; then
echo "<<< ERROR processing '$file'" | tee -a packfix_dir.log
else
if [ -n "$DUMPING" -a "$ALREADY_OK" ]; then
echo
echo "<<< ERROR reading packfix exit status" | tee -a packfix_dir.log
echo "$file" >> packfix_dir_error.files
elif [ -n "$DUMPING" ]; then
echo
echo "<<< FIXED" | tee -a packfix_dir.log
if [ ! -s fixed.mpg ]; then
echo "<<< ERROR fixed.mpg doesn't exist or zero file size, skipping!" |
tee -a packfix_dir.log
else
# save ownership to reapply later
ORIG_USER=`ls -l "$file" | awk -- '{print $3}'`
ORIG_GROUP=`ls -l "$file" | awk -- '{print $4}'`
# sanity check file size! new file should be smaller, but within 99% of
original
SIZE_ORIG=`ls -l "$file" | awk -- '{print $5}'`
SIZE_FIXED=`ls -l fixed.mpg | awk -- '{print $5}'`
PERCENT=`expr "$SIZE_FIXED" '*' 100 / "$SIZE_ORIG"`
if [ "$PERCENT" != "99" ]; then
echo "<<< ERROR fixed.mpg size not 99% of original, skipping! \
fixed='$SIZE_FIXED',orig='$SIZE_ORIG',percent='$PERCENT'" | tee -a
packfix_dir.log
else
# looks good! Replace file with fixed.mpg!
FILE_BASENAME=`basename "$file"`
mv "$file" "$FILE_BASENAME.bak" && mv "fixed.mpg" "$file" && chown
"$ORIG_USER:$ORIG_GROUP" "$file"
MV_EXIT_CODE=$?
if [ "$MV_EXIT_CODE" -ne "0" ]; then
echo "<<< ERROR installing" | tee -a packfix_dir.log
else
# if you don't have enough disk space to archive everything, and you
# trust me, uncomment this line:
#rm "$FILE_BASENAME.bak"
echo "<<< INSTALLED" | tee -a packfix_dir.log
fi
fi
fi
echo "$file" >> packfix_dir_fixed.files
elif [ -n "$ALREADY_OK" ]; then
echo
echo "<<< ALREADY_OK" | tee -a packfix_dir.log
echo "$file" >> packfix_dir_already_ok.files
else
echo
echo "<<< ERROR default case reached. This should never happen!" | tee -a
packfix_dir.log
fi
fi
echo
echo "<<< DATETIME:" `date` | tee -a packfix_dir.log
done
echo "<<< DONE" | tee -a packfix_dir.log