我正在尝试使用c创建tar文件。出于某种原因,我无法使用
system("tar -cvf xxxx.tar xxxx");
我的代码是:
#include <stdio.h>
#include <libtar.h>
#include <fcntl.h>
int main(void) {
TAR *pTar;
char *tarFilename = "file.tar";
char *srcDir = "directory";
char *extractTo = ".";
tar_open(&pTar, tarFilename, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU);
tar_append_tree(pTar, srcDir, extractTo);
tar_close(pTar);
return (0);
}
运行此代码后,当我想用
解压缩时tar -xvf file.tar
我收到错误
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
我的c代码有什么问题?
答案 0 :(得分:9)
我认为你需要在关闭tar文件之前调用tar_append_eof http://linux.die.net/man/3/tar_append_eof。
tar_append_eof()函数将EOF标记(全部为两个块)写入与t关联的tar文件中。
答案 1 :(得分:0)
如您所说的修改后的代码,但执行失败,
int main(int argc, char** argv)
{
TAR *pTar;
enter code here
if (argc == 4)
{
char *tarFilename = argv[1];
char *srcDir = argv[2];
char *extractTo = argv[3];
tar_open(&pTar, tarFilename, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU);
tar_append_tree(pTar, srcDir, extractTo);
tar_append_eof(pTar);
tar_close(pTar);
}
else
{
fprintf(stdout,"Usage : arg1 : file.tar \n \ "
" arg2 : directory \n \ "
" arg3 : Extract to \n");
}
return (0);
}
但这失败了:
$ ./TarProg makefile.tar /home/prashant/prashant/eclipse2/TarProg/Debug /home/prashant/prashant/eclipse2/TarProg/Debug/tmp/
*** buffer overflow detected ***: ./TarProg terminated
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x45)[0xb76bbeb5]
/lib/i386-linux-gnu/libc.so.6(+0x103c8a)[0xb76bac8a]
/lib/i386-linux-gnu/libc.so.6(+0x1031b2)[0xb76ba1b2]
./TarProg[0x8049bca]
./TarProg[0x8049851]
./TarProg[0x8049376]
./TarProg[0x804ba7a]
./TarProg[0x8048ed5]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75d04d3]
./TarProg[0x8048dc1]
======= Memory map: ========
08048000-0804f000 r-xp 00000000 08:02 810831 /home/prashant/prashant/eclipse2/TarProg/Debug/TarProg
0804f000-08050000 r--p 00006000 08:02 810831 /home/prashant/prashant/eclipse2/TarProg/Debug/TarProg
08050000-08051000 rw-p 00007000 08:02 810831 /home/prashant/prashant/eclipse2/TarProg/Debug/TarProg
08051000-08053000 rw-p 00000000 00:00 0
0974b000-0976c000 rw-p 00000000 00:00 0 [heap]
b7548000-b7564000 r-xp 00000000 08:02 392629 /lib/i386-linux-gnu/libgcc_s.so.1
b7564000-b7565000 r--p 0001b000 08:02 392629 /lib/i386-linux-gnu/libgcc_s.so.1
b7565000-b7566000 rw-p 0001c000 08:02 392629 /lib/i386-linux-gnu/libgcc_s.so.1
b757a000-b7585000 r-xp 00000000 08:02 403881 /lib/i386-linux-gnu/libnss_files-2.15.so
b7585000-b7586000 r--p 0000a000 08:02 403881 /lib/i386-linux-gnu/libnss_files-2.15.so
b7586000-b7587000 rw-p 0000b000 08:02 403881 /lib/i386-linux-gnu/libnss_files-2.15.so
b7587000-b7591000 r-xp 00000000 08:02 403880 /lib/i386-linux-gnu/libnss_nis-2.15.so
b7591000-b7592000 r--p 00009000 08:02 403880 /lib/i386-linux-gnu/libnss_nis-2.15.so
b7592000-b7593000 rw-p 0000a000 08:02 403880 /lib/i386-linux-gnu/libnss_nis-2.15.so
b7593000-b75a9000 r-xp 00000000 08:02 403725 /lib/i386-linux-gnu/libnsl-2.15.so
b75a9000-b75aa000 r--p 00015000 08:02 403725 /lib/i386-linux-gnu/libnsl-2.15.so
b75aa000-b75ab000 rw-p 00016000 08:02 403725 /lib/i386-linux-gnu/libnsl-2.15.so
b75ab000-b75ad000 rw-p 00000000 00:00 0
b75ad000-b75b4000 r-xp 00000000 08:02 403882 /lib/i386-linux-gnu/libnss_compat-2.15.so
b75b4000-b75b5000 r--p 00006000 08:02 403882 /lib/i386-linux-gnu/libnss_compat-2.15.so
b75b5000-b75b6000 rw-p 00007000 08:02 403882 /lib/i386-linux-gnu/libnss_compat-2.15.so
b75b6000-b75b7000 rw-p 00000000 00:00 0
b75b7000-b775b000 r-xp 00000000 08:02 403884 /lib/i386-linux-gnu/libc-2.15.so
b775b000-b775d000 r--p 001a4000 08:02 403884 /lib/i386-linux-gnu/libc-2.15.so
b775d000-b775e000 rw-p 001a6000 08:02 403884 /lib/i386-linux-gnu/libc-2.15.so
b775e000-b7761000 rw-p 00000000 00:00 0
b7774000-b7777000 rw-p 00000000 00:00 0
b7777000-b7778000 r-xp 00000000 00:00 0 [vdso]
b7778000-b7798000 r-xp 00000000 08:02 403783 /lib/i386-linux-gnu/ld-2.15.so
b7798000-b7799000 r--p 0001f000 08:02 403783 /lib/i386-linux-gnu/ld-2.15.so
b7799000-b779a000 rw-p 00020000 08:02 403783 /lib/i386-linux-gnu/ld-2.15.so
bf82f000-bf850000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
答案 2 :(得分:0)
而不是tar_open(&amp; pTar,tarFilename,NULL,O_WRONLY | O_CREAT,0644,TAR_GNU);
使用以下内容: tar_open(&amp; pTar,tarFilename,NULL,O_WRONLY | O_CREAT,0644,TAR_IGNORE_MAGIC); 要么 tar_open(&amp; pTar,tarFilename,NULL,O_WRONLY | O_CREAT,0644,TAR_VERBOSE);
原因可能是:&#34; char magic [6]&#34;是6.如果选项是TAR_GNU,某处库试图将8字节复制到魔法中,如下所示。因此,在这种情况下,我们必须传递除TAR_GNU之外的选项。
if (t->options & TAR_GNU)
strncpy(t->th_buf.magic, "ustar ", 8);