Allow checkown to create files too.
This commit is contained in:
parent
40a1beabe0
commit
50ea72ef4b
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
checkown.c
|
checkown.c
|
||||||
Checks for the existance of a directory and creates it
|
Checks for the existance of a file or directory and creates it
|
||||||
if necessary. It can also correct its ownership.
|
if necessary. It can also correct its ownership.
|
||||||
|
|
||||||
Copyright 2007 Gentoo Foundation
|
Copyright 2007 Gentoo Foundation
|
||||||
@ -11,6 +11,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
@ -24,21 +25,34 @@
|
|||||||
|
|
||||||
static char *applet = NULL;
|
static char *applet = NULL;
|
||||||
|
|
||||||
static int do_check (char *path, uid_t uid, gid_t gid, mode_t mode)
|
static int do_check (char *path, uid_t uid, gid_t gid, mode_t mode, bool file)
|
||||||
{
|
{
|
||||||
struct stat dirstat;
|
struct stat dirstat;
|
||||||
|
|
||||||
memset (&dirstat, 0, sizeof (dirstat));
|
memset (&dirstat, 0, sizeof (dirstat));
|
||||||
|
|
||||||
if (stat (path, &dirstat)) {
|
if (stat (path, &dirstat)) {
|
||||||
einfo ("%s: creating directory", path);
|
if (file) {
|
||||||
if (! mode)
|
int fd;
|
||||||
mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
|
einfo ("%s: creating file", path);
|
||||||
if (mkdir (path, mode)) {
|
if ((fd = open (path, O_CREAT)) == -1) {
|
||||||
eerror ("%s: mkdir: %s", applet, strerror (errno));
|
eerror ("%s: open: %s", applet, strerror (errno));
|
||||||
return (-1);
|
return (-1);
|
||||||
|
}
|
||||||
|
close (fd);
|
||||||
|
} else {
|
||||||
|
einfo ("%s: creating directory", path);
|
||||||
|
if (! mode)
|
||||||
|
mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
|
||||||
|
if (mkdir (path, mode)) {
|
||||||
|
eerror ("%s: mkdir: %s", applet, strerror (errno));
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
mode = 0;
|
||||||
}
|
}
|
||||||
} else if (mode && (dirstat.st_mode & 0777) != mode) {
|
}
|
||||||
|
|
||||||
|
if (mode && (dirstat.st_mode & 0777) != mode) {
|
||||||
einfo ("%s: correcting mode", applet);
|
einfo ("%s: correcting mode", applet);
|
||||||
if (chmod (path, mode)) {
|
if (chmod (path, mode)) {
|
||||||
eerror ("%s: chmod: %s", applet, strerror (errno));
|
eerror ("%s: chmod: %s", applet, strerror (errno));
|
||||||
@ -108,8 +122,10 @@ static struct group *get_group (const char *name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#include "_usage.h"
|
#include "_usage.h"
|
||||||
#define getoptstring "m:g:u:" getoptstring_COMMON
|
#define getoptstring "fm:g:u:" getoptstring_COMMON
|
||||||
static struct option longopts[] = {
|
static struct option longopts[] = {
|
||||||
|
{ "directory", 0, NULL, 'd'},
|
||||||
|
{ "file", 0, NULL, 'f'},
|
||||||
{ "mode", 1, NULL, 'm'},
|
{ "mode", 1, NULL, 'm'},
|
||||||
{ "user", 1, NULL, 'u'},
|
{ "user", 1, NULL, 'u'},
|
||||||
{ "group", 1, NULL, 'g'},
|
{ "group", 1, NULL, 'g'},
|
||||||
@ -124,9 +140,10 @@ int checkown (int argc, char **argv)
|
|||||||
uid_t uid = geteuid();
|
uid_t uid = geteuid();
|
||||||
gid_t gid = getgid();
|
gid_t gid = getgid();
|
||||||
mode_t mode = 0;
|
mode_t mode = 0;
|
||||||
|
|
||||||
struct passwd *pw = NULL;
|
struct passwd *pw = NULL;
|
||||||
struct group *gr = NULL;
|
struct group *gr = NULL;
|
||||||
|
bool file = 0;
|
||||||
|
|
||||||
char *p;
|
char *p;
|
||||||
int retval = EXIT_SUCCESS;
|
int retval = EXIT_SUCCESS;
|
||||||
|
|
||||||
@ -136,6 +153,12 @@ int checkown (int argc, char **argv)
|
|||||||
longopts, (int *) 0)) != -1)
|
longopts, (int *) 0)) != -1)
|
||||||
{
|
{
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
|
case 'd':
|
||||||
|
file = 0;
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
file = 1;
|
||||||
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
if (parse_mode (&mode, optarg))
|
if (parse_mode (&mode, optarg))
|
||||||
eerrorx ("%s: invalid mode `%s'", applet, optarg);
|
eerrorx ("%s: invalid mode `%s'", applet, optarg);
|
||||||
@ -165,7 +188,7 @@ int checkown (int argc, char **argv)
|
|||||||
gid = gr->gr_gid;
|
gid = gr->gr_gid;
|
||||||
|
|
||||||
while (optind < argc) {
|
while (optind < argc) {
|
||||||
if (do_check (argv[optind], uid, gid, mode))
|
if (do_check (argv[optind], uid, gid, mode, file))
|
||||||
retval = EXIT_FAILURE;
|
retval = EXIT_FAILURE;
|
||||||
optind++;
|
optind++;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user