2016-02-12 08:33:09 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <errno.h>
|
2017-07-03 17:58:49 -04:00
|
|
|
#include <fcntl.h>
|
2016-02-12 08:33:09 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int Dflag = 0;
|
|
|
|
static gid_t group;
|
|
|
|
static uid_t owner;
|
|
|
|
static mode_t mode = 0755;
|
|
|
|
|
|
|
|
static void
|
|
|
|
make_dir(char *dir, int was_missing)
|
|
|
|
{
|
|
|
|
if (!mkdir(dir, was_missing ? 0755 : mode)) {
|
|
|
|
if (!was_missing && (lchown(dir, owner, group) < 0))
|
|
|
|
eprintf("lchmod %s:", dir);
|
|
|
|
} else if (errno != EEXIST) {
|
|
|
|
eprintf("mkdir %s:", dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
make_dirs(char *dir, int was_missing)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
for (p = strchr(dir + (dir[0] == '/'), '/'); p; p = strchr(p + 1, '/')) {
|
|
|
|
*p = '\0';
|
|
|
|
make_dir(dir, was_missing);
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
make_dir(dir, was_missing);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
install(const char *s1, const char *s2, int depth)
|
|
|
|
{
|
2017-07-03 17:58:49 -04:00
|
|
|
int f1, f2;
|
2016-02-12 08:33:09 -05:00
|
|
|
|
2020-03-01 20:26:48 -05:00
|
|
|
if ((f1 = open(s1, O_RDONLY)) < 0)
|
|
|
|
eprintf("open %s:", s1);
|
|
|
|
if ((f2 = creat(s2, 0600)) < 0) {
|
2016-02-12 08:33:09 -05:00
|
|
|
if (unlink(s2) < 0 && errno != ENOENT)
|
|
|
|
eprintf("unlink %s:", s2);
|
2020-03-01 20:26:48 -05:00
|
|
|
if ((f2 = creat(s2, 0600)) < 0)
|
|
|
|
eprintf("creat %s:", s2);
|
|
|
|
}
|
|
|
|
if (concat(f1, s1, f2, s2) < 0)
|
2020-05-24 23:58:36 -04:00
|
|
|
goto fail;
|
|
|
|
if (fchmod(f2, mode) < 0) {
|
|
|
|
weprintf("fchmod %s:", s2);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (fchown(f2, owner, group) < 0) {
|
|
|
|
weprintf("fchown %s:", s2);
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-02-12 08:33:09 -05:00
|
|
|
|
2020-03-01 20:26:48 -05:00
|
|
|
close(f1);
|
|
|
|
close(f2);
|
2016-02-12 08:33:09 -05:00
|
|
|
|
|
|
|
return 0;
|
2020-05-24 23:58:36 -04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
unlink(s2);
|
|
|
|
exit(1);
|
2016-02-12 08:33:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2016-12-03 06:51:14 -05:00
|
|
|
eprintf("usage: %s [-g group] [-o owner] [-m mode] (-d dir ... | [-D] (-t dest source ... | source ... dest))\n", argv0);
|
2016-02-12 08:33:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int dflag = 0;
|
|
|
|
char *gflag = 0;
|
|
|
|
char *oflag = 0;
|
|
|
|
char *mflag = 0;
|
|
|
|
char *tflag = 0;
|
|
|
|
struct group *gr;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct stat st;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
2016-07-08 13:24:09 -04:00
|
|
|
case 'c':
|
|
|
|
/* no-op for compatibility */
|
|
|
|
break;
|
2016-02-12 08:33:09 -05:00
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
Dflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-12-03 06:51:14 -05:00
|
|
|
/* no-op for compatibility */
|
2016-02-12 08:33:09 -05:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
gflag = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
oflag = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
mflag = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tflag = EARGF(usage());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND
|
|
|
|
|
2016-12-03 06:51:14 -05:00
|
|
|
if (argc < 1 + (!tflag & !dflag) || dflag & (Dflag | !!tflag))
|
2016-02-12 08:33:09 -05:00
|
|
|
usage();
|
|
|
|
|
|
|
|
if (gflag) {
|
|
|
|
errno = 0;
|
|
|
|
gr = getgrnam(gflag);
|
|
|
|
if (gr) {
|
|
|
|
group = gr->gr_gid;
|
|
|
|
} else {
|
|
|
|
if (errno)
|
|
|
|
eprintf("getgrnam %s:", gflag);
|
|
|
|
group = estrtonum(gflag, 0, UINT_MAX);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
group = getgid();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oflag) {
|
|
|
|
errno = 0;
|
|
|
|
pw = getpwnam(oflag);
|
|
|
|
if (pw) {
|
|
|
|
owner = pw->pw_uid;
|
|
|
|
} else {
|
|
|
|
if (errno)
|
|
|
|
eprintf("getpwnam %s:", oflag);
|
|
|
|
owner = estrtonum(oflag, 0, UINT_MAX);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
owner = getuid();
|
|
|
|
}
|
|
|
|
|
2016-12-01 02:27:59 -05:00
|
|
|
if (mflag)
|
2016-02-12 08:33:09 -05:00
|
|
|
mode = parsemode(mflag, mode, 0);
|
|
|
|
|
2020-01-11 05:10:04 -05:00
|
|
|
if (dflag) {
|
|
|
|
for (; *argv; argc--, argv++)
|
|
|
|
make_dirs(*argv, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:33:09 -05:00
|
|
|
if (tflag) {
|
2016-12-27 07:48:31 -05:00
|
|
|
argv = memmove(argv - 1, argv, argc * sizeof(*argv));
|
2016-02-12 08:33:09 -05:00
|
|
|
argv[argc++] = tflag;
|
|
|
|
}
|
|
|
|
if (tflag || argc > 2) {
|
|
|
|
if (stat(argv[argc - 1], &st) < 0) {
|
|
|
|
if ((errno == ENOENT) && Dflag) {
|
|
|
|
make_dirs(argv[argc - 1], 1);
|
|
|
|
} else {
|
|
|
|
eprintf("stat %s:", argv[argc - 1]);
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(st.st_mode)) {
|
|
|
|
eprintf("%s: not a directory\n", argv[argc - 1]);
|
|
|
|
}
|
|
|
|
}
|
2020-01-11 05:10:04 -05:00
|
|
|
if (stat(argv[argc - 1], &st) < 0) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
eprintf("stat %s:", argv[argc - 1]);
|
|
|
|
if (tflag || Dflag || argc > 2) {
|
|
|
|
if ((p = strrchr(argv[argc - 1], '/')) != NULL) {
|
|
|
|
*p = '\0';
|
|
|
|
make_dirs(argv[argc - 1], 1);
|
|
|
|
*p = '/';
|
2016-02-12 08:33:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-11 05:10:04 -05:00
|
|
|
enmasse(argc, argv, install);
|
2016-02-12 08:33:09 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|