Skip to content

Commit

Permalink
package-indexer: use sha512 insead of md5
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
  • Loading branch information
alltilla committed May 19, 2024
1 parent 88b31f9 commit ec928bd
Showing 1 changed file with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import logging
from abc import ABC, abstractmethod
from enum import Enum, auto
from hashlib import md5
from hashlib import sha512
from pathlib import Path
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -172,10 +172,10 @@ def __download_file(self, relative_file_path: str) -> None:
self._download_file(relative_file_path)
self._log_info("Successfully downloaded file.", remote_path=relative_file_path, local_path=str(download_path))

md5sum = md5(download_path.read_bytes()).digest()
md5sum_file_path = self.__get_remote_md5sum_file_path(relative_file_path)
md5sum_file_path.parent.mkdir(exist_ok=True, parents=True)
md5sum_file_path.write_bytes(md5sum)
sha512sum = sha512(download_path.read_bytes()).digest()
sha512sum_file_path = self.__get_remote_sha512sum_file_path(relative_file_path)
sha512sum_file_path.parent.mkdir(exist_ok=True, parents=True)
sha512sum_file_path.write_bytes(sha512sum)

@abstractmethod
def _upload_file(self, relative_file_path: str) -> None:
Expand Down Expand Up @@ -213,21 +213,21 @@ def _get_relative_file_path_for_remote_file(self, file: Dict[str, Any]) -> str:
def _get_local_file_path_for_relative_file(self, relative_file_path: str) -> Path:
return Path(self.local_dir.root_dir, relative_file_path).resolve()

def __get_md5_of_local_file(self, relative_file_path: str) -> bytes:
def __get_sha512_of_local_file(self, relative_file_path: str) -> bytes:
file = Path(self.local_dir.root_dir, relative_file_path)
return md5(file.read_bytes()).digest()
return sha512(file.read_bytes()).digest()

def __get_remote_md5sum_file_path(self, relative_file_path: str) -> Path:
path = Path(self.local_dir.root_dir, "package-indexer-md5sums", relative_file_path).resolve()
return Path(path.parent, path.name + ".package-indexer-md5sum")
def __get_remote_sha512sum_file_path(self, relative_file_path: str) -> Path:
path = Path(self.local_dir.root_dir, "package-indexer-sha512sums", relative_file_path).resolve()
return Path(path.parent, path.name + ".package-indexer-sha512sum")

def __get_md5_of_remote_file(self, relative_file_path: str) -> bytes:
md5sum_file_path = self.__get_remote_md5sum_file_path(relative_file_path)
return md5sum_file_path.read_bytes()
def __get_sha512_of_remote_file(self, relative_file_path: str) -> bytes:
sha512sum_file_path = self.__get_remote_sha512sum_file_path(relative_file_path)
return sha512sum_file_path.read_bytes()

def __get_file_sync_state(self, relative_file_path: str) -> FileSyncState:
try:
local_md5 = self.__get_md5_of_local_file(relative_file_path)
local_sha512 = self.__get_sha512_of_local_file(relative_file_path)
except FileNotFoundError:
self._log_debug(
"Remote file is not available locally.",
Expand All @@ -237,7 +237,7 @@ def __get_file_sync_state(self, relative_file_path: str) -> FileSyncState:
return FileSyncState.NOT_IN_LOCAL

try:
remote_md5 = self.__get_md5_of_remote_file(relative_file_path)
remote_sha512 = self.__get_sha512_of_remote_file(relative_file_path)
except FileNotFoundError:
self._log_debug(
"Local file is not available remotely.",
Expand All @@ -246,21 +246,21 @@ def __get_file_sync_state(self, relative_file_path: str) -> FileSyncState:
)
return FileSyncState.NOT_IN_REMOTE

if remote_md5 != local_md5:
if remote_sha512 != local_sha512:
self._log_debug(
"File differs locally and remotely.",
remote_path=str(Path(self.remote_dir.root_dir, relative_file_path)),
local_path=str(Path(self.local_dir.root_dir, relative_file_path)),
remote_md5sum=remote_md5.hex(),
local_md5sum=local_md5.hex(),
remote_sha512sum=remote_sha512.hex(),
local_sha512sum=local_sha512.hex(),
)
return FileSyncState.DIFFERENT

self._log_debug(
"File is in sync.",
remote_path=str(Path(self.remote_dir.root_dir, relative_file_path)),
local_path=str(Path(self.local_dir.root_dir, relative_file_path)),
md5sum=remote_md5.hex(),
sha512sum=remote_sha512.hex(),
)
return FileSyncState.IN_SYNC

Expand Down

0 comments on commit ec928bd

Please sign in to comment.