diff --git a/Makefile b/Makefile index 1b2bb77..9ea1215 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/file.c b/file.c deleted file mode 100644 index 7633b0a..0000000 --- a/file.c +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include -#include -#include -#include -#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)); - -} diff --git a/file.h b/file.h deleted file mode 100644 index 838e91b..0000000 --- a/file.h +++ /dev/null @@ -1,5 +0,0 @@ -#include "config.h" -#include "utstring.h" - - -FILE *pathn(char *mode, char *prefix, UT_string *user, UT_string *device, char *suffix); diff --git a/ghash2lmdb.c b/ghash2lmdb.c index f48ae3c..75bb12a 100644 --- a/ghash2lmdb.c +++ b/ghash2lmdb.c @@ -1,3 +1,4 @@ +/* ghash2lmdb: read keyvalue separated lines and add them to an LMDB database */ #include #include #include "gcache.h" @@ -32,5 +33,5 @@ int main(int argc,char * argv[]) } gcache_close(gc); - return 0; + return (0); } diff --git a/misc.c b/misc.c index c1484c8..ae615f5 100644 --- a/misc.c +++ b/misc.c @@ -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) { diff --git a/ot-recorder.c b/ot-recorder.c index 37c2415..cc124ef 100644 --- a/ot-recorder.c +++ b/ot-recorder.c @@ -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" diff --git a/safewrite.c b/safewrite.c deleted file mode 100644 index 561f6d6..0000000 --- a/safewrite.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#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); -} diff --git a/safewrite.h b/safewrite.h deleted file mode 100644 index a7b6e5d..0000000 --- a/safewrite.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef SAFEWRITE_H_INCLUDED -# define SAFEWRITE_H_INCLUDED - -int safewrite(char *filename, char *buf); - -#endif diff --git a/util.c b/util.c index 01d849d..dab5374 100644 --- a/util.c +++ b/util.c @@ -3,9 +3,14 @@ #include #include #include "util.h" +#include "misc.h" #include #include #include +#include +#include +#include +#include #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); +} diff --git a/util.h b/util.h index e215a69..6d452a5 100644 --- a/util.h +++ b/util.h @@ -7,8 +7,11 @@ #endif #include +#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