|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace SQLBakVersion.Class |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Version values stored in an SQL Server MDF boot page. |
| 9 | + /// </summary> |
| 10 | + public sealed class MdfVersion |
| 11 | + { |
| 12 | + public static readonly MdfVersion Unknown = new MdfVersion(null, null, null, null); |
| 13 | + |
| 14 | + public MdfVersion(int? year, int? internalVersion, int? createVersion, string failureReason) |
| 15 | + { |
| 16 | + Year = year; |
| 17 | + InternalVersion = internalVersion; |
| 18 | + CreateVersion = createVersion; |
| 19 | + FailureReason = failureReason; |
| 20 | + } |
| 21 | + |
| 22 | + public int? Year { get; private set; } |
| 23 | + public int? InternalVersion { get; private set; } |
| 24 | + public int? CreateVersion { get; private set; } |
| 25 | + public string FailureReason { get; private set; } |
| 26 | + |
| 27 | + public static MdfVersion Unavailable(string failureReason) |
| 28 | + { |
| 29 | + return new MdfVersion(null, null, null, failureReason); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Detects the SQL Server version of an MDF file from its boot page. |
| 35 | + /// </summary> |
| 36 | + public static class MdfVersionDetector |
| 37 | + { |
| 38 | + private const int PageSize = 8192; |
| 39 | + private const int BootPageNumber = 9; |
| 40 | + private const int PageHeaderSize = 96; |
| 41 | + private const int DbiVersionOffset = 4; |
| 42 | + private const int DbiCreateVersionOffset = 6; |
| 43 | + private const long DbiVersionFileOffset = (long)BootPageNumber * PageSize + PageHeaderSize + DbiVersionOffset; |
| 44 | + private const long DbiCreateVersionFileOffset = (long)BootPageNumber * PageSize + PageHeaderSize + DbiCreateVersionOffset; |
| 45 | + |
| 46 | + private static readonly IDictionary<int, int> VersionYears = new Dictionary<int, int> |
| 47 | + { |
| 48 | + { 539, 2000 }, |
| 49 | + { 611, 2005 }, |
| 50 | + { 612, 2005 }, |
| 51 | + { 655, 2008 }, |
| 52 | + { 660, 2008 }, |
| 53 | + { 661, 2008 }, |
| 54 | + { 684, 2012 }, |
| 55 | + { 706, 2012 }, |
| 56 | + { 782, 2014 }, |
| 57 | + { 852, 2016 }, |
| 58 | + { 868, 2017 }, |
| 59 | + { 869, 2017 }, |
| 60 | + { 895, 2019 }, |
| 61 | + { 896, 2019 }, |
| 62 | + { 897, 2019 }, |
| 63 | + { 902, 2019 }, |
| 64 | + { 904, 2019 }, |
| 65 | + { 950, 2022 }, |
| 66 | + { 957, 2022 } |
| 67 | + }; |
| 68 | + |
| 69 | + public static MdfVersion Detect(string path) |
| 70 | + { |
| 71 | + if (string.IsNullOrWhiteSpace(path)) |
| 72 | + { |
| 73 | + return MdfVersion.Unavailable("No MDF file was selected."); |
| 74 | + } |
| 75 | + |
| 76 | + try |
| 77 | + { |
| 78 | + using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 79 | + { |
| 80 | + if (stream.Length < DbiCreateVersionFileOffset + sizeof(ushort)) |
| 81 | + { |
| 82 | + return MdfVersion.Unavailable("The file is too small to contain the MDF boot page."); |
| 83 | + } |
| 84 | + |
| 85 | + stream.Position = DbiVersionFileOffset; |
| 86 | + byte[] bytes = new byte[sizeof(ushort) * 2]; |
| 87 | + if (!ReadExactly(stream, bytes)) |
| 88 | + { |
| 89 | + return MdfVersion.Unavailable("The MDF boot page could not be read completely."); |
| 90 | + } |
| 91 | + |
| 92 | + int internalVersion = ToUInt16LittleEndian(bytes, 0); |
| 93 | + int createVersion = ToUInt16LittleEndian(bytes, sizeof(ushort)); |
| 94 | + int year; |
| 95 | + |
| 96 | + return VersionYears.TryGetValue(internalVersion, out year) |
| 97 | + ? new MdfVersion(year, internalVersion, createVersion, null) |
| 98 | + : new MdfVersion(null, internalVersion, createVersion, null); |
| 99 | + } |
| 100 | + } |
| 101 | + catch (UnauthorizedAccessException exception) |
| 102 | + { |
| 103 | + return MdfVersion.Unavailable("The MDF file cannot be accessed: " + exception.Message); |
| 104 | + } |
| 105 | + catch (IOException exception) |
| 106 | + { |
| 107 | + return MdfVersion.Unavailable("The MDF file cannot be read. If it is attached to SQL Server, stop the service or use a copy of the file. " + exception.Message); |
| 108 | + } |
| 109 | + catch (Exception exception) |
| 110 | + { |
| 111 | + return MdfVersion.Unavailable("Unable to read the MDF boot page: " + exception.Message); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static bool ReadExactly(Stream stream, byte[] bytes) |
| 116 | + { |
| 117 | + int totalBytesRead = 0; |
| 118 | + |
| 119 | + while (totalBytesRead < bytes.Length) |
| 120 | + { |
| 121 | + int bytesRead = stream.Read(bytes, totalBytesRead, bytes.Length - totalBytesRead); |
| 122 | + if (bytesRead == 0) |
| 123 | + { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + totalBytesRead += bytesRead; |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + private static int ToUInt16LittleEndian(byte[] bytes, int offset) |
| 134 | + { |
| 135 | + return bytes[offset] | (bytes[offset + 1] << 8); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments