Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file equals to other

Well i have one file on my server and other on my computer. What i want to do is a simple updater that checks if the file of my computer is equal to the one uploaded in the server. (If it's equal then it hasn't been updated, if it's not equal then download)

I'm using QNetworkAccessManager to download files. Any idea?

like image 284
Kazuma Avatar asked Dec 15 '11 08:12

Kazuma


People also ask

How do you check if two files are exactly the same in Linux?

In that case it is recommended to make use of the cmp command. The cmp command is a Linux utility command that is used to compare two files.

How do you check if a file contains a specific string?

You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.


1 Answers

You can generate a checksum from a file in the following way:

QCryptographicHash hash( QCryptographicHash::Sha1 );
QFile file( fileName );

if ( file.open( QIODevice::ReadOnly ) ) {
    hash.addData( file.readAll() );
} else {
    // Handle "cannot open file" error
}

// Retrieve the SHA1 signature of the file
QByteArray sig = hash.result();

Do this for both files (while somehow getting the signature from one machine to the other) and compare the results.

like image 140
JediLlama Avatar answered Oct 09 '22 08:10

JediLlama