summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2008-03-01 12:25:14 +0100
committerTobias Klauser <tklauser@xenon.tklauser.home>2008-03-01 12:25:14 +0100
commitce6f9f6dab399a907f9f623f1986870df34f4663 (patch)
tree668b6a80d258692d8176c36af24a37406f51b6f1
parent66911157cfad3ccb5e24c11c77d3b0ff3252abd3 (diff)
parenta4d7f34d0210344d2e25e5af1c2e61057422521d (diff)
Merge branch 'pipe'
Conflicts: inotail.c
-rw-r--r--inotail.c261
-rw-r--r--inotail.h18
2 files changed, 260 insertions, 19 deletions
diff --git a/inotail.c b/inotail.c
index c19ed4f..7e2b6ac 100644
--- a/inotail.c
+++ b/inotail.c
@@ -89,7 +89,7 @@ static inline void setup_file(struct file_struct *f)
{
f->fd = f->i_watch = -1;
f->size = 0;
- f->blksize = DEFAULT_BUFFER_SIZE;
+ f->blksize = BUFSIZ;
f->ignore = 0;
}
@@ -236,24 +236,244 @@ static off_t bytes_to_offset(struct file_struct *f, unsigned long n_bytes)
return offset;
}
-static ssize_t tail_pipe(struct file_struct *f)
+static int tail_pipe_from_begin(struct file_struct *f, unsigned long n_units, const char mode)
{
- ssize_t rc;
- char *buf = emalloc(f->blksize);
+ int bytes_read = 0;
+ char buf[BUFSIZ];
- if (verbose)
- write_header(f->name);
+ if (n_units)
+ n_units--;
+
+ while (n_units > 0) {
+ if ((bytes_read = read(f->fd, buf, BUFSIZ)) <= 0) {
+ if (bytes_read < 0 && (errno == EINTR || errno == EAGAIN))
+ continue;
+ else
+ return bytes_read;
+ }
+
+ if (mode == M_LINES) {
+ int i;
+ ssize_t block_size = BUFSIZ;
+
+ if (bytes_read < BUFSIZ)
+ block_size = bytes_read;
+
+ for (i = 0; i < block_size; i++) {
+ if (buf[i] == '\n') {
+ if (--n_units == 0)
+ break;
+ }
+ }
+
+ if (++i < block_size)
+ write(STDOUT_FILENO, &buf[i], bytes_read - i);
+ } else {
+ if ((unsigned long) bytes_read > n_units) {
+ write(STDOUT_FILENO, &buf[n_units], bytes_read - n_units);
+ bytes_read = n_units;
+ }
+
+ n_units -= bytes_read;
+ }
+ }
+
+ while ((bytes_read = read(f->fd, buf, BUFSIZ)) > 0)
+ write(STDOUT_FILENO, buf, (size_t) bytes_read);
+
+ return 0;
+}
+
+static int tail_pipe_lines(struct file_struct *f, unsigned long n_lines)
+{
+ struct line_buf *first, *last, *tmp;
+ int rc;
+ unsigned long total_lines = 0;
+ const char *p;
- /* We will just tail everything here */
- while ((rc = read(f->fd, buf, f->blksize)) > 0) {
- if ((rc = write(STDOUT_FILENO, buf, (size_t) rc)) <= 0) {
- /* e.g. when writing to a pipe which gets closed */
+ if (from_begin)
+ return tail_pipe_from_begin(f, n_lines, M_LINES);
+
+ if (n_lines == 0)
+ return 0;
+
+ first = last = emalloc(sizeof(struct line_buf));
+ first->n_bytes = first->n_lines = 0;
+ first->next = NULL;
+ tmp = emalloc(sizeof(struct line_buf));
+
+ while (1) {
+ if ((rc = read(f->fd, tmp->buf, BUFSIZ)) <= 0) {
+ if (rc < 0 && (errno == EINTR || errno == EAGAIN))
+ continue;
+ else
+ break;
+ }
+ tmp->n_bytes = rc;
+ tmp->n_lines = 0;
+ tmp->next = NULL;
+ p = tmp->buf;
+
+ /* Count the lines in the current buffer */
+ while ((p = memchr(p, '\n', tmp->buf + rc - p))) {
+ p++;
+ tmp->n_lines++;
+ }
+ total_lines += tmp->n_lines;
+
+ /* Try to append to the previous buffer if there's enough free
+ * space
+ */
+ if (tmp->n_bytes + last->n_bytes < BUFSIZ) {
+ memcpy(&last->buf[last->n_bytes], tmp->buf, tmp->n_bytes);
+ last->n_bytes += tmp->n_bytes;
+ last->n_lines += tmp->n_lines;
+ } else {
+ last = last->next = tmp;
+ if (total_lines - first->n_lines > n_lines) {
+ tmp = first;
+ total_lines -= first->n_lines;
+ first = first->next;
+ } else
+ tmp = emalloc(sizeof(struct line_buf));
+ }
+ }
+
+ free(tmp);
+
+ if (rc < 0) {
+ fprintf(stderr, "Error: Could not read from %s\n", pretty_name(f->name));
+ goto out;
+ }
+
+ if (last->n_bytes == 0)
+ goto out;
+
+ /* Count incomplete lines */
+ if (last->buf[last->n_bytes - 1] != '\n') {
+ last->n_lines++;
+ total_lines++;
+ }
+
+ /* Skip unneeded buffers */
+ for (tmp = first; total_lines - tmp->n_lines > n_lines; tmp = tmp->next)
+ total_lines -= tmp->n_lines;
+
+ p = tmp->buf;
+
+ if (total_lines > n_lines) {
+ unsigned long j;
+ for (j = total_lines - n_lines; j; --j) {
+ p = memchr(p, '\n', tmp->buf + tmp->n_bytes - p);
+ p++;
+ }
+ }
+
+ if ((rc = write(STDOUT_FILENO, p, tmp->buf + tmp->n_bytes - p)) <= 0) {
+ /* e.g. when writing to a pipe which gets closed */
+ fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno));
+ goto out;
+ }
+
+ for (tmp = tmp->next; tmp; tmp = tmp->next)
+ if ((rc = write(STDOUT_FILENO, tmp->buf, tmp->n_bytes)) <= 0) {
fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno));
- break;
+ goto out;
}
+
+ rc = 0;
+out:
+ while (first) {
+ tmp = first->next;
+ free(first);
+ first = tmp;
+ }
+
+ return rc;
+}
+
+static int tail_pipe_bytes(struct file_struct *f, unsigned long n_bytes)
+{
+ struct char_buf *first, *last, *tmp;
+ int rc;
+ unsigned long total_bytes = 0;
+ unsigned long i = 0; /* Index into buffer */
+
+ if (from_begin)
+ return tail_pipe_from_begin(f, n_bytes, M_BYTES);
+
+ /* XXX: Needed? */
+ if (n_bytes == 0)
+ return 0;
+
+ first = last = emalloc(sizeof(struct char_buf));
+ first->n_bytes = 0;
+ first->next = NULL;
+ tmp = emalloc(sizeof(struct char_buf));
+
+ while(1) {
+ if ((rc = read(f->fd, tmp->buf, BUFSIZ)) <= 0) {
+ if (rc < 0 && (errno == EINTR || errno == EAGAIN))
+ continue;
+ else
+ break;
+ }
+ total_bytes += rc;
+ tmp->n_bytes = rc;
+ tmp->next = NULL;
+
+ /* Try to append to the previous buffer if there's enough free
+ * space
+ */
+ if (tmp->n_bytes + last->n_bytes < BUFSIZ) {
+ memcpy(&last->buf[last->n_bytes], tmp->buf, tmp->n_bytes);
+ last->n_bytes += tmp->n_bytes;
+ } else {
+ last = last->next = tmp;
+ if (total_bytes - first->n_bytes > n_bytes) {
+ tmp = first;
+ total_bytes -= first->n_bytes;
+ first = first->next;
+ } else
+ tmp = emalloc(sizeof(struct char_buf));
+ }
+ }
+
+ free(tmp);
+
+ if (rc < 0) {
+ fprintf(stderr, "Error: Could not read from %s\n", pretty_name(f->name));
+ goto out;
+ }
+
+ /* Skip unneeded buffers */
+ for (tmp = first; total_bytes - tmp->n_bytes > n_bytes; tmp = tmp->next)
+ total_bytes -= tmp->n_bytes;
+
+ if (total_bytes > n_bytes)
+ i = total_bytes - n_bytes;
+
+ if ((rc = write(STDOUT_FILENO, &tmp->buf[i], tmp->n_bytes - i)) <= 0) {
+ /* e.g. when writing to a pipe which gets closed */
+ fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno));
+ goto out;
+ }
+
+ for (tmp = tmp->next; tmp; tmp = tmp->next)
+ if ((rc = write(STDOUT_FILENO, tmp->buf, tmp->n_bytes)) <= 0) {
+ fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno));
+ goto out;
+ }
+
+
+ rc = 0;
+out:
+ while (first) {
+ tmp = first->next;
+ free(first);
+ first = tmp;
}
- free(buf);
return rc;
}
@@ -288,8 +508,15 @@ static int tail_file(struct file_struct *f, unsigned long n_units, mode_t mode,
}
/* Cannot seek on these */
- if (IS_PIPELIKE(finfo.st_mode) || f->fd == STDIN_FILENO)
- return tail_pipe(f);
+ if (IS_PIPELIKE(finfo.st_mode) || f->fd == STDIN_FILENO) {
+ if (verbose)
+ write_header(f->name);
+
+ if (mode == M_LINES)
+ return tail_pipe_lines(f, n_units);
+ else
+ return tail_pipe_bytes(f, n_units);
+ }
f->size = finfo.st_size;
f->blksize = finfo.st_blksize; /* TODO: Can this value be 0? */
@@ -305,14 +532,14 @@ static int tail_file(struct file_struct *f, unsigned long n_units, mode_t mode,
return -1;
}
- if (verbose)
- write_header(f->name);
-
if (lseek(f->fd, offset, SEEK_SET) == (off_t) -1) {
fprintf(stderr, "Error: Could not seek in file '%s' (%s)\n", f->name, strerror(errno));
return -1;
}
+ if (verbose)
+ write_header(f->name);
+
buf = emalloc(f->blksize);
while ((bytes_read = read(f->fd, buf, f->blksize)) > 0)
diff --git a/inotail.h b/inotail.h
index ff77b26..ea680cb 100644
--- a/inotail.h
+++ b/inotail.h
@@ -10,8 +10,7 @@
#include <sys/types.h>
#include "inotify.h"
-/* Number of items to tail. */
-#define DEFAULT_N_LINES 10
+#define DEFAULT_N_LINES 10 /* Number of items to tail. */
#define DEFAULT_BUFFER_SIZE 4096
/* inotify event buffer length for one file */
@@ -30,6 +29,21 @@ struct file_struct {
int i_watch; /* Inotify watch associated with file_struct */
};
+/* struct for linked list of buffers/lines in tail_pipe_lines */
+struct line_buf {
+ char buf[BUFSIZ];
+ size_t n_lines;
+ size_t n_bytes;
+ struct line_buf *next;
+};
+
+/* struct for linked list of byte buffers in tail_pipe_bytes */
+struct char_buf {
+ char buf[BUFSIZ];
+ size_t n_bytes;
+ struct char_buf *next;
+};
+
#define IS_PIPELIKE(mode) \
(S_ISFIFO(mode) || S_ISSOCK(mode))