Skip to content

Commit

Permalink
cs3: fixed lock and error handling on writefile, closes #137
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Nov 28, 2023
1 parent 9a5a7a8 commit 4aad810
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/core/cs3iface.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ def readfile(endpoint, filepath, userid, lockid):
try:
protocol = [p for p in res.protocols if p.protocol == "simple" or p.protocol == "spaces"][0]
headers = {
'x-access-token': userid,
'x-reva-transfer': protocol.token # needed if the downloads pass through the data gateway in reva
'X-Access-Token': userid,
'X-Reva-Transfer': protocol.token
}
fileget = requests.get(url=protocol.download_endpoint, headers=headers, verify=ctx['ssl_verify'], timeout=30, stream=True)
except requests.exceptions.RequestException as e:
Expand Down Expand Up @@ -438,21 +438,25 @@ def writefile(endpoint, filepath, userid, content, lockmd, islock=False):

# prepare endpoint
if lockmd:
_, lockid = lockmd # TODO we are not validating the holder on write, only the lock_id
appname, lockid = lockmd
else:
lockid = None
appname = lockid = ''
if isinstance(content, str):
content = bytes(content, 'UTF-8')
size = str(len(content))
reference = _getcs3reference(endpoint, filepath)
req = cs3sp.InitiateFileUploadRequest(ref=reference, lock_id=lockid, opaque=types.Opaque(
map={"Upload-Length": types.OpaqueEntry(decoder="plain", value=str.encode(size))}))
map={'Upload-Length': types.OpaqueEntry(decoder="plain", value=str.encode(size)),
'Lock-Holder': types.OpaqueEntry(decoder="plain", value=str.encode(appname))
}))
res = ctx['cs3gw'].InitiateFileUpload(request=req, metadata=[('x-access-token', userid)])
if res.status.code == cs3code.CODE_FAILED_PRECONDITION:
log.info('msg="Failed precondition on initiateFileUpload" filepath="%s" appname="%s" trace="%s" reason="%s"' %
(filepath, appname, res.status.trace, res.status.message.replace('"', "'")))
raise IOError(common.EXCL_ERROR)
if res.status.code != cs3code.CODE_OK:
log.error('msg="Failed to initiateFileUpload on write" filepath="%s" trace="%s" code="%s" reason="%s"' %
(filepath, res.status.trace, res.status.code, res.status.message.replace('"', "'")))
if '_lock_' in res.status.message: # TODO find the error code returned by Reva once this is implemented
raise IOError(common.EXCL_ERROR)
raise IOError(res.status.message)
tend = time.time()
log.debug('msg="writefile: InitiateFileUploadRes returned" trace="%s" protocols="%s"' %
Expand All @@ -462,9 +466,9 @@ def writefile(endpoint, filepath, userid, content, lockmd, islock=False):
try:
protocol = [p for p in res.protocols if p.protocol == "simple" or p.protocol == "spaces"][0]
headers = {
'x-access-token': userid,
'X-Access-Token': userid,
'Upload-Length': size,
'x-reva-transfer': protocol.token # needed if the uploads pass through the data gateway in reva
'X-Reva-Transfer': protocol.token
}
putres = requests.put(url=protocol.upload_endpoint, data=content, headers=headers, verify=ctx['ssl_verify'], timeout=30)
except requests.exceptions.RequestException as e:
Expand Down

0 comments on commit 4aad810

Please sign in to comment.