static byte[] ReadDfsFileContent(FastXml fx, int filestart, int fileend, string dfsfile) { long size; { int sizestart, sizeend; if (!fx.FindTag("Size", filestart, fileend, out sizestart, out sizeend)) { throw new Exception("LoadDfsFileContents: Expected <Size> for DFS file '" + dfsfile + "'"); } string ssize = fx.GetInnerText(sizestart, sizeend); if (!long.TryParse(ssize, out size) || size < 0) { throw new FormatException("LoadDfsFileContents: <Size> for DFS file '" + dfsfile + "' has invalid value: " + ssize); } if (size > 1342177280) { throw new Exception("LoadDfsFileContents: file size (" + size + ") for DFS file '" + dfsfile + "' too large to load into memory"); } } byte[] content = new byte[size]; int contentpos = 0; { int cstart = filestart; int cend = fileend; for (; ; ) { int nodestart, nodeend; if (!fx.FindTag("FileNode", cstart, cend, out nodestart, out nodeend)) { break; } string chunkname = fx.GetTagInnerText("Name", nodestart, nodeend); string[] chunkhosts = fx.GetTagInnerText("Host", nodestart, nodeend).Split(';'); int chunkpos = 0; // Excluding header. for (int ichunkhost = 0; ; ) { try { string chunkpath = ToNetworkPath(DfsDir, chunkhosts[ichunkhost]) + @"\" + chunkname; using (System.IO.FileStream fs = new System.IO.FileStream(chunkpath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { { if (null == headerbuf) { headerbuf = new byte[32]; } // Skip the dfs-chunk file-header... int headerlength = 0; { if (4 != fs.Read(headerbuf, 0, 4)) { throw new System.IO.IOException("Unable to read DFS chunk header: " + chunkpath); } { headerlength = MyBytesToInt(headerbuf, 0); if (headerlength > 4) { int remain = headerlength - 4; if (remain > headerbuf.Length) { headerbuf = new byte[remain + 100]; } MyStreamReadExact(fs, headerbuf, remain); } } } } if (0 != chunkpos) { fs.Seek(chunkpos, System.IO.SeekOrigin.Current); } for (; ; ) { int read = fs.Read(content, contentpos, content.Length - contentpos); if (read < 1) { break; } chunkpos += read; contentpos += read; if (contentpos > content.Length) { throw new Exception("LoadDfsFileContent: read too much data from DFS file '" + dfsfile + "'; file size reported is inaccurate"); } } } break; } catch (System.IO.IOException) { ichunkhost++; if (ichunkhost >= chunkhosts.Length) { throw; } continue; } } cstart = nodeend; } } if (contentpos < content.Length) { #if DEBUG throw new Exception("DEBUG: LoadDfsFileContent: (contentpos{" + contentpos + "} < content.Length{" + content.Length + "}) for DFS file '" + dfsfile + "'"); #endif byte[] newcontent = new byte[contentpos]; Buffer.BlockCopy(content, 0, newcontent, 0, contentpos); content = newcontent; } return content; }
internal static void _Test_() { #if DEBUG { FastXml fx = new FastXml("foo <foo> foo </foo> foo"); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 4 != start || 20 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("foo <foo > foo </foo> foo"); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 4 != start || 21 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("foo <foo foo=`hi`> foo </foo> foo".Replace('`', '"')); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 4 != start || 29 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("foo <foo/> foo </foo> foo"); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 4 != start || 10 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foofoo> <foo> foo </foo> foo"); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 9 != start || 25 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foofoo> <foo> foo </foofoo> </foo> foo"); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 9 != start || 35 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foofoo foo=`foo`> <foo/> foo </foofoo> </foo> foo".Replace('`', '"')); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 19 != start || 25 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foofoo/> <foo/> foo </foofoo> </foo> foo"); int start, end; if (!fx.FindTag("foo", 0, out start, out end) || 10 != start || 16 != end) { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foo>bar</foo>"); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "bar") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml(" <foo> bar </foo> "); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "bar") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml(" <foo> <hello> bar </hello> baz </foo> "); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "bar baz") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foo> bar<x/><hello>baz</hello>"); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "bar baz") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foo> bar<x/><hello/>baz</foo>"); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "bar baz") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml("<foo> bar<x>qux</x><hello/>baz</foo>"); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "bar qux baz") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml(" a <foo/> b "); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } { FastXml fx = new FastXml(" <foo> a <x/> < b </foo> "); int start, end; fx.FindTag("foo", 0, out start, out end); string s = fx.GetInnerText(start, end); if (s != "a < b") { System.Diagnostics.Debugger.Launch(); throw new Exception("TEST FAILED"); } } #endif }
public static string GetFileSizeString(string dfsfile) { if (dfsfile.StartsWith("dfs://", StringComparison.OrdinalIgnoreCase)) { dfsfile = dfsfile.Substring(6); } ValidateDfsProtocolDfsFileName(dfsfile); FastXml fx; using (LockDfsMutex()) { fx = new FastXml(ReadDfs_unlocked(DfsDir + @"\dfs.xml")); } int filestart, fileend; if (!DfsFindFile(fx, dfsfile, out filestart, out fileend)) { throw new Exception("File not found in DFS: " + dfsfile); } int sizestart, sizeend; if (!fx.FindTag("Size", filestart, fileend, out sizestart, out sizeend)) { throw new Exception("Expected <Size> for DFS file '" + dfsfile + "'"); } string ssize = fx.GetInnerText(sizestart, sizeend); return ssize; }
static bool DfsFindFile(FastXml fx, string dfsfile, out int tagstart, out int tagend) { int filestart, fileend; for (int pos = 0; fx.FindTag("DfsFile", pos, out filestart, out fileend); pos = fileend) { int namestart, nameend; if (fx.FindTag("Name", filestart, fileend, out namestart, out nameend)) { string onname = fx.GetInnerText(namestart, nameend); if (0 == string.Compare(onname, dfsfile, StringComparison.OrdinalIgnoreCase)) { tagstart = filestart; tagend = fileend; return true; } } } tagstart = 0; tagend = 0; return false; }