Re: Wrapper script
On Tue, Apr 24, 2007 at 11:33:15AM +0200, Manuel Prinz wrote:
> Hi everyone,
> I ITP a package that provides a single binary that expects its input
> file named in a special way, and writes two files with fixed names. One
> of those is only temporary and not really usefull, so one usually
> deletes it. I thought of writing a wrapper script, so the user can give
> the input and output filename, the renaming would be done by the script,
> as well as taking care of not overriding files if they already exist.
Okay, it would be good to provide an option to inhibit deletion of the
file, and it would be good if the renames would be guaranteed to be on
the same filesystem and thus atomic.
Also be sure to use "exec" if this is a shells script, so you don't
have a needless bash process which is hanging around just going to do
> Are there any guideline how this is done? I've seen packages using
> "foo.bin" for the original binary and "foo" for the wrapper script, or
> some using a different location such as /usr/lib/foo/bin for the binary.
> Or should one leave the binary as "foo" to provide the know behavior and
> use "foo-wrapper" for the script?
These are all possibilities from which to choose depending on your
goals. Another possibility is to make source changes to support
--input, --output, --[no-]remove.
> I'd also be glad if you could point me out to some good source of how to
> use /tmp in a secure way in scripts;
The important thing is that (at a low level) you open files with
O_CREAT|O_EXCL. If you do this, it is safe: you won't clobber any
existing file. If you don't, it is unsafe. See also this manpages
> I'd like to run the binary there
> because I'm not a fan of joking around in the filesystem.
Check if TMPDIR is set, and use it instead.
> Any advise would be very welcome!
The Debian tools to create tempfiles are tmpfile and mktemp. These
will use /tmp by default, and handle TMPDIR. Or, if you put the
tempfiles in the same dir as the final pathnames, then you can do "mv"
and it will be atomic. Or, you can use mkdtemp -d to make a directory
directly below the final pathname (also guaranteed to be on the same
FS). Since it's a new dir, you can assume it's empty. If ther user
modifies things in it you can assume they know what they're doing.