comparison src/org/tmatesoft/hg/internal/RevlogDump.java @ 376:d45ad07dc94c

Allow content dump for separate .i and .d files
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 02 Feb 2012 16:10:50 +0100
parents e9d275fb0bc0
children 656a6c1346ff
comparison
equal deleted inserted replaced
375:729ba8f7d14e 376:d45ad07dc94c
20 import java.io.DataInput; 20 import java.io.DataInput;
21 import java.io.DataInputStream; 21 import java.io.DataInputStream;
22 import java.io.File; 22 import java.io.File;
23 import java.io.FileInputStream; 23 import java.io.FileInputStream;
24 import java.math.BigInteger; 24 import java.math.BigInteger;
25 import java.nio.ByteBuffer;
26 import java.nio.channels.FileChannel;
25 import java.util.zip.Inflater; 27 import java.util.zip.Inflater;
26 28
27 /** 29 /**
28 * Utility to test/debug/troubleshoot 30 * Utility to test/debug/troubleshoot
29 * 31 *
48 repo = args[0]; 50 repo = args[0];
49 filename = args[1]; 51 filename = args[1];
50 dumpData = args.length > 2 ? "dumpData".equals(args[2]) : false; 52 dumpData = args.length > 2 ? "dumpData".equals(args[2]) : false;
51 } 53 }
52 // 54 //
53 DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File(repo + filename)))); 55 DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File(repo, filename))));
54 DataInput di = dis; 56 DataInput di = dis;
55 dis.mark(10); 57 dis.mark(10);
56 int versionField = di.readInt(); 58 int versionField = di.readInt();
57 dis.reset(); 59 dis.reset();
58 final int INLINEDATA = 1 << 16; 60 final int INLINEDATA = 1 << 16;
59 61
60 boolean inlineData = (versionField & INLINEDATA) != 0; 62 final boolean inlineData = (versionField & INLINEDATA) != 0;
61 System.out.printf("%#8x, inline: %b\n", versionField, inlineData); 63 System.out.printf("%#8x, inline: %b\n", versionField, inlineData);
64 FileChannel dataStream = null;
65 if (!inlineData && dumpData) {
66 dataStream = new FileInputStream(new File(repo, filename.substring(0, filename.length()-2) + ".d")).getChannel();
67 }
62 System.out.println("Index Offset Flags Packed Actual Base Rev Link Rev Parent1 Parent2 nodeid"); 68 System.out.println("Index Offset Flags Packed Actual Base Rev Link Rev Parent1 Parent2 nodeid");
63 int entryCount = 0; 69 int entryIndex = 0;
64 while (dis.available() > 0) { 70 while (dis.available() > 0) {
65 long l = di.readLong(); 71 long l = di.readLong();
66 long offset = l >>> 16; 72 long offset = entryIndex == 0 ? 0 : (l >>> 16);
67 int flags = (int) (l & 0X0FFFF); 73 int flags = (int) (l & 0X0FFFF);
68 int compressedLen = di.readInt(); 74 int compressedLen = di.readInt();
69 int actualLen = di.readInt(); 75 int actualLen = di.readInt();
70 int baseRevision = di.readInt(); 76 int baseRevision = di.readInt();
71 int linkRevision = di.readInt(); 77 int linkRevision = di.readInt();
75 di.readFully(buf, 12, 20); 81 di.readFully(buf, 12, 20);
76 dis.skipBytes(12); 82 dis.skipBytes(12);
77 // CAN'T USE skip() here without extra precautions. E.g. I ran into situation when 83 // CAN'T USE skip() here without extra precautions. E.g. I ran into situation when
78 // buffer was 8192 and BufferedInputStream was at position 8182 before attempt to skip(12). 84 // buffer was 8192 and BufferedInputStream was at position 8182 before attempt to skip(12).
79 // BIS silently skips available bytes and leaves me two extra bytes that ruin the rest of the code. 85 // BIS silently skips available bytes and leaves me two extra bytes that ruin the rest of the code.
80 System.out.printf("%4d:%14d %6X %10d %10d %10d %10d %8d %8d %040x\n", entryCount, offset, flags, compressedLen, actualLen, baseRevision, linkRevision, parent1Revision, parent2Revision, new BigInteger(buf)); 86 System.out.printf("%4d:%14d %6X %10d %10d %10d %10d %8d %8d %040x\n", entryIndex, offset, flags, compressedLen, actualLen, baseRevision, linkRevision, parent1Revision, parent2Revision, new BigInteger(buf));
87 String resultString;
88 byte[] data = new byte[compressedLen];
81 if (inlineData) { 89 if (inlineData) {
82 String resultString;
83 byte[] data = new byte[compressedLen];
84 di.readFully(data); 90 di.readFully(data);
85 if (data[0] == 0x78 /* 'x' */) { 91 } else if (dumpData) {
86 Inflater zlib = new Inflater(); 92 dataStream.position(offset);
87 zlib.setInput(data, 0, compressedLen); 93 dataStream.read(ByteBuffer.wrap(data));
88 byte[] result = new byte[actualLen*2]; 94 }
89 int resultLen = zlib.inflate(result); 95 if (dumpData) {
90 zlib.end(); 96 if (compressedLen == 0) {
91 resultString = new String(result, 0, resultLen, "UTF-8"); 97 resultString = "<NO DATA>";
92 } else if (data[0] == 0x75 /* 'u' */) {
93 resultString = new String(data, 1, data.length - 1, "UTF-8");
94 } else { 98 } else {
95 resultString = new String(data); 99 if (data[0] == 0x78 /* 'x' */) {
100 Inflater zlib = new Inflater();
101 zlib.setInput(data, 0, compressedLen);
102 byte[] result = new byte[actualLen*2];
103 int resultLen = zlib.inflate(result);
104 zlib.end();
105 resultString = new String(result, 0, resultLen, "UTF-8");
106 } else if (data[0] == 0x75 /* 'u' */) {
107 resultString = new String(data, 1, data.length - 1, "UTF-8");
108 } else {
109 resultString = new String(data);
110 }
96 } 111 }
97 if (dumpData) { 112 System.out.println(resultString);
98 System.out.println(resultString);
99 }
100 } 113 }
101 entryCount++; 114 entryIndex++;
102 } 115 }
103 dis.close(); 116 dis.close();
117 if (dataStream != null) {
118 dataStream.close();
119 }
104 // 120 //
105 } 121 }
106 } 122 }