Skip to content

Commit 7ecaead

Browse files
committed
Sync files by default, introduce option to disable
1 parent 7a5f681 commit 7ecaead

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Repository head:
2+
3+
* Sync data by default, add --no-sync switch to disable. New
4+
version of FLACs are written out to prevent missing/incomplete
5+
files in the face of a crash.
6+
17
1.1 (2016-04-25):
28

39
* Add --no-action, enabling the view of files that would be

README.adoc

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ Requirements
2525
Both `flac` and `metaflac` need to be in your $PATH. These programs
2626
are normally packaged together as “flac” by distributions.
2727

28+
The script both depends on Bash and GNU features of other core
29+
utilities. Versions of `mv`, `getopt`, `sync`, etc from other sources
30+
(such as the *BSD operating systems) might work, but require
31+
compatibility with the same switches found in the GNU versions.
32+
33+
reflac has only been developed on tested on GNU+Linux, but should work
34+
on any other operating system with the appropriate tools.
35+
36+
Safety
37+
------
38+
39+
By default, reflac performs a sync on files after two points of its
40+
process: after moving the recompressed file from the temporary
41+
directory to the original location, and after renaming the file from
42+
“*.new” to the original name. This should provide safety in the case
43+
of system crashes or processes being killed.
44+
45+
Assuming file system syncs have not been disabled, one of two
46+
scenarios should arise in the worst case:
47+
48+
1. No new files appear in the directory. A complete FLAC file may
49+
or may not still be available in a temporary staging directory under
50+
/tmp.
51+
2. There exists an additional “*.new” file in the directory being
52+
processed, which may or may not be a complete FLAC file, the status
53+
of which can be tested with `flac -t`.
54+
2855
Running
2956
-------
3057
@@ -39,6 +66,8 @@ Usage: reflac [OPTION]... [--] DIRECTORY...
3966
-r --recursive Recurse into directories.
4067
-n --no-action Do not recompress. With --verbose, displays a list of
4168
files that would be processed.
69+
-s --no-sync Do not synchronize file data. Will return faster, with
70+
the potential danger to lose your files in a system crash.
4271
-0 --fast Use the fastest, but worst, compression possible.
4372
-1..-7 Adjust FLAC compresion between these standard ranges.
4473
The default is -5, the same as for flac itself.
@@ -53,7 +82,7 @@ Bugs
5382
----
5483

5584
What, bugs? This program is flawless! Joking aside, although the
56-
author tries to resolve in `reflac` itself, it is at the mercy of bugs
85+
author tries to resolve in reflac itself, it is at the mercy of bugs
5786
and limitations from flac and metaflac.
5887

5988
Tag preservation in the light of malformed tags simply does not exist.
@@ -67,7 +96,7 @@ $ reflac FLAC
6796
field contains no '=' character
6897
....
6998

70-
`reflac` will not continue after the error, resulting in the
99+
reflac will not continue after the error, resulting in the
71100
untagged-but-recompressed file remaining in the temporary directory,
72101
the path of which should be part of the error message as in the
73102
example. The original file will not have been overwritten,

reflac

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ VERSION=1.1
77
VERBOSE=0
88
RECURSIVE=0
99
NOACTION=0
10+
SYNC=1
1011
FLAC_LEVEL=5 # Default FLAC compression level
1112

1213
TMPDIR="$(mktemp --tmpdir -d reflac.XXXXXXXXXX)"
1314
CWD="$(pwd)"
1415
SELF="$(readlink -f "$0")"
15-
OPTS=$(getopt -o Vvhrn012345678 \
16-
-l version,verbose,help,recursive,no-action,fast,best \
16+
OPTS=$(getopt -o Vvhrns012345678 \
17+
-l version,verbose,help,recursive,no-action,no-sync,fast,best \
1718
-n reflac -- "$@")
1819

1920
eval set -- "$OPTS"
@@ -35,6 +36,8 @@ Usage: reflac [OPTION]... [--] DIRECTORY...
3536
-r --recursive Recurse into directories.
3637
-n --no-action Do not recompress. With --verbose, displays a list of
3738
files that would be processed.
39+
-s --no-sync Do not synchronize file data. Will return faster, with
40+
the potential danger to lose your files in a system crash.
3841
-0 --fast Use the fastest, but worst, compression possible.
3942
-1..-7 Adjust FLAC compresion between these standard ranges.
4043
The default is -5, the same as for flac itself.
@@ -73,7 +76,14 @@ recompress()
7376
metaflac --no-utf8-convert \
7477
--import-tags-from="$TMPDIR/$(basename -s flac -- "$flac")tag" \
7578
-- "$TMPDIR/$flac"
76-
mv -f -- "$TMPDIR/$flac" .
79+
mv -f -- "$TMPDIR/$flac" "$flac.new"
80+
if [ $SYNC -eq 1 ]; then
81+
sync "$flac.new"
82+
mv -f "$flac.new" "$flac"
83+
sync "$flac"
84+
else
85+
mv -f "$flac.new" "$flac"
86+
fi
7787
rm -f -- "$TMPDIR/$(basename -s flac -- "$flac")tag"
7888
fi
7989
done
@@ -99,6 +109,9 @@ while true ; do
99109
-n|--no-action)
100110
NOACTION=1
101111
shift ;;
112+
-s|--no-sync)
113+
SYNC=0
114+
shift ;;
102115
-V|--version)
103116
echo "reflac version $VERSION"
104117
exit 0 ;;

reflac.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ enabled.
4444
Do not recompress any files. Combined with the *-v* option, reflac
4545
will display a list of files it would otherwise recompress.
4646

47+
*-s*, *--no-sync*::
48+
Do not synchronize file data or the rename operation. The script will
49+
return to prompt faster, but introduces a chance to lose files or have
50+
incomplete files after a system crash.
51+
4752
*-0*, *--fast*::
4853
Passes *-0* onto the *flac* command. This is the least attainable
4954
compression possible, and it is also the least CPU-intensive for both

0 commit comments

Comments
 (0)