Reputation: 69
I'm trying to create a basic version of the linux "tar" command using C, i used perror to see if there are any errors during execution, and i got this
./tar
Error2: Bad file descriptor
and this is what i did so far
#include <stdio.h>
#include <libtar.h>
#include <fcntl.h>
int main(void)
{
TAR *pTar;
char *prefix = ".";
char *filename = "file.tar";
if ((tar_open(&pTar, filename, NULL, O_WRONLY, 0644, TAR_GNU)) == -1)
perror("Error1");
else if ((tar_extract_all(pTar, prefix)) == -1)
perror("Error2");
else if ((tar_close(pTar)) == -1)
perror("Error3");
}
Thanks in advance:)
Upvotes: 1
Views: 5661
Reputation: 1
For any one referencing to this in the future,
while creating the tar - say abc.tar if you have appended the abc.tar (it is also in the same directory and append_tree appends all file, including the one), your abc.tar file, on tar_extract_all, this .tar file (with the same name) is also extracted overwriting the original tar file.
atleast this was what causing the mysterious "Invalid argument" for me. I fixed it by renaming the original file before extracting.
Upvotes: 0
Reputation: 140196
you're opening your tar
file in O_WRONLY
mode, so it truncates the existing file instead of opening it for reading.
When you try to extract from the file you get an error (probably when reading the header), that's expected because file contents are clobbered by the previous (successul) call.
Check working examples here:
O_RDONLY
To sum it up, my fix: replace
if ((tar_open(&pTar, filename, NULL, O_WRONLY, 0644, TAR_GNU)) == -1)
by
if ((tar_open(&pTar, filename, NULL, O_RDONLY, 0644, TAR_GNU)) == -1)
(I don't think all parameters are useful in read mode, like permissions or tar type, but that should work, it's difficult to find proper examples for that library)
Upvotes: 4