简体   繁体   English

macos中目录的阴影

[英]shasum of a directory in macos

I'm writing a shell script that uses the shasum to check if the contents of a directory have changed. 我正在编写一个使用shasum来检查目录内容是否已更改的shell脚本。

On Linux and FreeBSD, the shasum have the same behavior when I do shasum <directory> however, on MacOS the shasum give me hashes for files only . 在Linux和FreeBSD上,当我执行shasum <directory>时, shasum具有相同的行为,但是,在MacOS上, shasum给我文件散列。

FreeBSD FreeBSD的

$ shasum CONTENTS/
7f986e5e5289c59db1bba48df92ffe4707830aaa  CONTENTS/

Linux Linux的

$ shasum CONTENTS/
7f986e5e5289c59db1bba48df92ffe4707830aaa  CONTENTS/

MacOS 苹果系统

$ shasum CONTENTS/
shasum: CONTENTS/: 

How could I calculate the hash of a directory in MacOS? 如何在MacOS中计算目录的哈希值?

TRY 1: Using TAR with pipes 试用1:将TAR与管道一起使用

Tried to use but seems that this tar option doesn't work on MacOS. 尝试使用,但似乎此tar选项在MacOS上不起作用。

tar cO CONTENTS/ | shasum
tar: Option -O is not permitted in mode -c
da39a3ee5e6b4b0d3255bfef95601890afd80709  -

TRY 2: Using FIND/EXEC 尝试2:使用查找/ EXEC

It was consistent between MacOS and FreeBSD, but Linux returned a weird hash MacOS和FreeBSD之间是一致的,但是Linux返回了一个奇怪的哈希

find CONTENTS -type f -exec shasum {} \; | sort -k 2 | shasum

Linux Linux的

c2ddb9bc5f543e956f5cdcc76750cb78cc5f26f3

FreeBSD FreeBSD的

3ac2a9d4e2fc5d2d2ec3c7f612e680990cc35824

MacOS 苹果系统

3ac2a9d4e2fc5d2d2ec3c7f612e680990cc35824

OTHER FINDINGS ON TAR tar would be excellent as it "archives" a folder and then I could shasum it, however the order of how tar "walk" the folder structure is not consistent across operating systems . 关于TAR的其他发现tar会非常好,因为它可以“归档”文件夹,然后我可以shasum处理,但是tar如何“遍历”文件夹结构的顺序在整个操作系统中不一致 As some helpers mentioned in the comments that I should use the same version of tar in all systems. 正如评论中的一些帮助者所述,我应该在所有系统中使用相同版本的tar

Just an example, on system 1 I have this order: 只是一个例子,在系统1上,我有以下命令:

drwxr-xr-x  0 root   wheel       0 27 Jul 07:23 usr/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f1/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f1/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f1/f0/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f1/f0/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f2/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f2/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/f0/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/f0/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f3/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f3/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/f1/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/f1/aaa

and on system 2 I have the following order: 在系统2上,我的命令如下:

drwxr-xr-x  0 root   wheel       0 27 Jul 07:23 usr/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f1/
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f2/
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f3/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f3/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/f1/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f3/f2/f1/aaa
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f2/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/f0/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f2/f1/f0/aaa
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f1/aaa
drwxr-xr-x  0 root   wheel       0 27 Jul 07:25 usr/f1/f0/
-rw-r--r--  0 root   wheel       0 27 Jul 07:25 usr/f1/f0/aaa

From a tar standpoint it if all good, but due to the order, the shasum produces a different hash. tar角度来看,如果一切都很好,但是由于顺序, shasum会产生不同的哈希值。

CONCLUSION 结论

shasum is consistent among Linux and BSDs to check an individual file hash, but, when it comes to directories the consistency happens only on MacOS and FreeBSD, perhaps due to how files are sorted. shasum在Linux和BSD之间检查单个文件的哈希值是一致的,但是,涉及目录时,一致性仅在MacOS和FreeBSD上发生,这可能是由于文件的排序方式所致。

If sorting is enforced using the find command, consistency is only obtained in FreeBSD and MacOS, however this method is time prohibitive as it takes a significant amount of time to calculate the hashes for every single file and then the whole structure hash. 如果使用find命令强制进行排序,则只能在FreeBSD和MacOS中获得一致性,但是此方法禁止时间,因为要花费大量时间来计算每个文件的哈希值,然后计算整个结构的哈希值。

Using tar to create a temporary file and then doing a shasum also found to be inconsistent between Linux and BSDs, perhaps because of difference in the archiving method. 还发现使用tar创建一个临时文件,然后进行一次shasum ,这在Linux和BSD之间是不一致的,这可能是由于归档方法的不同。

I think the only way forward is to redesign my solution . 我认为前进的唯一方法是重新设计解决方案

I had this problem some time ago and I ended up tarring the directory and generating a hash from the tar. 我前段时间遇到了这个问题,最终我将目录打包并从tar生成哈希。

$ mkdir -p test
$ echo 1 > test/tmp
$ tar cO test/ | md5sum
7b18a99a8ccfef1ebbfd1e7a8b2852ee
$ echo 2 > test/tmp
$ tar cO test/ | md5sum
644042dd530157e604641ea89b4e9152

Note that if you write the same content to a file, the modified stat is updated and a new hash will be generated for that directory. 请注意,如果您将相同的内容写入文件,则modified统计信息将更新,并且将为该目录生成新的哈希。

$ echo 2 > test/tmp
$ tar cO test/ | md5sum
da25819594f123563a837d5786e51950  -
$ echo 2 > test/tmp 
$ tar cO test/ | md5sum
9407b64d43b809a5828a9fc2297b4e9c  -

ps you should change md5sum for shasum :-) ps你应该为shasum更改md5sum :-)

EDIT: 编辑:
Discussed it shortly with a friend who uses MacOSX and he came up with: 与一位使用MacOSX的朋友讨论了一下,他想到了:

$ mkdir tmp; echo 1 > test/a;
$ tar -cf - tmp/ | md5sum
26c43adc9eca9f63279d08a0d145dd7d  - 
$ echo 2 > test/a
$ tar -cf - tmp/ | md5sum
dc3a68cac0b0224be9b202d86e69c5bd  -

Note that MACosx uses md5 instead of md5sum . 请注意,MACosx使用md5而不是md5sum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM