MATLAB: Is copied data integrity assured after success = copyfile(source,target) and success = 1

copydata integrityMATLAB

I plan to back up clinical data and need to know that original and source are identical; it's a QA thing. Data will first be copied from a USB drive to an internal HD using "success = copyfile (usbdata,hddata)". What exactly does "success = 1" mean? Was a checksum performed or did the function simply not crash?

Best Answer

If you shot a bullet on the harddisk even this will not be safe:
  • Create a hash on the original source.
  • Copy the hash on the destination.
  • Restart the machine on which the destination is stored.
  • Check the hash.
function CreateHash(Folder)
List = dir(fullfile(Folder, '*.*')); % Or a recursive DIR from the FileExchange

for iFile = 1:numel(List)
% Insert a progressbar ...
File = List(iFile).name;
Hash = GetMD5(fullfile(Folder, File), 'File', 'hex');
HashFile = fullfile(Folder, [File, '.hash']);
fid = fopen(HashFile, 'w');
if fid == -1
error(Cannot create file: %s', HashFile);

end
fwrite(fid, Hash, 'char');
fclose(fid);
end
And:
function CheckHash(Folder)
List = dir(fullfile(Folder, '*.*')); % Or a recursive DIR from the FileExchange
for iFile = 1:numel(List)
% Insert a progressbar here if this takes a lot of time ...
File = fullfile(Folder, List(iFile).name);
if strcmpi(File(end-5:end, '.hash'))
continue;
end
HashFile = [File, '.hash'];
if exist(HashFile, 'file') & ~exist(HashFile, 'dir')
Hash = GetMD5(File, 'File', 'hex');
fid = fopen(HashFile, 'w');
if fid == -1
error(Cannot create file: %s', HashFile);
end
StoredHash = fgetl(fid);
fclose(fid);
if ~strcmp(Hash, StoredHash)
error('CORRUPTED DATA: %s', File);
end
else
fprintf('### No hash file: %s\n', fullfile(Folder, File));
end
end