bug-gnu-libiconv
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnu-libiconv] Add -i inplace option for iconv.


From: Heikki Orsila
Subject: Re: [bug-gnu-libiconv] Add -i inplace option for iconv.
Date: Mon, 30 Jul 2018 21:33:38 +0300
User-agent: Mutt/1.5.23 (2014-03-12)

On Tue, Jul 24, 2018 at 02:11:42AM +0200, Bruno Haible wrote:
> Hi,
> 
> > I often convert my old text files encoded in iso-8859-1 to utf-8, but
> > iconv can not do it inplace. So I created a patch that adds -i option for 
> > iconv
> > that allows to convert files "inplace".
> > 
> > Please see the attached patch.
> 
> The patch is technically nearly right; the only problematic points are:
>   - missing error checking of the fclose(outfile) call,
>   - refusal to handle non-regular files (such as symlinks),
>   - use of <libgen.h> - better use the corresponding functions from gnulib.
> 
> The major problem, however, is:
> 
>   - It takes about 60 lines of code to implement something that is better
>     done in 1 line of shell script. Needless added complexity. Violates
>     the Unix philosophy.
>       mv "${f}" "${f}~" && iconv ... < "${f}~" > "${f}" && rm -f "${f}~"

That shell script is broken in principle because it assumes "${f}-" does not
exist or is not useful. A shell script should create safe and reliable
temp files to do the processing: temp files for large files (not everything
fits in memory), temp files inside the same filesystem to preserve data
confidentiality due to encryption of the filesystem,
and reliability by being able to atomically replace the new
file with the old one which needs the same filesystem.

The latter two points (imo) being more important here.
So I thought of writing a suitable shell script that would use

        $(tempfile -d "$(dirname "$f")") || error_check

and realized everyone who needs this use-case is just doing redundant work,
so why not just create it for iconv tool like "sed -i" does?

Do you consider this use-case important enough to be supported by iconv?
If yes, I can address the techical problems. If no, thanks for reviewing the
code :)

> 
> Why is it _better_ done in a shell script?
> 
>   - Often when using iconv, the user should prepare a backup of the old
>     contents.
> 
>   - The way to handle symlinks is specific to the user's environment
>     (e.g. they don't want to convert the same file twice).
> 
>   - The way to handle the timestamp (modification time) is specific to the
>     user's environment.
> 
> So, these issues need to be decided by the user. This can be done in a
> better way in a shell script.
> 
> > I have a question regarding the environmental deps of iconv. Is using
> > fstat(), rename(), remove(), mkstemp() allowed?
> 
> Yes, these are POSIX functions. Gnulib modules can be used for those that
> are not fully portable.
> 
> Bruno
> 

-- 
Heikki Orsila
address@hidden
http://www.iki.fi/shd



reply via email to

[Prev in Thread] Current Thread [Next in Thread]