summaryrefslogtreecommitdiffstats
path: root/Utilities/cmtar/wrapper.c
blob: b194717f332dbe74738bf816fbf9a5b5147371d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
**  Copyright 1998-2003 University of Illinois Board of Trustees
**  Copyright 1998-2003 Mark D. Roth
**  All rights reserved.
**
**  wrapper.c - libtar high-level wrapper code
**
**  Mark D. Roth <roth@uiuc.edu>
**  Campus Information Technologies and Educational Services
**  University of Illinois at Urbana-Champaign
*/

#include <libtarint/internal.h>

#include <stdio.h>
#include <stdlib.h>

#include <libtar/compat.h>
#if defined(HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#if defined(HAVE_DIRENT_H)
#include <dirent.h>
#else
#include <libtarint/filesystem.h>
#endif
#include <errno.h>

#ifdef HAVE_FNMATCH_H
#include <fnmatch.h>
#endif

#ifdef STDC_HEADERS
# include <string.h>
#endif


int
tar_extract_glob(TAR *t, char *globname, char *prefix)
{
  char *filename;
  char buf[TAR_MAXPATHLEN];
  int i;
  char *pathname;

  while ((i = th_read(t)) == 0)
  {
    pathname = th_get_pathname(t);
    filename = pathname;

    if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
    {
      if (pathname)
        {
        free(pathname);
        }

      if (TH_ISREG(t) && tar_skip_regfile(t))
        return -1;
      continue;
    }

    if (t->options & TAR_VERBOSE)
      th_print_long_ls(t);

    if (prefix != NULL)
      snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
    else
      strlcpy(buf, filename, sizeof(buf));

    if (tar_extract_file(t, filename) != 0)
      {
      if (pathname)
        {
        free(pathname);
        }
      return -1;
      }

    if (pathname)
      {
      free(pathname);
      }
  }

  return (i == 1 ? 0 : -1);
}


int
tar_extract_all(TAR *t, char *prefix)
{
  char *filename;
  char buf[TAR_MAXPATHLEN];
  int i;
  char *pathname;

#ifdef DEBUG
  printf("==> tar_extract_all(TAR *t, \"%s\")\n",
         (prefix ? prefix : "(null)"));
#endif

  while ((i = th_read(t)) == 0)
  {
#ifdef DEBUG
    puts("    tar_extract_all(): calling th_get_pathname()");
#endif

    pathname = th_get_pathname(t);
    filename = pathname;

    if (t->options & TAR_VERBOSE)
      th_print_long_ls(t);
    if (prefix != NULL)
      snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
    else
      strlcpy(buf, filename, sizeof(buf));

    if (pathname)
      {
      free(pathname);
      }

#ifdef DEBUG
    printf("    tar_extract_all(): calling tar_extract_file(t, "
           "\"%s\")\n", buf);
#endif

    if (tar_extract_file(t, buf) != 0)
      return -1;
  }

  return (i == 1 ? 0 : -1);
}


int
tar_append_tree(TAR *t, char *realdir, char *savedir)
{
  char realpath[TAR_MAXPATHLEN];
  char savepath[TAR_MAXPATHLEN];
  size_t plen;
#if defined(HAVE_DIRENT_H)
  struct dirent *dent;
  DIR *dp;
#else  
  kwDirEntry * dent;
  kwDirectory *dp;
#endif  
  struct stat s;
  strncpy(realpath, realdir, sizeof(realpath));
  realpath[sizeof(realpath)-1] = 0;
  plen = strlen(realpath);
  if ( realpath[plen-1] == '/' )
    {
    realpath[plen-1] = 0;
    }
  

#ifdef DEBUG
  printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
         t, realdir, (savedir ? savedir : "[NULL]"));
#endif

  if (tar_append_file(t, realdir, savedir) != 0)
    return -1;

#ifdef DEBUG
  puts("    tar_append_tree(): done with tar_append_file()...");
#endif

  if ( stat(realpath, &s) != 0 )
    {
    return -1;   
    }
  if ( 
#if defined(_WIN32) && !defined(__CYGWIN__)
    (s.st_mode & _S_IFDIR) == 0
#else
    !S_ISDIR(s.st_mode)
#endif
  )
    return 0;
#if defined(HAVE_DIRENT_H)
  dp = opendir(realdir);
#else
  dp = kwOpenDir(realdir);
#endif

  if (dp == NULL)
  {
    if (errno == ENOTDIR)
      return 0;
    return -1;
  }
#if defined(HAVE_DIRENT_H)
  while ((dent = readdir(dp)) != NULL)
#else
  while ((dent = kwReadDir(dp)) != NULL)
#endif
  {
    if (strcmp(dent->d_name, ".") == 0 ||
        strcmp(dent->d_name, "..") == 0)
      continue;

    snprintf(realpath, TAR_MAXPATHLEN, "%s/%s", realdir,
       dent->d_name);
    if (savedir)
      snprintf(savepath, TAR_MAXPATHLEN, "%s/%s", savedir,
         dent->d_name);

#ifndef WIN32
    if (lstat(realpath, &s) != 0)
      return -1;
#else
    if (stat(realpath, &s) != 0)
      return -1;
#endif
    if (S_ISDIR(s.st_mode))
    {
      if (tar_append_tree(t, realpath,
              (savedir ? savepath : NULL)) != 0)
        return -1;
      continue;
    }

    if (tar_append_file(t, realpath,
            (savedir ? savepath : NULL)) != 0)
      return -1;
  }

#if defined(HAVE_DIRENT_H)
  closedir(dp);
#else
  kwCloseDir(dp);
#endif

  return 0;
}