Skip to content

Commit af1afdd

Browse files
committed
Merge pull request #2 from smartfile/lockfs
lockfs
2 parents 8e6ae64 + 41ba462 commit af1afdd

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

fs/base.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ def _setcontents(self,
802802
errors=None,
803803
chunk_size=1024 * 64,
804804
progress_callback=None,
805-
finished_callback=None):
805+
finished_callback=None,
806+
bypass_lock=False):
806807
"""Does the work of setcontents. Factored out, so that `setcontents_async` can use it"""
807808
if progress_callback is None:
808809
progress_callback = lambda bytes_written: None
@@ -822,9 +823,10 @@ def _setcontents(self,
822823
read = data.read
823824
chunk = read(chunk_size)
824825
if isinstance(chunk, six.text_type):
825-
f = self.open(path, 'wt', encoding=encoding, errors=errors)
826+
f = self.open(path, 'wt', encoding=encoding, errors=errors,
827+
bypass_lock=bypass_lock)
826828
else:
827-
f = self.open(path, 'wb')
829+
f = self.open(path, 'wb', bypass_lock=bypass_lock)
828830
write = f.write
829831
try:
830832
while chunk:
@@ -848,7 +850,8 @@ def _setcontents(self,
848850
finished_callback()
849851
return bytes_written
850852

851-
def setcontents(self, path, data=b'', encoding=None, errors=None, chunk_size=1024 * 64):
853+
def setcontents(self, path, data=b'', encoding=None, errors=None,
854+
chunk_size=1024 * 64, bypass_lock=False):
852855
"""A convenience method to create a new file from a string or file-like object
853856
854857
:param path: a path of the file to create
@@ -858,7 +861,7 @@ def setcontents(self, path, data=b'', encoding=None, errors=None, chunk_size=102
858861
:param chunk_size: Number of bytes to read in a chunk, if the implementation has to resort to a read / copy loop
859862
860863
"""
861-
return self._setcontents(path, data, encoding=encoding, errors=errors, chunk_size=1024 * 64)
864+
return self._setcontents(path, data, encoding=encoding, errors=errors, chunk_size=1024 * 64, bypass_lock=bypass_lock)
862865

863866
def setcontents_async(self,
864867
path,
@@ -1143,7 +1146,8 @@ def copy(self, src, dst, overwrite=False, chunk_size=1024 * 64):
11431146
src_file = None
11441147
try:
11451148
src_file = self.open(src, "rb")
1146-
self.setcontents(dst, src_file, chunk_size=chunk_size)
1149+
self.setcontents(dst, src_file, chunk_size=chunk_size,
1150+
bypass_lock=True)
11471151
except ResourceNotFoundError:
11481152
if self.exists(src) and not self.exists(dirname(dst)):
11491153
raise ParentDirectoryMissingError(dst)
@@ -1205,8 +1209,9 @@ def move(self, src, dst, overwrite=False, chunk_size=16384):
12051209
return
12061210
except OSError:
12071211
pass
1208-
self.copy(src, dst, overwrite=overwrite, chunk_size=chunk_size)
1209-
self.remove(src)
1212+
self.copy(src, dst, overwrite=overwrite, chunk_size=chunk_size,
1213+
bypass_lock=True)
1214+
self.remove(src, bypass_lock=True)
12101215

12111216
def movedir(self, src, dst, overwrite=False, ignore_errors=False, chunk_size=16384):
12121217
"""moves a directory from one location to another.
@@ -1274,7 +1279,10 @@ def movefile_noerrors(src, dst, **kwargs):
12741279
dst_filename = pathjoin(dst_dirpath, filename)
12751280
movefile(src_filename, dst_filename, overwrite=overwrite, chunk_size=chunk_size)
12761281

1277-
self.removedir(dirname)
1282+
if dirname == src:
1283+
self.removedir(dirname, bypass_lock=True)
1284+
else:
1285+
self.removedir(dirname)
12781286

12791287
def copydir(self, src, dst, overwrite=False, ignore_errors=False, chunk_size=16384):
12801288
"""copies a directory from one location to another.

0 commit comments

Comments
 (0)