Subject: Re: bug#8766: Bug in sha1sum?

From: Pádraig Brady <[email-masked]>
Date: Tue, 31 May 2011 09:49:52 +0100

tags 8766 notabug

On 30/05/11 11:51, Theo Band wrote:
> Hi
> 
> I'm not sure, but I think I found a bug in sha1sum. It's easy to
> reproduce with any file that contains a backslash (\) in the name:
> echo test > test
> $ sha1sum test
> 4e1243bd22c66e76c2ba9eddc1f91394e57f9f83  test
> $ mv test 'test\test'
> $ sha1sum 'test\test'
> \4e1243bd22c66e76c2ba9eddc1f91394e57f9f83  test\\test
> 
> I expect the file sha1sum to be the same after renaming the file (a
> backslash is prepended to the otherwise correct result).
> 
> sha1sum --version
> sha1sum (GNU coreutils) 5.97
> coreutils-5.97-23.el5_6.4

This is expected.
Here is a shell function I use in FSlint to
clean the output from these utilities
when we know we'll not have files with \n chars.

cleanup_sum() {
  sed '
  # md5sum and sha1sum et. al. from coreutils at least,
  # to deal with \n in filenames, convert any \ and \n chars
  # to \\ and \\n respectively. Currently we ignore files with \n
  # so just undo this problematic escaping
  /^\\/{s/^\\//; s/\\\\/\\/g};

  # These utils also add a "*" flag character for normal files
  # on platforms where O_BINARY is significant (like CYGWIN).
  # We always process in binary mode and so remove that flag here
  s/^\([^ ]*\) \*/\1  /;
  '
}

So you can just:

sha1sum test | cleanup_sum

cheers,
Pádraig.