Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save FileCache over tmpfile #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions python2/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import time
import random
import errno
import tempfile
try:
from hashlib import sha1 as _sha, md5 as _md5
except ImportError:
Expand Down Expand Up @@ -726,9 +727,13 @@ def get(self, key):

def set(self, key, value):
cacheFullPath = os.path.join(self.cache, self.safe(key))
f = file(cacheFullPath, "wb")
f.write(value)
f.close()
fd, tmpPath = tempfile.mkstemp()
os.write(fd, value)
os.close(fd)
try:
os.rename(tmpPath, cacheFullPath)
except OSError:
pass

def delete(self, key):
cacheFullPath = os.path.join(self.cache, self.safe(key))
Expand Down Expand Up @@ -1312,6 +1317,8 @@ def _conn_request(self, conn, request_uri, method, body, headers):
try:
if hasattr(conn, 'sock') and conn.sock is None:
conn.connect()
if hasattr(body, 'tell') and body.tell() > 0:
body.seek(0) # rewind for retry send file
conn.request(method, request_uri, body, headers)
except socket.timeout:
raise
Expand Down
13 changes: 10 additions & 3 deletions python3/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import time
import random
import errno
import tempfile
from hashlib import sha1 as _sha, md5 as _md5
import hmac
from gettext import gettext as _
Expand Down Expand Up @@ -687,9 +688,13 @@ def get(self, key):

def set(self, key, value):
cacheFullPath = os.path.join(self.cache, self.safe(key))
f = open(cacheFullPath, "wb")
f.write(value)
f.close()
fd, tmpPath = tempfile.mkstemp()
os.write(fd, value)
os.close(fd)
try:
os.rename(tmpPath, cacheFullPath)
except OSError:
pass

def delete(self, key):
cacheFullPath = os.path.join(self.cache, self.safe(key))
Expand Down Expand Up @@ -993,6 +998,8 @@ def _conn_request(self, conn, request_uri, method, body, headers):
try:
if conn.sock is None:
conn.connect()
if hasattr(body, 'tell') and body.tell() > 0:
body.seek(0) # rewind for retry send file
conn.request(method, request_uri, body, headers)
except socket.timeout:
conn.close()
Expand Down