protected void SaveAllBinaries(Context context, string source, string fileName, byte[][] binaries) { XmlSerializer xml = new XmlSerializer(typeof(BinaryMetaInfo)); TestAndCreateDirectory(BinaryPath); using (BinaryMetaInfo bmi = BinaryMetaInfo.FromPath(BinaryPath, FileAccess.ReadWrite, FileShare.None)) { for (int i = 0; i < context.Devices.Length; i++) { Device device = context.Devices[i]; string binaryFileName; MetaFile mf = bmi.FindMetaFile(source, fileName, context.Platform.Name, device.Name, device.DriverVersion, Defines, BuildOptions); if (mf == null) { mf = bmi.CreateMetaFile(source, fileName, context.Platform.Name, device.Name, device.DriverVersion, Defines, BuildOptions); } binaryFileName = BinaryPath + FileSystem.GetDirectorySeparator() + mf.BinaryName; FileSystem.WriteAllBytes(binaryFileName, binaries[i]); } bmi.TrimBinaryCache(FileSystem, MaxCachedBinaries); bmi.Save(); } }
protected byte[][] LoadAllBinaries(Context context, string source, string fileName) { string sourcePath = SourcePath + FileSystem.GetDirectorySeparator() + fileName; DateTime sourceDateTime = FileSystem.GetLastWriteTime(sourcePath); byte[][] binaries = new byte[context.Devices.Length][]; if (!Directory.Exists(BinaryPath)) { throw new DirectoryNotFoundException(BinaryPath); } using (BinaryMetaInfo bmi = BinaryMetaInfo.FromPath(BinaryPath, FileAccess.Read, FileShare.Read)) { Device[] devices; devices = context.Devices; for (int i = 0; i < devices.Length; i++) { if (binaries[i] != null) { continue; } Device device = devices[i]; string binaryFilePath; MetaFile mf = bmi.FindMetaFile("", fileName, Context.Platform.Name, device.Name, device.DriverVersion, Defines, BuildOptions); if (mf == null) { throw new FileNotFoundException("No compiled binary file present in MetaFile"); } binaryFilePath = BinaryPath + FileSystem.GetDirectorySeparator() + mf.BinaryName; if (AttemptUseSource) { // This exception will be caught inside the manager and cause recompilation if (FileSystem.GetLastWriteTime(binaryFilePath) < sourceDateTime) { throw new Exception("Binary older than source"); } } binaries[i] = FileSystem.ReadAllBytes(binaryFilePath); // Check of there are other identical devices that can use the binary we just loaded // If there are, patch it in in the proper slots in the list of binaries for (int j = i + 1; j < devices.Length; j++) { if (devices[i].Name == devices[j].Name && devices[i].Vendor == devices[j].Vendor && devices[i].Version == devices[j].Version && devices[i].AddressBits == devices[j].AddressBits && devices[i].DriverVersion == devices[j].DriverVersion && devices[i].EndianLittle == devices[j].EndianLittle) { binaries[j] = binaries[i]; } } } } return(binaries); }
public static BinaryMetaInfo FromPath(String path, FileAccess fileAccess, FileShare fileShare) { var rnd = new Random(); BinaryMetaInfo bmi; var xml = new XmlSerializer(typeof(BinaryMetaInfo)); var metaFileName = path + Path.DirectorySeparatorChar + "metainfo.xml"; if (File.Exists(metaFileName)) { var obtainLockStart = DateTime.Now; FileStream fs = null; while (true) { var obtainLockNow = DateTime.Now; var dt = obtainLockNow - obtainLockStart; if (dt.TotalSeconds > 30) { break; } try { fs = File.Open(metaFileName, FileMode.Open, fileAccess, fileShare); break; } catch (Exception) { Thread.CurrentThread.Join(50 + rnd.Next(50)); } } var xmlReader = XmlReader.Create(fs); try { bmi = (BinaryMetaInfo)xml.Deserialize(xmlReader); bmi.FileStream = fs; bmi.Root = path; xmlReader.Close(); } catch (Exception) { xmlReader.Close(); bmi = new BinaryMetaInfo(); bmi.Root = path; bmi.FileStream = fs; } } else { FileStream fs = null; try { fs = File.Open(metaFileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); } catch (Exception) { if (File.Exists(metaFileName)) { // Another process created the file just before us. Just call ourselves recursively, // which should land us in the other branch of the if-statement now that the metaFile exists return(BinaryMetaInfo.FromPath(path, fileAccess, fileShare)); } } bmi = new BinaryMetaInfo(); bmi.Root = path; bmi.FileStream = fs; } return(bmi); }