[chronojump] Safer FileReadable method



commit 37d554a4ae1174016d50eaea7ee2d03c12ec2134
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sat Aug 6 16:33:08 2016 +0200

    Safer FileReadable method

 src/encoderRProc.cs |   20 +++++---------------
 src/util.cs         |   14 ++++++++++++++
 2 files changed, 19 insertions(+), 15 deletions(-)
---
diff --git a/src/encoderRProc.cs b/src/encoderRProc.cs
index 1fabd22..281c736 100644
--- a/src/encoderRProc.cs
+++ b/src/encoderRProc.cs
@@ -465,9 +465,9 @@ public class EncoderRProcAnalyze : EncoderRProc
 
 
                        if(outputFileCheck2 == "")
-                               while ( ! ( fileWritten(outputFileCheck) || CancelRScript) );
+                               while ( ! ( Util.FileReadable(outputFileCheck) || CancelRScript) );
                        else
-                               while ( ! ( (fileWritten(outputFileCheck) && fileWritten(outputFileCheck2)) 
|| CancelRScript ) );
+                               while ( ! ( (Util.FileReadable(outputFileCheck) && 
Util.FileReadable(outputFileCheck2)) || CancelRScript ) );
 
                        //copy export from temp file to the file that user has selected
                        if(es.Ep.Analysis == "exportCSV" && ! CancelRScript)
@@ -530,9 +530,9 @@ public class EncoderRProcAnalyze : EncoderRProc
 
                LogB.Debug("waiting files");
                if(outputFileCheck2 == "")
-                       while ( ! ( fileWritten(outputFileCheck) || CancelRScript) );
+                       while ( ! ( Util.FileReadable(outputFileCheck) || CancelRScript) );
                else
-                       while ( ! ( (fileWritten(outputFileCheck) && fileWritten(outputFileCheck2)) || 
CancelRScript ) );
+                       while ( ! ( (Util.FileReadable(outputFileCheck) && 
Util.FileReadable(outputFileCheck2)) || CancelRScript ) );
                        
                //copy export from temp file to the file that user has selected
                if(es.Ep.Analysis == "exportCSV" && ! CancelRScript)
@@ -583,17 +583,7 @@ public class EncoderRProcAnalyze : EncoderRProc
                LogB.Information("Deleting... " + filename);
                if (File.Exists(filename))
                        File.Delete(filename);
-       }
-
-       private bool fileWritten(string filename)
-       {
-               if(File.Exists(filename)) {
-                       FileInfo fi = new FileInfo(filename);
-                       if(fi.Length > 0)
-                               return true;
-               }
-
-               return false;
+               LogB.Information("Deleted " + filename);
        }
 
 }
diff --git a/src/util.cs b/src/util.cs
index bd072cd..3b51ff8 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -1062,6 +1062,20 @@ public class Util
        public static bool FileExists(string fileName){
                return File.Exists(fileName);
        }
+       
+       public static bool FileReadable(string filename)
+       {
+               //http://stackoverflow.com/a/17318735
+               try {
+                       File.Open(filename, FileMode.Open, FileAccess.Read).Dispose();
+                       //LogB.Information("success at Util.FileReadable: " + filename);
+                       return true;
+               }
+               catch (IOException) {
+                       System.Threading.Thread.Sleep(10);
+                       return false;
+               }
+       }
 
        //not recommended, better use below method. Better for bigger files
        public static string ReadFile(string fileName, bool removeEOL)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]