blob: 07befce171d76fcb687c97d44843d7ff3a7452cc [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2** Copyright 1998-2003 University of Illinois Board of Trustees
3** Copyright 1998-2003 Mark D. Roth
4** All rights reserved.
5**
6** wrapper.c - libtar high-level wrapper code
7**
8** Mark D. Roth <roth@uiuc.edu>
9** Campus Information Technologies and Educational Services
10** University of Illinois at Urbana-Champaign
11*/
12
bigbiff bigbiff9c754052013-01-09 09:09:08 -050013#include <internal.h>
14
15#include <stdio.h>
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050016#include <stdlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050017#include <sys/param.h>
18#include <dirent.h>
19#include <errno.h>
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050020
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021#ifdef STDC_HEADERS
22# include <string.h>
23#endif
24
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050025
bigbiff bigbiff9c754052013-01-09 09:09:08 -050026int
27tar_extract_glob(TAR *t, char *globname, char *prefix)
28{
29 char *filename;
30 char buf[MAXPATHLEN];
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050031 int i, fd = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050032
33 while ((i = th_read(t)) == 0)
34 {
35 filename = th_get_pathname(t);
36 if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
37 {
38 if (TH_ISREG(t) && tar_skip_regfile(t))
39 return -1;
40 continue;
41 }
42 if (t->options & TAR_VERBOSE)
43 th_print_long_ls(t);
44 if (prefix != NULL)
45 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
46 else
47 strlcpy(buf, filename, sizeof(buf));
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050048 if (tar_extract_file(t, buf, prefix, &fd) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050049 return -1;
50 }
51
52 return (i == 1 ? 0 : -1);
53}
54
55
56int
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050057tar_extract_all(TAR *t, char *prefix, const int *progress_fd)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050058{
59 char *filename;
60 char buf[MAXPATHLEN];
61 int i;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050062
bigbiff bigbiff9c754052013-01-09 09:09:08 -050063#ifdef DEBUG
Mohd Faraz566ef6b2023-01-16 16:16:21 +010064 LOG("==> tar_extract_all(TAR *t, \"%s\")\n",
bigbiff bigbiff9c754052013-01-09 09:09:08 -050065 (prefix ? prefix : "(null)"));
66#endif
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050067
bigbiff bigbiff9c754052013-01-09 09:09:08 -050068 while ((i = th_read(t)) == 0)
69 {
70#ifdef DEBUG
Mohd Faraz566ef6b2023-01-16 16:16:21 +010071 LOG(" tar_extract_all(): calling th_get_pathname()");
bigbiff bigbiff9c754052013-01-09 09:09:08 -050072#endif
73 filename = th_get_pathname(t);
74 if (t->options & TAR_VERBOSE)
75 th_print_long_ls(t);
76 if (prefix != NULL)
77 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
78 else
79 strlcpy(buf, filename, sizeof(buf));
80#ifdef DEBUG
Mohd Faraz566ef6b2023-01-16 16:16:21 +010081 LOG(" tar_extract_all(): calling tar_extract_file(t, "
bigbiff bigbiff9c754052013-01-09 09:09:08 -050082 "\"%s\")\n", buf);
83#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050084 if (tar_extract_file(t, buf, prefix, progress_fd) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050085 return -1;
86 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050087
bigbiff bigbiff9c754052013-01-09 09:09:08 -050088 return (i == 1 ? 0 : -1);
89}
90
91
92int
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050093tar_append_tree(TAR *t, char *realdir, char *savedir)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050094{
95 char realpath[MAXPATHLEN];
96 char savepath[MAXPATHLEN];
97 struct dirent *dent;
98 DIR *dp;
99 struct stat s;
100
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500101#ifdef DEBUG
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100102 LOG("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500103 t, realdir, (savedir ? savedir : "[NULL]"));
104#endif
105
106 if (tar_append_file(t, realdir, savedir) != 0)
107 return -1;
108
109#ifdef DEBUG
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100110 LOG(" tar_append_tree(): done with tar_append_file()...");
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500111#endif
112
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500113 dp = opendir(realdir);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500114 if (dp == NULL)
115 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500116 if (errno == ENOTDIR)
117 return 0;
118 return -1;
119 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500120 while ((dent = readdir(dp)) != NULL)
121 {
122 if (strcmp(dent->d_name, ".") == 0 ||
123 strcmp(dent->d_name, "..") == 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500124 continue;
125
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500126 snprintf(realpath, MAXPATHLEN, "%s/%s", realdir,
127 dent->d_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500128 if (savedir)
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500129 snprintf(savepath, MAXPATHLEN, "%s/%s", savedir,
130 dent->d_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500131
132 if (lstat(realpath, &s) != 0)
133 return -1;
134
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500135 if (S_ISDIR(s.st_mode))
136 {
137 if (tar_append_tree(t, realpath,
138 (savedir ? savepath : NULL)) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500139 return -1;
140 continue;
141 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500142
143 if (tar_append_file(t, realpath,
144 (savedir ? savepath : NULL)) != 0)
145 return -1;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400146 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500147
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500148 closedir(dp);
149
150 return 0;
151}
n0d33b511632013-03-06 21:14:15 +0200152
153
154int
155tar_find(TAR *t, char *searchstr)
156{
157 if (!searchstr)
158 return 0;
159
160 char *filename;
161 int i, entryfound = 0;
162#ifdef DEBUG
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100163 LOG("==> tar_find(0x%lx, %s)\n", (long unsigned int)t, searchstr);
n0d33b511632013-03-06 21:14:15 +0200164#endif
165 while ((i = th_read(t)) == 0) {
166 filename = th_get_pathname(t);
167 if (fnmatch(searchstr, filename, FNM_FILE_NAME | FNM_PERIOD) == 0) {
168 entryfound++;
169#ifdef DEBUG
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100170 LOG("Found matching entry: %s\n", filename);
n0d33b511632013-03-06 21:14:15 +0200171#endif
172 break;
173 }
174 }
175#ifdef DEBUG
176 if (!entryfound)
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100177 LOG("No matching entry found.\n");
n0d33b511632013-03-06 21:14:15 +0200178#endif
179
180 return entryfound;
181}