##// END OF EJS Templates
code: fixed typos
code: fixed typos

File last commit:

r1300:a680a605 default
r1326:f4bef470 default
Show More
lib.py
185 lines | 6.2 KiB | text/x-python | PythonLexer
git-lfs: added vcsserver handling of git-lfs objects....
r180 # RhodeCode VCSServer provides access to different vcs backends via network.
source-code: updated copyrights to 2023
r1126 # Copyright (C) 2014-2023 RhodeCode GmbH
git-lfs: added vcsserver handling of git-lfs objects....
r180 #
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os
import shutil
import logging
from collections import OrderedDict
log = logging.getLogger(__name__)
lint: auto-fixes
r1152 class OidHandler:
git-lfs: added vcsserver handling of git-lfs objects....
r180
def __init__(self, store, repo_name, auth, oid, obj_size, obj_data, obj_href,
obj_verify_href=None):
self.current_store = store
self.repo_name = repo_name
self.auth = auth
self.oid = oid
self.obj_size = obj_size
self.obj_data = obj_data
self.obj_href = obj_href
self.obj_verify_href = obj_verify_href
def get_store(self, mode=None):
return self.current_store
def get_auth(self):
"""returns auth header for re-use in upload/download"""
return " ".join(self.auth)
def download(self):
store = self.get_store()
response = None
has_errors = None
if not store.has_oid():
# error reply back to client that something is wrong with dl
vcsserver: modernize code for python3
r1130 err_msg = f'object: {store.oid} does not exist in store'
git-lfs: added vcsserver handling of git-lfs objects....
r180 has_errors = OrderedDict(
error=OrderedDict(
code=404,
message=err_msg
)
)
download_action = OrderedDict(
href=self.obj_href,
header=OrderedDict([("Authorization", self.get_auth())])
)
if not has_errors:
response = OrderedDict(download=download_action)
return response, has_errors
def upload(self, skip_existing=True):
"""
Write upload action for git-lfs server
"""
store = self.get_store()
response = None
has_errors = None
# verify if we have the OID before, if we do, reply with empty
if store.has_oid():
log.debug('LFS: store already has oid %s', store.oid)
git-lfs: always validate uploaded files size....
r197
# validate size
git-lfs: fixed tests for upload, and added new for file verification.
r201 store_size = store.size_oid()
size_match = store_size == self.obj_size
git-lfs: always validate uploaded files size....
r197 if not size_match:
git-lfs: fixed tests for upload, and added new for file verification.
r201 log.warning(
'LFS: size mismatch for oid:%s, in store:%s expected: %s',
self.oid, store_size, self.obj_size)
git-lfs: always validate uploaded files size....
r197 elif skip_existing:
git-lfs: added vcsserver handling of git-lfs objects....
r180 log.debug('LFS: skipping further action as oid is existing')
return response, has_errors
git-lfs: report chunked encoding support properly to the client....
r199 chunked = ("Transfer-Encoding", "chunked")
git-lfs: added vcsserver handling of git-lfs objects....
r180 upload_action = OrderedDict(
href=self.obj_href,
git-lfs: report chunked encoding support properly to the client....
r199 header=OrderedDict([("Authorization", self.get_auth()), chunked])
git-lfs: added vcsserver handling of git-lfs objects....
r180 )
if not has_errors:
response = OrderedDict(upload=upload_action)
# if specified in handler, return the verification endpoint
if self.obj_verify_href:
verify_action = OrderedDict(
href=self.obj_verify_href,
header=OrderedDict([("Authorization", self.get_auth())])
)
response['verify'] = verify_action
return response, has_errors
def exec_operation(self, operation, *args, **kwargs):
handler = getattr(self, operation)
log.debug('LFS: handling request using %s handler', handler)
return handler(*args, **kwargs)
lint: auto-fixes
r1152 class LFSOidStore:
git-lfs: added vcsserver handling of git-lfs objects....
r180
git-lfs: don't store oid under repo-path....
r181 def __init__(self, oid, repo, store_location=None):
git-lfs: added vcsserver handling of git-lfs objects....
r180 self.oid = oid
self.repo = repo
fix(LFS): fixed LFSOidStorage, enbled LFS fetching on repo fetch, fixed tests and python3 transition related leftovers. Fixes: RCCE-8
r1194 defined_store_path = store_location or self.get_default_store()
self.store_suffix = f"/objects/{oid[:2]}/{oid[2:4]}"
self.store_path = f"{defined_store_path.rstrip('/')}{self.store_suffix}"
git-lfs: added vcsserver handling of git-lfs objects....
r180 self.tmp_oid_path = os.path.join(self.store_path, oid + '.tmp')
self.oid_path = os.path.join(self.store_path, oid)
self.fd = None
def get_engine(self, mode):
"""
engine = .get_engine(mode='wb')
with engine as f:
f.write('...')
"""
lint: more autofixes
r1153 class StoreEngine:
fix(git-lfs): fixed security problem with allowing off-chain attacks to replace OID data without validating hash for already present oids....
r1300 _cleanup = None
git-lfs: added vcsserver handling of git-lfs objects....
r180 def __init__(self, mode, store_path, oid_path, tmp_oid_path):
self.mode = mode
self.store_path = store_path
self.oid_path = oid_path
self.tmp_oid_path = tmp_oid_path
fix(git-lfs): fixed security problem with allowing off-chain attacks to replace OID data without validating hash for already present oids....
r1300 def cleanup(self):
self._cleanup = True
git-lfs: added vcsserver handling of git-lfs objects....
r180 def __enter__(self):
if not os.path.isdir(self.store_path):
os.makedirs(self.store_path)
# TODO(marcink): maybe write metadata here with size/oid ?
fd = open(self.tmp_oid_path, self.mode)
self.fd = fd
return fd
def __exit__(self, exc_type, exc_value, traceback):
self.fd.close()
fix(git-lfs): fixed security problem with allowing off-chain attacks to replace OID data without validating hash for already present oids....
r1300
if self._cleanup is None:
# close tmp file, and rename to final destination
shutil.move(self.tmp_oid_path, self.oid_path)
else:
os.remove(self.tmp_oid_path)
git-lfs: added vcsserver handling of git-lfs objects....
r180
return StoreEngine(
mode, self.store_path, self.oid_path, self.tmp_oid_path)
def get_default_store(self):
"""
Default store, consistent with defaults of Mercurial large files store
which is /home/username/.cache/largefiles
"""
user_home = os.path.expanduser("~")
return os.path.join(user_home, '.cache', 'lfs-store')
def has_oid(self):
return os.path.exists(os.path.join(self.store_path, self.oid))
def size_oid(self):
size = -1
if self.has_oid():
oid = os.path.join(self.store_path, self.oid)
size = os.stat(oid).st_size
git-lfs: don't store oid under repo-path....
r181 return size