From 20cb6bed0de04f8496bebd3015aa83e4b7da351b Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 14 Jan 2008 15:16:09 +0100 Subject: inotail.c: Handle file truncation in follow mode --- inotail.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'inotail.c') diff --git a/inotail.c b/inotail.c index 8f6fd8d..651c104 100644 --- a/inotail.c +++ b/inotail.c @@ -340,12 +340,23 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct * if (inev->mask & IN_MODIFY) { char *fbuf; - ssize_t rc; + ssize_t bytes_read; struct stat finfo; if (verbose) write_header(f->name); + if ((ret = fstat(f->fd, &finfo)) < 0) { + fprintf(stderr, "Error: Could not stat file '%s' (%s)\n", f->name, strerror(errno)); + goto ignore; + } + + /* Regular file got truncated */ + if (S_ISREG(finfo.st_mode) && finfo.st_size < f->size) { + fprintf(stderr, "File '%s' truncated\n", f->name); + f->size = finfo.st_size; + } + /* Seek to old file size */ if ((ret = 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)); @@ -354,17 +365,11 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct * fbuf = emalloc(f->blksize); - while ((rc = read(f->fd, fbuf, f->blksize)) != 0) - write(STDOUT_FILENO, fbuf, (size_t) rc); - - if ((ret = fstat(f->fd, &finfo)) < 0) { - fprintf(stderr, "Error: Could not stat file '%s' (%s)\n", f->name, strerror(errno)); - free(fbuf); - goto ignore; + while ((bytes_read = read(f->fd, fbuf, f->blksize)) != 0) { + write(STDOUT_FILENO, fbuf, (size_t) bytes_read); + f->size += bytes_read; } - f->size = finfo.st_size; - free(fbuf); return ret; } else if (inev->mask & IN_DELETE_SELF) { -- cgit v1.2.3-54-g00ecf