Reputation: 509
I am trying to make a tar file using c. For a reason I can not 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 untar with
tar -xvf file.tar
I am getting 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 ?
Upvotes: 9
Views: 10484
Reputation: 1
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);
Reason could be: Size of char magic[6]
is 6. Somewhere library is trying to copy 8 bytes into magic
as below if option is TAR_GNU
. So we have to pass option other than TAR_GNU
in this case.
if (t->options & TAR_GNU)
strncpy(t->th_buf.magic, "ustar ", 8);
Upvotes: 0
Reputation: 199
Modified code as you said, but it fails in execution,
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)
Upvotes: 0
Reputation: 35276
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 an EOF marker (two blocks of all zeros) to the tar file associated with t.
Upvotes: 11