问题
I have a little problem when I'm trying to extract a file using Libtar.
This is my code:
int htlp_decompress_decompress(char * filename) {
TAR * tar_file;
char rootdir[200];
strcpy(rootdir, "/var/cache/htpackage/");
if (tar_open(&tar_file, filename, NULL, O_RDONLY, 0, TAR_GNU) == -1) {
fprintf(stderr, "tar_open(): %s\n", strerror(errno));
return -1;
}
if (tar_extract_all(tar_file, rootdir) != 0) {
fprintf(stderr, "tar_extract_all(): %s\n", strerror(errno));
return -1;
}
if (tar_close(tar_file) != 0) {
fprintf(stderr, "tar_close(): %s\n", strerror(errno));
return -1;
}
return 0;
}
The problem is that I'm getting the error "Invalid Argument" in tar_extract_all() function. But I can not know what is causing this error.
Does anyone know what is happening?
Thank you for your attention.
回答1:
According to the man-page, the function declaration is:
int tar_open(TAR **t, char *pathname, tartype_t *type, int oflags, int mode, int options);
That means you're passing O_RDONLY
as the tartype_t *type
parameter. This is incorrect. Perhaps you meant this:
tar_open(&tar_file, filename, NULL, O_RDONLY, 0, TAR_GNU)
来源:https://stackoverflow.com/questions/17267372/extract-file-with-libtar