changeset 17:09d2f54562ec

pasterd-clean: initial import, closes #2473
author David Demelier <markand@malikania.fr>
date Thu, 06 Feb 2020 14:17:14 +0100
parents 29033b4fe558
children ba9fa4869a4b
files .hgignore Makefile pasterd-clean.8.in pasterd-clean.c
diffstat 4 files changed, 135 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Feb 06 13:25:22 2020 +0100
+++ b/.hgignore	Thu Feb 06 14:17:14 2020 +0100
@@ -21,4 +21,5 @@
 
 # Executables.
 ^pasterd(\.8)?$
+^pasterd-clean(\.8)?$
 ^paster(\.1)?$
--- a/Makefile	Thu Feb 06 13:25:22 2020 +0100
+++ b/Makefile	Thu Feb 06 14:17:14 2020 +0100
@@ -22,7 +22,7 @@
 CFLAGS=         -std=c18 -pedantic -D_XOPEN_SOURCE=700 -DNDEBUG -O3
 LDFLAGS=        -static -lkcgi -lkcgihtml -lz
 
-SRCS=           config.c database.c http.c log.c pasterd.c paste.c util.c
+SRCS=           config.c database.c http.c log.c paste.c util.c
 OBJS=           ${SRCS:.c=.o}
 DEPS=           ${SRCS:.c=.d}
 
@@ -42,7 +42,7 @@
 .SUFFIXES:
 .SUFFIXES: .c .o .in
 
-all: pasterd paster
+all: pasterd pasterd-clean paster
 
 -include ${DEPS}
 
@@ -60,8 +60,11 @@
 extern/libsqlite3.a: extern/sqlite3.o
 	${AR} -rc $@ $<
 
-pasterd: ${OBJS} extern/libsqlite3.a paster.8
-	${CC} -o $@ ${OBJS} ${LDFLAGS} extern/libsqlite3.a
+pasterd: ${OBJS} extern/libsqlite3.a pasterd.o pasterd.8
+	${CC} -o $@ ${OBJS} pasterd.o ${LDFLAGS} extern/libsqlite3.a
+
+pasterd-clean: ${OBJS} extern/libsqlite3.a pasterd-clean.o pasterd-clean.8
+	${CC} -o $@ ${OBJS} pasterd-clean.o ${LDFLAGS} extern/libsqlite3.a
 
 paster: paster.sh paster.1
 	cp paster.sh paster
@@ -73,12 +76,15 @@
 
 install-paster:
 	mkdir -p ${DESTDIR}${BINDIR}
+	mkdir -p ${DESTDIR}${MANDIR}/man1
 	cp paster ${DESTDIR}${BINDIR}
 	cp paster.1 ${DESTDIR}${MANDIR}/man1/paster.1
 	
 install-pasterd:
 	mkdir -p ${DESTDIR}${BINDIR}
+	mkdir -p ${DESTDIR}${MANDIR}/man8
 	cp pasterd ${DESTDIR}${BINDIR}
+	cp pasterd-clean ${DESTDIR}${BINDIR}
 	mkdir -p ${DESTDIR}${SHAREDIR}/paster
 	cp -R themes ${DESTDIR}${SHAREDIR}/paster
 	cp pasterd.8 ${DESTDIR}${MANDIR}/man8/pasterd.8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pasterd-clean.8.in	Thu Feb 06 14:17:14 2020 +0100
@@ -0,0 +1,63 @@
+.\"
+.\" Copyright (c) 2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd 06 February, 2020
+.Dt PASTERD-CLEAN 8
+.Os
+.\" NAME
+.Sh NAME
+.Nm pasterd-clean
+.Nd simple paste service cleaner
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.Nm
+.Op Fl d Ar database-path
+.\" DESCRIPTION
+.Sh DESCRIPTION
+This utility should be used at periodic intervals to clean up the SQLite
+database. It will remove deprecated pastes.
+.Pp
+Like
+.Xr pasterd 8
+it can use environment variables or option to specify the database.
+.Pp
+Available options:
+.Bl -tag -width Ds
+.It Fl d Ar database-path
+Specify an alternate path for the database.
+.El
+.\" USAGE
+.Sh USAGE
+This command should be placed in a cron job (or equivalent) at periodic
+intervals. Since the minimal lifetime for a paste is one hour a periodic job
+every hour seems appropriate.
+.Pp
+Note: make sure to run the cron job in the same user as the pasterd service,
+otherwise you may change file permissions.
+.Pp
+Example of cron job:
+.Bd -literal -offset Ds
+0 */1 * * * www pasterd-clean -d /var/paster/paster.db
+.Ed
+.\" ENVIRONMENT
+.Sh ENVIRONMENT
+The following environment variables are detected:
+.Bl -tag -width Ds
+.It Va PASTERD_DATABASE_PATH No (string)
+Path to the SQLite database.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr pasterd 8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pasterd-clean.c	Thu Feb 06 14:17:14 2020 +0100
@@ -0,0 +1,61 @@
+/*
+ * pasterd-clean.c -- main pasterd-clean(8) file
+ *
+ * Copyright (c) 2020 David Demelier <markand@malikania.fr>
+ * 
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdnoreturn.h>
+#include <unistd.h>
+
+#include "database.h"
+#include "util.h"
+
+static noreturn void
+usage(void)
+{
+	fprintf(stderr, "usage: pasterd-clean [-d database-path]\n");
+	exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+	const char *value;
+	char path[PATH_MAX] = VARDIR "/paster/paster.db";
+	int ch;
+
+	/* Seek environment first. */
+	if ((value = getenv("PASTERD_DATABASE_PATH")))
+		snprintf(path, sizeof (path), "%s", value);
+
+	while ((ch = getopt(argc, argv, "d:")) != -1) {
+		switch (ch) {
+		case 'd':
+			snprintf(path, sizeof (path), "%s", optarg);
+			break;
+		default:
+			usage();
+			break;
+		}
+	}
+
+	if (!path[0])
+		die("abort: no database specified");
+
+	database_clear();
+}