2014-04-23 06:24:08 +00:00
|
|
|
import hashlib
|
2014-03-27 21:06:03 +00:00
|
|
|
import os
|
|
|
|
|
2016-01-12 00:00:58 +00:00
|
|
|
from lockfile import LockFile
|
|
|
|
from lockfile.mkdirlockfile import MkdirLockFile
|
2014-03-27 21:06:03 +00:00
|
|
|
|
2015-04-28 17:32:10 +00:00
|
|
|
from ..cache import BaseCache
|
|
|
|
from ..controller import CacheController
|
|
|
|
|
2014-03-27 21:06:03 +00:00
|
|
|
|
2014-04-23 06:24:08 +00:00
|
|
|
def _secure_open_write(filename, fmode):
|
|
|
|
# We only want to write to this file, so open it in write only mode
|
|
|
|
flags = os.O_WRONLY
|
|
|
|
|
|
|
|
# os.O_CREAT | os.O_EXCL will fail if the file already exists, so we only
|
|
|
|
# will open *new* files.
|
|
|
|
# We specify this because we want to ensure that the mode we pass is the
|
|
|
|
# mode of the file.
|
|
|
|
flags |= os.O_CREAT | os.O_EXCL
|
|
|
|
|
|
|
|
# Do not follow symlinks to prevent someone from making a symlink that
|
|
|
|
# we follow and insecurely open a cache file.
|
|
|
|
if hasattr(os, "O_NOFOLLOW"):
|
|
|
|
flags |= os.O_NOFOLLOW
|
|
|
|
|
|
|
|
# On Windows we'll mark this file as binary
|
|
|
|
if hasattr(os, "O_BINARY"):
|
|
|
|
flags |= os.O_BINARY
|
|
|
|
|
|
|
|
# Before we open our file, we want to delete any existing file that is
|
|
|
|
# there
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except (IOError, OSError):
|
|
|
|
# The file must not exist already, so we can just skip ahead to opening
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Open our file, the use of os.O_CREAT | os.O_EXCL will ensure that if a
|
|
|
|
# race condition happens between the os.remove and this line, that an
|
|
|
|
# error will be raised. Because we utilize a lockfile this should only
|
|
|
|
# happen if someone is attempting to attack us.
|
|
|
|
fd = os.open(filename, flags, fmode)
|
|
|
|
try:
|
|
|
|
return os.fdopen(fd, "wb")
|
|
|
|
except:
|
|
|
|
# An error occurred wrapping our FD in a file object
|
|
|
|
os.close(fd)
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2015-04-28 17:32:10 +00:00
|
|
|
class FileCache(BaseCache):
|
2014-04-23 06:24:08 +00:00
|
|
|
def __init__(self, directory, forever=False, filemode=0o0600,
|
2016-01-12 00:00:58 +00:00
|
|
|
dirmode=0o0700, use_dir_lock=None, lock_class=None):
|
|
|
|
|
|
|
|
if use_dir_lock is not None and lock_class is not None:
|
|
|
|
raise ValueError("Cannot use use_dir_lock and lock_class together")
|
|
|
|
|
|
|
|
if use_dir_lock:
|
|
|
|
lock_class = MkdirLockFile
|
|
|
|
|
|
|
|
if lock_class is None:
|
|
|
|
lock_class = LockFile
|
|
|
|
|
2014-03-27 21:06:03 +00:00
|
|
|
self.directory = directory
|
|
|
|
self.forever = forever
|
2014-04-23 06:24:08 +00:00
|
|
|
self.filemode = filemode
|
2015-04-28 17:32:10 +00:00
|
|
|
self.dirmode = dirmode
|
2016-01-12 00:00:58 +00:00
|
|
|
self.lock_class = lock_class
|
|
|
|
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def encode(x):
|
2014-04-23 06:24:08 +00:00
|
|
|
return hashlib.sha224(x.encode()).hexdigest()
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
def _fn(self, name):
|
2015-04-28 17:32:10 +00:00
|
|
|
# NOTE: This method should not change as some may depend on it.
|
|
|
|
# See: https://github.com/ionrock/cachecontrol/issues/63
|
|
|
|
hashed = self.encode(name)
|
|
|
|
parts = list(hashed[:5]) + [hashed]
|
|
|
|
return os.path.join(self.directory, *parts)
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
name = self._fn(key)
|
|
|
|
if not os.path.exists(name):
|
|
|
|
return None
|
|
|
|
|
|
|
|
with open(name, 'rb') as fh:
|
2014-04-23 06:24:08 +00:00
|
|
|
return fh.read()
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
def set(self, key, value):
|
|
|
|
name = self._fn(key)
|
2015-04-28 17:32:10 +00:00
|
|
|
|
|
|
|
# Make sure the directory exists
|
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(name), self.dirmode)
|
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
2016-01-12 00:00:58 +00:00
|
|
|
with self.lock_class(name) as lock:
|
2015-04-28 17:32:10 +00:00
|
|
|
# Write our actual file
|
2014-04-23 06:24:08 +00:00
|
|
|
with _secure_open_write(lock.path, self.filemode) as fh:
|
|
|
|
fh.write(value)
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
def delete(self, key):
|
|
|
|
name = self._fn(key)
|
2015-06-15 01:57:36 +00:00
|
|
|
if not self.forever and os.path.exists(name):
|
2014-04-23 06:24:08 +00:00
|
|
|
os.remove(name)
|
2015-04-28 17:32:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def url_to_file_path(url, filecache):
|
|
|
|
"""Return the file cache path based on the URL.
|
|
|
|
|
|
|
|
This does not ensure the file exists!
|
|
|
|
"""
|
|
|
|
key = CacheController.cache_url(url)
|
|
|
|
return filecache._fn(key)
|