Move hasquota() function to libutil.
This commit is contained in:
parent
581fce8e7d
commit
916e406eb5
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/projects/quota64/; revision=188568
@ -142,11 +142,13 @@ int pidfile_remove(struct pidfh *pfh);
|
|||||||
|
|
||||||
#ifdef _UFS_UFS_QUOTA_H_
|
#ifdef _UFS_UFS_QUOTA_H_
|
||||||
struct quotafile;
|
struct quotafile;
|
||||||
|
struct fstab;
|
||||||
struct quotafile *quota_open(const char *);
|
struct quotafile *quota_open(const char *);
|
||||||
struct quotafile *quota_create(const char *);
|
struct quotafile *quota_create(const char *);
|
||||||
void quota_close(struct quotafile *);
|
void quota_close(struct quotafile *);
|
||||||
int quota_read(struct quotafile *, struct dqblk *, int);
|
int quota_read(struct quotafile *, struct dqblk *, int);
|
||||||
int quota_write(struct quotafile *, const struct dqblk *, int);
|
int quota_write(struct quotafile *, const struct dqblk *, int);
|
||||||
|
int hasquota(struct fstab *, int, char **);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
@ -29,16 +29,19 @@
|
|||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/endian.h>
|
#include <sys/endian.h>
|
||||||
|
#include <sys/mount.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <ufs/ufs/quota.h>
|
#include <ufs/ufs/quota.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <fstab.h>
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <libutil.h>
|
#include <libutil.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -48,6 +51,8 @@ struct quotafile {
|
|||||||
int type; /* 32 or 64 */
|
int type; /* 32 or 64 */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static const char *qfextension[] = INITQFNAMES;
|
||||||
|
|
||||||
struct quotafile *
|
struct quotafile *
|
||||||
quota_open(const char *fn)
|
quota_open(const char *fn)
|
||||||
{
|
{
|
||||||
@ -231,7 +236,7 @@ quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id)
|
|||||||
off = id * sizeof(struct dqblk32);
|
off = id * sizeof(struct dqblk32);
|
||||||
if (lseek(qf->fd, off, SEEK_SET) == -1)
|
if (lseek(qf->fd, off, SEEK_SET) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
return (write(qf->fd, &dqb32, sizeof(dqb32)) == -1);
|
return (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -252,7 +257,7 @@ quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id)
|
|||||||
off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
|
off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
|
||||||
if (lseek(qf->fd, off, SEEK_SET) == -1)
|
if (lseek(qf->fd, off, SEEK_SET) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
return (write(qf->fd, &dqb64, sizeof(dqb64)) == -1);
|
return (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64));
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -270,3 +275,50 @@ quota_write(struct quotafile *qf, const struct dqblk *dqb, int id)
|
|||||||
}
|
}
|
||||||
/* not reached */
|
/* not reached */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check to see if a particular quota is to be enabled.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
hasquota(struct fstab *fs, int type, char **qfnamep)
|
||||||
|
{
|
||||||
|
char *opt;
|
||||||
|
char *cp;
|
||||||
|
struct statfs sfb;
|
||||||
|
static char initname, usrname[100], grpname[100];
|
||||||
|
static char buf[BUFSIZ];
|
||||||
|
|
||||||
|
if (!initname) {
|
||||||
|
(void)snprintf(usrname, sizeof(usrname), "%s%s",
|
||||||
|
qfextension[USRQUOTA], QUOTAFILENAME);
|
||||||
|
(void)snprintf(grpname, sizeof(grpname), "%s%s",
|
||||||
|
qfextension[GRPQUOTA], QUOTAFILENAME);
|
||||||
|
initname = 1;
|
||||||
|
}
|
||||||
|
strcpy(buf, fs->fs_mntops);
|
||||||
|
for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
|
||||||
|
if ((cp = index(opt, '=')))
|
||||||
|
*cp++ = '\0';
|
||||||
|
if (type == USRQUOTA && strcmp(opt, usrname) == 0)
|
||||||
|
break;
|
||||||
|
if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!opt)
|
||||||
|
return (0);
|
||||||
|
if (cp)
|
||||||
|
*qfnamep = cp;
|
||||||
|
else {
|
||||||
|
(void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
|
||||||
|
QUOTAFILENAME, qfextension[type]);
|
||||||
|
*qfnamep = buf;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Ensure that the filesystem is mounted.
|
||||||
|
*/
|
||||||
|
if (statfs(fs->fs_file, &sfb) != 0 ||
|
||||||
|
strcmp(fs->fs_file, sfb.f_mntonname)) {
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
@ -83,7 +83,6 @@ __FBSDID("$FreeBSD$");
|
|||||||
#define dbtokb(db) (db)
|
#define dbtokb(db) (db)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char *qfname = QUOTAFILENAME;
|
|
||||||
const char *qfextension[] = INITQFNAMES;
|
const char *qfextension[] = INITQFNAMES;
|
||||||
const char *quotagroup = QUOTAGROUP;
|
const char *quotagroup = QUOTAGROUP;
|
||||||
char tmpfil[] = _PATH_TMP;
|
char tmpfil[] = _PATH_TMP;
|
||||||
@ -109,7 +108,6 @@ char *fmthumanvalinos(int64_t);
|
|||||||
void freeprivs(struct quotause *);
|
void freeprivs(struct quotause *);
|
||||||
int getentry(const char *, int);
|
int getentry(const char *, int);
|
||||||
struct quotause *getprivs(long, int, char *);
|
struct quotause *getprivs(long, int, char *);
|
||||||
int hasquota(struct fstab *, int, char **);
|
|
||||||
void putprivs(long, int, struct quotause *);
|
void putprivs(long, int, struct quotause *);
|
||||||
int readprivs(struct quotause *, char *);
|
int readprivs(struct quotause *, char *);
|
||||||
int readtimes(struct quotause *, char *);
|
int readtimes(struct quotause *, char *);
|
||||||
@ -230,6 +228,8 @@ main(int argc, char *argv[])
|
|||||||
if ((protoid = getentry(protoname, quotatype)) == -1)
|
if ((protoid = getentry(protoname, quotatype)) == -1)
|
||||||
exit(1);
|
exit(1);
|
||||||
protoprivs = getprivs(protoid, quotatype, fspath);
|
protoprivs = getprivs(protoid, quotatype, fspath);
|
||||||
|
if (protoprivs == NULL)
|
||||||
|
exit(0);
|
||||||
for (qup = protoprivs; qup; qup = qup->next) {
|
for (qup = protoprivs; qup; qup = qup->next) {
|
||||||
qup->dqblk.dqb_btime = 0;
|
qup->dqblk.dqb_btime = 0;
|
||||||
qup->dqblk.dqb_itime = 0;
|
qup->dqblk.dqb_itime = 0;
|
||||||
@ -259,20 +259,20 @@ main(int argc, char *argv[])
|
|||||||
*argv);
|
*argv);
|
||||||
if ((id = getentry(buf, quotatype)) < 0)
|
if ((id = getentry(buf, quotatype)) < 0)
|
||||||
continue;
|
continue;
|
||||||
if (eflag) {
|
if (!eflag) {
|
||||||
for (qup = protoprivs; qup;
|
putprivs(id, quotatype, protoprivs);
|
||||||
qup = qup->next) {
|
continue;
|
||||||
curprivs = getprivs(id,
|
}
|
||||||
quotatype, qup->fsname);
|
for (qup = protoprivs; qup;
|
||||||
if (curprivs == NULL)
|
qup = qup->next) {
|
||||||
continue;
|
curprivs = getprivs(id, quotatype,
|
||||||
strcpy(qup->qfname,
|
qup->fsname);
|
||||||
curprivs->qfname);
|
if (curprivs == NULL)
|
||||||
strcpy(qup->fsname,
|
continue;
|
||||||
curprivs->fsname);
|
strcpy(qup->qfname, curprivs->qfname);
|
||||||
}
|
strcpy(qup->fsname, curprivs->fsname);
|
||||||
|
putprivs(id, quotatype, protoprivs);
|
||||||
}
|
}
|
||||||
putprivs(id, quotatype, protoprivs);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -280,12 +280,12 @@ main(int argc, char *argv[])
|
|||||||
tmpfd = mkstemp(tmpfil);
|
tmpfd = mkstemp(tmpfil);
|
||||||
fchown(tmpfd, getuid(), getgid());
|
fchown(tmpfd, getuid(), getgid());
|
||||||
if (tflag) {
|
if (tflag) {
|
||||||
protoprivs = getprivs(0, quotatype, fspath);
|
if ((protoprivs = getprivs(0, quotatype, fspath)) != NULL) {
|
||||||
if (writetimes(protoprivs, tmpfd, quotatype) == 0)
|
if (writetimes(protoprivs, tmpfd, quotatype) != 0 &&
|
||||||
exit(1);
|
editit(tmpfil) && readtimes(protoprivs, tmpfil))
|
||||||
if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
|
putprivs(0L, quotatype, protoprivs);
|
||||||
putprivs(0L, quotatype, protoprivs);
|
freeprivs(protoprivs);
|
||||||
freeprivs(protoprivs);
|
}
|
||||||
close(tmpfd);
|
close(tmpfd);
|
||||||
unlink(tmpfil);
|
unlink(tmpfil);
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -293,7 +293,8 @@ main(int argc, char *argv[])
|
|||||||
for ( ; argc > 0; argc--, argv++) {
|
for ( ; argc > 0; argc--, argv++) {
|
||||||
if ((id = getentry(*argv, quotatype)) == -1)
|
if ((id = getentry(*argv, quotatype)) == -1)
|
||||||
continue;
|
continue;
|
||||||
curprivs = getprivs(id, quotatype, fspath);
|
if ((curprivs = getprivs(id, quotatype, fspath)) == NULL)
|
||||||
|
exit(1);
|
||||||
if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
|
if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
|
||||||
continue;
|
continue;
|
||||||
if (editit(tmpfil) && readprivs(curprivs, tmpfil))
|
if (editit(tmpfil) && readprivs(curprivs, tmpfil))
|
||||||
@ -412,6 +413,9 @@ getprivs(long id, int quotatype, char *fspath)
|
|||||||
quptail = qup;
|
quptail = qup;
|
||||||
qup->next = 0;
|
qup->next = 0;
|
||||||
}
|
}
|
||||||
|
if (quphead == NULL) {
|
||||||
|
warnx("No quotas on %s", fspath ? fspath : "any filesystems");
|
||||||
|
}
|
||||||
endfsent();
|
endfsent();
|
||||||
return (quphead);
|
return (quphead);
|
||||||
}
|
}
|
||||||
@ -462,7 +466,7 @@ putprivs(long id, int quotatype, struct quotause *quplist)
|
|||||||
qup->dqblk.dqb_itime = 0;
|
qup->dqblk.dqb_itime = 0;
|
||||||
qup->dqblk.dqb_curinodes = dqbuf.dqb_curinodes;
|
qup->dqblk.dqb_curinodes = dqbuf.dqb_curinodes;
|
||||||
qup->dqblk.dqb_curblocks = dqbuf.dqb_curblocks;
|
qup->dqblk.dqb_curblocks = dqbuf.dqb_curblocks;
|
||||||
if (quota_write(qf, &qup->dqblk, id) != 0) {
|
if (quota_write(qf, &qup->dqblk, id) == 0) {
|
||||||
warn("%s", qup->qfname);
|
warn("%s", qup->qfname);
|
||||||
}
|
}
|
||||||
quota_close(qf);
|
quota_close(qf);
|
||||||
@ -994,54 +998,3 @@ alldigits(const char *s)
|
|||||||
} while ((c = *s++));
|
} while ((c = *s++));
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Check to see if a particular quota is to be enabled.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
hasquota(struct fstab *fs, int type, char **qfnamep)
|
|
||||||
{
|
|
||||||
char *opt;
|
|
||||||
char *cp;
|
|
||||||
struct statfs sfb;
|
|
||||||
static char initname, usrname[100], grpname[100];
|
|
||||||
static char buf[BUFSIZ];
|
|
||||||
|
|
||||||
if (!initname) {
|
|
||||||
(void)snprintf(usrname, sizeof(usrname), "%s%s",
|
|
||||||
qfextension[USRQUOTA], qfname);
|
|
||||||
(void)snprintf(grpname, sizeof(grpname), "%s%s",
|
|
||||||
qfextension[GRPQUOTA], qfname);
|
|
||||||
initname = 1;
|
|
||||||
}
|
|
||||||
strcpy(buf, fs->fs_mntops);
|
|
||||||
for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
|
|
||||||
if ((cp = index(opt, '=')))
|
|
||||||
*cp++ = '\0';
|
|
||||||
if (type == USRQUOTA && strcmp(opt, usrname) == 0)
|
|
||||||
break;
|
|
||||||
if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!opt)
|
|
||||||
return (0);
|
|
||||||
if (cp)
|
|
||||||
*qfnamep = cp;
|
|
||||||
else {
|
|
||||||
(void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
|
|
||||||
qfname, qfextension[type]);
|
|
||||||
*qfnamep = buf;
|
|
||||||
}
|
|
||||||
if (statfs(fs->fs_file, &sfb) != 0) {
|
|
||||||
warn("cannot statfs mount point %s", fs->fs_file);
|
|
||||||
sleep(3);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
if (strcmp(fs->fs_file, sfb.f_mntonname)) {
|
|
||||||
warnx("%s not mounted for %s quotas", fs->fs_file,
|
|
||||||
type == USRQUOTA ? "user" : "group");
|
|
||||||
sleep(3);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user