From 2bc261d8289552d21ac7ae652c95d121f5b9561c Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 21 May 2007 17:52:48 +0200 Subject: Use optimal buffer size depending on the filesystem containing the file The st_blksize attribute of struct stat conatains the blocksize for filesystem I/O (see stat(2)) which is the optimal size for reading/writing chunks of data. Based on a patch by Folkert van Heusden --- inotail.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'inotail.h') diff --git a/inotail.h b/inotail.h index 4c57867..d1874b5 100644 --- a/inotail.h +++ b/inotail.h @@ -7,6 +7,8 @@ #ifndef _INOTAIL_H #define _INOTAIL_H +#include + /* Number of items to tail. */ #define DEFAULT_N_LINES 10 @@ -18,6 +20,7 @@ struct file_struct { char *name; /* Name of file (or '-' for stdin) */ int fd; /* File descriptor (or -1 if file is not open */ off_t st_size; /* File size */ + blksize_t st_blksize; /* Blocksize for filesystem I/O */ unsigned ignore; /* Whether to ignore the file in further processing */ int i_watch; /* Inotify watch associated with file_struct */ }; -- cgit v1.2.3-54-g00ecf From cbb5d0cebbe344ef13aa3f4baf3e8af2b31e7c75 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 20 Jun 2007 14:47:53 +0200 Subject: inotail.h: Update copyright --- inotail.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inotail.h') diff --git a/inotail.h b/inotail.h index b215b43..f7c1406 100644 --- a/inotail.h +++ b/inotail.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2006, Tobias Klauser + * Copyright (C) 2005-2007, Tobias Klauser * * Licensed under the terms of the GNU General Public License; version 2 or later. */ -- cgit v1.2.3-54-g00ecf From fb63f7543d0e5a79bcd9cc1b4650360267314703 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 7 Sep 2007 12:43:56 +0200 Subject: Rename file_struct members The rationale behind this is to only use the 'st_' prefix for values gotten by calls to fstat() --- inotail.c | 40 ++++++++++++++++++++-------------------- inotail.h | 4 ++-- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'inotail.h') diff --git a/inotail.c b/inotail.c index 993bd68..920d5a5 100644 --- a/inotail.c +++ b/inotail.c @@ -94,8 +94,8 @@ static void usage(const int status) static inline void setup_file(struct file_struct *f) { f->fd = f->i_watch = -1; - f->st_size = 0; - f->st_blksize = DEFAULT_BUFFER_SIZE; + f->size = 0; + f->blksize = DEFAULT_BUFFER_SIZE; f->ignore = 0; } @@ -130,14 +130,14 @@ static void write_header(char *filename) static off_t lines_to_offset_from_end(struct file_struct *f, unsigned long n_lines) { - off_t offset = f->st_size; - char *buf = emalloc(f->st_blksize); + off_t offset = f->size; + char *buf = emalloc(f->blksize); n_lines++; /* We also count the last \n */ while (offset > 0 && n_lines > 0) { int i; - ssize_t rc, block_size = f->st_blksize; /* Size of the current block we're reading */ + ssize_t rc, block_size = f->blksize; /* Size of the current block we're reading */ if (offset < block_size) block_size = offset; @@ -182,11 +182,11 @@ static off_t lines_to_offset_from_begin(struct file_struct *f, unsigned long n_l return 0; n_lines--; - buf = emalloc(f->st_blksize); + buf = emalloc(f->blksize); - while (offset <= f->st_size && n_lines > 0) { + while (offset <= f->size && n_lines > 0) { int i; - ssize_t rc, block_size = f->st_blksize; + ssize_t rc, block_size = f->blksize; 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)); @@ -234,8 +234,8 @@ static off_t bytes_to_offset(struct file_struct *f, unsigned long n_bytes) if (from_begin) { if (n_bytes > 0) offset = (off_t) n_bytes - 1; - } else if ((off_t) n_bytes < f->st_size) - offset = f->st_size - (off_t) n_bytes; + } else if ((off_t) n_bytes < f->size) + offset = f->size - (off_t) n_bytes; return offset; } @@ -243,13 +243,13 @@ 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 = emalloc(f->st_blksize); + char *buf = emalloc(f->blksize); if (verbose) write_header(f->name); /* We will just tail everything here */ - while ((rc = read(f->fd, buf, f->st_blksize)) > 0) { + while ((rc = read(f->fd, buf, f->blksize)) > 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)); @@ -296,8 +296,8 @@ 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); - f->st_size = finfo.st_size; - f->st_blksize = finfo.st_blksize; /* TODO: Can this value be 0? */ + f->size = finfo.st_size; + f->blksize = finfo.st_blksize; /* TODO: Can this value be 0? */ if (mode == M_LINES) offset = lines_to_offset(f, n_units); @@ -318,9 +318,9 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch return -1; } - buf = emalloc(f->st_blksize); + buf = emalloc(f->blksize); - while ((bytes_read = read(f->fd, buf, f->st_blksize)) > 0) + while ((bytes_read = read(f->fd, buf, f->blksize)) > 0) write(STDOUT_FILENO, buf, (size_t) bytes_read); if (!forever) { @@ -348,15 +348,15 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct * write_header(f->name); /* Seek to old file size */ - if (lseek(f->fd, f->st_size, SEEK_SET) == (off_t) -1) { + if (lseek(f->fd, f->size, SEEK_SET) == (off_t) -1) { fprintf(stderr, "Error: Could not seek in file '%s' (%s)\n", f->name, strerror(errno)); ret = -1; goto ignore; } - fbuf = emalloc(f->st_blksize); + fbuf = emalloc(f->blksize); - while ((rc = read(f->fd, fbuf, f->st_blksize)) != 0) + while ((rc = read(f->fd, fbuf, f->blksize)) != 0) write(STDOUT_FILENO, fbuf, (size_t) rc); if (fstat(f->fd, &finfo) < 0) { @@ -366,7 +366,7 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct * goto ignore; } - f->st_size = finfo.st_size; + f->size = finfo.st_size; free(fbuf); return ret; diff --git a/inotail.h b/inotail.h index f7c1406..c95c4f8 100644 --- a/inotail.h +++ b/inotail.h @@ -19,8 +19,8 @@ enum { M_LINES, M_BYTES }; struct file_struct { char *name; /* Name of file (or '-' for stdin) */ int fd; /* File descriptor (or -1 if file is not open */ - off_t st_size; /* File size */ - blksize_t st_blksize; /* Blocksize for filesystem I/O */ + off_t size; /* File size */ + blksize_t blksize; /* Blocksize for filesystem I/O */ unsigned ignore; /* Whether to ignore the file in further processing */ int i_watch; /* Inotify watch associated with file_struct */ }; -- cgit v1.2.3-54-g00ecf