best way to remember informations about opened files
-
hello there! I'm writing a comics file reader, and I wanted to remember the index of the page of every file opened with my application. I used Settings to store the file path as key and the index as value, and it works well as long as the file remains in the same position.
However, if the file is moved or renamed the application consider it as a new file, and if another (different) file is moved in the same position as it with the same name it may cause errorswhat is the best way to handle this? is there perhaps a way of creating like an hash of the file, and use it as a key?
thanks in advance!
-
hello there! I'm writing a comics file reader, and I wanted to remember the index of the page of every file opened with my application. I used Settings to store the file path as key and the index as value, and it works well as long as the file remains in the same position.
However, if the file is moved or renamed the application consider it as a new file, and if another (different) file is moved in the same position as it with the same name it may cause errorswhat is the best way to handle this? is there perhaps a way of creating like an hash of the file, and use it as a key?
thanks in advance!
@tubbadu said in best way to remember informations about opened files:
However, if the file is moved or renamed the application consider it as a new file, and if another (different) file is moved in the same position as it with the same name it may cause errors
Indeed, nothing can allow for that and know that a file was renamed or whatever.
is there perhaps a way of creating like an hash of the file, and use it as a key?
If you really want to. Makes it slower to recognise, and you will have to keep the hash up-to-date if changes.
-
@tubbadu said in best way to remember informations about opened files:
However, if the file is moved or renamed the application consider it as a new file, and if another (different) file is moved in the same position as it with the same name it may cause errors
Indeed, nothing can allow for that and know that a file was renamed or whatever.
is there perhaps a way of creating like an hash of the file, and use it as a key?
If you really want to. Makes it slower to recognise, and you will have to keep the hash up-to-date if changes.
@JonB thanks for your reply!
Indeed, nothing can allow for that and know that a file was renamed or whatever.
isn't possible to run some kind of algorithm that takes the file as input and outputs a string or something as a "fingerprint" of that file, ignoring its filename? For instance my file manager (Dolphin, which is Qt based) when moving a file in a directory with another file with the same name does something similar to what I'm looking for, it says "the files are identical" or "the files are different")
the files I need to open are not bigger than 500Mb, do you think this operation will take too much time?
-
@JonB thanks for your reply!
Indeed, nothing can allow for that and know that a file was renamed or whatever.
isn't possible to run some kind of algorithm that takes the file as input and outputs a string or something as a "fingerprint" of that file, ignoring its filename? For instance my file manager (Dolphin, which is Qt based) when moving a file in a directory with another file with the same name does something similar to what I'm looking for, it says "the files are identical" or "the files are different")
the files I need to open are not bigger than 500Mb, do you think this operation will take too much time?
@tubbadu said in best way to remember informations about opened files:
do you think this operation will take too much time?
You can do it in two steps:
- Check the file sizes: if both files have different sizes they are different
- If size is same calculate hashes from the content of both files using https://doc.qt.io/qt-5/qcryptographichash.html and compare these hashes
-
@JonB thanks for your reply!
Indeed, nothing can allow for that and know that a file was renamed or whatever.
isn't possible to run some kind of algorithm that takes the file as input and outputs a string or something as a "fingerprint" of that file, ignoring its filename? For instance my file manager (Dolphin, which is Qt based) when moving a file in a directory with another file with the same name does something similar to what I'm looking for, it says "the files are identical" or "the files are different")
the files I need to open are not bigger than 500Mb, do you think this operation will take too much time?
@tubbadu said in best way to remember informations about opened files:
the files I need to open are not bigger than 500Mb, do you think this operation will take too much time?
0.5GB to read on opening is a fair size! Up to you, but I would not want to calculate a hash on the complete content, only on some "unique sample" from it.
-
@tubbadu said in best way to remember informations about opened files:
the files I need to open are not bigger than 500Mb, do you think this operation will take too much time?
0.5GB to read on opening is a fair size! Up to you, but I would not want to calculate a hash on the complete content, only on some "unique sample" from it.
@JonB said in best way to remember informations about opened files:
only on some "unique sample" from it
What is "unique sample"? :-)
-
@JonB said in best way to remember informations about opened files:
only on some "unique sample" from it
What is "unique sample"? :-)
-
@tubbadu said in best way to remember informations about opened files:
do you think this operation will take too much time?
You can do it in two steps:
- Check the file sizes: if both files have different sizes they are different
- If size is same calculate hashes from the content of both files using https://doc.qt.io/qt-5/qcryptographichash.html and compare these hashes
If size is same calculate hashes from the content of both files using https://doc.qt.io/qt-5/qcryptographichash.html and compare these hashes
that really looks like what I was looking for, thank you very much!
0.5GB to read on opening is a fair size! Up to you, but I would not want to calculate a hash on the complete content, only on some "unique sample" from it.
that's an amazing idea! the files I'm reading are basically zip or rar archives of jpg, so I may just check the qcryptographichash of, let's say, the first and the last image plus the file size, and if they are all the same I can consider it as identical!
thanks to both, I'll try and post here the result!
-
@tubbadu said in best way to remember informations about opened files:
do you think this operation will take too much time?
You can do it in two steps:
- Check the file sizes: if both files have different sizes they are different
- If size is same calculate hashes from the content of both files using https://doc.qt.io/qt-5/qcryptographichash.html and compare these hashes
@jsulm said in best way to remember informations about opened files:
calculate hashes from the content of both files using https://doc.qt.io/qt-5/qcryptographichash.html
@tubbadu
I don't believe you need any kind of cryptographic hash for your purpose of hash-content-for-compare. I believe Qt gives you an adequate implementation for your purpose in e.g. uint qHash(const QByteArray &key, uint seed = 0).