#include <sys/cdefs.h>
#ifdef __FBSDID
__FBSDID("$FreeBSD$");
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <archive.h>
#include <archive_entry.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "tree.h"
static int b_opt;
static int r_opt;
static char *o_arg;
static void
usage(void)
{
fprintf(stderr, "Usage: shar [-br] [-o filename] file ...\n");
exit(EX_USAGE);
}
static struct archive *
shar_create(void)
{
struct archive *a;
if ((a = archive_write_new()) == NULL)
errx(EXIT_FAILURE, "%s", archive_error_string(a));
if (b_opt)
archive_write_set_format_shar_dump(a);
else
archive_write_set_format_shar(a);
archive_write_set_compression_none(a);
if (archive_write_open_filename(a, o_arg) != ARCHIVE_OK)
errx(EX_CANTCREAT, "%s", archive_error_string(a));
return (a);
}
static char buffer[32768];
static int
shar_write_entry_data(struct archive *a, const int fd)
{
ssize_t bytes_read, bytes_written;
assert(a != NULL);
assert(fd >= 0);
bytes_read = read(fd, buffer, sizeof(buffer));
while (bytes_read != 0) {
if (bytes_read < 0) {
archive_set_error(a, errno, "Read failed");
return (ARCHIVE_WARN);
}
bytes_written = archive_write_data(a, buffer, bytes_read);
if (bytes_written < 0)
return (ARCHIVE_WARN);
bytes_read = read(fd, buffer, sizeof(buffer));
}
return (ARCHIVE_OK);
}
static int
shar_write_entry(struct archive *a, const char *pathname, const char *accpath,
const struct stat *st)
{
struct archive_entry *entry;
int fd = -1;
int ret = ARCHIVE_OK;
assert(a != NULL);
assert(pathname != NULL);
assert(accpath != NULL);
assert(st != NULL);
entry = archive_entry_new();
if (S_ISREG(st->st_mode) && st->st_size > 0) {
if ((fd = open(accpath, O_RDONLY)) == -1) {
warn("%s", accpath);
ret = ARCHIVE_WARN;
goto out;
}
} else if (S_ISLNK(st->st_mode)) {
char lnkbuff[PATH_MAX + 1];
int lnklen;
if ((lnklen = readlink(accpath, lnkbuff, PATH_MAX)) == -1) {
warn("%s", accpath);
ret = ARCHIVE_WARN;
goto out;
}
lnkbuff[lnklen] = '\0';
archive_entry_set_symlink(entry, lnkbuff);
}
archive_entry_copy_stat(entry, st);
archive_entry_set_pathname(entry, pathname);
if (!S_ISREG(st->st_mode) || st->st_size == 0)
archive_entry_set_size(entry, 0);
if (archive_write_header(a, entry) != ARCHIVE_OK) {
warnx("%s: %s", pathname, archive_error_string(a));
ret = ARCHIVE_WARN;
goto out;
}
if (fd >= 0) {
if ((ret = shar_write_entry_data(a, fd)) != ARCHIVE_OK)
warnx("%s: %s", accpath, archive_error_string(a));
}
out:
archive_entry_free(entry);
if (fd >= 0)
close(fd);
return (ret);
}
static int
shar_write_path(struct archive *a, const char *pathname)
{
struct stat st;
assert(a != NULL);
assert(pathname != NULL);
if ((stat(pathname, &st)) == -1) {
warn("%s", pathname);
return (ARCHIVE_WARN);
}
return (shar_write_entry(a, pathname, pathname, &st));
}
static int
shar_write_tree(struct archive *a, const char *pathname)
{
struct tree *t;
const struct stat *lst, *st;
int error = 0;
int tree_ret;
int first;
assert(a != NULL);
assert(pathname != NULL);
t = tree_open(pathname);
for (first = 1; (tree_ret = tree_next(t)); first = 0) {
if (tree_ret == TREE_ERROR_DIR) {
warnx("%s: %s", tree_current_path(t),
strerror(tree_errno(t)));
error = 1;
continue;
} else if (tree_ret != TREE_REGULAR)
continue;
if ((lst = tree_current_lstat(t)) == NULL) {
warn("%s", tree_current_path(t));
error = 1;
continue;
}
if (first && S_ISLNK(lst->st_mode)) {
if ((st = tree_current_stat(t)) == NULL) {
warn("%s", tree_current_path(t));
error = 1;
continue;
}
} else
st = lst;
if (shar_write_entry(a, tree_current_path(t),
tree_current_access_path(t), st) != ARCHIVE_OK)
error = 1;
tree_descend(t);
}
tree_close(t);
return ((error != 0) ? ARCHIVE_WARN : ARCHIVE_OK);
}
static int
shar_write(char **fn, size_t nfn)
{
struct archive *a;
size_t i;
int error = 0;
assert(fn != NULL);
assert(nfn > 0);
a = shar_create();
for (i = 0; i < nfn; i++) {
if (r_opt) {
if (shar_write_tree(a, fn[i]) != ARCHIVE_OK)
error = 1;
} else {
if (shar_write_path(a, fn[i]) != ARCHIVE_OK)
error = 1;
}
}
if (archive_write_finish(a) != ARCHIVE_OK)
errx(EXIT_FAILURE, "%s", archive_error_string(a));
if (error != 0)
warnx("Error exit delayed from previous errors.");
return (error);
}
int
main(int argc, char **argv)
{
int opt;
while ((opt = getopt(argc, argv, "bro:")) != -1) {
switch (opt) {
case 'b':
b_opt = 1;
break;
case 'o':
o_arg = optarg;
break;
case 'r':
r_opt = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if(argc < 1)
usage();
if (shar_write(argv, argc) != 0)
exit(EXIT_FAILURE);
else
exit(EXIT_SUCCESS);
}