]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/vfs.py
vfs.copy_item: don't try to copy an integer mode
[bup.git] / lib / bup / vfs.py
index f231f4d790ddd48913dd1c2ad8025a33aa28b4db..1c67577f000b6c458c781edd7086e050fe1d57e5 100644 (file)
@@ -13,7 +13,9 @@ Each path is represented by an item that has least an item.meta which
 may be either a Metadata object, or an integer mode.  Functions like
 item_mode() and item_size() will return the mode and size in either
 case.  Any item.meta Metadata instances must not be modified directly.
-Make a copy to modify via item.meta.copy() if needed.
+Make a copy to modify via item.meta.copy() if needed, or call
+copy_item().
+
 
 The want_meta argument is advisory for calls that accept it, and it
 may not be honored.  Callers must be able to handle an item.meta value
@@ -49,11 +51,13 @@ from __future__ import absolute_import, print_function
 from collections import namedtuple
 from errno import ELOOP, ENOENT, ENOTDIR
 from itertools import chain, dropwhile, groupby, izip, tee
+from random import randrange
 from stat import S_IFDIR, S_IFLNK, S_IFREG, S_ISDIR, S_ISLNK, S_ISREG
 from time import localtime, strftime
 import exceptions, re, sys
 
 from bup import client, git, metadata
+from bup.compat import range
 from bup.git import BUP_CHUNKED, cp, get_commit_items, parse_commit, tree_decode
 from bup.helpers import debug2, last
 from bup.metadata import Metadata
@@ -272,13 +276,15 @@ def cache_notice(key, value):
     assert is_valid_cache_key(key)
     if key in _cache:
         return
-    _cache[key] = value
     if len(_cache) < _cache_max_items:
+        _cache_keys.append(key)
+        _cache[key] = value
         return
-    victim_i = random.randrange(0, len(_cache_keys))
+    victim_i = randrange(0, len(_cache_keys))
     victim = _cache_keys[victim_i]
+    del _cache[victim]
     _cache_keys[victim_i] = key
-    _cache.pop(victim)
+    _cache[key] = value
 
 
 def cache_get_commit_item(oid, need_meta=True):
@@ -308,9 +314,9 @@ def copy_item(item):
 
     """
     meta = getattr(item, 'meta', None)
-    if not meta:
-        return item
-    return(item._replace(meta=meta.copy()))
+    if isinstance(meta, Metadata):
+        return(item._replace(meta=meta.copy()))
+    return item
 
 def item_mode(item):
     """Return the integer mode (stat st_mode) for item."""
@@ -576,7 +582,7 @@ def _reverse_suffix_duplicates(strs):
         else:
             ndig = len(str(ndup - 1))
             fmt = '%s-' + '%0' + str(ndig) + 'd'
-            for i in xrange(ndup - 1, -1, -1):
+            for i in range(ndup - 1, -1, -1):
                 yield fmt % (name, i)
 
 def parse_rev(f):