[pacman-dev] [PATCH] BSD/Mac OS X sed inplace backups

Allan McRae allan at archlinux.org
Sun Feb 15 20:19:53 EST 2009


Kevin Barry wrote:
>> What does BSD sed currently do?  I am assuming it leaves a backup file(s) but can you clarify the numbers and names of these?
>>     
>
> On Mac OS X (Man page is from FreeBSD, so I believe it would be the same there):
> sed -i -e 's/foo/bar/' filename
> will create a backup file named filename-e
>
> sed -i -e 's/foo/bar' -e '/asd/dsa/' filename
> will give an error message because
>
> sed -i 's/foo/bar' filename will give an error message
>
> sed -i '' 's/foo/bar/' filename will work without creating a backup
> file, but fails on GNU/sed
>
> It's really annoying that BSD sed doesn't like sed -i'' and GNU/sed
> doesn't like sed -i ''
>
>
> This patch changes the way that sed inplace is run in makepkg when
> running in versionpkg mode. Previous, sed would run with no backup
> file, called in the GNU way. It seems there is no cross platform
> method of calling sed inplace with no backup (BSD/sed uses sed -i '',
> GNU/sed uses sed -i'' or simply sed -i).
>
> Instead, this patch creates a backup file (.pkgsave) and removes it
> after the succesful completion of the sed operation. This means the
> user should not see the backup file, unless something went run (full
> hard drive?) in which case they don't lose the PKGBUILD as they would
> have before.
>
> Signed-off-by: Kevin Barry <barryk gmail com>
> ---
>  scripts/makepkg.sh.in |    5 +++--
>  1 files changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
> index e10c345..8ddf88f 100644
> --- a/scripts/makepkg.sh.in
> +++ b/scripts/makepkg.sh.in
> @@ -1199,8 +1199,9 @@ devel_update() {
>     if [ -n "$newpkgver" ]; then
>         if [ "$newpkgver" != "$pkgver" ]; then
>             if [ -f "./$BUILDSCRIPT" ]; then
> -               sed -i "s/^pkgver=[^ ]*/pkgver=$newpkgver/" "./$BUILDSCRIPT"
> -               sed -i "s/^pkgrel=[^ ]*/pkgrel=1/" "./$BUILDSCRIPT"
> +               sed -i.pkgsave -e "s/^pkgver=[^ ]*/pkgver=$newpkgver/" \
> +                          -e "s/^pkgrel=[^ ]*/pkgrel=1/" "./$BUILDSCRIPT" &&
> +                   rm -f "./${BUILDSCRIPT}.pkgsave"
>                 source "$BUILDSCRIPT"
>             fi
>         fi
>   


Thanks for the explaination.  This change make sense to me now.

I assume that if PKGBUILD.pkgsave actually existed on someones system 
then this would overwrite it.  It would be better to get the temporary 
filename using "mktemp -u ./$BUILDSCRIPT.xxxxxxxx".  You can then 
extract the .xxxx part using bash string manipulation.

Allan





More information about the pacman-dev mailing list