Explorar o código

chunker algorithms: use constants to avoid typos

Thomas Waldmann %!s(int64=6) %!d(string=hai) anos
pai
achega
ac0803fe0b
Modificáronse 3 ficheiros con 10 adicións e 6 borrados
  1. 1 1
      src/borg/archive.py
  2. 6 2
      src/borg/constants.py
  3. 3 3
      src/borg/helpers/parseformat.py

+ 1 - 1
src/borg/archive.py

@@ -1922,7 +1922,7 @@ class ArchiveRecreater:
         source_chunker_params = tuple(archive.metadata.get('chunker_params', []))
         if len(source_chunker_params) == 4 and isinstance(source_chunker_params[0], int):
             # this is a borg < 1.2 chunker_params tuple, no chunker algo specified, but we only had buzhash:
-            source_chunker_params = ('buzhash', ) + source_chunker_params
+            source_chunker_params = (CH_BUZHASH, ) + source_chunker_params
         target.recreate_rechunkify = self.rechunkify and source_chunker_params != target.chunker_params
         if target.recreate_rechunkify:
             logger.debug('Rechunking archive from %s to %s', source_chunker_params or '(unknown)', target.chunker_params)

+ 6 - 2
src/borg/constants.py

@@ -59,11 +59,15 @@ CHUNK_MAX_EXP = 23  # 2**23 == 8MiB
 HASH_WINDOW_SIZE = 0xfff  # 4095B
 HASH_MASK_BITS = 21  # results in ~2MiB chunks statistically
 
+# chunker algorithms
+CH_BUZHASH = 'buzhash'
+CH_FIXED = 'fixed'
+
 # defaults, use --chunker-params to override
-CHUNKER_PARAMS = ('buzhash', CHUNK_MIN_EXP, CHUNK_MAX_EXP, HASH_MASK_BITS, HASH_WINDOW_SIZE)
+CHUNKER_PARAMS = (CH_BUZHASH, CHUNK_MIN_EXP, CHUNK_MAX_EXP, HASH_MASK_BITS, HASH_WINDOW_SIZE)
 
 # chunker params for the items metadata stream, finer granularity
-ITEMS_CHUNKER_PARAMS = ('buzhash', 15, 19, 17, HASH_WINDOW_SIZE)
+ITEMS_CHUNKER_PARAMS = (CH_BUZHASH, 15, 19, 17, HASH_WINDOW_SIZE)
 
 # operating mode of the files cache (for fast skipping of unchanged files)
 DEFAULT_FILES_CACHE_MODE_UI = 'ctime,size,inode'

+ 3 - 3
src/borg/helpers/parseformat.py

@@ -113,7 +113,7 @@ def ChunkerParams(s):
     if count == 0:
         raise ValueError('no chunker params given')
     algo = params[0].lower()
-    if algo == 'fixed' and 2 <= count <= 3:  # fixed, block_size[, header_size]
+    if algo == CH_FIXED and 2 <= count <= 3:  # fixed, block_size[, header_size]
         block_size = int(params[1])
         header_size = int(params[2]) if count == 3 else 0
         if block_size < 64:
@@ -129,7 +129,7 @@ def ChunkerParams(s):
     if algo == 'default' and count == 1:  # default
         return CHUNKER_PARAMS
     # this must stay last as it deals with old-style compat mode (no algorithm, 4 params, buzhash):
-    if algo == 'buzhash' and count == 5 or count == 4:  # [buzhash, ]chunk_min, chunk_max, chunk_mask, window_size
+    if algo == CH_BUZHASH and count == 5 or count == 4:  # [buzhash, ]chunk_min, chunk_max, chunk_mask, window_size
         chunk_min, chunk_max, chunk_mask, window_size = [int(p) for p in params[count - 4:]]
         if not (chunk_min <= chunk_mask <= chunk_max):
             raise ValueError('required: chunk_min <= chunk_mask <= chunk_max')
@@ -138,7 +138,7 @@ def ChunkerParams(s):
             raise ValueError('min. chunk size exponent must not be less than 6 (2^6 = 64B min. chunk size)')
         if chunk_max > 23:
             raise ValueError('max. chunk size exponent must not be more than 23 (2^23 = 8MiB max. chunk size)')
-        return 'buzhash', chunk_min, chunk_max, chunk_mask, window_size
+        return CH_BUZHASH, chunk_min, chunk_max, chunk_mask, window_size
     raise ValueError('invalid chunker params')