1 # SPDX-FileCopyrightText: 2015 Eric Larson
3 # SPDX-License-Identifier: Apache-2.0
7 from textwrap import dedent
9 from ..cache import BaseCache, SeparateBodyBaseCache
10 from ..controller import CacheController
16 FileNotFoundError = (IOError, OSError)
19 def _secure_open_write(filename, fmode):
20 # We only want to write to this file, so open it in write only mode
23 # os.O_CREAT | os.O_EXCL will fail if the file already exists, so we only
24 # will open *new* files.
25 # We specify this because we want to ensure that the mode we pass is the
27 flags |= os.O_CREAT | os.O_EXCL
29 # Do not follow symlinks to prevent someone from making a symlink that
30 # we follow and insecurely open a cache file.
31 if hasattr(os, "O_NOFOLLOW"):
32 flags |= os.O_NOFOLLOW
34 # On Windows we'll mark this file as binary
35 if hasattr(os, "O_BINARY"):
38 # Before we open our file, we want to delete any existing file that is
42 except (IOError, OSError):
43 # The file must not exist already, so we can just skip ahead to opening
46 # Open our file, the use of os.O_CREAT | os.O_EXCL will ensure that if a
47 # race condition happens between the os.remove and this line, that an
48 # error will be raised. Because we utilize a lockfile this should only
49 # happen if someone is attempting to attack us.
50 fd = os.open(filename, flags, fmode)
52 return os.fdopen(fd, "wb")
55 # An error occurred wrapping our FD in a file object
60 class _FileCacheMixin:
61 """Shared implementation for both FileCache variants."""
73 if use_dir_lock is not None and lock_class is not None:
74 raise ValueError("Cannot use use_dir_lock and lock_class together")
77 from lockfile import LockFile
78 from lockfile.mkdirlockfile import MkdirLockFile
82 NOTE: In order to use the FileCache you must have
83 lockfile installed. You can install it via pip:
87 raise ImportError(notice)
91 lock_class = MkdirLockFile
93 elif lock_class is None:
96 self.directory = directory
97 self.forever = forever
98 self.filemode = filemode
99 self.dirmode = dirmode
100 self.lock_class = lock_class
104 return hashlib.sha224(x.encode()).hexdigest()
107 # NOTE: This method should not change as some may depend on it.
108 # See: https://github.com/ionrock/cachecontrol/issues/63
109 hashed = self.encode(name)
110 parts = list(hashed[:5]) + [hashed]
111 return os.path.join(self.directory, *parts)
116 with open(name, "rb") as fh:
119 except FileNotFoundError:
122 def set(self, key, value, expires=None):
124 self._write(name, value)
126 def _write(self, path, data: bytes):
128 Safely write the data to the given path.
130 # Make sure the directory exists
132 os.makedirs(os.path.dirname(path), self.dirmode)
133 except (IOError, OSError):
136 with self.lock_class(path) as lock:
137 # Write our actual file
138 with _secure_open_write(lock.path, self.filemode) as fh:
141 def _delete(self, key, suffix):
142 name = self._fn(key) + suffix
146 except FileNotFoundError:
150 class FileCache(_FileCacheMixin, BaseCache):
152 Traditional FileCache: body is stored in memory, so not suitable for large
156 def delete(self, key):
157 self._delete(key, "")
160 class SeparateBodyFileCache(_FileCacheMixin, SeparateBodyBaseCache):
162 Memory-efficient FileCache: body is stored in a separate file, reducing
166 def get_body(self, key):
167 name = self._fn(key) + ".body"
169 return open(name, "rb")
170 except FileNotFoundError:
173 def set_body(self, key, body):
174 name = self._fn(key) + ".body"
175 self._write(name, body)
177 def delete(self, key):
178 self._delete(key, "")
179 self._delete(key, ".body")
182 def url_to_file_path(url, filecache):
183 """Return the file cache path based on the URL.
185 This does not ensure the file exists!
187 key = CacheController.cache_url(url)
188 return filecache._fn(key)