| 64 | } |
| 65 | |
| 66 | MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path, QString expected_etag) |
| 67 | { |
| 68 | auto entry = getEntry(base, resource_path); |
| 69 | // it's not present? generate a default stale entry |
| 70 | if (!entry) |
| 71 | { |
| 72 | return staleEntry(base, resource_path); |
| 73 | } |
| 74 | |
| 75 | auto &selected_base = m_entries[base]; |
| 76 | QString real_path = FS::PathCombine(selected_base.base_path, resource_path); |
| 77 | QFileInfo finfo(real_path); |
| 78 | |
| 79 | // is the file really there? if not -> stale |
| 80 | if (!finfo.isFile() || !finfo.isReadable()) |
| 81 | { |
| 82 | // if the file doesn't exist, we disown the entry |
| 83 | selected_base.entry_list.remove(resource_path); |
| 84 | return staleEntry(base, resource_path); |
| 85 | } |
| 86 | |
| 87 | if (!expected_etag.isEmpty() && expected_etag != entry->etag) |
| 88 | { |
| 89 | // if the etag doesn't match expected, we disown the entry |
| 90 | selected_base.entry_list.remove(resource_path); |
| 91 | return staleEntry(base, resource_path); |
| 92 | } |
| 93 | |
| 94 | // if the file changed, check md5sum |
| 95 | qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch(); |
| 96 | if (file_last_changed != entry->local_changed_timestamp) |
| 97 | { |
| 98 | QFile input(real_path); |
| 99 | input.open(QIODevice::ReadOnly); |
| 100 | QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5) |
| 101 | .toHex() |
| 102 | .constData(); |
| 103 | if (entry->md5sum != md5sum) |
| 104 | { |
| 105 | selected_base.entry_list.remove(resource_path); |
| 106 | return staleEntry(base, resource_path); |
| 107 | } |
| 108 | // md5sums matched... keep entry and save the new state to file |
| 109 | entry->local_changed_timestamp = file_last_changed; |
| 110 | SaveEventually(); |
| 111 | } |
| 112 | |
| 113 | // entry passed all the checks we cared about. |
| 114 | entry->basePath = getBasePath(base); |
| 115 | return entry; |
| 116 | } |
| 117 | |
| 118 | bool HttpMetaCache::updateEntry(MetaEntryPtr stale_entry) |
| 119 | { |
no test coverage detected