aboutsummaryrefslogtreecommitdiffstats
path: root/archivers/unmakeself/files/unmakeself.c
blob: bc275d19b6e232bf50f1ce6baf1614b83a0fe6dc (plain) (blame)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * unmakeself - extracts Makeself archives
 *
 * Copyright (C) 2005, 2006 Jean-Yves Lefort
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of Jean-Yves Lefort nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <getopt.h>
#include <archive.h>
#include "config.h"

#define N_ELEMENTS(arr)     (sizeof(arr) / sizeof((arr)[0]))

static char *self;                  /* program name */
static char *filename;
static struct archive *archive;
static int extract_flags = ARCHIVE_EXTRACT_TIME;    /* bsdtar default */

#ifndef HAS_MEMMEM
/* taken from lib/libc/string/memmem.c */
static void *
memmem(l, l_len, s, s_len)
    const void *l; size_t l_len;
    const void *s; size_t s_len;
{
    register char *cur, *last;
    const char *cl = (const char *)l;
    const char *cs = (const char *)s;

    /* we need something to compare */
    if (l_len == 0 || s_len == 0)
        return NULL;

    /* "s" must be smaller or equal to "l" */
    if (l_len < s_len)
        return NULL;

    /* special case where s_len == 1 */
    if (s_len == 1)
        return memchr(l, (int)*cs, l_len);

    /* the last position where its possible to find "s" in "l" */
    last = (char *)cl + l_len - s_len;

    for (cur = (char *)cl; cur <= last; cur++)
        if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
            return cur;

    return NULL;
}
#endif /* HAS_MEMMEM */

static void
unmakeself_print_help (void)
{
  printf("Synopsis:\n");
  printf("  %s [OPTIONS] FILE\n", self);
  printf("\n");
  printf("Options:\n");
  printf("  -?, --help                  Show this help\n");
  printf("  -v, --version               Show version information\n");
  printf("  -k, --keep-old-files        Do not overwrite existing files\n");
  printf("  -m, --modification-time     Do not extract modification time\n");
  printf("  -o, --no-same-owner         Do not extract ownership\n");
  printf("  -p, --preserve-permissions  Preserve file permissions\n");
  printf("  -U, --unlink-first          Unlink files before creating them\n");
  printf("      --print-offset          Only print the offset, do not extract\n");
}

static void
unmakeself_print_version (void)
{
  printf("unmakeself version 1.1\n");
  printf("Copyright (C) 2005, 2006 Jean-Yves Lefort\n");
}

static void
unmakeself_error (const char *func, const char *error)
{
  if (func)
    fprintf(stderr, "%s %s: %s() error: %s\n", self, filename, func, error);
  else
    fprintf(stderr, "%s %s: error: %s\n", self, filename, error);
  exit(1);
}

static void
unmakeself_handle_libc_result (int result, const char *func)
{
  if (result < 0)
    unmakeself_error(func, strerror(errno));
}

static void
unmakeself_handle_libarchive_result_full (int ok, const char *func)
{
  if (! ok)
    unmakeself_error(func, archive_error_string(archive));
}

static void
unmakeself_handle_libarchive_result (int result, const char *func)
{
  unmakeself_handle_libarchive_result_full(result == ARCHIVE_OK, func);
}

static ssize_t
unmakeself_read (int fd, void *buf, size_t nbytes)
{
  ssize_t result;

  result = read(fd, buf, nbytes);
  unmakeself_handle_libc_result(result, "read");

  return result;
}

static off_t
unmakeself_lseek (int fd, off_t offset, int whence)
{
  off_t result;

  result = lseek(fd, offset, whence);
  unmakeself_handle_libc_result(result, "lseek");

  return result;
}

static off_t
unmakeself_get_offset (int fd)
{
  static const char *magic_numbers[] = {
    "\n\037\213",       /* newline + gzip */
    "\nBZh"         /* newline + bzip2 */
  };
  char buf[4096];
  ssize_t len;
  off_t pos = 0;
  off_t last_newline = -1;
  int i;

  /*
   * First try to find a gzip or bzip2 magic number following a newline.
   */

  while ((len = unmakeself_read(fd, buf, sizeof(buf))) > 0)
    {
      for (i = 0; i < N_ELEMENTS(magic_numbers); i++)
    {
      char *start;

      start = memmem(buf, len, magic_numbers[i], strlen(magic_numbers[i]));
      if (start)
        return pos + (start - buf) + 1;
    }
      pos += len;
    }

  /*
   * Then fallback to locating the first non-printable character
   * following a newline. The archive should start immediately after
   * that newline.
   */

  unmakeself_lseek(fd, 0, SEEK_SET);
  pos = 0;

  while ((len = unmakeself_read(fd, buf, sizeof(buf))) > 0)
    for (i = 0; i < len; i++, pos++)
      if (buf[i] == '\n')
    last_newline = pos;
      else if (buf[i] != '\t' && (buf[i] < 32 || buf[i] > 126))
    {
      if (last_newline != -1)
        return last_newline + 1;
    }

  return -1;            /* not found */
}

static void
unmakeself_extract (int print_offset)
{
  int fd;
  off_t offset;

  fd = open(filename, O_RDONLY);
  unmakeself_handle_libc_result(fd, "open");

  offset = unmakeself_get_offset(fd);
  if (offset != -1)     /* offset found */
    {
      if (print_offset)     /* only print the offset */
    printf("%ji\n", offset);
      else          /* extract using libarchive */
    {
      struct archive_entry *entry;
      int status;

      unmakeself_lseek(fd, offset, SEEK_SET);

      archive = archive_read_new();

      if (archive_read_support_compression_all(archive) != ARCHIVE_OK)
        fprintf(stderr, "%s: warning: unable to support all compression formats: %s\n", self, archive_error_string(archive));
      if (archive_read_support_format_all(archive) != ARCHIVE_OK)
        fprintf(stderr, "%s: warning: unable to support all archive formats: %s\n", self, archive_error_string(archive));

      /* same block size as DEFAULT_BYTES_PER_BLOCK in bsdtar.h */
      unmakeself_handle_libarchive_result(archive_read_open_fd(archive, fd, 20 * 512), "archive_read_open_fd");

      while ((status = archive_read_next_header(archive, &entry)) == ARCHIVE_OK)
        unmakeself_handle_libarchive_result(archive_read_extract(archive, entry, extract_flags), "archive_read_extract");

      unmakeself_handle_libarchive_result_full(status == ARCHIVE_EOF, "archive_read_next_header");

      unmakeself_handle_libarchive_result(archive_read_close(archive), "archive_read_close");

      archive_read_finish(archive);
    }
    }
  else              /* offset not found */
    unmakeself_error(NULL, "invalid Makeself archive");

  unmakeself_handle_libc_result(close(fd), "close");
}

int
main (int argc, char **argv)
{
  int print_offset = 0;
  const struct option options[] = {
    { "help",           no_argument,    NULL,       '?' },
    { "version",        no_argument,    NULL,       'v' },
    { "keep-old-files",     no_argument,    NULL,       'k' },
    { "modification-time",  no_argument,    NULL,       'm' },
    { "no-same-owner",      no_argument,    NULL,       'o' },
    { "preserve-permissions",   no_argument,    NULL,       'p' },
    { "unlink",         no_argument,    NULL,       'U' },
    { "unlink-first",       no_argument,    NULL,       'U' },
    { "print-offset",       no_argument,    &print_offset,  1   },
    { NULL,         0,      NULL,       0   }
  };
  int c;

  self = argc > 0 ? strdup(argv[0]) : "unmakeself";
  setlocale(LC_ALL, "");

  if (geteuid() == 0)       /* bsdtar does this */
    extract_flags |= ARCHIVE_EXTRACT_OWNER;

  while ((c = getopt_long(argc, argv, "?vkmpUx", options, NULL)) != -1)
    switch (c)
      {
      case '?':
    unmakeself_print_help();
    exit(0);
    break;

      case 'v':
    unmakeself_print_version();
    exit(0);
    break;

      case 'k':         /* GNU tar, bsdtar */
    extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
    break;

      case 'm':         /* SUSv2, bsdtar */
    extract_flags &= ~ARCHIVE_EXTRACT_TIME;
    break;

      case 'o':         /* bsdtar */
    extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
    break;

      case 'p':         /* GNU tar, star, bsdtar */
    extract_flags |= ARCHIVE_EXTRACT_PERM;
    extract_flags |= ARCHIVE_EXTRACT_ACL;
    extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
    break;

      case 'U':         /* GNU tar, bsdtar */
    extract_flags |= ARCHIVE_EXTRACT_UNLINK;
    break;

      case 0:
    break;

      default:
    abort();
      }

  argc -= optind;
  argv += optind;

  if (argc != 1)
    {
      fprintf(stderr, "%s: a file to extract must be specified\n", self);
      exit(1);
    }

  filename = argv[0];
  unmakeself_extract(print_offset);

  /* on some systems, the return value of main() is ignored */
  exit(0);

  return 0;
}