summaryrefslogtreecommitdiff
path: root/inotail.c
diff options
context:
space:
mode:
Diffstat (limited to 'inotail.c')
-rw-r--r--inotail.c83
1 files changed, 64 insertions, 19 deletions
diff --git a/inotail.c b/inotail.c
index 1081aec..fc021dd 100644
--- a/inotail.c
+++ b/inotail.c
@@ -40,7 +40,7 @@
#include "inotail.h"
#define PROGRAM_NAME "inotail"
-#define BUFFER_SIZE 4096
+#define DEFAULT_BUFFER_SIZE 4096
/* inotify event buffer length for one file */
#define INOTIFY_BUFLEN (4 * sizeof(struct inotify_event))
@@ -84,6 +84,7 @@ static inline void setup_file(struct file_struct *f)
{
f->fd = -1;
f->st_size = 0;
+ f->st_blksize = DEFAULT_BUFFER_SIZE;
f->ignore = 0;
f->i_watch = -1;
}
@@ -115,18 +116,33 @@ static void write_header(char *filename)
last = filename;
}
+static char *alloc_buffer(struct file_struct *f)
+{
+ char *buf;
+
+ buf = malloc(f->st_blksize);
+ if (!buf) {
+ fprintf(stderr, "Error: Failed to allocate memory (%s)\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ return buf;
+}
+
static off_t lines_to_offset_from_end(struct file_struct *f, unsigned long n_lines)
{
- char buf[BUFFER_SIZE];
+ char *buf;
off_t offset = f->st_size;
+ buf = alloc_buffer(f);
+
n_lines++; /* We also count the last \n */
while (offset > 0 && n_lines > 0) {
int i;
- ssize_t rc, block_size = BUFFER_SIZE; /* Size of the current block we're reading */
+ ssize_t rc, block_size = f->st_blksize; /* Size of the current block we're reading */
- if (offset < BUFFER_SIZE)
+ if (offset < block_size)
block_size = offset;
/* Start of current block */
@@ -137,23 +153,27 @@ static off_t lines_to_offset_from_end(struct file_struct *f, unsigned long n_lin
rc = read(f->fd, buf, block_size);
if (unlikely(rc < 0)) {
fprintf(stderr, "Error: Could not read from file '%s' (%s)\n", f->name, strerror(errno));
+ free(buf);
return -1;
}
for (i = block_size - 1; i > 0; i--) {
if (buf[i] == '\n') {
- if (--n_lines == 0)
+ if (--n_lines == 0) {
+ free(buf);
return offset += i + 1; /* We don't want the first \n */
+ }
}
}
}
+ free(buf);
return offset;
}
static off_t lines_to_offset_from_begin(struct file_struct *f, unsigned long n_lines)
{
- char buf[BUFFER_SIZE];
+ char *buf;
off_t offset = 0;
/* tail everything for 'inotail -n +0' */
@@ -161,30 +181,35 @@ static off_t lines_to_offset_from_begin(struct file_struct *f, unsigned long n_l
return 0;
n_lines--;
+ buf = alloc_buffer(f);
while (offset <= f->st_size && n_lines > 0) {
int i;
- ssize_t rc, block_size = BUFFER_SIZE;
+ ssize_t rc, block_size = f->st_blksize;
lseek(f->fd, offset, SEEK_SET);
rc = read(f->fd, buf, block_size);
if (unlikely(rc < 0)) {
fprintf(stderr, "Error: Could not read from file '%s' (%s)\n", f->name, strerror(errno));
+ free(buf);
return -1;
} else if (rc < block_size)
block_size = rc;
for (i = 0; i < block_size; i++) {
if (buf[i] == '\n') {
- if (--n_lines == 0)
+ if (--n_lines == 0) {
+ free(buf);
return offset + i + 1;
+ }
}
}
offset += block_size;
}
+ free(buf);
return offset;
}
@@ -214,21 +239,25 @@ static off_t bytes_to_offset(struct file_struct *f, unsigned long n_bytes)
static ssize_t tail_pipe(struct file_struct *f)
{
- ssize_t rc;
- char buf[BUFFER_SIZE];
+ ssize_t rc, buffer_size = f->st_blksize;
+ char *buf;
+
+ buf = alloc_buffer(f);
if (verbose)
write_header(f->name);
/* We will just tail everything here */
- while ((rc = read(f->fd, buf, BUFFER_SIZE)) > 0) {
+ while ((rc = read(f->fd, buf, buffer_size)) > 0) {
if (write(STDOUT_FILENO, buf, (size_t) rc) < 0) {
/* e.g. when writing to a pipe which gets closed */
fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno));
+ free(buf);
return -1;
}
}
+ free(buf);
return rc;
}
@@ -236,7 +265,8 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
{
ssize_t bytes_read = 0;
off_t offset = 0;
- char buf[BUFFER_SIZE];
+ char *buf = NULL;
+ ssize_t buffer_size;
struct stat finfo;
if (strcmp(f->name, "-") == 0)
@@ -266,7 +296,10 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
if (IS_PIPELIKE(finfo.st_mode) || f->fd == STDIN_FILENO)
return tail_pipe(f);
+ buf = alloc_buffer(f);
+
f->st_size = finfo.st_size;
+ f->st_blksize = finfo.st_blksize; /* TODO: Can this value be 0 or negative? */
if (mode == M_LINES)
offset = lines_to_offset(f, n_units);
@@ -276,6 +309,7 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
/* We only get negative offsets on errors */
if (unlikely(offset < 0)) {
ignore_file(f);
+ free(buf);
return -1;
}
@@ -284,12 +318,14 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
lseek(f->fd, offset, SEEK_SET);
- while ((bytes_read = read(f->fd, buf, BUFFER_SIZE)) > 0)
+ buffer_size = f->st_blksize;
+ while ((bytes_read = read(f->fd, buf, buffer_size)) > 0)
write(STDOUT_FILENO, buf, (size_t) bytes_read);
if (!forever) {
if (close(f->fd) < 0) {
fprintf(stderr, "Error: Could not close file '%s' (%s)\n", f->name, strerror(errno));
+ free(buf);
return -1;
}
} /* Let the fd open otherwise, we'll need it */
@@ -299,18 +335,20 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
static int handle_inotify_event(struct inotify_event *inev, struct file_struct *f)
{
+ char *fbuf;
int ret = 0;
if (inev->mask & IN_MODIFY) {
- ssize_t rc;
- char fbuf[BUFFER_SIZE];
+ ssize_t rc, buffer_size = f->st_blksize;
struct stat finfo;
+ fbuf = alloc_buffer(f);
+
if (verbose)
write_header(f->name);
lseek(f->fd, f->st_size, SEEK_SET); /* Old file size */
- while ((rc = read(f->fd, fbuf, BUFFER_SIZE)) != 0)
+ while ((rc = read(f->fd, fbuf, buffer_size)) != 0)
write(STDOUT_FILENO, fbuf, (size_t) rc);
if (fstat(f->fd, &finfo) < 0) {
@@ -321,6 +359,7 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct *
f->st_size = finfo.st_size;
+ free(fbuf);
return ret;
} else if (inev->mask & IN_DELETE_SELF) {
fprintf(stderr, "File '%s' deleted.\n", f->name);
@@ -333,7 +372,7 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct *
ignore:
ignore_file(f);
-
+ free(fbuf);
return ret;
}
@@ -460,8 +499,14 @@ int main(int argc, char **argv)
specified and standard input is a pipe. */
if (forever) {
struct stat finfo;
- if (fstat(STDIN_FILENO, &finfo) == 0
- && IS_PIPELIKE(finfo.st_mode))
+ int rc = fstat(STDIN_FILENO, &finfo);
+
+ if (rc == -1) {
+ fprintf(stderr, "Error: Could not stat stdin (%s)\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (rc == 0 && IS_PIPELIKE(finfo.st_mode))
forever = 0;
}
}