public void TestDerivedFileName() { string path; using (DisposingList l = new DisposingList()) { TempFile ftarget = new TempFile(); ftarget.Delete(); l.Add(ftarget); TempFile fbigone = TempFile.Attach(String.Format("{0}.~{1:x4}", ftarget.TempPath, 0x10008 - 25)); fbigone.Create().Dispose(); Assert.IsTrue(fbigone.Exists); l.Add(fbigone); path = ftarget.TempPath; string tmpName; Dictionary <string, object> names = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); for (int i = 0; i < 25; i++) { Stream s = ReplaceFile.CreateDerivedFile(ftarget.TempPath, out tmpName); l.Add(TempFile.Attach(tmpName)); l.Add(s); names.Add(tmpName, null); } fbigone.Delete(); Assert.AreEqual(25, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length); } Assert.AreEqual(0, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length); }
public void Dispose_Then_ItemsShouldBeDisposedInExpectedOrder() { var expectedOrder = new[] { 1, 2 }; var testee = new DisposingList <DisposeAction>(); var disposeOrder = new List <int>(); testee.Add(new DisposeAction(() => disposeOrder.Add(1))); testee.Add(new DisposeAction(() => disposeOrder.Add(2))); testee.Dispose(); disposeOrder.Should().Equal(expectedOrder); }
public ContentStorage(string directory, bool asReadonly) { _disposables = new DisposingList(); _readonly = asReadonly; _storageDir = directory; _dataDir = Path.Combine(directory, "content"); if (!_readonly && !Directory.Exists(_dataDir)) { Directory.CreateDirectory(_dataDir); } _indexDir = Path.Combine(directory, "index"); BPlusTree <string, ContentRecord> .Options options = new BPlusTree <string, ContentRecord> .Options( PrimitiveSerializer.Instance, new ProtoSerializer <ContentRecord, ContentRecord.Builder>() ); options.CacheKeepAliveMaximumHistory = 1000; options.CacheKeepAliveMinimumHistory = 100; options.CacheKeepAliveTimeout = int.MaxValue; options.CachePolicy = asReadonly ? CachePolicy.All : CachePolicy.Recent; options.CreateFile = asReadonly ? CreatePolicy.Never : CreatePolicy.IfNeeded; options.FileName = Path.Combine(directory, "content.index"); options.FileBlockSize = 0x02000; //8kb options.ReadOnly = asReadonly; options.CallLevelLock = asReadonly ? (ILockStrategy) new IgnoreLocking() : new SimpleReadWriteLocking(); options.LockingFactory = asReadonly ? (ILockFactory) new LockFactory <IgnoreLocking>() : new LockFactory <SimpleReadWriteLocking>(); options.CalcBTreeOrder(64, 256); _index = new BPlusTree <string, ContentRecord>(options); _disposables.Add(_index); _index.EnableCount(); }
public void ListClearsOnDisposal() { DisposingList <DisposalbleItem> test = new DisposingList <DisposalbleItem>(); const int MAX = 10; for (int i = 0; i < MAX; i++) { test.Add(new DisposalbleItem()); } // We pass the references off somewhere else so we can keep track of them. List <DisposalbleItem> check = new List <DisposalbleItem>(); for (int i = 0; i < test.Count; i++) { check.Add(test[i]); Assert.IsFalse(test[i].IsDisposed); } // Now dispose it. test.Dispose(); Assert.AreEqual(0, test.Count, "There should be no more items in the list!"); for (int i = 0; i < check.Count; i++) { Assert.IsTrue(check[i].IsDisposed, "The item at index {0} should be disposed!", i); } }
public void TestFileStreamFactoryCreateABunch() { using (TempFile tempFile = new TempFile()) { FileStreamFactory factory = new FileStreamFactory(tempFile.TempPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); using (DisposingList <Stream> open = new DisposingList <Stream>()) { for (int i = 0; i < 50; i++) { open.Add(factory.Create()); } } } }
public void TestGeneric() { disposeOrder.Clear(); DisposingList <DisposeInOrder> list = new DisposingList <DisposeInOrder>(); DisposeInOrder a = new DisposeInOrder(); DisposeInOrder b = new DisposeInOrder(); list.Add(a); list.Add(b); list.Add(null); list.Dispose(); //Removed from list? Assert.AreEqual(0, list.Count); //All were disposed? Assert.AreEqual(2, disposeOrder.Count); //Disposed in reverse order of creation? Assert.IsTrue(object.ReferenceEquals(b, disposeOrder[0])); Assert.IsTrue(object.ReferenceEquals(a, disposeOrder[1])); Assert.AreEqual(2, new DisposingList <DisposeInOrder>(new DisposeInOrder[] { a, b }).Count); Assert.AreEqual(0, new DisposingList <DisposeInOrder>(5).Count); }
public void TestGeneric() { disposeOrder.Clear(); DisposingList<DisposeInOrder> list = new DisposingList<DisposeInOrder>(); DisposeInOrder a = new DisposeInOrder(); DisposeInOrder b = new DisposeInOrder(); list.Add(a); list.Add(b); list.Add(null); list.Dispose(); //Removed from list? Assert.AreEqual(0, list.Count); //All were disposed? Assert.AreEqual(2, disposeOrder.Count); //Disposed in reverse order of creation? Assert.IsTrue(object.ReferenceEquals(b, disposeOrder[0])); Assert.IsTrue(object.ReferenceEquals(a, disposeOrder[1])); Assert.AreEqual(2, new DisposingList<DisposeInOrder>(new DisposeInOrder[] { a, b }).Count); Assert.AreEqual(0, new DisposingList<DisposeInOrder>(5).Count); }
public void TestFileStreamFactoryCreateABunch() { using (TempFile tempFile = new TempFile()) { FileStreamFactory factory = new FileStreamFactory(tempFile.TempPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); using (DisposingList<Stream> open = new DisposingList<Stream>()) { for( int i = 0; i < 50; i++ ) open.Add(factory.Create()); } } }
public void RunCsc(string output, string win32res, string header, string cscOptions) { using (DisposingList <TempFile> files = new DisposingList <TempFile>()) { TempFile asm = TempFile.FromExtension(".dll"); files.Add(asm); TempFile assemblyInfo = TempFile.FromExtension(".cs"); files.Add(assemblyInfo); assemblyInfo.WriteAllText(VersionInfo.AssemblyInfoCode); TempFile installer = TempFile.FromExtension(".cs"); files.Add(installer); installer.WriteAllText(CreateInstaller()); TempFile constants = TempFile.FromExtension(".cs"); files.Add(constants); constants.WriteAllText(CreateConstants(header)); CSharpCodeProvider csc = new CSharpCodeProvider(); CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; args.IncludeDebugInformation = false; args.OutputAssembly = asm.TempPath; args.Win32Resource = win32res; args.CompilerOptions = cscOptions; args.ReferencedAssemblies.Add("System.dll"); args.ReferencedAssemblies.Add("System.Configuration.Install.dll"); CompilerResults results = csc.CompileAssemblyFromFile(args, assemblyInfo.TempPath, installer.TempPath, constants.TempPath); StringWriter sw = new StringWriter(); foreach (CompilerError ce in results.Errors) { if (ce.IsWarning) { continue; } String line = String.Format("{0}({1},{2}): error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText); Trace.WriteLine(line); sw.WriteLine(line); } string errorText = sw.ToString(); try { if (errorText.Length > 0) { throw new ApplicationException(errorText); } if (!asm.Exists) { throw new FileNotFoundException(new FileNotFoundException().Message, asm.TempPath); } } catch { GC.KeepAlive(files); throw; } File.Copy(asm.TempPath, output, true); } }
public void ReadFrom(string versionInfo) { StringWriter asmInfo = new StringWriter(); asmInfo.WriteLine("using System;"); asmInfo.WriteLine(); using (DisposingList disposable = new DisposingList()) { if (versionInfo.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) { string dir = Path.GetDirectoryName(versionInfo); XmlLightDocument proj = new XmlLightDocument(File.ReadAllText(versionInfo)); foreach (XmlLightElement xref in proj.Select("/Project/ItemGroup/Compile")) { if (!xref.Attributes.ContainsKey("Include") || !xref.Attributes["Include"].EndsWith("AssemblyInfo.cs", StringComparison.OrdinalIgnoreCase)) { continue; } string include = xref.Attributes["Include"]; versionInfo = Path.Combine(dir, include); break; } if (versionInfo.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) { throw new ApplicationException("Unable to locate AssemblyInfo.cs"); } } if (versionInfo.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) { _assemblyInfo = File.ReadAllText(versionInfo); TempFile dll = TempFile.FromExtension(".dll"); disposable.Add(dll); dll.Delete(); CSharpCodeProvider csc = new CSharpCodeProvider(); CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; args.IncludeDebugInformation = false; args.OutputAssembly = dll.TempPath; args.ReferencedAssemblies.Add("System.dll"); CompilerResults results = csc.CompileAssemblyFromFile(args, versionInfo); StringWriter sw = new StringWriter(); foreach (CompilerError ce in results.Errors) { if (ce.IsWarning) { continue; } String line = String.Format("{0}({1},{2}): error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText); Trace.WriteLine(line); sw.WriteLine(line); } string errorText = sw.ToString(); if (errorText.Length > 0) { throw new ApplicationException(errorText); } versionInfo = dll.TempPath; } if (!File.Exists(versionInfo) || (!versionInfo.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !versionInfo.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))) { throw new ApplicationException("Expected an existing dll: " + versionInfo); } try { Regex truefalse = new Regex(@"(?<=[^\w_\.])(?:True)|(?:False)(?=[^\w_\.])"); _fileVersion = FileVersionInfo.GetVersionInfo(versionInfo); if (_assemblyInfo == null) { Assembly asm = Assembly.ReflectionOnlyLoad(File.ReadAllBytes(versionInfo)); IList <CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes(asm); foreach (CustomAttributeData data in attrs) { if (!data.ToString().StartsWith("[System.Runtime.CompilerServices.")) { string attribute = data.ToString().Trim(); if (attribute[0] != '[') { continue; //unexpected... } attribute = attribute.Insert(1, "assembly: "); attribute = StringUtils.Transform(attribute, truefalse, delegate(Match x) { return(x.Value.ToLower()); }); asmInfo.WriteLine(attribute); } } } } catch { } } _assemblyInfo = _assemblyInfo ?? asmInfo.ToString(); }
static int Main(string[] raw) { ArgumentList args = new ArgumentList(raw); using (DisposingList dispose = new DisposingList()) using (Log.AppStart(Environment.CommandLine)) { if (args.Contains("nologo") == false) { Console.WriteLine("XhtmlValidate.exe"); Console.WriteLine("Copyright 2010 by Roger Knapp, Licensed under the Apache License, Version 2.0"); Console.WriteLine(""); } if ((args.Unnamed.Count == 0) || args.Contains("?") || args.Contains("help")) return DoHelp(); try { FileList files = new FileList(); files.RecurseFolders = true; foreach (string spec in args.Unnamed) { Uri uri; if (Uri.TryCreate(spec, UriKind.Absolute, out uri) && !(uri.IsFile || uri.IsUnc)) { using(WebClient wc = new WebClient()) { TempFile tfile = new TempFile(); dispose.Add(tfile); wc.DownloadFile(uri, tfile.TempPath); files.Add(tfile.Info); } } else files.Add(spec); } if( files.Count == 0 ) return 1 + DoHelp(); XhtmlValidation validator = new XhtmlValidation(XhtmlDTDSpecification.Any); foreach (FileInfo f in files) validator.Validate(f.FullName); } catch (ApplicationException ae) { Log.Error(ae); Console.Error.WriteLine(); Console.Error.WriteLine(ae.Message); Environment.ExitCode = -1; } catch (Exception e) { Log.Error(e); Console.Error.WriteLine(); Console.Error.WriteLine(e.ToString()); Environment.ExitCode = -1; } } if (args.Contains("wait")) { Console.WriteLine(); Console.WriteLine("Press [Enter] to continue..."); Console.ReadLine(); } return Environment.ExitCode; }
static int Main(string[] raw) { ArgumentList args = new ArgumentList(raw); using (DisposingList dispose = new DisposingList()) using (Log.AppStart(Environment.CommandLine)) { if (args.Contains("nologo") == false) { Console.WriteLine("XhtmlValidate.exe"); Console.WriteLine("Copyright 2010 by Roger Knapp, Licensed under the Apache License, Version 2.0"); Console.WriteLine(""); } if ((args.Unnamed.Count == 0) || args.Contains("?") || args.Contains("help")) { return(DoHelp()); } try { FileList files = new FileList(); files.RecurseFolders = true; foreach (string spec in args.Unnamed) { Uri uri; if (Uri.TryCreate(spec, UriKind.Absolute, out uri) && !(uri.IsFile || uri.IsUnc)) { using (WebClient wc = new WebClient()) { TempFile tfile = new TempFile(); dispose.Add(tfile); wc.DownloadFile(uri, tfile.TempPath); files.Add(tfile.Info); } } else { files.Add(spec); } } if (files.Count == 0) { return(1 + DoHelp()); } XhtmlValidation validator = new XhtmlValidation(XhtmlDTDSpecification.Any); foreach (FileInfo f in files) { validator.Validate(f.FullName); } } catch (ApplicationException ae) { Log.Error(ae); Console.Error.WriteLine(); Console.Error.WriteLine(ae.Message); Environment.ExitCode = -1; } catch (Exception e) { Log.Error(e); Console.Error.WriteLine(); Console.Error.WriteLine(e.ToString()); Environment.ExitCode = -1; } } if (args.Contains("wait")) { Console.WriteLine(); Console.WriteLine("Press [Enter] to continue..."); Console.ReadLine(); } return(Environment.ExitCode); }
public void RunCsc(string output, string win32res, string header, string cscOptions) { using (DisposingList<TempFile> files = new DisposingList<TempFile>()) { TempFile asm = TempFile.FromExtension(".dll"); files.Add(asm); TempFile assemblyInfo = TempFile.FromExtension(".cs"); files.Add(assemblyInfo); assemblyInfo.WriteAllText(VersionInfo.AssemblyInfoCode); TempFile installer = TempFile.FromExtension(".cs"); files.Add(installer); installer.WriteAllText(CreateInstaller()); TempFile constants = TempFile.FromExtension(".cs"); files.Add(constants); constants.WriteAllText(CreateConstants(header)); CSharpCodeProvider csc = new CSharpCodeProvider(); CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; args.IncludeDebugInformation = false; args.OutputAssembly = asm.TempPath; args.Win32Resource = win32res; args.CompilerOptions = cscOptions; args.ReferencedAssemblies.Add("System.dll"); args.ReferencedAssemblies.Add("System.Configuration.Install.dll"); CompilerResults results = csc.CompileAssemblyFromFile(args, assemblyInfo.TempPath, installer.TempPath, constants.TempPath); StringWriter sw = new StringWriter(); foreach (CompilerError ce in results.Errors) { if (ce.IsWarning) continue; String line = String.Format("{0}({1},{2}): error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText); Trace.WriteLine(line); sw.WriteLine(line); } string errorText = sw.ToString(); try { if (errorText.Length > 0) throw new ApplicationException(errorText); if (!asm.Exists) throw new FileNotFoundException(new FileNotFoundException().Message, asm.TempPath); } catch { GC.KeepAlive(files); throw; } File.Copy(asm.TempPath, output, true); } }
public void ReadFrom(string versionInfo) { StringWriter asmInfo = new StringWriter(); asmInfo.WriteLine("using System;"); asmInfo.WriteLine(); using (DisposingList disposable = new DisposingList()) { if (versionInfo.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) { string dir = Path.GetDirectoryName(versionInfo); XmlLightDocument proj = new XmlLightDocument(File.ReadAllText(versionInfo)); foreach (XmlLightElement xref in proj.Select("/Project/ItemGroup/Compile")) { if (!xref.Attributes.ContainsKey("Include") || !xref.Attributes["Include"].EndsWith("AssemblyInfo.cs", StringComparison.OrdinalIgnoreCase)) continue; string include = xref.Attributes["Include"]; versionInfo = Path.Combine(dir, include); break; } if (versionInfo.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) throw new ApplicationException("Unable to locate AssemblyInfo.cs"); } if (versionInfo.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) { _assemblyInfo = File.ReadAllText(versionInfo); TempFile dll = TempFile.FromExtension(".dll"); disposable.Add(dll); dll.Delete(); CSharpCodeProvider csc = new CSharpCodeProvider(); CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; args.IncludeDebugInformation = false; args.OutputAssembly = dll.TempPath; args.ReferencedAssemblies.Add("System.dll"); CompilerResults results = csc.CompileAssemblyFromFile(args, versionInfo); StringWriter sw = new StringWriter(); foreach (CompilerError ce in results.Errors) { if (ce.IsWarning) continue; String line = String.Format("{0}({1},{2}): error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText); Trace.WriteLine(line); sw.WriteLine(line); } string errorText = sw.ToString(); if (errorText.Length > 0) throw new ApplicationException(errorText); versionInfo = dll.TempPath; } if (!File.Exists(versionInfo) || (!versionInfo.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !versionInfo.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))) throw new ApplicationException("Expected an existing dll: " + versionInfo); try { Regex truefalse = new Regex(@"(?<=[^\w_\.])(?:True)|(?:False)(?=[^\w_\.])"); _fileVersion = FileVersionInfo.GetVersionInfo(versionInfo); if (_assemblyInfo == null) { Assembly asm = Assembly.ReflectionOnlyLoad(File.ReadAllBytes(versionInfo)); IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes(asm); foreach (CustomAttributeData data in attrs) { if (!data.ToString().StartsWith("[System.Runtime.CompilerServices.")) { string attribute = data.ToString().Trim(); if (attribute[0] != '[') continue; //unexpected... attribute = attribute.Insert(1, "assembly: "); attribute = StringUtils.Transform(attribute, truefalse, delegate(Match x) { return x.Value.ToLower(); }); asmInfo.WriteLine(attribute); } } } } catch { } } _assemblyInfo = _assemblyInfo ?? asmInfo.ToString(); }
/// <summary> Adds an object to this stream that will be disposed when the stream is disposed. </summary> public DisposingStream WithDisposeOf(IDisposable disposable) { _disposables.Add(disposable); return(this); }
public void TestDerivedFileName() { string path; using(DisposingList l = new DisposingList()) { TempFile ftarget = new TempFile(); ftarget.Delete(); l.Add(ftarget); TempFile fbigone = TempFile.Attach(String.Format("{0}.~{1:x4}", ftarget.TempPath, 0x10008 - 25)); fbigone.Create().Dispose(); Assert.IsTrue(fbigone.Exists); l.Add(fbigone); path = ftarget.TempPath; string tmpName; Dictionary<string, object> names = new Dictionary<string,object>(StringComparer.OrdinalIgnoreCase); for( int i=0; i < 25; i++ ) { Stream s = ReplaceFile.CreateDerivedFile(ftarget.TempPath, out tmpName); l.Add(TempFile.Attach(tmpName)); l.Add(s); names.Add(tmpName, null); } fbigone.Delete(); Assert.AreEqual(25, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length); } Assert.AreEqual(0, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length); }