33 #include <tqptrlist.h>
35 #include <kinstance.h>
36 #include <tdelocale.h>
37 #include <tdecmdlineargs.h>
38 #include <tdeglobal.h>
39 #include <kstandarddirs.h>
40 #include <dcopclient.h>
41 #include <tdeprotocolmanager.h>
51 static const char appName[] =
"tdeio_http_cache_cleaner";
53 static const char description[] = I18N_NOOP(
"TDE HTTP cache maintenance tool");
55 static const char version[] =
"1.0.0";
57 static const TDECmdLineOptions options[] =
59 {
"clear-all", I18N_NOOP(
"Empty the cache"), 0},
69 template class TQPtrList<FileInfo>;
71 class FileInfoList :
public TQPtrList<FileInfo>
74 FileInfoList() : TQPtrList<FileInfo>() { }
75 int compareItems(TQPtrCollection::Item item1, TQPtrCollection::Item item2)
76 {
return ((FileInfo *)item1)->age - ((FileInfo *)item2)->age; }
81 #define CACHE_REVISION "7\n"
83 FileInfo *readEntry(
const TQString &filename)
85 TQCString CEF = TQFile::encodeName(filename);
86 FILE *fs = fopen( CEF.data(),
"r");
94 if (ok && (!fgets(buffer, 400, fs)))
96 if (ok && (strcmp(buffer, CACHE_REVISION) != 0))
100 if (ok && (!fgets(buffer, 400, fs)))
107 if (ok && (!fgets(buffer, 400, fs)))
111 creationDate = (time_t) strtoul(buffer, 0, 10);
112 age = (int) difftime(currentDate, creationDate);
113 if ( m_maxCacheAge && ( age > m_maxCacheAge))
120 if (ok && (!fgets(buffer, 400, fs)))
127 expireDate = (time_t) strtoul(buffer, 0, 10);
128 if (expireDate && (expireDate < currentDate))
134 if (ok && (!fgets(buffer, 400, fs)))
142 if (ok && (!fgets(buffer, 400, fs)))
153 FileInfo *info =
new FileInfo;
164 void scanDirectory(FileInfoList &fileEntries,
const TQString &name,
const TQString &strDir)
167 if (!dir.exists())
return;
169 TQFileInfoList *newEntries = (TQFileInfoList *) dir.entryInfoList();
171 if (!newEntries)
return;
173 for(TQFileInfo *qFileInfo = newEntries->first();
175 qFileInfo = newEntries->next())
177 if (qFileInfo->isFile())
179 FileInfo *fileInfo = readEntry( strDir +
"/" + qFileInfo->fileName());
182 fileInfo->name = name +
"/" + qFileInfo->fileName();
183 fileInfo->size = (qFileInfo->size() + 1023) / 1024;
184 fileEntries.append(fileInfo);
190 extern "C" TDE_EXPORT
int kdemain(
int argc,
char **argv)
192 TDELocale::setMainCatalogue(
"tdelibs");
193 TDECmdLineArgs::init( argc, argv, appName,
194 I18N_NOOP(
"TDE HTTP cache maintenance tool"),
195 description, version,
true);
197 TDECmdLineArgs::addCmdLineOptions( options );
199 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
201 bool deleteAll = args->isSet(
"clear-all");
203 TDEInstance ins( appName );
207 DCOPClient *dcop =
new DCOPClient();
208 TQCString name = dcop->registerAs(appName,
false);
209 if (!name.isEmpty() && (name != appName))
211 fprintf(stderr,
"%s: Already running! (%s)\n", appName, name.data());
216 currentDate = time(0);
217 m_maxCacheAge = KProtocolManager::maxCacheAge();
218 m_maxCacheSize = KProtocolManager::maxCacheSize();
223 TQString strCacheDir = TDEGlobal::dirs()->saveLocation(
"cache",
"http");
225 TQDir cacheDir( strCacheDir );
226 if (!cacheDir.exists())
228 fprintf(stderr,
"%s: '%s' does not exist.\n", appName, strCacheDir.ascii());
232 TQStringList dirs = cacheDir.entryList( );
234 FileInfoList cachedEntries;
236 for(TQStringList::Iterator it = dirs.begin();
242 scanDirectory( cachedEntries, *it, strCacheDir +
"/" + *it);
246 cachedEntries.sort();
248 int maxCachedSize = m_maxCacheSize / 2;
250 for(FileInfo *fileInfo = cachedEntries.first();
252 fileInfo = cachedEntries.next())
254 if (fileInfo->size > maxCachedSize)
256 TQCString filename = TQFile::encodeName( strCacheDir +
"/" + fileInfo->name);
257 unlink(filename.data());
264 for(FileInfo *fileInfo = cachedEntries.first();
266 fileInfo = cachedEntries.next())
268 if ((totalSize + fileInfo->size) > m_maxCacheSize)
270 TQCString filename = TQFile::encodeName( strCacheDir +
"/" + fileInfo->name);
271 unlink(filename.data());
276 totalSize += fileInfo->size;
280 kdDebug () << appName <<
": Current size of cache = " << totalSize <<
" kB." << endl;