diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-14 15:16:09 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-14 15:16:09 +0100 |
commit | 20cb6bed0de04f8496bebd3015aa83e4b7da351b (patch) | |
tree | 689ea401d735e1e6fae864e61c26ee211f5fb4f1 | |
parent | 17e4a56f94c0f10835492bf2bc770b0df8fadf6f (diff) |
inotail.c: Handle file truncation in follow mode
-rw-r--r-- | inotail.c | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -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) { |