Commit 6147b0dc authored by Marius Bobin's avatar Marius Bobin

Add md5_hexdigest to compute MD% hashes for files

Rename `hexdigest` to `sha256_hexdigest` because we're adding a
`md5_hexdigest` method.
parent 369af359
...@@ -8,8 +8,12 @@ module Checksummable ...@@ -8,8 +8,12 @@ module Checksummable
Zlib.crc32(data) Zlib.crc32(data)
end end
def hexdigest(path) def sha256_hexdigest(path)
::Digest::SHA256.file(path).hexdigest ::Digest::SHA256.file(path).hexdigest
end end
def md5_hexdigest(path)
::Digest::MD5.file(path).hexdigest
end
end end
end end
...@@ -49,7 +49,7 @@ class LfsObject < ApplicationRecord ...@@ -49,7 +49,7 @@ class LfsObject < ApplicationRecord
end end
def self.calculate_oid(path) def self.calculate_oid(path)
self.hexdigest(path) self.sha256_hexdigest(path)
end end
end end
......
...@@ -67,7 +67,7 @@ class Upload < ApplicationRecord ...@@ -67,7 +67,7 @@ class Upload < ApplicationRecord
self.checksum = nil self.checksum = nil
return unless needs_checksum? return unless needs_checksum?
self.checksum = self.class.hexdigest(absolute_path) self.checksum = self.class.sha256_hexdigest(absolute_path)
end end
# Initialize the associated Uploader class with current model # Initialize the associated Uploader class with current model
......
...@@ -84,7 +84,7 @@ module Geo ...@@ -84,7 +84,7 @@ module Geo
def calculate_checksum def calculate_checksum
raise 'File is not checksummable' unless checksummable? raise 'File is not checksummable' unless checksummable?
model.hexdigest(blob_path) model.sha256_hexdigest(blob_path)
end end
# Returns whether the file exists on disk or in remote storage # Returns whether the file exists on disk or in remote storage
......
...@@ -36,7 +36,7 @@ module Gitlab ...@@ -36,7 +36,7 @@ module Gitlab
# Remove this when we implement checksums for files on the Object Storage # Remove this when we implement checksums for files on the Object Storage
return true unless recorded_file.local? return true unless recorded_file.local?
extra_params[:checksum] == Upload.hexdigest(recorded_file.absolute_path) extra_params[:checksum] == Upload.sha256_hexdigest(recorded_file.absolute_path)
end end
end end
end end
......
...@@ -3,7 +3,7 @@ require 'spec_helper' ...@@ -3,7 +3,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Geo::Replication::BlobRetriever, :aggregate_failures do RSpec.describe Gitlab::Geo::Replication::BlobRetriever, :aggregate_failures do
let(:package_file) { create(:package_file, :npm) } let(:package_file) { create(:package_file, :npm) }
let(:package_checksum) { package_file.class.hexdigest(package_file.file.path) } let(:package_checksum) { package_file.class.sha256_hexdigest(package_file.file.path) }
let(:replicator_class) { Geo::PackageFileReplicator } let(:replicator_class) { Geo::PackageFileReplicator }
let(:replicator) { replicator_class.new(model_record_id: package_file.id) } let(:replicator) { replicator_class.new(model_record_id: package_file.id) }
......
...@@ -207,7 +207,7 @@ RSpec.shared_examples 'a blob replicator' do ...@@ -207,7 +207,7 @@ RSpec.shared_examples 'a blob replicator' do
context 'when the file is locally stored' do context 'when the file is locally stored' do
context 'when the file exists' do context 'when the file exists' do
it 'returns hexdigest of the file' do it 'returns hexdigest of the file' do
expected = described_class.model.hexdigest(subject.blob_path) expected = described_class.model.sha256_hexdigest(subject.blob_path)
expect(subject.calculate_checksum).to eq(expected) expect(subject.calculate_checksum).to eq(expected)
end end
......
...@@ -261,7 +261,7 @@ module Gitlab ...@@ -261,7 +261,7 @@ module Gitlab
project: job.project, project: job.project,
file_type: :trace, file_type: :trace,
file: stream, file: stream,
file_sha256: self.class.hexdigest(path)) file_sha256: self.class.sha256_hexdigest(path))
trace_metadata.track_archival!(trace_artifact.id) trace_metadata.track_archival!(trace_artifact.id)
end end
......
...@@ -28,7 +28,7 @@ module Gitlab ...@@ -28,7 +28,7 @@ module Gitlab
end end
def actual_checksum(upload) def actual_checksum(upload)
Upload.hexdigest(upload.absolute_path) Upload.sha256_hexdigest(upload.absolute_path)
end end
def remote_object_exists?(upload) def remote_object_exists?(upload)
......
...@@ -13,11 +13,19 @@ RSpec.describe Checksummable do ...@@ -13,11 +13,19 @@ RSpec.describe Checksummable do
end end
end end
describe ".hexdigest" do describe ".sha256_hexdigest" do
it 'returns the SHA256 sum of the file' do it 'returns the SHA256 sum of the file' do
expected = Digest::SHA256.file(__FILE__).hexdigest expected = Digest::SHA256.file(__FILE__).hexdigest
expect(subject.hexdigest(__FILE__)).to eq(expected) expect(subject.sha256_hexdigest(__FILE__)).to eq(expected)
end
end
describe ".md5_hexdigest" do
it 'returns the MD5 sum of the file' do
expected = Digest::MD5.file(__FILE__).hexdigest
expect(subject.md5_hexdigest(__FILE__)).to eq(expected)
end end
end end
end end
...@@ -497,7 +497,7 @@ RSpec.shared_examples 'trace with disabled live trace feature' do ...@@ -497,7 +497,7 @@ RSpec.shared_examples 'trace with disabled live trace feature' do
expect(build.job_artifacts_trace.file.filename).to eq('job.log') expect(build.job_artifacts_trace.file.filename).to eq('job.log')
expect(File.exist?(src_path)).to be_falsy expect(File.exist?(src_path)).to be_falsy
expect(src_checksum) expect(src_checksum)
.to eq(described_class.hexdigest(build.job_artifacts_trace.file.path)) .to eq(described_class.sha256_hexdigest(build.job_artifacts_trace.file.path))
expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum) expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum)
end end
end end
...@@ -523,7 +523,7 @@ RSpec.shared_examples 'trace with disabled live trace feature' do ...@@ -523,7 +523,7 @@ RSpec.shared_examples 'trace with disabled live trace feature' do
expect(build.job_artifacts_trace.file.filename).to eq('job.log') expect(build.job_artifacts_trace.file.filename).to eq('job.log')
expect(build.old_trace).to be_nil expect(build.old_trace).to be_nil
expect(src_checksum) expect(src_checksum)
.to eq(described_class.hexdigest(build.job_artifacts_trace.file.path)) .to eq(described_class.sha256_hexdigest(build.job_artifacts_trace.file.path))
expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum) expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum)
end end
end end
...@@ -861,7 +861,7 @@ RSpec.shared_examples 'trace with enabled live trace feature' do ...@@ -861,7 +861,7 @@ RSpec.shared_examples 'trace with enabled live trace feature' do
expect(build.job_artifacts_trace.file.filename).to eq('job.log') expect(build.job_artifacts_trace.file.filename).to eq('job.log')
expect(Ci::BuildTraceChunk.where(build: build)).not_to be_exist expect(Ci::BuildTraceChunk.where(build: build)).not_to be_exist
expect(src_checksum) expect(src_checksum)
.to eq(described_class.hexdigest(build.job_artifacts_trace.file.path)) .to eq(described_class.sha256_hexdigest(build.job_artifacts_trace.file.path))
expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum) expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment