Using libtar library in c

I am trying to create a tar file using c. For some reason I canโ€™t use

system("tar -cvf xxxx.tar xxxx");

my code is:

 #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); } 

After running this code, when I want to unzip

 tar -xvf file.tar 

I get an error

 tar: This does not look like a tar archive tar: Exiting with failure status due to previous errors 

What is the problem with my c code?

+6
source share
3 answers

I think you need to call tar_append_eof http://linux.die.net/man/3/tar_append_eof before closing the tar file.

The tar_append_eof () function writes the EOF token (two blocks of all zeros) to the tar file associated with t.

+8
source

The modified code, as you said, but it does not execute,

 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); } 

But this fails:

 $ ./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) 
0
source

Instead of tar_open (& pTar, tarFilename, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU);

Use the following: tar_open (& pTar, tarFilename, NULL, O_WRONLY | O_CREAT, 0644, TAR_IGNORE_MAGIC); Or tar_open (& pTar, tarFilename, NULL, O_WRONLY | O_CREAT, 0644, TAR_VERBOSE);

The reason may be: The size of "char magic [6]" is 6. Somewhere, the library is trying to copy 8 bytes to the magic, as shown below, if the option is TAR_GNU. Therefore, in this case, we must pass an option other than TAR_GNU.

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

Source: https://habr.com/ru/post/972667/


All Articles