consolidate includes/code

This commit is contained in:
Jan-Piet Mens
2015-09-01 17:15:00 +02:00
parent 19499bf2f6
commit e09b93d95d
10 changed files with 139 additions and 163 deletions
+4 -8
View File
@@ -9,8 +9,6 @@ OTR_OBJS = json.o \
geo.o \
geohash.o \
mkpath.o \
file.o \
safewrite.o \
base64.o \
misc.o \
util.o \
@@ -39,10 +37,8 @@ ot-recorder: ot-recorder.o $(OTR_OBJS)
ot-recorder.o: ot-recorder.c storage.h util.h Makefile geo.h udata.h config.h json.h http.h
geo.o: geo.h geo.c udata.h Makefile config.mk config.h
geohash.o: geohash.h geohash.c udata.h Makefile config.mk
file.o: file.h file.c config.h misc.h Makefile config.mk
base64.o: base64.h base64.c
gcache.o: gcache.c gcache.h json.h
safewrite.o: safewrite.h safewrite.c
jget.o: jget.c jget.h json.h Makefile config.mk
misc.o: misc.c misc.h udata.h Makefile config.mk
http.o: http.c mongoose.h util.h http.h storage.h
@@ -54,8 +50,8 @@ ocat: ocat.o storage.o json.o geohash.o mkpath.o util.o gcache.o
ocat.o: ocat.c storage.h
storage.o: storage.c storage.h config.h util.h
ghashfind: ghashfind.o util.o json.o
$(CC) $(CFLAGS) -o ghashfind ghashfind.o util.o json.o
ghashfind: ghashfind.o util.o json.o storage.o geohash.o mkpath.o gcache.o
$(CC) $(CFLAGS) -o ghashfind ghashfind.o util.o json.o storage.o geohash.o mkpath.o gcache.o $(LIBS)
ghashfind.o: ghashfind.c util.h
mongoose.o: mongoose.c mongoose.h
@@ -69,5 +65,5 @@ mdb/liblmdb.a:
gcache-dump: gcache-dump.o
$(CC) $(CFLAGS) -o gcache-dump gcache-dump.c $(LIBS)
ghash2lmdb: ghash2lmdb.o util.o json.o gcache.o
$(CC) $(CFLAGS) -o ghash2lmdb ghash2lmdb.c util.o json.o gcache.o $(LIBS)
ghash2lmdb: ghash2lmdb.o util.o json.o gcache.o misc.o storage.o geohash.o mkpath.o
$(CC) $(CFLAGS) -o ghash2lmdb ghash2lmdb.c util.o json.o gcache.o misc.o storage.o geohash.o mkpath.o $(LIBS)
-84
View File
@@ -1,84 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <time.h>
#include "file.h"
#include "misc.h"
#include "util.h"
static void ut_lower(UT_string *us)
{
char *p;
for (p = utstring_body(us); p && *p; p++) {
if (!isalnum(*p) || isspace(*p))
*p = '-';
else if (isupper(*p))
*p = tolower(*p);
}
}
static void ut_clean(UT_string *us)
{
char *p;
for (p = utstring_body(us); p && *p; p++) {
if (isspace(*p))
*p = '-';
}
}
/* Return an open append file pointer to storage for user/device,
creating directories on the fly. If device is NULL, omit it.
*/
FILE *pathn(char *mode, char *prefix, UT_string *user, UT_string *device, char *suffix)
{
static UT_string *path = NULL;
time_t now;
utstring_renew(path);
ut_lower(user);
if (device) {
ut_lower(device);
utstring_printf(path, "%s/%s/%s/%s", STORAGEDIR, prefix, utstring_body(user), utstring_body(device));
} else {
utstring_printf(path, "%s/%s/%s", STORAGEDIR, prefix, utstring_body(user));
}
ut_clean(path);
if (mkpath(utstring_body(path)) < 0) {
perror(utstring_body(path));
return (NULL);
}
#if 0
if (device) {
utstring_printf(path, "/%s-%s.%s",
utstring_body(user), utstring_body(device), suffix);
} else {
utstring_printf(path, "/%s.%s",
utstring_body(user), suffix);
}
#endif
if (strcmp(prefix, "rec") == 0) {
time(&now);
utstring_printf(path, "/%s.%s", yyyymm(now), suffix);
} else {
utstring_printf(path, "/%s.%s",
utstring_body(user), suffix);
}
ut_clean(path);
return (fopen(utstring_body(path), mode));
}
-5
View File
@@ -1,5 +0,0 @@
#include "config.h"
#include "utstring.h"
FILE *pathn(char *mode, char *prefix, UT_string *user, UT_string *device, char *suffix);
+2 -1
View File
@@ -1,3 +1,4 @@
/* ghash2lmdb: read key<space>value separated lines and add them to an LMDB database */
#include <stdio.h>
#include <string.h>
#include "gcache.h"
@@ -32,5 +33,5 @@ int main(int argc,char * argv[])
}
gcache_close(gc);
return 0;
return (0);
}
+1 -1
View File
@@ -4,7 +4,7 @@
#include "ctype.h"
#include "udata.h"
#include "misc.h"
#include "safewrite.h"
#include "util.h"
char *bindump(char *buf, long buflen)
{
-2
View File
@@ -16,8 +16,6 @@
#include "geo.h"
#include "config.h"
#include "geohash.h"
#include "file.h"
#include "safewrite.h"
#include "base64.h"
#include "misc.h"
#include "util.h"
-56
View File
@@ -1,56 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "safewrite.h"
int safewrite(char *filename, char *buf)
{
char *tmpfile = malloc(strlen(filename) + 3);
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
int fd;
if (tmpfile == NULL)
return (-1);
sprintf(tmpfile, "%s~", filename);
if (unlink(tmpfile) == -1) {
if (errno != ENOENT) {
fprintf(stderr, "Failed to remove %s (errno=%d)\n", tmpfile, errno);
free(tmpfile);
return (-1);
}
}
if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, mode)) == -1) {
fprintf(stderr, "Failed to create %s (errno=%d)\n", tmpfile, errno);
free(tmpfile);
return (-1);
}
if (write(fd, buf, strlen(buf)) != strlen(buf)) {
fprintf(stderr, "Failed to write to %s (errno=%d)\n", tmpfile, errno);
free(tmpfile);
close(fd);
return (-1);
}
/* Ensure NL-terminated */
if (buf[strlen(buf) - 1] != '\n') {
write(fd, "\n", 1);
}
close(fd);
if ((rename(tmpfile, filename)) == -1) {
fprintf(stderr, "Failed to rename %s to %s (errno=%d)\n", tmpfile, filename, errno);
free(tmpfile);
}
free(tmpfile);
return (0);
}
-6
View File
@@ -1,6 +0,0 @@
#ifndef SAFEWRITE_H_INCLUDED
# define SAFEWRITE_H_INCLUDED
int safewrite(char *filename, char *buf);
#endif
+127
View File
@@ -3,9 +3,14 @@
#include <string.h>
#include <syslog.h>
#include "util.h"
#include "misc.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#ifndef LINESIZE
# define LINESIZE 8192
@@ -362,3 +367,125 @@ int tac(char *filename, long lines, int (*func)(char *, void *), void *param)
fclose(fp);
return (0);
}
static void ut_lower(UT_string *us)
{
char *p;
for (p = utstring_body(us); p && *p; p++) {
if (!isalnum(*p) || isspace(*p))
*p = '-';
else if (isupper(*p))
*p = tolower(*p);
}
}
static void ut_clean(UT_string *us)
{
char *p;
for (p = utstring_body(us); p && *p; p++) {
if (isspace(*p))
*p = '-';
}
}
/* Return an open append file pointer to storage for user/device,
creating directories on the fly. If device is NULL, omit it.
*/
FILE *pathn(char *mode, char *prefix, UT_string *user, UT_string *device, char *suffix)
{
static UT_string *path = NULL;
time_t now;
utstring_renew(path);
ut_lower(user);
if (device) {
ut_lower(device);
utstring_printf(path, "%s/%s/%s/%s", STORAGEDIR, prefix, utstring_body(user), utstring_body(device));
} else {
utstring_printf(path, "%s/%s/%s", STORAGEDIR, prefix, utstring_body(user));
}
ut_clean(path);
if (mkpath(utstring_body(path)) < 0) {
perror(utstring_body(path));
return (NULL);
}
#if 0
if (device) {
utstring_printf(path, "/%s-%s.%s",
utstring_body(user), utstring_body(device), suffix);
} else {
utstring_printf(path, "/%s.%s",
utstring_body(user), suffix);
}
#endif
if (strcmp(prefix, "rec") == 0) {
time(&now);
utstring_printf(path, "/%s.%s", yyyymm(now), suffix);
} else {
utstring_printf(path, "/%s.%s",
utstring_body(user), suffix);
}
ut_clean(path);
return (fopen(utstring_body(path), mode));
}
int safewrite(char *filename, char *buf)
{
char *tmpfile = malloc(strlen(filename) + 3);
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
int fd;
if (tmpfile == NULL)
return (-1);
sprintf(tmpfile, "%s~", filename);
if (unlink(tmpfile) == -1) {
if (errno != ENOENT) {
fprintf(stderr, "Failed to remove %s (errno=%d)\n", tmpfile, errno);
free(tmpfile);
return (-1);
}
}
if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, mode)) == -1) {
fprintf(stderr, "Failed to create %s (errno=%d)\n", tmpfile, errno);
free(tmpfile);
return (-1);
}
if (write(fd, buf, strlen(buf)) != strlen(buf)) {
fprintf(stderr, "Failed to write to %s (errno=%d)\n", tmpfile, errno);
free(tmpfile);
close(fd);
return (-1);
}
/* Ensure NL-terminated */
if (buf[strlen(buf) - 1] != '\n') {
write(fd, "\n", 1);
}
close(fd);
if ((rename(tmpfile, filename)) == -1) {
fprintf(stderr, "Failed to rename %s to %s (errno=%d)\n", tmpfile, filename, errno);
free(tmpfile);
}
free(tmpfile);
return (0);
}
+5
View File
@@ -7,8 +7,11 @@
#endif
#include <time.h>
#include "config.h"
#include "json.h"
#include "utstring.h"
int mkpath(char *path);
int is_directory(char *path);
const char *isotime(time_t t);
char *slurp_file(char *filename, int fold_newlines);
@@ -21,5 +24,7 @@ int syslog_facility_code(char *facility);
const char *yyyymm(time_t t);
int tac(char *filename, long lines, int (*func)(char *, void *), void *param);
int cat(char *filename, int (*func)(char *, void *), void *param);
FILE *pathn(char *mode, char *prefix, UT_string *user, UT_string *device, char *suffix);
int safewrite(char *filename, char *buf);
#endif