坑人的macos tar 命令 (实际上是bsdtar)换用 gnu tar

周末 看着笔记本上好用的朗文当代高级词典(mac版)和其它两部词典,准备复制到黑苹果台式机上去。考虑到词典内容有太多小文件,普通复制传输太慢,毫无疑问用 tar 打包肯定快而且能保留原始文件的各种信息。命令如下:

bash 复制代码
time tar czf ~/tmp/Diction.tgz ./LDOCE5.dictionary ./ccald.dictionary 

大约3分钟左右结束,sftp 上传台式机。运行 解压命令:

bash 复制代码
$ tar xf ~/Diction.tgz 
tar: Special header too large
tar: Error exit delayed from previous errors.

这什么意思?赶快在笔记本上测试一遍,结果一样。用了这么多年Linux和macos 这是第一次碰到tar命令创建成功 但解压却失败,如果这是备份关键文件,后果无法想象。网上搜索了一下,看到下面的内容:

https://forums.freebsd.org/threads/tar-special-header-too-large-error.73612/

bash 复制代码
tar: Special header too large error | The FreeBSD Forums
Jan 8, 2020 · I'm piping a bzip2 compressed tar file into tar on FreeBSD 11.2-RELEASE and seeing this error. Anyone any idea what this may indicated? I suspect that after this 

嗯 对解决问题毫无用途,不过发现一点,这个应该是 bsdtar出现的问题。

bash 复制代码
$ ls -l `which tar`
lrwxr-xr-x  1 root  wheel  6 Feb 26 13:47 /usr/bin/tar -> bsdtar

去GNU官网 ​​​​​​https://ftp.gnu.org/gnu/tar/ 下载最新版 tar ,命令行编译:

bash 复制代码
$ tar xf ~/Downloads/tar-1.35.tar.xz
$ cd tar-1.35
$ ./configure
$ make

.......
  CC       xattrs.o
  CCLD     tar
Undefined symbols for architecture x86_64:
  "_iconv", referenced from:
      _utf8_convert in utf8.o
  "_iconv_open", referenced from:
      _utf8_convert in utf8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [tar] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
make  35.08s user 11.97s system 90% cpu 52.118 total

本以为是个简单的库文件问题,结果搜索半天毫无进展,花的时间可以直接传输几遍了,不过这么简单的问题都解决不了,还怎么开发程序?终于下定决心,直接改Makefile,为保险起见,先添加 -v 参数到Link命令参数中:

bash 复制代码
$ mvim src/Makefile
  LDFLAGS = -v
$ make
  CCLD     tar
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 12.0.0 13.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o tar -L/usr/local/lib buffer.o checkpoint.o compare.o create.o delete.o exit.o exclist.o extract.o xheader.o incremen.o list.o map.o misc.o names.o sparse.o suffix.o system.o tar.o transform.o unlink.o update.o utf8.o warning.o xattrs.o ../lib/libtar.a ../gnu/libgnu.a -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/lib/darwin/libclang_rt.osx.a

仔细看 没有 iconv库,虽然 Makefile 中有 LIBICONV = -liconv

但链接时却没有库,原因未知,哎 算了,简单点直接在 src/Makefile 中修改LDFLAGS 如下:

bash 复制代码
LDFLAGS = -liconv -v

再次 make 成功!用 gnu tar 打包 ,没有任何问题。那么,用 bsdtar 就没有办法了?还是有的,用man 查看了 bsdtar的用法 可用下述命令解决:

bash 复制代码
time bsdtar -c --no-mac-metadata -z -f ~/tmp/Diction4.tgz ./LDOCE5.dictionary ./ccald.dictionary

没错,加个 --no-mac-metadata 参数即可,当然这会损失部分mac特有的元数据信息。

网上有很多对比 bsdtar 和 gnu tar 的,都认为 bsdtar 在 稀疏文件有优势,不过经历这个创建文件不报错,解压却会报错的特例,还是小心为上,最好备份后验证一下;或者选用 gnu tar 吧好像还没听说过有类似问题。

一个小小的问题,浪费大半天时间,还是写下来 以备参考。

相关推荐
2501_915106321 小时前
在Mac上搭建iOS开发环境的详细步骤与注意事项
ide·vscode·macos·ios·个人开发·swift·敏捷流程
想个名字想老半天1 小时前
uni 离线打包 ios,适用于自定义 ios系统最低适配 保姆级教程
macos·ios·cocoa
zzzb1234561 小时前
CC-Switch 全平台下载、安装与使用全指南(Windows/macOS/Linux)
linux·windows·macos·ai编程·cc-switch
BugShare2 小时前
macOS 原生定时任务应用,简洁优雅、所见所得
macos
CyL_Cly21 小时前
localsend安卓手机下载 支持win/mac/ubuntu
android·macos·智能手机
A懿轩A21 小时前
Thaw:让 macOS 菜单栏重获新生的免费全能管理工具,适配 macOS Tache 26.4.1 系统,Ice平替活跃版,解决 Ice 常见报错问题
macos
无效的名字1 天前
mac下安装openclaw
macos
00后程序员张1 天前
完整指南 iOS App上架到App Store的步骤详解
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
开开心心loky1 天前
[OC 底层] (二)类与对象底层原理
macos·ios·objective-c·cocoa
谢斯2 天前
[esp-idf]macos 环境安装 v6.0
macos