Release version 1.0:
- Document options in --help output - Add --print-offset option
This commit is contained in:
parent
5b95e58dfa
commit
c5e73221bc
Notes:
svn2git
2021-03-31 03:12:20 +00:00
svn path=/head/; revision=137333
2 changed files with 152 additions and 138 deletions
|
@ -6,7 +6,7 @@
|
|||
#
|
||||
|
||||
PORTNAME= unmakeself
|
||||
PORTVERSION= 0.99
|
||||
PORTVERSION= 1.0
|
||||
CATEGORIES= archivers
|
||||
DISTFILES=
|
||||
|
||||
|
@ -32,7 +32,6 @@ do-build:
|
|||
${FILESDIR}/unmakeself.c ${LDFLAGS}
|
||||
|
||||
do-install:
|
||||
${MKDIR} ${PREFIX}/bin
|
||||
${INSTALL_PROGRAM} ${WRKSRC}/unmakeself ${PREFIX}/bin
|
||||
|
||||
.include <bsd.port.post.mk>
|
||||
|
|
|
@ -46,23 +46,158 @@
|
|||
static char *self = NULL; /* program name */
|
||||
static int extract_flags = ARCHIVE_EXTRACT_TIME; /* bsdtar default */
|
||||
|
||||
static void unmakeself_print_help (void);
|
||||
static void unmakeself_print_version (void);
|
||||
static void unmakeself_extract (const char *filename);
|
||||
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.0\n");
|
||||
printf("Copyright (C) 2005 Jean-Yves Lefort\n");
|
||||
}
|
||||
|
||||
static void
|
||||
unmakeself_extract (const char *filename, int print_offset)
|
||||
{
|
||||
int fd;
|
||||
char buf[4096];
|
||||
int pos = 0;
|
||||
int last_newline = -1;
|
||||
int offset = -1;
|
||||
int done = 0;
|
||||
ssize_t len;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to open %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* We find the start offset of the archive inside the shell script
|
||||
* by locating the first non-printable character following a
|
||||
* newline. The archive starts immediately after that newline.
|
||||
*/
|
||||
while (! done && (len = read(fd, buf, sizeof(buf))) > 0)
|
||||
{
|
||||
int i;
|
||||
|
||||
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)
|
||||
offset = last_newline + 1;
|
||||
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (offset != -1) /* offset found */
|
||||
{
|
||||
if (print_offset) /* only print the offset */
|
||||
printf("%i\n", offset);
|
||||
else /* extract using libarchive */
|
||||
{
|
||||
struct archive *archive;
|
||||
struct archive_entry *entry;
|
||||
int status;
|
||||
|
||||
if (lseek(fd, offset, SEEK_SET) < 0)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to seek into %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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 */
|
||||
if (archive_read_open_fd(archive, fd, 20 * 512) != ARCHIVE_OK)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to open %s with libarchive: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while ((status = archive_read_next_header(archive, &entry)) == ARCHIVE_OK)
|
||||
if (archive_read_extract(archive, entry, extract_flags) != ARCHIVE_OK)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to extract %s: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (status != ARCHIVE_EOF)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to read next header of %s: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (archive_read_close(archive) != ARCHIVE_OK)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to close %s with libarchive: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
archive_read_finish(archive);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (len >= 0) /* EOF, or couldn't find start offset */
|
||||
{
|
||||
fprintf(stderr, "%s: %s is not a valid Makeself archive\n", self, filename);
|
||||
exit(1);
|
||||
}
|
||||
else /* read error */
|
||||
{
|
||||
fprintf(stderr, "%s: unable to read %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (close(fd) < 0)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to close %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
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' },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
{ "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;
|
||||
|
||||
|
@ -107,6 +242,9 @@ main (int argc, char **argv)
|
|||
extract_flags |= ARCHIVE_EXTRACT_UNLINK;
|
||||
break;
|
||||
|
||||
case 0:
|
||||
break;
|
||||
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
|
@ -120,133 +258,10 @@ main (int argc, char **argv)
|
|||
exit(1);
|
||||
}
|
||||
|
||||
unmakeself_extract(argv[0]);
|
||||
unmakeself_extract(argv[0], print_offset);
|
||||
|
||||
/* on some systems, the return value of main() is ignored */
|
||||
exit(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
unmakeself_print_help (void)
|
||||
{
|
||||
printf("Synopsis:\n");
|
||||
printf(" %s {-? | -v}\n", self);
|
||||
printf(" %s [-kmopU] FILE\n", self);
|
||||
}
|
||||
|
||||
static void
|
||||
unmakeself_print_version (void)
|
||||
{
|
||||
printf("unmakeself version 0.99\n");
|
||||
printf("Copyright (C) 2005 Jean-Yves Lefort\n");
|
||||
}
|
||||
|
||||
static void
|
||||
unmakeself_extract (const char *filename)
|
||||
{
|
||||
int fd;
|
||||
char buf[4096];
|
||||
int pos = 0;
|
||||
int last_newline = -1;
|
||||
int offset = -1;
|
||||
int done = 0;
|
||||
ssize_t len;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to open %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* We find the start offset of the archive inside the shell script
|
||||
* by locating the first non-printable character following a
|
||||
* newline. The archive starts immediately after that newline.
|
||||
*/
|
||||
while (! done && (len = read(fd, buf, sizeof(buf))) > 0)
|
||||
{
|
||||
int i;
|
||||
|
||||
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)
|
||||
offset = last_newline + 1;
|
||||
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (offset != -1) /* offset found, extract using libarchive */
|
||||
{
|
||||
struct archive *archive;
|
||||
struct archive_entry *entry;
|
||||
int status;
|
||||
|
||||
if (lseek(fd, offset, SEEK_SET) < 0)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to seek into %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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 */
|
||||
if (archive_read_open_fd(archive, fd, 20 * 512) != ARCHIVE_OK)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to open %s with libarchive: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while ((status = archive_read_next_header(archive, &entry)) == ARCHIVE_OK)
|
||||
if (archive_read_extract(archive, entry, extract_flags) != ARCHIVE_OK)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to extract %s: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (status != ARCHIVE_EOF)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to read next header of %s: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (archive_read_close(archive) != ARCHIVE_OK)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to close %s with libarchive: %s\n", self, filename, archive_error_string(archive));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
archive_read_finish(archive);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (len >= 0) /* EOF, or couldn't find start offset */
|
||||
{
|
||||
fprintf(stderr, "%s: %s is not a valid Makeself archive\n", self, filename);
|
||||
exit(1);
|
||||
}
|
||||
else /* read error */
|
||||
{
|
||||
fprintf(stderr, "%s: unable to read %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (close(fd) < 0)
|
||||
{
|
||||
fprintf(stderr, "%s: unable to close %s: %s\n", self, filename, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue