Skip to content

Commit b86aa9e

Browse files
committed
Modify store() to create the subdirs when writing the file (1 of 4)
1 parent 1acab3b commit b86aa9e

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

diskcache/core.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,26 @@ def store(self, value, read, key=UNKNOWN):
206206
return 0, MODE_RAW, None, sqlite3.Binary(value)
207207
else:
208208
filename, full_path = self.filename(key, value)
209+
full_dir, _ = op.split(full_path)
209210

210-
with open(full_path, 'xb') as writer:
211-
writer.write(value)
211+
for count in range(11):
212+
with cl.suppress(OSError):
213+
os.makedirs(full_dir)
214+
215+
try:
216+
# Another cache may have deleted the directory before
217+
# the file could be opened.
218+
writer = open(full_path, 'xb')
219+
except OSError:
220+
if count == 10:
221+
# Give up after 10 tries to open the file.
222+
raise
223+
continue
224+
225+
with writer:
226+
writer.write(value)
227+
228+
break
212229

213230
return len(value), MODE_BINARY, filename, None
214231
elif type_value is str:

0 commit comments

Comments
 (0)