manageiq-smartstate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (305) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +4 -0
  4. data/.rspec_ci +4 -0
  5. data/.travis.yml +15 -0
  6. data/Gemfile +9 -0
  7. data/LICENSE.txt +202 -0
  8. data/README.md +45 -0
  9. data/Rakefile +23 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/MiqContainerGroup/MiqContainerGroup.rb +31 -0
  13. data/lib/MiqVm/MiqLocalVm.rb +50 -0
  14. data/lib/MiqVm/MiqRhevmVm.rb +179 -0
  15. data/lib/MiqVm/MiqVm.rb +355 -0
  16. data/lib/MiqVm/miq_azure_vm.rb +96 -0
  17. data/lib/MiqVm/miq_scvmm_vm.rb +38 -0
  18. data/lib/MiqVm/test/camcorder_fleece_test.rb +60 -0
  19. data/lib/MiqVm/test/localVm.rb +45 -0
  20. data/lib/MiqVm/test/partitionAlignmentCheck.rb +76 -0
  21. data/lib/MiqVm/test/remoteVm.rb +65 -0
  22. data/lib/MiqVm/test/rhevmNfsTest.rb +62 -0
  23. data/lib/MiqVm/test/rhevmNfsTest2.rb +66 -0
  24. data/lib/MiqVm/test/rhevmTest.rb +70 -0
  25. data/lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackCommon.rb +107 -0
  26. data/lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackImage.rb +67 -0
  27. data/lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb +182 -0
  28. data/lib/Scvmm/miq_hyperv_disk.rb +273 -0
  29. data/lib/Scvmm/miq_scvmm_parse_powershell.rb +75 -0
  30. data/lib/Scvmm/miq_scvmm_vm_ssa_info.rb +135 -0
  31. data/lib/Scvmm/test/miq_hyperv_disk_test.rb +33 -0
  32. data/lib/Scvmm/test/miq_scvmm_vm_ssa_info_test.rb +41 -0
  33. data/lib/VmLocalDiskAccess/test/localCfg.rb +97 -0
  34. data/lib/VolumeManager/LVM/logical_volume.rb +75 -0
  35. data/lib/VolumeManager/LVM/lv_segment.rb +43 -0
  36. data/lib/VolumeManager/LVM/lvm2disk.rb +158 -0
  37. data/lib/VolumeManager/LVM/parser.rb +138 -0
  38. data/lib/VolumeManager/LVM/physical_volume.rb +19 -0
  39. data/lib/VolumeManager/LVM/scanner.rb +156 -0
  40. data/lib/VolumeManager/LVM/thin/btree.rb +83 -0
  41. data/lib/VolumeManager/LVM/thin/constants.rb +86 -0
  42. data/lib/VolumeManager/LVM/thin/data_map.rb +44 -0
  43. data/lib/VolumeManager/LVM/thin/mapping_tree.rb +19 -0
  44. data/lib/VolumeManager/LVM/thin/space_maps.rb +58 -0
  45. data/lib/VolumeManager/LVM/thin/superblock.rb +136 -0
  46. data/lib/VolumeManager/LVM/thin.rb +6 -0
  47. data/lib/VolumeManager/LVM/volume_group.rb +97 -0
  48. data/lib/VolumeManager/LVM.rb +8 -0
  49. data/lib/VolumeManager/MiqLdm.rb +546 -0
  50. data/lib/VolumeManager/MiqLvm.rb +17 -0
  51. data/lib/VolumeManager/MiqNativeVolumeManager.rb +150 -0
  52. data/lib/VolumeManager/MiqVolumeManager.rb +277 -0
  53. data/lib/VolumeManager/VolMgrPlatformSupport.rb +18 -0
  54. data/lib/VolumeManager/VolMgrPlatformSupportLinux.rb +77 -0
  55. data/lib/VolumeManager/VolMgrPlatformSupportWin.rb +17 -0
  56. data/lib/VolumeManager/test/blockDevTest.rb +40 -0
  57. data/lib/VolumeManager/test/ldm.rb +97 -0
  58. data/lib/blackbox/VmBlackBox.rb +103 -0
  59. data/lib/blackbox/xmlStorage.rb +180 -0
  60. data/lib/db/MiqBdb/MiqBdb.rb +309 -0
  61. data/lib/db/MiqBdb/MiqBdbBtree.rb +219 -0
  62. data/lib/db/MiqBdb/MiqBdbHash.rb +199 -0
  63. data/lib/db/MiqBdb/MiqBdbPage.rb +159 -0
  64. data/lib/db/MiqBdb/MiqBdbUtil.rb +18 -0
  65. data/lib/db/MiqSqlite/MiqSqlite3.rb +330 -0
  66. data/lib/db/MiqSqlite/MiqSqlite3Cell.rb +167 -0
  67. data/lib/db/MiqSqlite/MiqSqlite3Page.rb +151 -0
  68. data/lib/db/MiqSqlite/MiqSqlite3Table.rb +124 -0
  69. data/lib/db/MiqSqlite/MiqSqlite3Util.rb +32 -0
  70. data/lib/disk/DiskProbe.rb +68 -0
  71. data/lib/disk/MiqDisk.rb +317 -0
  72. data/lib/disk/camcorder_test.rb +90 -0
  73. data/lib/disk/dos_mbr.img +0 -0
  74. data/lib/disk/modules/AzureBlobDisk.rb +101 -0
  75. data/lib/disk/modules/LocalDevMod.rb +47 -0
  76. data/lib/disk/modules/LocalDevProbe.rb +6 -0
  77. data/lib/disk/modules/MSCommon.rb +352 -0
  78. data/lib/disk/modules/MSVSDiffDisk.rb +91 -0
  79. data/lib/disk/modules/MSVSDiskProbe.rb +61 -0
  80. data/lib/disk/modules/MSVSDynamicDisk.rb +42 -0
  81. data/lib/disk/modules/MSVSFixedDisk.rb +45 -0
  82. data/lib/disk/modules/MiqLargeFile.rb +63 -0
  83. data/lib/disk/modules/MiqLargeFileWin32.rb +107 -0
  84. data/lib/disk/modules/QcowDisk.rb +692 -0
  85. data/lib/disk/modules/QcowDiskProbe.rb +34 -0
  86. data/lib/disk/modules/RawBlockIO.rb +116 -0
  87. data/lib/disk/modules/RawDisk.rb +45 -0
  88. data/lib/disk/modules/RawDiskProbe.rb +7 -0
  89. data/lib/disk/modules/RhevmDescriptor.rb +167 -0
  90. data/lib/disk/modules/RhevmDiskProbe.rb +52 -0
  91. data/lib/disk/modules/VMWareCowdDisk.rb +207 -0
  92. data/lib/disk/modules/VMWareDescriptor.rb +214 -0
  93. data/lib/disk/modules/VMWareDiskProbe.rb +74 -0
  94. data/lib/disk/modules/VMWareSparseDisk.rb +189 -0
  95. data/lib/disk/modules/VhdxDisk.rb +625 -0
  96. data/lib/disk/modules/VhdxDiskProbe.rb +46 -0
  97. data/lib/disk/modules/VixDiskMod.rb +54 -0
  98. data/lib/disk/modules/VixDiskProbe.rb +6 -0
  99. data/lib/disk/modules/miq_disk_cache.rb +135 -0
  100. data/lib/disk/modules/miq_dummy_disk.rb +41 -0
  101. data/lib/disk/modules/vhdx_bat_entry.rb +10 -0
  102. data/lib/disk/test.rb +66 -0
  103. data/lib/fs/MetakitFS/MetakitFS.rb +530 -0
  104. data/lib/fs/MetakitFS/test/Makefile +14 -0
  105. data/lib/fs/MetakitFS/test/MkCollectFiles.rb +165 -0
  106. data/lib/fs/MetakitFS/test/MkSelectFiles.rb +30 -0
  107. data/lib/fs/MetakitFS/test/collect_files.yaml +70 -0
  108. data/lib/fs/MetakitFS/test/init.rb +3 -0
  109. data/lib/fs/MetakitFS/test/mk2vmdk.rb +64 -0
  110. data/lib/fs/MetakitFS/test/mk4test.c +92 -0
  111. data/lib/fs/MetakitFS/test/mkFsTest.rb +113 -0
  112. data/lib/fs/MetakitFS/test/proto.rb +97 -0
  113. data/lib/fs/MiqFS/FsProbe.rb +39 -0
  114. data/lib/fs/MiqFS/MiqFS.rb +515 -0
  115. data/lib/fs/MiqFS/modules/AUFSProbe.rb +26 -0
  116. data/lib/fs/MiqFS/modules/Ext3.rb +305 -0
  117. data/lib/fs/MiqFS/modules/Ext3Probe.rb +25 -0
  118. data/lib/fs/MiqFS/modules/Ext4.rb +304 -0
  119. data/lib/fs/MiqFS/modules/Ext4Probe.rb +25 -0
  120. data/lib/fs/MiqFS/modules/Fat32.rb +318 -0
  121. data/lib/fs/MiqFS/modules/Fat32Probe.rb +30 -0
  122. data/lib/fs/MiqFS/modules/HFSProbe.rb +18 -0
  123. data/lib/fs/MiqFS/modules/Iso9660.rb +293 -0
  124. data/lib/fs/MiqFS/modules/Iso9660Probe.rb +18 -0
  125. data/lib/fs/MiqFS/modules/LocalFS.rb +105 -0
  126. data/lib/fs/MiqFS/modules/NTFS.rb +287 -0
  127. data/lib/fs/MiqFS/modules/NTFSProbe.rb +21 -0
  128. data/lib/fs/MiqFS/modules/NativeFS.rb +155 -0
  129. data/lib/fs/MiqFS/modules/ReFSProbe.rb +17 -0
  130. data/lib/fs/MiqFS/modules/RealFS.rb +79 -0
  131. data/lib/fs/MiqFS/modules/RealFSProbe.rb +6 -0
  132. data/lib/fs/MiqFS/modules/Reiser4Probe.rb +18 -0
  133. data/lib/fs/MiqFS/modules/ReiserFS.rb +315 -0
  134. data/lib/fs/MiqFS/modules/ReiserFSProbe.rb +42 -0
  135. data/lib/fs/MiqFS/modules/UnionFSProbe.rb +18 -0
  136. data/lib/fs/MiqFS/modules/WebDAV.rb +127 -0
  137. data/lib/fs/MiqFS/modules/WebDAVFile.rb +68 -0
  138. data/lib/fs/MiqFS/modules/XFS.rb +300 -0
  139. data/lib/fs/MiqFS/modules/XFSProbe.rb +26 -0
  140. data/lib/fs/MiqFS/modules/ZFSProbe.rb +18 -0
  141. data/lib/fs/MiqFS/test.rb +59 -0
  142. data/lib/fs/MiqFsUtil.rb +383 -0
  143. data/lib/fs/MiqMountManager.rb +209 -0
  144. data/lib/fs/MiqNativeMountManager.rb +101 -0
  145. data/lib/fs/MountManagerProbe.rb +29 -0
  146. data/lib/fs/ReiserFS/block.rb +209 -0
  147. data/lib/fs/ReiserFS/directory.rb +136 -0
  148. data/lib/fs/ReiserFS/directory_entry.rb +140 -0
  149. data/lib/fs/ReiserFS/file_data.rb +111 -0
  150. data/lib/fs/ReiserFS/superblock.rb +140 -0
  151. data/lib/fs/ReiserFS/utils.rb +95 -0
  152. data/lib/fs/VimDatastoreFS/VimDatastoreFS.rb +192 -0
  153. data/lib/fs/ext3/alloc_bitmap.rb +38 -0
  154. data/lib/fs/ext3/block_pointers_path.rb +130 -0
  155. data/lib/fs/ext3/directory.rb +51 -0
  156. data/lib/fs/ext3/directory_entry.rb +67 -0
  157. data/lib/fs/ext3/ex_attrib_header.rb +14 -0
  158. data/lib/fs/ext3/ex_attrib_name.rb +23 -0
  159. data/lib/fs/ext3/file_data.rb +130 -0
  160. data/lib/fs/ext3/group_descriptor_entry.rb +65 -0
  161. data/lib/fs/ext3/group_descriptor_table.rb +54 -0
  162. data/lib/fs/ext3/hash_tree_entry.rb +18 -0
  163. data/lib/fs/ext3/hash_tree_header.rb +15 -0
  164. data/lib/fs/ext3/inode.rb +228 -0
  165. data/lib/fs/ext3/posix_acl_entry.rb +29 -0
  166. data/lib/fs/ext3/posix_acl_header.rb +11 -0
  167. data/lib/fs/ext3/superblock.rb +406 -0
  168. data/lib/fs/ext3/test/tc_Ext3BlockPointersPath.rb +74 -0
  169. data/lib/fs/ext4/alloc_bitmap.rb +38 -0
  170. data/lib/fs/ext4/directory.rb +87 -0
  171. data/lib/fs/ext4/directory_entry.rb +77 -0
  172. data/lib/fs/ext4/ex_attrib_header.rb +14 -0
  173. data/lib/fs/ext4/ex_attrib_name.rb +23 -0
  174. data/lib/fs/ext4/extent.rb +35 -0
  175. data/lib/fs/ext4/extent_header.rb +40 -0
  176. data/lib/fs/ext4/extent_index.rb +33 -0
  177. data/lib/fs/ext4/group_descriptor_entry.rb +69 -0
  178. data/lib/fs/ext4/group_descriptor_table.rb +54 -0
  179. data/lib/fs/ext4/hash_tree_entry.rb +58 -0
  180. data/lib/fs/ext4/hash_tree_header.rb +35 -0
  181. data/lib/fs/ext4/inode.rb +465 -0
  182. data/lib/fs/ext4/posix_acl_entry.rb +29 -0
  183. data/lib/fs/ext4/posix_acl_header.rb +11 -0
  184. data/lib/fs/ext4/superblock.rb +412 -0
  185. data/lib/fs/fat32/boot_sect.rb +379 -0
  186. data/lib/fs/fat32/directory.rb +222 -0
  187. data/lib/fs/fat32/directory_entry.rb +540 -0
  188. data/lib/fs/fat32/file_data.rb +128 -0
  189. data/lib/fs/iso9660/boot_sector.rb +170 -0
  190. data/lib/fs/iso9660/directory.rb +90 -0
  191. data/lib/fs/iso9660/directory_entry.rb +147 -0
  192. data/lib/fs/iso9660/file_data.rb +78 -0
  193. data/lib/fs/iso9660/rock_ridge.rb +329 -0
  194. data/lib/fs/iso9660/util.rb +57 -0
  195. data/lib/fs/modules/LinuxMount.rb +300 -0
  196. data/lib/fs/modules/LinuxMountProbe.rb +29 -0
  197. data/lib/fs/modules/WinMount.rb +97 -0
  198. data/lib/fs/modules/WinMountProbe.rb +24 -0
  199. data/lib/fs/ntfs/attrib_attribute_list.rb +131 -0
  200. data/lib/fs/ntfs/attrib_bitmap.rb +26 -0
  201. data/lib/fs/ntfs/attrib_data.rb +74 -0
  202. data/lib/fs/ntfs/attrib_file_name.rb +110 -0
  203. data/lib/fs/ntfs/attrib_header.rb +194 -0
  204. data/lib/fs/ntfs/attrib_index_allocation.rb +19 -0
  205. data/lib/fs/ntfs/attrib_index_root.rb +247 -0
  206. data/lib/fs/ntfs/attrib_object_id.rb +40 -0
  207. data/lib/fs/ntfs/attrib_standard_information.rb +107 -0
  208. data/lib/fs/ntfs/attrib_type.rb +49 -0
  209. data/lib/fs/ntfs/attrib_volume_information.rb +53 -0
  210. data/lib/fs/ntfs/attrib_volume_name.rb +31 -0
  211. data/lib/fs/ntfs/boot_sect.rb +253 -0
  212. data/lib/fs/ntfs/data_run.rb +358 -0
  213. data/lib/fs/ntfs/directory_index_node.rb +114 -0
  214. data/lib/fs/ntfs/index_node_header.rb +69 -0
  215. data/lib/fs/ntfs/index_record_header.rb +85 -0
  216. data/lib/fs/ntfs/mft_entry.rb +288 -0
  217. data/lib/fs/ntfs/utils.rb +43 -0
  218. data/lib/fs/test/camcorder_fs_test.rb +108 -0
  219. data/lib/fs/test/collect_files_direct.yaml +22 -0
  220. data/lib/fs/test/collect_files_in.yaml +24 -0
  221. data/lib/fs/test/collect_files_in_nc.yaml +22 -0
  222. data/lib/fs/test/collect_files_out.yaml +6 -0
  223. data/lib/fs/test/collect_files_rm.yaml +6 -0
  224. data/lib/fs/test/copyTest.rb +126 -0
  225. data/lib/fs/test/fsTest.rb +87 -0
  226. data/lib/fs/test/updateTest.rb +184 -0
  227. data/lib/fs/xfs/allocation_group.rb +160 -0
  228. data/lib/fs/xfs/bmap_btree_block.rb +125 -0
  229. data/lib/fs/xfs/bmap_btree_record.rb +80 -0
  230. data/lib/fs/xfs/bmap_btree_root_node.rb +72 -0
  231. data/lib/fs/xfs/directory.rb +133 -0
  232. data/lib/fs/xfs/directory2_data_header.rb +27 -0
  233. data/lib/fs/xfs/directory3_data_header.rb +34 -0
  234. data/lib/fs/xfs/directory_block_tail.rb +22 -0
  235. data/lib/fs/xfs/directory_data_header.rb +46 -0
  236. data/lib/fs/xfs/directory_entry.rb +106 -0
  237. data/lib/fs/xfs/inode.rb +532 -0
  238. data/lib/fs/xfs/inode_map.rb +100 -0
  239. data/lib/fs/xfs/short_form_directory_entry.rb +91 -0
  240. data/lib/fs/xfs/short_form_header.rb +44 -0
  241. data/lib/fs/xfs/superblock.rb +556 -0
  242. data/lib/lib/tasks/azure.rake +52 -0
  243. data/lib/manageiq/smartstate/version.rb +5 -0
  244. data/lib/manageiq/smartstate.rb +7 -0
  245. data/lib/manageiq-smartstate.rb +1 -0
  246. data/lib/metadata/MIQExtract/MIQExtract.rb +297 -0
  247. data/lib/metadata/MIQExtract/test/extractTest.rb +41 -0
  248. data/lib/metadata/MIQExtract/test/full_extract_test.rb +68 -0
  249. data/lib/metadata/ScanProfile/HostScanItem.rb +4 -0
  250. data/lib/metadata/ScanProfile/HostScanProfile.rb +4 -0
  251. data/lib/metadata/ScanProfile/HostScanProfiles.rb +41 -0
  252. data/lib/metadata/ScanProfile/ScanItemBase.rb +63 -0
  253. data/lib/metadata/ScanProfile/ScanProfileBase.rb +51 -0
  254. data/lib/metadata/ScanProfile/ScanProfilesBase.rb +60 -0
  255. data/lib/metadata/ScanProfile/VmScanItem.rb +4 -0
  256. data/lib/metadata/ScanProfile/VmScanProfile.rb +4 -0
  257. data/lib/metadata/ScanProfile/VmScanProfiles.rb +38 -0
  258. data/lib/metadata/ScanProfile/modules/HostScanItemFile.rb +51 -0
  259. data/lib/metadata/ScanProfile/modules/HostScanItemNteventlog.rb +84 -0
  260. data/lib/metadata/ScanProfile/modules/VmScanItemFile.rb +39 -0
  261. data/lib/metadata/ScanProfile/modules/VmScanItemNteventlog.rb +34 -0
  262. data/lib/metadata/ScanProfile/modules/VmScanItemRegistry.rb +64 -0
  263. data/lib/metadata/VMMount/VMMount.rb +81 -0
  264. data/lib/metadata/VMMount/VMPlatformMount.rb +18 -0
  265. data/lib/metadata/VMMount/VMPlatformMountLinux.rb +75 -0
  266. data/lib/metadata/VMMount/VMPlatformMountWin.rb +13 -0
  267. data/lib/metadata/VmConfig/GetNativeCfg.rb +45 -0
  268. data/lib/metadata/VmConfig/VmConfig.rb +947 -0
  269. data/lib/metadata/VmConfig/cfgConfig.rb +45 -0
  270. data/lib/metadata/VmConfig/ovfConfig.rb +99 -0
  271. data/lib/metadata/VmConfig/test/GetVMwareCfgTest.rb +40 -0
  272. data/lib/metadata/VmConfig/vmcConfig.rb +116 -0
  273. data/lib/metadata/VmConfig/vmtxConfig.rb +4 -0
  274. data/lib/metadata/VmConfig/vmxConfig.rb +162 -0
  275. data/lib/metadata/VmConfig/xmlConfig.rb +79 -0
  276. data/lib/metadata/VmConfig/xmlMsHyperVConfig.rb +41 -0
  277. data/lib/metadata/linux/InitProcHash.rb +632 -0
  278. data/lib/metadata/linux/LinuxInitProcs.rb +142 -0
  279. data/lib/metadata/linux/LinuxOSInfo.rb +237 -0
  280. data/lib/metadata/linux/LinuxPackages.rb +209 -0
  281. data/lib/metadata/linux/LinuxSystemd.rb +130 -0
  282. data/lib/metadata/linux/LinuxUsers.rb +289 -0
  283. data/lib/metadata/linux/LinuxUtils.rb +197 -0
  284. data/lib/metadata/linux/MiqConaryPackages.rb +41 -0
  285. data/lib/metadata/linux/MiqRpmPackages.rb +160 -0
  286. data/lib/metadata/linux/test/Name +0 -0
  287. data/lib/metadata/linux/test/Packages +0 -0
  288. data/lib/metadata/linux/test/rpoTest.rb +5 -0
  289. data/lib/metadata/linux/test/tc_LinuxUtils.rb +4157 -0
  290. data/lib/metadata/util/event_log_filter.rb +61 -0
  291. data/lib/metadata/util/md5deep.rb +280 -0
  292. data/lib/metadata/util/win32/Win32Accounts.rb +764 -0
  293. data/lib/metadata/util/win32/Win32EventLog.rb +743 -0
  294. data/lib/metadata/util/win32/Win32Services.rb +86 -0
  295. data/lib/metadata/util/win32/Win32Software.rb +326 -0
  296. data/lib/metadata/util/win32/Win32System.rb +333 -0
  297. data/lib/metadata/util/win32/boot_info_win.rb +59 -0
  298. data/lib/metadata/util/win32/fleece_hives.rb +220 -0
  299. data/lib/metadata/util/win32/ms-registry.rb +650 -0
  300. data/lib/metadata/util/win32/peheader.rb +868 -0
  301. data/lib/metadata/util/win32/remote-registry.rb +142 -0
  302. data/lib/metadata/util/win32/system_path_win.rb +103 -0
  303. data/lib/metadata/util/win32/versioninfo.rb +17 -0
  304. data/manageiq-smartstate.gemspec +35 -0
  305. metadata +486 -0
@@ -0,0 +1,219 @@
1
+ require 'binary_struct'
2
+ require 'util/miq-hash_struct'
3
+
4
+ require_relative 'MiqBdbPage'
5
+
6
+ module MiqBerkeleyDB
7
+ class MiqBdbBtreeDatabase
8
+ BTREE_HEADER = BinaryStruct.new([
9
+ 'L', 'unused1', # 72-75: Unused space.
10
+ 'L', 'minkey', # 76-79: Btree: Minkey
11
+ 'L', 're_rlen', # 80-83: Recno: fixed-len record length
12
+ 'L', 're_pad', # 84-87: Recno: fixed-len record pad
13
+ 'L', 'root', # 88-91: Root page
14
+ 'a368', 'unused2', # 92-459: Unused space
15
+ 'L', 'crypto_magic', # 460-463: Crypto magic number.
16
+ 'a12', 'trash', # 464-475: Trash space - Do not use.
17
+ 'a16', 'iv', # 476-495: Crypto IV.
18
+ 'a20', 'chksum', # 496-511: Page chksum.
19
+ ])
20
+
21
+ SIZEOF_BTREE_HEADER = BTREE_HEADER.size
22
+
23
+ BINTERNAL_HEADER = BinaryStruct.new([
24
+ 'S', 'len', # 00-01: Key/data item length
25
+ 'C', 'ptype', # 02: Page type (AND DELETE FLAG)
26
+ 'C', 'unused', # 03: Padding, unused
27
+ 'L', 'pgno', # 04-07: Page number of referenced page
28
+ 'L', 'nrecs', # 08-11: Subtree record count
29
+ ])
30
+
31
+ SIZEOF_BINTERNAL_HEADER = BINTERNAL_HEADER.size
32
+
33
+ #
34
+ # Each index references a group of bytes on the page
35
+ #
36
+ B_KEYDATA = 1 # Key/data item
37
+ B_DUPLICATE = 2 # Duplicate key/data item
38
+ B_OVERFLOW = 3 # Overflow key/data item
39
+
40
+ B_DELETE = 0x80
41
+
42
+ OFFSET_LEN = 2
43
+
44
+ #
45
+ # The btree levels are numbered from the leaf to the root, starting
46
+ # with 1, so the leaf is level 1, its parent is level 2, and so on.
47
+ # We maintain this level on all btree pages, but the only place that
48
+ # we actually need it is on the root page. It would not be difficult
49
+ # to hide the byte on the root page once it becomes an internal page,
50
+ # so we could get this byte back if we needed it for something else.
51
+ #
52
+
53
+ attr_reader :bdb, :header
54
+
55
+ def initialize(bdb)
56
+ # Read pointer is positioned to hash header.
57
+ @bdb = bdb
58
+ @header = BTREE_HEADER.decode(@bdb.read(SIZEOF_BTREE_HEADER))
59
+ end
60
+
61
+ def close
62
+ @bdb = @header = nil
63
+ end
64
+
65
+ def keys(page)
66
+ leaves(page) do |leaf|
67
+ btree_leaf_keys(leaf) do |k|
68
+ yield k
69
+ end
70
+ end
71
+ end
72
+
73
+ def values(page)
74
+ leaves(page) do |leaf|
75
+ btree_leaf_values(leaf) do |v|
76
+ yield v
77
+ end
78
+ end
79
+ end
80
+
81
+ def pairs(page)
82
+ leaves(page) do |leaf|
83
+ btree_leaf(leaf) do |k, v|
84
+ yield k, v
85
+ end
86
+ end
87
+ end
88
+
89
+ def pages
90
+ pagenum = @header['root']
91
+ while page = MiqBdbPage.getPage(self, pagenum)
92
+
93
+ yield page
94
+
95
+ pagenum = page.next
96
+ end
97
+ end
98
+
99
+ def dump
100
+ out = ""
101
+ out << "B-Tree Database Header\n"
102
+ out << " minkey: #{@header['minkey']}\n"
103
+ out << " re_rlen: #{@header['re_rlen']}\n"
104
+ out << " re_pad: #{@header['re_pad']}\n"
105
+ out << " root: #{@header['root']}\n"
106
+ out << "\n"
107
+ out
108
+ end
109
+
110
+ private
111
+
112
+ def btreei_dump(h, index = nil)
113
+ out = ""
114
+ out << "B-Tree Internal Node"
115
+ out << " (#{index})" if index
116
+ out << "\n"
117
+ out << " key/data item length: #{h.len}\n"
118
+ out << " page number: #{h.pgno}\n"
119
+ out << " page type: #{MiqBdbPage.type2string(h.ptype)}\n"
120
+ out << " subtree record count: #{h.nrecs}\n"
121
+ out << "\n"
122
+ out
123
+ end
124
+
125
+ def btree_leaf(page)
126
+ i = 0
127
+ while i < page.nentries
128
+ key = entryData(i, page)
129
+ value = entryData(i + 1, page)
130
+
131
+ yield key, value if key.size > 0
132
+ i += 2 # skip value
133
+ end
134
+ end
135
+
136
+ def btree_leaf_keys(page)
137
+ i = 0
138
+ while i < page.nentries
139
+ key = entryData(i, page)
140
+
141
+ yield key if key.size > 0
142
+ i += 2 # skip value
143
+ end
144
+ end
145
+
146
+ def btree_leaf_values(page)
147
+ i = 0
148
+ while i < page.nentries
149
+ key = entryData(i, page)
150
+ value = entryData(i + 1, page)
151
+
152
+ yield value if key.size > 0
153
+ i += 2 # skip value
154
+ end
155
+ end
156
+
157
+ def leaves(page)
158
+ for index in 0..page.nentries - 1
159
+ header = BINTERNAL_HEADER.decode(page.buf[entryOffset(index, page), (SIZEOF_BINTERNAL_HEADER)])
160
+
161
+ btpage = MiqBdbPage.getPage(self, header['pgno'])
162
+ bttype = MiqBdbPage.type2string(btpage.ptype)
163
+ case bttype
164
+ when "btree internal" then leaves(btpage) { |leaf| yield leaf }
165
+ when "btree leaf" then yield btpage
166
+ else raise "Unexpected Page Type: #{bttype}"
167
+ end
168
+ end
169
+ end
170
+
171
+ def entryOffset(index, page)
172
+ i = index * OFFSET_LEN
173
+ page.data[i..i + 1].unpack('S')[0]
174
+ end
175
+
176
+ def entryType(index, page)
177
+ page.buf[entryOffset(index, page) + 2, 1].unpack('C')[0] & ~B_DELETE
178
+ end
179
+
180
+ def entryDataImmediate(index, page)
181
+ # B_KEYDATA
182
+ # 00-01: Key/data item length
183
+ # 02: Page type AND DELETE FLAG
184
+ # Data Follows
185
+ len = page.buf[entryOffset(index, page), 2].unpack('S')[0]
186
+ page.buf[entryOffset(index, page) + 3, len].unpack("A#{len}")[0]
187
+ end
188
+
189
+ def entryDataOverflow(index, page)
190
+ # B_DUPLICATE and B_OVERFLOW
191
+ # 00-01: Padding, unused
192
+ # 02: Page type AND DELETE FLAG
193
+ # 03: Padding, unused
194
+ # 04-07: Next page number
195
+ # 08-11: Total length of item
196
+ pgno = page.buf[entryOffset(index, page) + 4, 4].unpack("S1")[0]
197
+ # Not using the following at the moment
198
+ # tlen = page.buf[entryOffset(index,page)+8,4].unpack("S1")[0]
199
+
200
+ data = ""
201
+ while pgno != 0
202
+ opage = MiqBdbPage.getPage(self, pgno)
203
+ data << opage.data[0, opage.offset]
204
+ pgno = opage.next
205
+ end
206
+ data
207
+ end
208
+
209
+ def entryData(index, page)
210
+ type = entryType(index, page)
211
+ case type
212
+ when B_KEYDATA then return entryDataImmediate(index, page)
213
+ when B_OVERFLOW then return entryDataOverflow(index, page)
214
+ when B_DUPLICATE then raise "Unsupported Type: B_DUPLICATE"
215
+ else raise "Unknown Type: #{type}"
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,199 @@
1
+ require 'binary_struct'
2
+ require 'util/miq-hash_struct'
3
+
4
+ require_relative 'MiqBdbPage'
5
+
6
+ module MiqBerkeleyDB
7
+ class MiqBdbHashDatabase
8
+ # Hash constants.
9
+ DB_HASH_DUP = 0x01 # Duplicates.
10
+ DB_HASH_SUBDB = 0x02 # Subdatabases.
11
+ DB_HASH_DUPSORT = 0x04 # Duplicates are sorted.
12
+
13
+ HASH_HEADER = BinaryStruct.new([
14
+ 'L', 'max_bucket', # 72-75: ID of Maximum bucket in use.
15
+ 'L', 'high_mask', # 76-79: Modulo mask into table.
16
+ 'L', 'low_mask', # 80-83: Modulo mask into table lower half.
17
+ 'L', 'ffactor', # 84-87: Fill factor.
18
+ 'L', 'nelem', # 88-91: Number of keys in hash table.
19
+ 'L', 'h_charkey', # 92-95: Value of hash(CHARKEY).
20
+ 'a128', 'spares', # 96-223: Spare pages for overflow.
21
+ 'a236', 'unused', # 224-459: Unused space.
22
+ 'L', 'crypto_magic', # 460-463: Crypto magic number.
23
+ 'a12', 'trash', # 464-475: Trash space - Do not use.
24
+ 'a16', 'iv', # 476-495: Crypto IV.
25
+ 'a20', 'chksum', # 496-511: Page chksum.
26
+ # NOTE: There is a discrepency between the last two members.
27
+ # Offset notes show these as 20 and 16 bytes, when they are
28
+ # in fact 16 and 20 bytes respectively.
29
+ ])
30
+
31
+ SIZEOF_HASH_HEADER = HASH_HEADER.size
32
+
33
+ # Each index references a group of bytes on the page.
34
+ H_KEYDATA = 1 # Key/data item.
35
+ H_DUPLICATE = 2 # Duplicate key/data item.
36
+ H_OFFPAGE = 3 # Overflow key/data item.
37
+ H_OFFDUP = 4 # Overflow page of duplicates.
38
+
39
+ OFFSET_LEN = 2
40
+
41
+ attr_reader :bdb, :header
42
+
43
+ def initialize(bdb)
44
+ # Read pointer is positioned to hash header.
45
+ @bdb = bdb
46
+ @header = HASH_HEADER.decode(@bdb.read(SIZEOF_HASH_HEADER))
47
+ @header['spares'] = @header['spares'].unpack('L*')
48
+ end
49
+
50
+ def close
51
+ @bdb = @header = nil
52
+ end
53
+
54
+ def npages
55
+ @bdb.npages
56
+ end
57
+
58
+ def nkeys
59
+ @header['nelem']
60
+ end
61
+
62
+ def pages
63
+ 0.upto(@header['max_bucket']) do |b|
64
+ pagenum = bucket2page(b)
65
+ while page = MiqBdbPage.getPage(self, pagenum)
66
+ yield page
67
+
68
+ pagenum = page.next
69
+ end
70
+ end
71
+ end
72
+
73
+ def keys(page)
74
+ i = 0
75
+ while i < page.nentries
76
+ key = entryData(i, page)
77
+
78
+ yield key
79
+ i += 2 # skip value
80
+ end
81
+ end
82
+
83
+ def values(page)
84
+ i = 0
85
+ while i < page.nentries
86
+ value = entryData(i + 1, page)
87
+
88
+ yield value
89
+ i += 2 # skip key
90
+ end
91
+ end
92
+
93
+ def pairs(page)
94
+ i = 0
95
+ while i < page.nentries
96
+ key = entryData(i, page)
97
+ value = entryData(i + 1, page)
98
+
99
+ yield key, value
100
+ i += 2
101
+ end
102
+ end
103
+
104
+ def dump
105
+ out = ""
106
+ out << "Hash Database Header\n"
107
+ out << " nkeys: #{@header['nelem']}\n"
108
+ out << " high_mask: 0x#{'%01x' % @header['high_mask']}\n"
109
+ out << " low_mask: 0x#{'%01x' % @header['low_mask']}\n"
110
+ out << " ffactor: #{@header['ffactor']}\n"
111
+ out << " h_charkey: 0x#{'%08x' % @header['h_charkey']}\n"
112
+
113
+ out << " spare points: "
114
+ @header['spares'].each { |s| out << s.to_s << " " }
115
+ out << "\n"
116
+
117
+ out << "\n"
118
+ out
119
+ end
120
+
121
+ private
122
+
123
+ #
124
+ # The spares table indicates the page number at which each doubling begins.
125
+ # From this page number we subtract the number of buckets already allocated
126
+ # so that we can do a simple addition to calculate the page number here.
127
+ #
128
+
129
+ def bucket2page(bucket)
130
+ bucket + @header['spares'][MiqBdbHashDatabase.log2(bucket + 1)]
131
+ end
132
+
133
+ def self.log2(num)
134
+ limit = 1
135
+ i = 0
136
+ while limit < num
137
+ limit <<= 1
138
+ i += 1
139
+ end
140
+
141
+ i
142
+ end
143
+
144
+ def entryDataOverflow(_index, offset, page)
145
+ pgno = page.buf[offset + 4, 4].unpack("S1")[0]
146
+ # Not using the following at the moment
147
+ # tlen = page.buf[offset + 8, 4].unpack("S1")[0]
148
+
149
+ data = ""
150
+ while pgno != 0
151
+ opage = MiqBdbPage.getPage(self, pgno)
152
+ data << opage.data[0, opage.offset]
153
+ pgno = opage.next
154
+ end
155
+ data
156
+ end
157
+
158
+ def entryDataImmediate(index, offset, page)
159
+ len = entryLen(index, offset, page)
160
+ data = page.buf[offset + 1, len].unpack("A#{len - 1}")[0]
161
+ data
162
+ end
163
+
164
+ def entryData(index, page)
165
+ # $log.debug "Getting index #{index} (#{index + 1} / #{page.nentries}) for page #{page.current} (#{page.current + 1} / #{npages})"
166
+
167
+ type, offset = entryTypeAndOffset(index, page)
168
+ data = case type
169
+ when H_KEYDATA then entryDataImmediate(index, offset, page)
170
+ when H_OFFPAGE then entryDataOverflow(index, offset, page)
171
+ when H_DUPLICATE then raise "Unsupported Type: H_DUPLICATE"
172
+ when H_OFFDUP then raise "Unsupported Type: H_OFFDUP"
173
+ else raise "Unknown Type: #{type.inspect}"
174
+ end
175
+
176
+ data
177
+ end
178
+
179
+ def entryTypeAndOffset(index, page)
180
+ offset = entryOffset(index, page)
181
+ type = entryType(offset, page)
182
+ return type, offset
183
+ end
184
+
185
+ def entryType(offset, page)
186
+ page.buf[offset, 1].ord
187
+ end
188
+
189
+ def entryOffset(index, page)
190
+ i = index * OFFSET_LEN
191
+ page.data[i, 2].unpack('S')[0]
192
+ end
193
+
194
+ def entryLen(index, offset, page)
195
+ item_end = index == 0 ? page.pagesize : entryOffset(index - 1, page)
196
+ (item_end - offset)
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,159 @@
1
+ require 'enumerator'
2
+
3
+ require 'binary_struct'
4
+ require 'util/miq-hash_struct'
5
+
6
+ module MiqBerkeleyDB
7
+ class MiqBdbPage
8
+ # Page numbers.
9
+ PGNO_INVALID = 0
10
+ PGNO_BASE_MD = 0
11
+
12
+ # Page types
13
+ P_INVALID = 0 # Invalid page type
14
+ P_DUPLICATE = 1 # Duplicate. DEPRECATED in 3.1
15
+ P_HASH = 2 # Hash
16
+ P_IBTREE = 3 # Btree internal
17
+ P_IRECNO = 4 # Recno internal
18
+ P_LBTREE = 5 # Btree leaf
19
+ P_LRECNO = 6 # Recno leaf
20
+ P_OVERFLOW = 7 # Overflow
21
+ P_HASHMETA = 8 # Hash metadata page
22
+ P_BTREEMETA = 9 # Btree metadata page
23
+ P_QAMMETA = 10 # Queue metadata page
24
+ P_QAMDATA = 11 # Queue data page
25
+ P_LDUP = 12 # Off-page duplicate leaf
26
+
27
+ def self.type2string(t)
28
+ case t
29
+ when P_INVALID then "invalid"
30
+ when P_DUPLICATE then "duplicate (deprecated)"
31
+ when P_HASH then "hash"
32
+ when P_IBTREE then "btree internal"
33
+ when P_IRECNO then "recno internal"
34
+ when P_LBTREE then "btree leaf"
35
+ when P_LRECNO then "recno leaf"
36
+ when P_OVERFLOW then "overflow"
37
+ when P_HASHMETA then "hash metadata"
38
+ when P_BTREEMETA then "btree metadata"
39
+ when P_QAMMETA then "queue metadata"
40
+ when P_QAMDATA then "queue data"
41
+ when P_LDUP then "offpage duplicate leaf"
42
+ else "unknown value of #{t}"
43
+ end
44
+ end
45
+
46
+ # Page Header.
47
+ HEADER = BinaryStruct.new([
48
+ # 00-07: Log sequence number (LSN)
49
+ 'L', 'lsn_file', # 0-3: LSN File
50
+ 'L', 'lsn_offset', # 4-7: LSN Offset
51
+ 'L', 'pgno', # 08-11: Current page number
52
+ 'L', 'prev_pgno', # 12-15: Previous page number
53
+ 'L', 'next_pgno', # 16-19: Next page number
54
+ 'S', 'entries', # 20-21: Number of items on the page
55
+ 'S', 'hf_offset', # 22-23: High free byte page offset
56
+ 'C', 'level', # 24: Btree tree level
57
+ 'C', 'p_type' # 25: Page type
58
+ ])
59
+
60
+ SIZEOF_HEADER = HEADER.size
61
+
62
+ #
63
+ # With many compilers sizeof(PAGE) == 28, while SIZEOF_PAGE == 26.
64
+ # We add in other things directly after the page header and need
65
+ # the SIZEOF_PAGE. When giving the sizeof(), many compilers will
66
+ # pad it out to the next 4-byte boundary.
67
+ #
68
+ SIZEOF_PAGE = 26
69
+
70
+ attr_reader :header, :data, :pagesize, :buf
71
+
72
+ def self.getPage(db, pagenum)
73
+ return nil if pagenum == PGNO_INVALID
74
+ # return nil if pagenum >= db.npages
75
+
76
+ buf = db.bdb.readPage(pagenum)
77
+ return nil if buf.nil? || buf[25, 1].ord == P_INVALID
78
+ MiqBdbPage.new(buf, db)
79
+ end
80
+
81
+ def initialize(buf, db)
82
+ raise "Nil buffer." if buf.nil?
83
+ @db = db
84
+ @buf = buf
85
+ @pagesize = buf.size
86
+ @header = HEADER.decode(@buf[0, SIZEOF_HEADER])
87
+ @data = buf[SIZEOF_HEADER..-1]
88
+ end
89
+
90
+ def current
91
+ @header['pgno']
92
+ end
93
+
94
+ def next
95
+ @header['next_pgno']
96
+ end
97
+
98
+ def prev
99
+ @header['prev_pgno']
100
+ end
101
+
102
+ def nentries
103
+ @header['entries']
104
+ end
105
+
106
+ def offset
107
+ @header['hf_offset']
108
+ end
109
+
110
+ def level
111
+ @header['level']
112
+ end
113
+
114
+ def ptype
115
+ @header['p_type']
116
+ end
117
+
118
+ def keys
119
+ @db.keys(self) { |k| yield k }
120
+ end
121
+
122
+ def values
123
+ @db.values(self) { |v| yield v }
124
+ end
125
+
126
+ def pairs
127
+ @db.pairs(self) { |k, v| yield k, v }
128
+ end
129
+
130
+ # Dump page statistics like db_dump.
131
+ def dump
132
+ out = ""
133
+ out << "Page #{current}\n"
134
+ out << " type: #{MiqBdbPage.type2string(ptype)}\n"
135
+ out << " prev: #{prev}\n"
136
+ out << " next: #{@header['next_pgno']}\n"
137
+ out << " log seq num: file=#{@header['lsn_file']} offset=#{@header['lsn_offset']}\n"
138
+ out << " level: #{level}\n"
139
+
140
+ if @header['p_type'] == P_OVERFLOW
141
+ out << " ref cnt: #{nentries}\n"
142
+ out << " len: #{offset}\n"
143
+ else
144
+ out << " entries: #{nentries}\n"
145
+ out << " offset: #{offset}\n"
146
+ end
147
+ out << " data size: #{@data.size}\n"
148
+ out << " data: "
149
+
150
+ @data.bytes.take(20).each do |c|
151
+ out << sprintf("%.2x ", c)
152
+ end
153
+ out << "..." if @data.size > 20
154
+
155
+ out << "\n\n"
156
+ out
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'MiqBdb'
2
+ require_relative 'MiqBdbHash'
3
+ require_relative 'MiqBdbPage'
4
+
5
+ module MiqBerkeleyDB
6
+ class MiqBdbUtil
7
+ def initialize(fs = nil)
8
+ @fs = fs
9
+ end
10
+
11
+ def getkeys(fname)
12
+ bdb = MiqBdb.new(fname, @fs)
13
+ keys = bdb.keys
14
+ bdb.close
15
+ keys
16
+ end
17
+ end
18
+ end