-
Notifications
You must be signed in to change notification settings - Fork 0
/
TmpIfs.cpp
186 lines (152 loc) · 4 KB
/
TmpIfs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// TmpIfs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#undef max
#include <algorithm>
#include <ShlwApi.h>
#include <Wininet.h>
#pragma comment( lib, "Wininet.lib")
CWinApp theApp;
using std::cout;
using std::cerr;
// Column output delimiter
#define chSep '\t'
#define strSep "\t"
constexpr char* szHeader{
"Internet Address" strSep
"Size" strSep
"Last Modified" strSep
"Expires" strSep
"Last Accessed" strSep
"Last Checked" strSep
// "Use Count" strSep
"Hit Rate\n"
};
static void OutputTime( const FILETIME & ft, bool bForNoneDisplayNever )
{
cout << chSep;
if ( ( ft.dwHighDateTime != 0 ) || ( ft.dwLowDateTime != 0 ) )
{
FILETIME lft;
FileTimeToLocalFileTime( &ft, &lft );
SYSTEMTIME st;
FileTimeToSystemTime( &lft, &st );
TCHAR szDateTime[40];
GetDateFormat( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, szDateTime, sizeof( szDateTime ) );
cout << szDateTime;
cout << ' ';
GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, NULL, szDateTime, sizeof( szDateTime ) );
cout << szDateTime;
}
else
{
cout << (bForNoneDisplayNever ? "Never" : "None");
}
}
static void OutputCacheEntry( const INTERNET_CACHE_ENTRY_INFO & CacheEntry )
{
cout << CacheEntry.lpszSourceUrlName;
/*
if ( CacheEntry.lpszLocalFileName != NULL )
{
cout << chSep;
cout << CacheEntry.lpszLocalFileName;
}
*/
TCHAR szSize[40];
StrFormatByteSize( CacheEntry.dwSizeLow, szSize, sizeof( szSize ) );
cout << chSep;
cout << szSize;
OutputTime( CacheEntry.LastModifiedTime, false );
OutputTime( CacheEntry.ExpireTime, true );
OutputTime( CacheEntry.LastAccessTime, false );
OutputTime( CacheEntry.LastSyncTime, true );
//cout << chSep;
//cout << CacheEntry.dwUseCount;
//// _itoa( CacheEntry.dwUseCount, szSize, 10 );
//// fputs( szSize, stdout );
cout << chSep;
cout << CacheEntry.dwHitRate;
cout << "\n";
}
int main(int /*argc*/, char* /*argv*/[])
{
int nRetCode;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("Fatal Error: MFC initialization failed\n");
nRetCode = 1;
}
else
{
/* If COM isn't initialized, things just appear as normal folders! */
const auto hr = CoInitialize( NULL );
if ( SUCCEEDED( hr ) )
{
cerr << "TmpIfs V1.2.1.0\n";
// The buffer for the cache entry must be large enough to encompass the largest entry, not just the size of the structure!
// Code uses CacheEntry type, so map the 2 things together
union
{
BYTE Cache[10'000];
INTERNET_CACHE_ENTRY_INFO CacheEntry;
};
CacheEntry.dwStructSize = sizeof( INTERNET_CACHE_ENTRY_INFO );
cout << szHeader;
int NumItems = 0;
#ifdef _DEBUG
// Debug variable to ensure buffer is large enough
DWORD MaxEntrySizeEncountered{ 0 };
#endif
DWORD dwEntrySize{ sizeof( Cache ) };
HANDLE hFind = FindFirstUrlCacheEntryEx( NULL, 0, NORMAL_CACHE_ENTRY | COOKIE_CACHE_ENTRY, 0, &CacheEntry, &dwEntrySize, NULL, NULL, NULL );
if ( hFind != NULL )
{
#ifdef _DEBUG
MaxEntrySizeEncountered = dwEntrySize;
#endif
do
{
++NumItems;
OutputCacheEntry( CacheEntry );
// Reset the buffer size
dwEntrySize = sizeof( Cache );
const auto bval = FindNextUrlCacheEntryEx( hFind, &CacheEntry, &dwEntrySize, NULL, NULL, NULL );
if ( !bval )
{
#ifdef _DEBUG
// Check if the above API ever fails (because the buffer is too small)
auto le = GetLastError();
if ( le != ERROR_NO_MORE_ITEMS )
{
le = le;
}
#endif
break;
}
#ifdef _DEBUG
else
{
MaxEntrySizeEncountered = std::max( MaxEntrySizeEncountered, dwEntrySize );
}
#endif
}
while ( true );
FindCloseUrlCache( hFind );
}
#ifdef _DEBUG
cout << "MAX ENCOUNTERED: " << MaxEntrySizeEncountered << "\n";
#endif
cerr << "There are " << NumItems << " items\n";
CoUninitialize();
nRetCode = 0;
}
else
{
cerr << _T( "Fatal Error: COM initialization failed\n" );
nRetCode = 2;
}
}
return nRetCode;
}