summaryrefslogtreecommitdiffstats
path: root/lib/libutil/quotafile.c
diff options
context:
space:
mode:
authormckusick <mckusick@FreeBSD.org>2009-02-13 06:12:15 +0000
committermckusick <mckusick@FreeBSD.org>2009-02-13 06:12:15 +0000
commit44bede95eb16d0f75fcfc75671147d1a91773c00 (patch)
tree9ae3ac009f27dbb0491aa641ac8bf6c0e15aba8c /lib/libutil/quotafile.c
parent69ccea269cc97b0dc407dfb7aa6ba58ce2e98b3a (diff)
downloadFreeBSD-src-44bede95eb16d0f75fcfc75671147d1a91773c00.zip
FreeBSD-src-44bede95eb16d0f75fcfc75671147d1a91773c00.tar.gz
Move hasquota() function to libutil.
Diffstat (limited to 'lib/libutil/quotafile.c')
-rw-r--r--lib/libutil/quotafile.c56
1 files changed, 54 insertions, 2 deletions
diff --git a/lib/libutil/quotafile.c b/lib/libutil/quotafile.c
index c175df0..2520e9f 100644
--- a/lib/libutil/quotafile.c
+++ b/lib/libutil/quotafile.c
@@ -29,16 +29,19 @@
#include <sys/types.h>
#include <sys/endian.h>
+#include <sys/mount.h>
#include <sys/stat.h>
#include <ufs/ufs/quota.h>
#include <errno.h>
#include <fcntl.h>
+#include <fstab.h>
#include <grp.h>
#include <pwd.h>
#include <libutil.h>
#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -48,6 +51,8 @@ struct quotafile {
int type; /* 32 or 64 */
};
+static const char *qfextension[] = INITQFNAMES;
+
struct quotafile *
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);
if (lseek(qf->fd, off, SEEK_SET) == -1)
return (-1);
- return (write(qf->fd, &dqb32, sizeof(dqb32)) == -1);
+ return (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32));
}
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);
if (lseek(qf->fd, off, SEEK_SET) == -1)
return (-1);
- return (write(qf->fd, &dqb64, sizeof(dqb64)) == -1);
+ return (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64));
}
int
@@ -270,3 +275,50 @@ quota_write(struct quotafile *qf, const struct dqblk *dqb, int id)
}
/* 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);
+}
OpenPOWER on IntegriCloud