From 06e44e800cd31a64bdf640f795c11465a83816e9 Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 9 Sep 2026 14:11:40 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=AD=E3=82=B7=E3=81=AE?= =?UTF-8?q?=E4=BF=9D=E7=AE=A1=E3=81=A8=E7=85=A7=E5=90=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyEdit.vcxproj | 2 + ProxyEdit.vcxproj.filters | 6 ++ src/ProxyStore.cpp | 127 ++++++++++++++++++++++++++++++++++++++ src/ProxyStore.h | 27 ++++++++ 4 files changed, 162 insertions(+) create mode 100644 src/ProxyStore.cpp create mode 100644 src/ProxyStore.h diff --git a/ProxyEdit.vcxproj b/ProxyEdit.vcxproj index 804988b..70761e4 100644 --- a/ProxyEdit.vcxproj +++ b/ProxyEdit.vcxproj @@ -99,6 +99,7 @@ + @@ -109,6 +110,7 @@ + diff --git a/ProxyEdit.vcxproj.filters b/ProxyEdit.vcxproj.filters index 3b1faac..4d36978 100644 --- a/ProxyEdit.vcxproj.filters +++ b/ProxyEdit.vcxproj.filters @@ -40,6 +40,9 @@ ソース ファイル + + ソース ファイル + ソース ファイル @@ -63,6 +66,9 @@ ヘッダー ファイル + + ヘッダー ファイル + ヘッダー ファイル diff --git a/src/ProxyStore.cpp b/src/ProxyStore.cpp new file mode 100644 index 0000000..5d9c1a6 --- /dev/null +++ b/src/ProxyStore.cpp @@ -0,0 +1,127 @@ +#include "ProxyStore.h" + +#include + +#include + +#include "Log.h" +#include "Settings.h" + +namespace pe { + +namespace { + +struct StoreEntry { + std::wstring path; + long long size = 0; + long long used = 0; +}; + +unsigned long long HashPath(const std::wstring& path) { + unsigned long long hash = 1469598103934665603ULL; + for (wchar_t character : path) { + wchar_t lowered = (wchar_t)towlower(character); + hash ^= (unsigned long long)(lowered & 0xFF); + hash *= 1099511628211ULL; + hash ^= (unsigned long long)((lowered >> 8) & 0xFF); + hash *= 1099511628211ULL; + } + return hash; +} + +std::wstring BaseName(const std::wstring& path) { + size_t separator = path.find_last_of(L"\\/"); + std::wstring name = separator == std::wstring::npos ? path : path.substr(separator + 1); + size_t dot = name.find_last_of(L'.'); + if (dot != std::wstring::npos) name = name.substr(0, dot); + std::wstring cleaned; + for (wchar_t character : name) { + if (wcschr(L"\\/:*?\"<>|", character)) continue; + cleaned += character; + if (cleaned.size() >= 48) break; + } + if (cleaned.empty()) cleaned = L"source"; + return cleaned; +} + +std::vector CollectEntries() { + std::vector entries; + std::wstring pattern = EffectiveStorePath() + L"\\*.pxy"; + WIN32_FIND_DATAW found{}; + HANDLE search = FindFirstFileW(pattern.c_str(), &found); + if (search == INVALID_HANDLE_VALUE) return entries; + do { + if (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; + StoreEntry entry; + entry.path = EffectiveStorePath() + L"\\" + found.cFileName; + entry.size = ((long long)found.nFileSizeHigh << 32) | (long long)found.nFileSizeLow; + ULARGE_INTEGER used; + used.LowPart = found.ftLastAccessTime.dwLowDateTime; + used.HighPart = found.ftLastAccessTime.dwHighDateTime; + entry.used = (long long)used.QuadPart; + entries.push_back(entry); + } while (FindNextFileW(search, &found)); + FindClose(search); + return entries; +} + +} + +bool QuerySource(const std::wstring& path, SourceKey& key) { + WIN32_FILE_ATTRIBUTE_DATA data{}; + if (!GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &data)) return false; + if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return false; + key.path = path; + key.size = ((long long)data.nFileSizeHigh << 32) | (long long)data.nFileSizeLow; + ULARGE_INTEGER stamp; + stamp.LowPart = data.ftLastWriteTime.dwLowDateTime; + stamp.HighPart = data.ftLastWriteTime.dwHighDateTime; + key.time = (long long)stamp.QuadPart; + return true; +} + +bool SameSource(const SourceKey& key, long long size, long long time) { + return key.size == size && key.time == time; +} + +bool EnsureStoreDirectory() { + std::wstring path = EffectiveStorePath(); + if (CreateDirectoryW(path.c_str(), nullptr)) return true; + return GetLastError() == ERROR_ALREADY_EXISTS; +} + +std::wstring ProxyPathFor(const SourceKey& key) { + wchar_t suffix[32]; + _snwprintf_s(suffix, _TRUNCATE, L"_%016llX.pxy", HashPath(key.path)); + return EffectiveStorePath() + L"\\" + BaseName(key.path) + suffix; +} + +long long StoreUsage() { + long long total = 0; + for (const StoreEntry& entry : CollectEntries()) total += entry.size; + return total; +} + +void ReleaseCapacity(long long incoming, const std::wstring& keep) { + const long long capacity = CurrentSettings().capacity_bytes; + std::vector entries = CollectEntries(); + long long total = 0; + for (const StoreEntry& entry : entries) total += entry.size; + if (total + incoming <= capacity) return; + std::sort(entries.begin(), entries.end(), + [](const StoreEntry& left, const StoreEntry& right) { return left.used < right.used; }); + for (const StoreEntry& entry : entries) { + if (total + incoming <= capacity) break; + if (_wcsicmp(entry.path.c_str(), keep.c_str()) == 0) continue; + if (DeleteFileW(entry.path.c_str())) { + total -= entry.size; + Say(L"容量を空けるためプロキシを破棄しました: %s", entry.path.c_str()); + } + } +} + +void RemoveProxy(const std::wstring& proxy_path) { + DeleteFileW(proxy_path.c_str()); +} + +} diff --git a/src/ProxyStore.h b/src/ProxyStore.h new file mode 100644 index 0000000..8e6eb54 --- /dev/null +++ b/src/ProxyStore.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include +#include +#include + +namespace pe { + +struct SourceKey { + std::wstring path; + long long size = 0; + long long time = 0; +}; + +bool QuerySource(const std::wstring& path, SourceKey& key); +bool SameSource(const SourceKey& key, long long size, long long time); + +bool EnsureStoreDirectory(); +std::wstring ProxyPathFor(const SourceKey& key); + +long long StoreUsage(); +void ReleaseCapacity(long long incoming, const std::wstring& keep); +void RemoveProxy(const std::wstring& proxy_path); + +}