23 int ExtractZstd(
char *Source,
const char *OrigName,
char *Destination)
25 char Cmd[FILENAME_MAX * 4];
27 char TempSource[FILENAME_MAX];
28 char CWD[FILENAME_MAX];
29 char OutputName[FILENAME_MAX] = {
'\0'};
30 char format[6] = {
'\0'};
31 char formatCmd[17] = {
'\0'};
35 if ((NULL == Source) || (!strcmp(Source,
"")) || (NULL == Destination) ||
36 (!strcmp(Destination,
""))) {
40 if (getcwd(CWD,
sizeof(CWD)) == NULL) {
41 fprintf(stderr,
"ERROR: directory name longer than %d characters\n",
47 while (OrigName[i] !=
'\0') {
48 if (OrigName[i] ==
'.') {
49 const char *ext = &OrigName[i + 1];
50 if (strncasecmp(ext,
"zst", 3) == 0) {
51 strcpy(format,
"zstd");
54 else if (strncasecmp(ext,
"tzst", 4) == 0) {
55 strcpy(format,
"zstd");
58 else if (strncasecmp(ext,
"lzma", 4) == 0) {
59 strcpy(format,
"lzma");
62 else if (strncasecmp(ext,
"xz", 2) == 0) {
66 else if (strncasecmp(ext,
"txz", 3) == 0) {
70 else if (strncasecmp(ext,
"lz4", 3) == 0) {
71 strcpy(format,
"lz4");
74 else if (strncasecmp(ext,
"tlz4", 4) == 0) {
75 strcpy(format,
"lz4");
79 OutputName[i] = OrigName[i];
83 if (strlen(format) > 1) {
84 snprintf(formatCmd,
sizeof(formatCmd),
" --format=%s", format);
88 printf(
"CWD: %s\n", CWD);
89 if (!
Quiet) { fprintf(stderr,
"Extracting zstd: %s\n", Source); }
92 if (chdir(Destination) != 0) {
93 fprintf(stderr,
"ERROR %s.%d: Unable to change directory to %s\n",
94 __FILE__, __LINE__, Destination);
95 fprintf(stderr,
"ERROR: errno is: %s\n", strerror(errno));
98 if (
TaintString(TempSource, FILENAME_MAX, Source, 1, NULL)) {
101 memset(Cmd,
'\0',
sizeof(Cmd));
104 printf(
"Extracting zstd with output name: %s and format: %s\n", OutputName,
109 if (TempSource[0] !=
'/') {
110 snprintf(Cmd,
sizeof(Cmd),
111 " (zstd --decompress --output-dir-flat '%s' -o '%s/%s'%s "
112 "'%s/%s') 2>/dev/null", Destination, Destination, OutputName, formatCmd,
116 snprintf(Cmd,
sizeof(Cmd),
117 " (zstd --decompress --output-dir-flat '%s' -o '%s/%s'%s "
118 "'%s') 2>/dev/null", Destination, Destination, OutputName, formatCmd,
121 rc = WEXITSTATUS(system(Cmd));
123 fprintf(stderr,
"ERROR: Command failed (rc=%d): %s\n", rc, Cmd);
127 if (chdir(CWD) != 0) {
128 fprintf(stderr,
"ERROR %s.%d: Unable to change directory to %s\n", __FILE__, __LINE__, CWD);
129 fprintf(stderr,
"ERROR: errno is: %s\n", strerror(errno));
int Verbose
Verbose level.
Stores all extern variables used by the agent.
int Quiet
Run in quiet mode?
char * TaintString(char *S)
Create a string with taint quoting.
int ExtractZstd(char *Source, const char *OrigName, char *Destination)
Given a ZSTd file, extract the contents to the directory.