22
33/*
44 * Copyright 2015 Didier Fetter
5+ * Copyright 2017 Alberto González Palomo https://sentido-labs.com
56 *
67 * Licensed under the Apache License, Version 2.0 (the "License");
78 * you may not use this file except in compliance with the License.
@@ -136,7 +137,7 @@ private void processModifications() throws IOException {
136137 logger .trace ("Same signature size and value : file is the same" );
137138 continue ;
138139 } else if (oldState .getSignatureLength () < state .getSignatureLength ()){
139- long signature = FileSigner .computeSignature (state .getRandomAccessFile (), oldState .getSignatureLength ());
140+ long signature = FileSigner .computeSignature (state .getLogFile (), oldState .getSignatureLength ());
140141 if (signature == oldState .getSignature ()) {
141142 state .setOldFileState (oldState );
142143 logger .trace ("Same signature : file is the same" );
@@ -163,7 +164,7 @@ private void processModifications() throws IOException {
163164 logger .trace ("Same signature size and value : file is the same" );
164165 break ;
165166 } else if (otherState .getSignatureLength () < state .getSignatureLength ()){
166- long signature = FileSigner .computeSignature (state .getRandomAccessFile (), otherState .getSignatureLength ());
167+ long signature = FileSigner .computeSignature (state .getLogFile (), otherState .getSignatureLength ());
167168 if (signature == otherState .getSignature ()) {
168169 state .setOldFileState (otherState );
169170 logger .trace ("Same signature : file is the same" );
@@ -194,7 +195,7 @@ private void processModifications() throws IOException {
194195 logger .debug ("File " + state .getFile () + " has been replaced and not renamed, removing from watchMap" );
195196 }
196197 try {
197- oldState .getRandomAccessFile ().close ();
198+ oldState .getLogFile ().close ();
198199 } catch (Exception e ) {}
199200 oldWatchMap .remove (state .getFile ());
200201 }
@@ -223,12 +224,33 @@ private void processModifications() throws IOException {
223224 removeMarkedFilesFromWatchMap ();
224225 }
225226
227+ // This filter will accept anything that is not a directory,
228+ // including named pipes (FIFOs), sockets and device files.
229+ // The standard org.apache.commons.io.filefilter.FileFileFilter excludes
230+ // them even if their documentation says
231+ // "This filter accepts Files that are files (not directories)."
232+ protected class FileFileFilter implements IOFileFilter
233+ {
234+ @ Override
235+ public boolean accept (File file ) {
236+ return !file .isDirectory ();
237+ }
238+
239+ @ Override
240+ public boolean accept (File dir , String name ) {
241+ return accept (new File (dir , name ));
242+ }
243+ }
244+ protected IOFileFilter fileFileFilter () {
245+ return new FileFileFilter ();
246+ }
247+
226248 private void addSingleFile (String fileToWatch , Event fields , long deadTime , Multiline multiline , Filter filter ) throws Exception {
227249 logger .info ("Watching file : " + new File (fileToWatch ).getCanonicalPath ());
228250 String directory = FilenameUtils .getFullPath (fileToWatch );
229251 String fileName = FilenameUtils .getName (fileToWatch );
230252 IOFileFilter fileFilter = FileFilterUtils .and (
231- FileFilterUtils . fileFileFilter (),
253+ fileFileFilter (),
232254 FileFilterUtils .nameFileFilter (fileName ),
233255 new LastModifiedFileFilter (deadTime ));
234256 initializeWatchMap (new File (directory ), fileFilter , fields , multiline , filter );
@@ -240,7 +262,7 @@ private void addWildCardFiles(String filesToWatch, Event fields, long deadTime,
240262 String wildcard = FilenameUtils .getName (filesToWatch );
241263 logger .trace ("Directory : " + new File (directory ).getCanonicalPath () + ", wildcard : " + wildcard );
242264 IOFileFilter fileFilter = FileFilterUtils .and (
243- FileFilterUtils . fileFileFilter (),
265+ fileFileFilter (),
244266 new WildcardFileFilter (wildcard ),
245267 new LastModifiedFileFilter (deadTime ));
246268 initializeWatchMap (new File (directory ), fileFilter , fields , multiline , filter );
@@ -273,7 +295,7 @@ private void addFileToWatchMap(Map<File,FileState> map, File file, Event fields,
273295 state .setFields (fields );
274296 int signatureLength = (int ) (state .getSize () > maxSignatureLength ? maxSignatureLength : state .getSize ());
275297 state .setSignatureLength (signatureLength );
276- long signature = FileSigner .computeSignature (state .getRandomAccessFile (), signatureLength );
298+ long signature = FileSigner .computeSignature (state .getLogFile (), signatureLength );
277299 state .setSignature (signature );
278300 logger .trace ("Setting signature of size : " + signatureLength + " on file : " + file + " : " + signature );
279301 state .setMultiline (multiline );
@@ -331,7 +353,7 @@ private void removeMarkedFilesFromWatchMap() throws IOException {
331353 List <File > markedList = null ;
332354 for (File file : oldWatchMap .keySet ()) {
333355 FileState state = oldWatchMap .get (file );
334- if (state .getRandomAccessFile () == null ) {
356+ if (state .getLogFile () == null ) {
335357 state .setDeleted ();
336358 }
337359 if (state .isDeleted ()) {
@@ -342,7 +364,7 @@ private void removeMarkedFilesFromWatchMap() throws IOException {
342364 markedList .add (file );
343365 }
344366 try {
345- state .getRandomAccessFile ().close ();
367+ state .getLogFile ().close ();
346368 } catch (Exception e ) {}
347369 }
348370 }
@@ -358,7 +380,7 @@ public void close() throws IOException {
358380 logger .debug ("Closing all files" );
359381 for (File file : oldWatchMap .keySet ()) {
360382 FileState state = oldWatchMap .get (file );
361- state .getRandomAccessFile ().close ();
383+ state .getLogFile ().close ();
362384 }
363385 }
364386
0 commit comments