在c中使用libtar库

我正在尝试使用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代码有什么问题?

Solutions Collecting From Web of "在c中使用libtar库"

我想你需要在关闭tar文件之前调用tar_append_eof http://linux.die.net/man/3/tar_append_eof

tar_append_eof()函数将EOF标记(两个全部为0的块)写入与t关联的tar文件。

修改后的代码如你所说,但是执行失败,

 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) 

而不是tar_open(&pTar,tarFilename,NULL,O_WRONLY | O_CREAT,0644,TAR_GNU);

使用以下命令:tar_open(&pTar,tarFilename,NULL,O_WRONLY | O_CREAT,0644,TAR_IGNORE_MAGIC); 或tar_open(&pTar,tarFilename,NULL,O_WRONLY | O_CREAT,0644,TAR_VERBOSE);

原因可能是:“char magic [6]”的大小是6.如果选项是TAR_GNU,某个地方的图书馆正试图把8bytes复制到下面的魔术中。 所以在这种情况下我们必须通过TAR_GNU以外的选项。

 if (t->options & TAR_GNU) strncpy(t->th_buf.magic, "ustar ", 8);