2013-07-18 15:15:35 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-01-26 15:07:42 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-07-18 15:15:35 +00:00
|
|
|
#include <string.h>
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-07-18 15:15:35 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-16 18:47:36 +00:00
|
|
|
struct header {
|
2013-07-18 15:15:35 +00:00
|
|
|
char name[100];
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
|
|
|
char size[12];
|
|
|
|
char mtime[12];
|
|
|
|
char chksum[8];
|
|
|
|
char type;
|
|
|
|
char link[100];
|
2015-02-16 19:01:33 +00:00
|
|
|
char magic[6];
|
|
|
|
char version[2];
|
2013-07-18 15:15:35 +00:00
|
|
|
char uname[32];
|
|
|
|
char gname[32];
|
|
|
|
char major[8];
|
|
|
|
char minor[8];
|
2014-11-01 20:36:40 +00:00
|
|
|
char prefix[155];
|
2013-07-18 15:15:35 +00:00
|
|
|
};
|
|
|
|
|
2015-02-16 18:47:36 +00:00
|
|
|
#define BLKSIZ 512
|
2013-07-18 15:15:35 +00:00
|
|
|
|
|
|
|
enum Type {
|
2014-11-01 20:36:38 +00:00
|
|
|
REG = '0', AREG = '\0', HARDLINK = '1', SYMLINK = '2', CHARDEV = '3',
|
2013-07-19 16:05:28 +00:00
|
|
|
BLOCKDEV = '4', DIRECTORY = '5', FIFO = '6'
|
2013-07-18 15:15:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static FILE *tarfile;
|
2013-07-20 16:08:58 +00:00
|
|
|
static ino_t tarinode;
|
2013-07-28 16:12:03 +00:00
|
|
|
static dev_t tardev;
|
2013-07-18 15:15:35 +00:00
|
|
|
|
2015-02-16 18:47:36 +00:00
|
|
|
static int mflag;
|
2015-01-26 15:07:42 +00:00
|
|
|
static char filtermode;
|
2013-07-18 15:15:35 +00:00
|
|
|
|
2015-01-26 15:07:42 +00:00
|
|
|
static FILE *
|
|
|
|
decomp(FILE *fp)
|
2013-07-18 15:15:35 +00:00
|
|
|
{
|
2015-02-16 18:47:36 +00:00
|
|
|
int fds[2];
|
2015-01-26 15:07:42 +00:00
|
|
|
|
|
|
|
if (pipe(fds) < 0)
|
|
|
|
eprintf("pipe:");
|
|
|
|
|
2015-03-09 14:01:29 +00:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
2015-03-10 19:05:18 +00:00
|
|
|
eprintf("fork:");
|
2015-03-09 14:01:29 +00:00
|
|
|
case 0:
|
2015-01-26 15:07:42 +00:00
|
|
|
dup2(fileno(fp), 0);
|
|
|
|
dup2(fds[1], 1);
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
|
|
|
|
switch (filtermode) {
|
|
|
|
case 'j':
|
2015-03-09 00:04:34 +00:00
|
|
|
execlp("bzip2", "bzip2", "-cd", NULL);
|
|
|
|
weprintf("execlp bzip2:");
|
|
|
|
_exit(1);
|
2015-01-26 15:07:42 +00:00
|
|
|
case 'z':
|
2015-03-09 00:04:34 +00:00
|
|
|
execlp("gzip", "gzip", "-cd", NULL);
|
|
|
|
weprintf("execlp gzip:");
|
|
|
|
_exit(1);
|
2013-07-18 15:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-26 15:07:42 +00:00
|
|
|
close(fds[1]);
|
|
|
|
return fdopen(fds[0], "r");
|
2013-07-18 15:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2013-07-18 15:15:35 +00:00
|
|
|
putoctal(char *dst, unsigned num, int n)
|
|
|
|
{
|
2015-02-16 18:47:36 +00:00
|
|
|
snprintf(dst, n, "%.*o", n - 1, num);
|
2013-07-18 15:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static int
|
2014-01-30 14:12:12 +00:00
|
|
|
archive(const char* path)
|
2013-07-18 15:15:35 +00:00
|
|
|
{
|
2015-03-03 10:26:59 +00:00
|
|
|
FILE *f = NULL;
|
2013-07-18 15:15:35 +00:00
|
|
|
mode_t mode;
|
2015-02-16 18:47:36 +00:00
|
|
|
struct group *gr;
|
|
|
|
struct header *h;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct stat st;
|
2015-03-03 10:26:59 +00:00
|
|
|
size_t chksum, x;
|
|
|
|
ssize_t l;
|
2015-02-16 18:47:36 +00:00
|
|
|
unsigned char b[BLKSIZ];
|
2013-07-18 15:15:35 +00:00
|
|
|
|
|
|
|
lstat(path, &st);
|
2013-07-28 16:12:03 +00:00
|
|
|
if (st.st_ino == tarinode && st.st_dev == tardev) {
|
2013-07-20 16:08:58 +00:00
|
|
|
fprintf(stderr, "ignoring '%s'\n", path);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-18 15:15:35 +00:00
|
|
|
pw = getpwuid(st.st_uid);
|
|
|
|
gr = getgrgid(st.st_gid);
|
|
|
|
|
2015-02-16 18:47:36 +00:00
|
|
|
h = (void*)b;
|
|
|
|
memset(b, 0, sizeof(b));
|
|
|
|
snprintf(h->name, sizeof(h->name), "%s", path);
|
|
|
|
putoctal(h->mode, (unsigned)st.st_mode & 0777, sizeof(h->mode));
|
|
|
|
putoctal(h->uid, (unsigned)st.st_uid, sizeof(h->uid));
|
|
|
|
putoctal(h->gid, (unsigned)st.st_gid, sizeof(h->gid));
|
|
|
|
putoctal(h->size, 0, sizeof(h->size));
|
|
|
|
putoctal(h->mtime, (unsigned)st.st_mtime, sizeof(h->mtime));
|
2015-02-16 19:01:33 +00:00
|
|
|
memcpy(h->magic, "ustar", sizeof(h->magic));
|
|
|
|
memcpy(h->version, "00", sizeof(h->version));
|
2014-11-01 20:36:37 +00:00
|
|
|
snprintf(h->uname, sizeof h->uname, "%s", pw ? pw->pw_name : "");
|
|
|
|
snprintf(h->gname, sizeof h->gname, "%s", gr ? gr->gr_name : "");
|
2013-07-18 15:15:35 +00:00
|
|
|
|
|
|
|
mode = st.st_mode;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (S_ISREG(mode)) {
|
2013-07-18 15:15:35 +00:00
|
|
|
h->type = REG;
|
|
|
|
putoctal(h->size, (unsigned)st.st_size, sizeof h->size);
|
|
|
|
f = fopen(path, "r");
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (S_ISDIR(mode)) {
|
2013-07-18 15:15:35 +00:00
|
|
|
h->type = DIRECTORY;
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (S_ISLNK(mode)) {
|
2013-07-18 15:15:35 +00:00
|
|
|
h->type = SYMLINK;
|
|
|
|
readlink(path, h->link, (sizeof h->link)-1);
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (S_ISCHR(mode) || S_ISBLK(mode)) {
|
2013-07-18 15:15:35 +00:00
|
|
|
h->type = S_ISCHR(mode) ? CHARDEV : BLOCKDEV;
|
2014-01-30 16:14:33 +00:00
|
|
|
#if defined(major) && defined(minor)
|
2013-07-18 15:15:35 +00:00
|
|
|
putoctal(h->major, (unsigned)major(st.st_dev), sizeof h->major);
|
|
|
|
putoctal(h->minor, (unsigned)minor(st.st_dev), sizeof h->minor);
|
2014-01-30 16:14:33 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (S_ISFIFO(mode)) {
|
2013-07-18 15:15:35 +00:00
|
|
|
h->type = FIFO;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(h->chksum, ' ', sizeof h->chksum);
|
2014-11-13 17:29:30 +00:00
|
|
|
for (x = 0, chksum = 0; x < sizeof *h; x++)
|
2013-07-18 15:15:35 +00:00
|
|
|
chksum += b[x];
|
|
|
|
putoctal(h->chksum, chksum, sizeof h->chksum);
|
|
|
|
|
2015-02-16 18:47:36 +00:00
|
|
|
fwrite(b, BLKSIZ, 1, tarfile);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!f)
|
2013-07-18 15:15:35 +00:00
|
|
|
return 0;
|
2015-02-16 18:47:36 +00:00
|
|
|
while ((l = fread(b, 1, BLKSIZ, f)) > 0) {
|
|
|
|
if (l < BLKSIZ)
|
|
|
|
memset(b+l, 0, BLKSIZ-l);
|
|
|
|
fwrite(b, BLKSIZ, 1, tarfile);
|
2013-07-18 15:15:35 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
2013-07-19 16:05:28 +00:00
|
|
|
return 0;
|
2013-07-18 15:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static int
|
2015-02-16 18:47:36 +00:00
|
|
|
unarchive(char *fname, int l, char b[BLKSIZ])
|
2013-07-18 15:15:35 +00:00
|
|
|
{
|
|
|
|
FILE *f = NULL;
|
2014-11-03 10:20:09 +00:00
|
|
|
struct timeval times[2];
|
2015-02-16 18:47:36 +00:00
|
|
|
struct header *h = (void*)b;
|
|
|
|
unsigned long mode, major, minor, type, mtime;
|
|
|
|
char lname[101];
|
2013-07-18 15:15:35 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!mflag)
|
2014-11-01 20:36:39 +00:00
|
|
|
mtime = strtoul(h->mtime, 0, 8);
|
2013-07-18 15:15:35 +00:00
|
|
|
unlink(fname);
|
2014-11-13 17:29:30 +00:00
|
|
|
switch (h->type) {
|
2013-07-18 15:15:35 +00:00
|
|
|
case REG:
|
2014-11-01 20:36:38 +00:00
|
|
|
case AREG:
|
2013-07-18 15:15:35 +00:00
|
|
|
mode = strtoul(h->mode, 0, 8);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!(f = fopen(fname, "w")) || chmod(fname, mode))
|
2013-07-18 15:15:35 +00:00
|
|
|
perror(fname);
|
|
|
|
break;
|
|
|
|
case HARDLINK:
|
|
|
|
case SYMLINK:
|
2013-07-20 05:27:42 +00:00
|
|
|
snprintf(lname, sizeof lname, "%s", h->link);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!((h->type == HARDLINK) ? link : symlink)(lname, fname))
|
2013-07-18 15:15:35 +00:00
|
|
|
perror(fname);
|
|
|
|
break;
|
|
|
|
case DIRECTORY:
|
|
|
|
mode = strtoul(h->mode, 0, 8);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (mkdir(fname, (mode_t)mode))
|
2013-07-18 15:15:35 +00:00
|
|
|
perror(fname);
|
|
|
|
break;
|
|
|
|
case CHARDEV:
|
|
|
|
case BLOCKDEV:
|
2014-01-28 16:54:41 +00:00
|
|
|
#ifdef makedev
|
2013-07-18 15:15:35 +00:00
|
|
|
mode = strtoul(h->mode, 0, 8);
|
|
|
|
major = strtoul(h->major, 0, 8);
|
|
|
|
minor = strtoul(h->mode, 0, 8);
|
|
|
|
type = (h->type == CHARDEV) ? S_IFCHR : S_IFBLK;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (mknod(fname, type | mode, makedev(major, minor)))
|
2013-07-18 15:15:35 +00:00
|
|
|
perror(fname);
|
2014-01-28 16:54:41 +00:00
|
|
|
#endif
|
2013-07-18 15:15:35 +00:00
|
|
|
break;
|
|
|
|
case FIFO:
|
|
|
|
mode = strtoul(h->mode, 0, 8);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (mknod(fname, S_IFIFO | mode, 0))
|
2013-07-18 15:15:35 +00:00
|
|
|
perror(fname);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "usupported tarfiletype %c\n", h->type);
|
|
|
|
}
|
2015-02-16 18:47:36 +00:00
|
|
|
if (getuid() == 0 && chown(fname, strtoul(h->uid, 0, 8), strtoul(h->gid, 0, 8)))
|
2013-07-18 15:15:35 +00:00
|
|
|
perror(fname);
|
|
|
|
|
2015-02-16 18:47:36 +00:00
|
|
|
for (; l > 0; l -= BLKSIZ) {
|
|
|
|
fread(b, BLKSIZ, 1, tarfile);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (f)
|
2013-07-18 15:15:35 +00:00
|
|
|
fwrite(b, MIN(l, 512), 1, f);
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (f)
|
2013-07-18 15:15:35 +00:00
|
|
|
fclose(f);
|
2014-11-01 20:36:39 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!mflag) {
|
2014-11-01 20:36:39 +00:00
|
|
|
times[0].tv_sec = times[1].tv_sec = mtime;
|
2014-11-03 10:20:09 +00:00
|
|
|
times[0].tv_usec = times[1].tv_usec = 0;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (utimes(fname, times))
|
2014-11-01 20:36:39 +00:00
|
|
|
perror(fname);
|
|
|
|
}
|
2013-07-18 15:15:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static int
|
2015-02-16 18:47:36 +00:00
|
|
|
print(char * fname, int l, char b[BLKSIZ])
|
2013-07-18 15:15:35 +00:00
|
|
|
{
|
|
|
|
puts(fname);
|
2015-02-16 18:47:36 +00:00
|
|
|
for (; l > 0; l -= BLKSIZ)
|
|
|
|
fread(b, BLKSIZ, 1, tarfile);
|
2013-07-18 15:15:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2015-03-02 20:43:56 +00:00
|
|
|
c(const char *path, int depth)
|
2013-07-18 15:15:35 +00:00
|
|
|
{
|
2014-01-30 14:12:12 +00:00
|
|
|
archive(path);
|
2015-03-02 20:43:56 +00:00
|
|
|
recurse(path, c, depth);
|
2013-07-18 15:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2015-03-02 20:43:56 +00:00
|
|
|
xt(int (*fn)(char *, int, char[BLKSIZ]))
|
2013-07-18 15:15:35 +00:00
|
|
|
{
|
2015-02-16 18:47:36 +00:00
|
|
|
char b[BLKSIZ], fname[257], *s;
|
|
|
|
struct header *h = (void*)b;
|
2013-07-18 15:15:35 +00:00
|
|
|
|
2015-02-16 18:47:36 +00:00
|
|
|
while (fread(b, BLKSIZ, 1, tarfile) && h->name[0] != '\0') {
|
2014-11-01 20:36:40 +00:00
|
|
|
s = fname;
|
|
|
|
if (h->prefix[0] != '\0')
|
|
|
|
s += sprintf(s, "%.*s/", (int)sizeof h->prefix, h->prefix);
|
|
|
|
sprintf(s, "%.*s", (int)sizeof h->name, h->name);
|
2013-07-18 15:15:35 +00:00
|
|
|
fn(fname, strtol(h->size, 0, 8), b);
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 15:07:42 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-26 16:13:01 +00:00
|
|
|
eprintf("usage: tar [-f tarfile] [-C dir] -j|z -x[m]|t\n"
|
2015-01-26 16:14:45 +00:00
|
|
|
" tar [-f tarfile] [-C dir] -c dir\n");
|
2015-01-26 15:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-02-16 18:47:36 +00:00
|
|
|
struct stat st;
|
|
|
|
char *file = NULL, *dir = ".", mode = '\0';
|
2015-01-26 15:07:42 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'x':
|
|
|
|
case 'c':
|
|
|
|
case 't':
|
|
|
|
if (mode)
|
|
|
|
usage();
|
|
|
|
mode = ARGC();
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
dir = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
file = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
mflag = 1;
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
case 'z':
|
|
|
|
if (filtermode)
|
|
|
|
usage();
|
|
|
|
filtermode = ARGC();
|
|
|
|
break;
|
2015-02-09 19:53:24 +00:00
|
|
|
case 'h':
|
2015-03-02 20:43:56 +00:00
|
|
|
recurse_follow = 'L';
|
2015-02-09 19:53:24 +00:00
|
|
|
break;
|
2015-01-26 15:07:42 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (!mode || argc != (mode == 'c'))
|
|
|
|
usage();
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case 'c':
|
|
|
|
if (file) {
|
2015-02-01 15:55:30 +00:00
|
|
|
if (!(fp = fopen(file, "w")))
|
2015-01-26 15:07:42 +00:00
|
|
|
eprintf("fopen %s:", file);
|
|
|
|
if (lstat(file, &st) < 0)
|
|
|
|
eprintf("tar: stat '%s':", file);
|
|
|
|
tarinode = st.st_ino;
|
|
|
|
tardev = st.st_dev;
|
|
|
|
tarfile = fp;
|
|
|
|
} else {
|
|
|
|
tarfile = stdout;
|
|
|
|
}
|
|
|
|
chdir(dir);
|
2015-03-02 20:43:56 +00:00
|
|
|
c(argv[0], 0);
|
2015-01-26 15:07:42 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
case 'x':
|
|
|
|
if (file) {
|
2015-02-01 15:55:30 +00:00
|
|
|
if (!(fp = fopen(file, "r")))
|
2015-01-26 15:07:42 +00:00
|
|
|
eprintf("fopen %s:", file);
|
|
|
|
} else {
|
|
|
|
fp = stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (filtermode) {
|
|
|
|
case 'j':
|
|
|
|
case 'z':
|
|
|
|
tarfile = decomp(fp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tarfile = fp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
chdir(dir);
|
|
|
|
xt(mode == 'x' ? unarchive : print);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|