Changeset 1077 for trunk/CrypPluginBase


Ignore:
Timestamp:
Jan 13, 2010, 5:25:37 PM (12 years ago)
Author:
Matthäus Wander
Message:

CryptoolStream temp file handling:

  • removed static CryptoolStream.Dispose() which deletes all CT2 temp files (which leads to conflicts when two CT2 instances are running)
  • replaced by per-instance CryptoolStream.Dispose() and finalization which deletes its own temp file which the CryptoolStream instance has created (does not delete foreign temp files)
  • added FileAttributes.Temporary to CryptoolStream temp files
  • changed temp directory to %HOME%\AppData\Local\CrypTool2\Temp
  • new binaries

VS project stuff:

  • fixed release output path of Soap+Webservice
Location:
trunk/CrypPluginBase/IO
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPluginBase/IO/CryptoolStream.cs

    r976 r1077  
    2727        private FileStream fileStream;
    2828        private bool isReadOnly;
     29        private bool isDisposed;
    2930
    3031        public string FileName
     
    8081        }
    8182
     83        ~CryptoolStream()
     84        {
     85            Dispose(false);
     86        }
     87
     88        public new void Dispose()
     89        {
     90            if (!isDisposed)
     91            {
     92                Dispose(true);
     93                GC.SuppressFinalize(this);
     94            }
     95        }
     96
     97        protected override void Dispose(bool publicDispose)
     98        {
     99            if (!isDisposed)
     100            {
     101                // public dispose code only
     102                if (publicDispose)
     103                {
     104                    base.Dispose(publicDispose);
     105                }
     106
     107                // finalization and public dispose code
     108                isDisposed = true;
     109
     110                File.Delete(fileName);
     111            }
     112        }
     113
    82114        public override void SetLength(long value)
    83115        {
     
    97129          if (Bytes == null) throw new ArgumentException("Bytes");
    98130
    99           try
    100           {
    101             if (!Directory.Exists(DirectoryHelper.DirectoryLocalTemp)) Directory.CreateDirectory(DirectoryHelper.DirectoryLocalTemp);
    102 
    103             this.fileName = DirectoryHelper.GetNewTempFilePath(TempFileExtension);
    104             this.fileStream = new FileStream(this.fileName, FileMode.CreateNew);
    105 
    106             fileStream.Write(Bytes, 0, Bytes.Length);
    107 
    108             fileStream.Flush();
    109             fileStream.Close();
    110 
    111             fileStream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    112             length = fileStream.Length;
    113             position = fileStream.Position;
    114             isReadOnly = true;
    115           }
    116           catch
    117           { } // FIXME: either handle or don't catch
    118         }
    119 
     131          if (!Directory.Exists(DirectoryHelper.DirectoryLocalTemp)) Directory.CreateDirectory(DirectoryHelper.DirectoryLocalTemp);
     132
     133          this.fileName = DirectoryHelper.GetNewTempFilePath(TempFileExtension);
     134          this.fileStream = new FileStream(this.fileName, FileMode.CreateNew);
     135
     136          fileStream.Write(Bytes, 0, Bytes.Length);
     137
     138          fileStream.Flush();
     139          fileStream.Close();
     140
     141          fileStream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
     142          length = fileStream.Length;
     143          position = fileStream.Position;
     144          isReadOnly = true;
     145        }
    120146
    121147        [Obsolete("pluginCaption is not used anymore, use OpenRead(byte[]) instead")]
     
    172198                this.fileName = DirectoryHelper.GetNewTempFilePath(TempFileExtension);
    173199                this.fileStream = new FileStream(this.fileName, FileMode.CreateNew);
     200                File.SetAttributes(fileName, File.GetAttributes(fileName) | FileAttributes.Temporary);
    174201            }
    175202            isReadOnly = false;
     
    218245        public override void Flush()
    219246        {
    220           try
    221           {
    222             if (fileStream != null) fileStream.Flush();
    223           }
    224           catch { }
     247          if (fileStream != null) fileStream.Flush();
    225248        }
    226249
     
    231254        }
    232255
    233         public static void CleanTempFiles()
    234         {
    235             foreach (FileInfo fInfo in new DirectoryInfo(DirectoryHelper.DirectoryLocalTemp).GetFiles("*." + TempFileExtension))
    236             {
    237               fInfo.Delete();
    238             }
    239         }
    240 
    241         public static void Dispose()
    242         {
    243           CleanTempFiles();
    244           //Directory.Delete(DirectoryHelper.DirectoryLocalTemp);
    245         }
    246 
    247256    }
    248257}
  • trunk/CrypPluginBase/IO/DirectoryHelper.cs

    r975 r1077  
    2727        private const string crypTool2 = "CrypTool2";
    2828        private const string crypPlugins = "CrypPlugins";
    29         private const string tempFiles = "Temp Files";
     29        private const string tempFiles = "Temp";
    3030
    3131        public static string DirectoryCrypPlugins
     
    5656        public static string GetNewTempFilePath()
    5757        {
    58             string filePath;
    59 
    60             do
    61             {
    62                 filePath = Path.Combine(DirectoryHelper.DirectoryLocalTemp, Guid.NewGuid().ToString());
    63             } while (File.Exists(filePath)); // sanity check for GUID collision
    64 
    65             return filePath;
     58            return GetNewTempFilePath(string.Empty);
    6659        }
    6760
     61        /// <summary>
     62        /// Returns path to a unique file name which can be used for temporary files.
     63        /// The generated file name contains a Guid and does not use Path.GetTempFileName().
     64        /// </summary>
     65        /// <param name="extension"></param>
     66        /// <returns></returns>
    6867        public static string GetNewTempFilePath(string extension)
    6968        {
    70             // CHECK: use Path.GetTempFileName() instead?
    7169            string filePath;
    7270            do
    7371            {
    74                 filePath = Path.Combine(DirectoryHelper.DirectoryLocalTemp, Guid.NewGuid().ToString() + "." + extension);
     72                if (extension.Length > 0)
     73                {
     74                    filePath = Path.Combine(DirectoryHelper.DirectoryLocalTemp, Guid.NewGuid().ToString() + "." + extension);
     75                }
     76                else
     77                {
     78                    filePath = Path.Combine(DirectoryHelper.DirectoryLocalTemp, Guid.NewGuid().ToString());
     79                }
    7580            } while (File.Exists(filePath)); // sanity check for GUID collision
    7681
Note: See TracChangeset for help on using the changeset viewer.