Skip to content

Commit

Permalink
fix cache trashing by adding fs_safe_id function
Browse files Browse the repository at this point in the history
ids are not guranteed to be file system safe so we have to convert
them when we use them to generate file names
  • Loading branch information
tgbugs committed Nov 21, 2020
1 parent bad4ce3 commit 8488ac1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 5 additions & 4 deletions augpathlib/caches.py
Expand Up @@ -3,7 +3,7 @@
from augpathlib import exceptions as exc
from augpathlib.meta import PathMeta
from augpathlib.core import AugmentedPath, EatPath
from augpathlib.utils import log, default_cypher
from augpathlib.utils import log, default_cypher, fs_safe_id
from augpathlib.utils import LOCAL_DATA_DIR, SPARSE_MARKER
from augpathlib import remotes

Expand Down Expand Up @@ -150,7 +150,7 @@ def trash(self):

@property
def _trashed_path(self):
return self.trash / f'{self.parent.id}-{self.id}-{self.name}'
return self.trash / fs_safe_id(f'{self.parent.id}-{self.id}-{self.name}')

@property
def _trashed_path_short(self):
Expand Down Expand Up @@ -1062,8 +1062,9 @@ def meta(self, pathmeta):

# trash old versions instead of just unlinking
pc = self.local.cache
trash = pc.trash
self.rename(trash / f'{pc.parent.id}-{meta.id}-{self.name}') # FIXME broken on windows
self.rename(pc._trashed_path)
#trash = pc.trash
#self.rename(trash / fs_safe_id(f'{pc.parent.id}-{meta.id}-{self.name}'))
#self.unlink()

# FIXME if an id starts with / then the local name is overwritten due to pathlib logic
Expand Down
12 changes: 12 additions & 0 deletions augpathlib/utils.py
Expand Up @@ -38,6 +38,18 @@ def onerror_windows_readwrite_remove(action, name, exc):
os.remove(name)


def fs_safe_id(string):
""" Make a string safe for use on file systems.
NOTE this does NOT gurantee uniqueness!
NOTE this only deals with bad printable chars not
the full pathology that one might encounter. """
# see https://stackoverflow.com/questions/1976007/
evils = ' \n\t*|:\/<>"\\?'
for evil in evils:
string = string.replace(evil, '-')
return string


def sysidpath(ignore_options=False, path_class=Path):
""" get a unique identifier for the machine running this function """
# in the event we have to make our own
Expand Down

0 comments on commit 8488ac1

Please sign in to comment.