Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cloudfile_ext/v2/file_protection.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ def _regular_file(repo_id, path):
return file_id, head


def _virtual_views_current(cursor, branch_table, repo_id, path, file_id):
"""Origin Branch is held: virtual publishers take it before their Branch.

An accepted virtual write may still be awaiting native merge. Do not freeze
the old origin version during that interval. Read immutable virtual commits,
not cached HEAD metadata, and never alter Checkout leases.
"""
virtual_table = branch_table.rsplit('.', 1)[0] + '.`VirtualRepo`'
cursor.execute(
f"SELECT v.repo_id,v.path,b.commit_id FROM {virtual_table} v "
f"LEFT JOIN {branch_table} b ON b.repo_id=v.repo_id AND b.name='master' "
"WHERE v.origin_repo=%s ORDER BY v.repo_id LIMIT 1001 FOR UPDATE", [repo_id])
rows = cursor.fetchall()
if len(rows) > 1000:
raise ProtectionUnavailable("too many virtual views to verify")
for virtual_id, prefix, head in rows:
if not isinstance(prefix, str) or canonical_path(prefix) != prefix:
raise ProtectionUnavailable("invalid virtual path")
relative = path if prefix == '/' else path[len(prefix):] if path.startswith(prefix + '/') else None
if relative is None:
continue
if not HEX40.fullmatch(str(head)):
raise ProtectionUnavailable("virtual head unavailable")
if seafile_api.get_file_id_by_commit_and_path(virtual_id, head, relative) != file_id:
return False
return True


def _state_change(user, body, protect):
if not isinstance(body, dict) or set(body) != {'repo_id', 'path', 'expected_file_id'}:
raise ValueError("explicit native file version required")
Expand All @@ -168,6 +196,8 @@ def _state_change(user, body, protect):
row = cursor.fetchone()
if row is None or row[0] != original_head:
return _reply({'code': 'FILE_VERSION_CHANGED'}, 409)
if protect and not _virtual_views_current(cursor, branch_table, repo_id, path, file_id):
return _reply({'code': 'VIRTUAL_REPO_SYNC_PENDING'}, 409)
cursor.execute(
f'SELECT file_id,path FROM {table} WHERE repo_id=%s AND path_hash=%s FOR UPDATE',
[repo_id, digest])
Expand Down
14 changes: 14 additions & 0 deletions cloudfile_ext/v2/tests/test_file_protection.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,17 @@ def test_concurrent_head_change_cannot_mark_file_protected(self):
admin, {'repo_id': REPO, 'path': FILE, 'expected_file_id': FILE_ID}, True)
self.assertEqual(response.status_code, 409)
self.assertEqual(cursor.execute.call_count, 1) # No INSERT/DELETE

def test_pending_virtual_write_blocks_old_origin_protection(self):
cursor = MagicMock()
cursor.fetchall.return_value = [(REPO, '/design', HEAD)]
for candidate, allowed in [(FILE_ID, True), ('c' * 40, False), (None, False)]:
with self.subTest(candidate=candidate), patch.object(
protection.seafile_api, 'get_file_id_by_commit_and_path', return_value=candidate) as read:
self.assertEqual(protection._virtual_views_current(
cursor, '`native`.`Branch`', REPO, FILE, FILE_ID), allowed)
read.assert_called_once_with(REPO, HEAD, '/original.prt')
cursor.fetchall.return_value = [(REPO, '/design-other', HEAD)]
with patch.object(protection.seafile_api, 'get_file_id_by_commit_and_path') as read:
self.assertTrue(protection._virtual_views_current(cursor, '`native`.`Branch`', REPO, FILE, FILE_ID))
read.assert_not_called()
Loading