WeiHuang95 commited on
Commit
b86f022
·
verified ·
1 Parent(s): 5316050

Validate existing LMDB keys without scanning raw source paths

Browse files
Files changed (1) hide show
  1. unibuild_lmdb.py +8 -5
unibuild_lmdb.py CHANGED
@@ -25,9 +25,6 @@ def split_keys(data_root, split_root):
25
  if (len(rel.parts) < 2 or rel.is_absolute() or '..' in rel.parts
26
  or '\\' in key or ':' in key or rel.as_posix() != key):
27
  raise ValueError(f'Unsafe LMDB key: {key}')
28
- source = data_root.joinpath(*rel.parts)
29
- if not source.resolve().is_relative_to(data_root):
30
- raise ValueError(f'Input leaves dataset root: {key}')
31
  groups[rel.parts[0]].add(key)
32
  if not pairs:
33
  raise ValueError(f'Empty splits: {split_root}')
@@ -44,8 +41,12 @@ def verify_database(path, data_root, keys, compare_bytes=False):
44
  for key in keys:
45
  if not cursor.set_key(key.encode('utf-8')):
46
  raise KeyError(f'Missing key {key} in {path}')
47
- if compare_bytes and bytes(cursor.value()) != (Path(data_root) / key).read_bytes():
48
- raise ValueError(f'LMDB bytes differ from source: {path}: {key}')
 
 
 
 
49
  finally:
50
  env.close()
51
 
@@ -71,6 +72,8 @@ def ensure_dataset_lmdb(data_root, split_root, compare_existing=False):
71
  sizes = []
72
  for key in keys:
73
  source = root / key
 
 
74
  if not source.is_file():
75
  raise FileNotFoundError(source)
76
  sizes.append(source.stat().st_size)
 
25
  if (len(rel.parts) < 2 or rel.is_absolute() or '..' in rel.parts
26
  or '\\' in key or ':' in key or rel.as_posix() != key):
27
  raise ValueError(f'Unsafe LMDB key: {key}')
 
 
 
28
  groups[rel.parts[0]].add(key)
29
  if not pairs:
30
  raise ValueError(f'Empty splits: {split_root}')
 
41
  for key in keys:
42
  if not cursor.set_key(key.encode('utf-8')):
43
  raise KeyError(f'Missing key {key} in {path}')
44
+ if compare_bytes:
45
+ source = Path(data_root) / key
46
+ if not source.resolve().is_relative_to(Path(data_root).resolve()):
47
+ raise ValueError(f'Input leaves dataset root: {key}')
48
+ if bytes(cursor.value()) != source.read_bytes():
49
+ raise ValueError(f'LMDB bytes differ from source: {path}: {key}')
50
  finally:
51
  env.close()
52
 
 
72
  sizes = []
73
  for key in keys:
74
  source = root / key
75
+ if not source.resolve().is_relative_to(root):
76
+ raise ValueError(f'Input leaves dataset root: {key}')
77
  if not source.is_file():
78
  raise FileNotFoundError(source)
79
  sizes.append(source.stat().st_size)