Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion diskcache/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def __repr__(self):
ENOVAL = Constant('ENOVAL')
UNKNOWN = Constant('UNKNOWN')

CACHEDIR_TAG = 'CACHEDIR.TAG'
CACHEDIR_TAG_CONTENTS = (
b'Signature: 8a477f597d28d172789f06886806bc55\n'
b'# This file is a cache directory tag automatically created by diskcache.\n'
b'# For information about cache directory tags, see:\n'
b'#\thttps://bford.info/cachedir/\n'
)

MODE_NONE = 0
MODE_RAW = 1
MODE_BINARY = 2
Expand Down Expand Up @@ -453,6 +461,14 @@ def __init__(self, directory=None, timeout=60, disk=Disk, **settings):
' and could not be created' % self._directory,
) from None

tag_path = op.join(directory, CACHEDIR_TAG)
if not op.exists(tag_path):
try:
with open(tag_path, 'xb') as writer:
writer.write(CACHEDIR_TAG_CONTENTS)
except OSError:
pass

sql = self._sql_retry

# Setup Settings table.
Expand Down Expand Up @@ -1967,7 +1983,7 @@ def check(self, fix=False, retry=False):
error = set(paths) - filenames

for full_path in error:
if DBNAME in full_path:
if DBNAME in full_path or CACHEDIR_TAG in full_path:
continue

message = 'unknown file: %s' % full_path
Expand Down
11 changes: 11 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,17 @@ def test_least_frequently_used(cache):
assert len(cache.check()) == 0


def test_cachedir_tag(cache):
tag_path = op.join(cache.directory, dc.core.CACHEDIR_TAG)
assert op.exists(tag_path)
with open(tag_path, 'rb') as reader:
assert reader.read().startswith(b'Signature: 8a477f597d28d172789f06886806bc55\n')

# The tag file should not be reported (or removed) as an unknown file.
assert len(cache.check(fix=True)) == 0
assert op.exists(tag_path)


def test_check(cache):
blob = b'a' * 2**20
keys = (0, 1, 1234, 56.78, 'hello', b'world', None)
Expand Down