Skip to content

Commit

Permalink
Merge pull request #5 from nanopack/feature/truncate
Browse files Browse the repository at this point in the history
Add ability to truncate files when they reach a limit
  • Loading branch information
notxarb authored Feb 12, 2018
2 parents e0dc3f7 + 87980bc commit 0640f8e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

AC_PREREQ(2.61)

AC_INIT([narcd], [0.2.1])
AC_INIT([narcd], [0.2.2])
AM_INIT_AUTOMAKE([foreign subdir-objects])
AC_CONFIG_SRCDIR([src/narc.c])

Expand Down
4 changes: 3 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ load_server_config_from_string(char *config)
server.rate_limit = atoi(argv[1]);
} else if (!strcasecmp(argv[0],"rate-time") && argc == 2) {
server.rate_time = atoi(argv[1]);
} else {
} else if (!strcasecmp(argv[0],"truncate-limit") && argc == 2) {
server.truncate_limit = atoi(argv[1]);
} else {
err = "Bad directive or wrong number of arguments"; goto loaderr;
}
sdsfreesplitres(argv,argc);
Expand Down
1 change: 1 addition & 0 deletions src/narc.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ init_server_config(void)
server.connect_retry_delay = NARC_DEFAULT_CONNECT_DELAY;
server.rate_limit = NARC_DEFAULT_RATE_LIMIT;
server.rate_time = NARC_DEFAULT_RATE_TIME;
server.truncate_limit = NARC_DEFAULT_TRUNCATE_LIMIT;
server.streams = listCreate();
listSetFreeMethod(server.streams, free_stream);
}
Expand Down
2 changes: 2 additions & 0 deletions src/narc.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#define NARC_DEFAULT_CONNECT_DELAY 3000
#define NARC_DEFAULT_RATE_LIMIT 100
#define NARC_DEFAULT_RATE_TIME 10
#define NARC_DEFAULT_TRUNCATE_LIMIT 1024*1024*32 /* Default truncate files when they get to 32MB */

/* Log levels */
#define NARC_DEBUG 0
Expand Down Expand Up @@ -125,6 +126,7 @@ struct narc_server {
int stream_priority; /* Syslog stream priority */
int rate_limit; /* log rate limit */
int rate_time; /* log rate time */
int truncate_limit; /* size limit for truncating */

/* Time of day */
uv_timer_t time_timer; /* runs ever hald second to update the current time */
Expand Down
15 changes: 15 additions & 0 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include <stdio.h> /* standard buffered input/output */
#include <stdlib.h> /* standard library definitions */
#include <unistd.h> /* standard symbolic constants and types */
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <uv.h> /* Event driven programming library */
#include <string.h> /* string operations */

Expand Down Expand Up @@ -207,6 +210,11 @@ handle_file_stat(uv_fs_t* req)
stream->offset = 0;
}

// does the file need to be truncated?
if ((long int)stat->st_size > (long int)server.truncate_limit){
stream->truncate = 1;
}

stream->size = stat->st_size;

start_file_read(stream);
Expand Down Expand Up @@ -276,6 +284,13 @@ handle_file_read(uv_fs_t *req)
}
}

if (stream->truncate == 1) {
if (truncate(stream->file, 0) == -1) {
narc_log(NARC_WARNING, "Truncate error (%s): %s", stream->file, strerror(errno));
}
stream->truncate = 0;
}

unlock_stream(stream);

if (req->result == NARC_MAX_BUFF_SIZE -1)
Expand Down
1 change: 1 addition & 0 deletions src/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct {
int missed_count; /* */
int message_header_size;
int64_t offset;
int truncate;
uv_fs_event_t *fs_events;
uv_timer_t *open_timer;
} narc_stream;
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define NARC_VERSION "0.0.1"
#define NARC_VERSION "0.2.2"

0 comments on commit 0640f8e

Please sign in to comment.