• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeioslave/http
 

tdeioslave/http

  • tdeioslave
  • http
http_cache_cleaner.cpp
1 /*
2 This file is part of KDE
3 
4  Copyright (C) 1999-2000 Waldo Bastian (bastian@kde.org)
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 //----------------------------------------------------------------------------
24 //
25 // KDE Http Cache cleanup tool
26 // $Id$
27 
28 #include <time.h>
29 #include <stdlib.h>
30 
31 #include <tqdir.h>
32 #include <tqstring.h>
33 #include <tqptrlist.h>
34 
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>
42 
43 #include <unistd.h>
44 
45 #include <kdebug.h>
46 
47 time_t currentDate;
48 int m_maxCacheAge;
49 int m_maxCacheSize;
50 
51 static const char appName[] = "tdeio_http_cache_cleaner";
52 
53 static const char description[] = I18N_NOOP("TDE HTTP cache maintenance tool");
54 
55 static const char version[] = "1.0.0";
56 
57 static const TDECmdLineOptions options[] =
58 {
59  {"clear-all", I18N_NOOP("Empty the cache"), 0},
60  TDECmdLineLastOption
61 };
62 
63 struct FileInfo {
64  TQString name;
65  int size; // Size in Kb.
66  int age;
67 };
68 
69 template class TQPtrList<FileInfo>;
70 
71 class FileInfoList : public TQPtrList<FileInfo>
72 {
73 public:
74  FileInfoList() : TQPtrList<FileInfo>() { }
75  int compareItems(TQPtrCollection::Item item1, TQPtrCollection::Item item2)
76  { return ((FileInfo *)item1)->age - ((FileInfo *)item2)->age; }
77 };
78 
79 // !START OF SYNC!
80 // Keep the following in sync with the cache code in http.cpp
81 #define CACHE_REVISION "7\n"
82 
83 FileInfo *readEntry( const TQString &filename)
84 {
85  TQCString CEF = TQFile::encodeName(filename);
86  FILE *fs = fopen( CEF.data(), "r");
87  if (!fs)
88  return 0;
89 
90  char buffer[401];
91  bool ok = true;
92 
93  // CacheRevision
94  if (ok && (!fgets(buffer, 400, fs)))
95  ok = false;
96  if (ok && (strcmp(buffer, CACHE_REVISION) != 0))
97  ok = false;
98 
99  // Full URL
100  if (ok && (!fgets(buffer, 400, fs)))
101  ok = false;
102 
103  time_t creationDate;
104  int age =0;
105 
106  // Creation Date
107  if (ok && (!fgets(buffer, 400, fs)))
108  ok = false;
109  if (ok)
110  {
111  creationDate = (time_t) strtoul(buffer, 0, 10);
112  age = (int) difftime(currentDate, creationDate);
113  if ( m_maxCacheAge && ( age > m_maxCacheAge))
114  {
115  ok = false; // Expired
116  }
117  }
118 
119  // Expiration Date
120  if (ok && (!fgets(buffer, 400, fs)))
121  ok = false;
122  if (ok)
123  {
124 //WABA: It seems I slightly misunderstood the meaning of "Expire:" header.
125 #if 0
126  time_t expireDate;
127  expireDate = (time_t) strtoul(buffer, 0, 10);
128  if (expireDate && (expireDate < currentDate))
129  ok = false; // Expired
130 #endif
131  }
132 
133  // ETag
134  if (ok && (!fgets(buffer, 400, fs)))
135  ok = false;
136  if (ok)
137  {
138  // Ignore ETag
139  }
140 
141  // Last-Modified
142  if (ok && (!fgets(buffer, 400, fs)))
143  ok = false;
144  if (ok)
145  {
146  // Ignore Last-Modified
147  }
148 
149 
150  fclose(fs);
151  if (ok)
152  {
153  FileInfo *info = new FileInfo;
154  info->age = age;
155  return info;
156  }
157 
158  unlink( CEF.data());
159  return 0;
160 }
161 // Keep the above in sync with the cache code in http.cpp
162 // !END OF SYNC!
163 
164 void scanDirectory(FileInfoList &fileEntries, const TQString &name, const TQString &strDir)
165 {
166  TQDir dir(strDir);
167  if (!dir.exists()) return;
168 
169  TQFileInfoList *newEntries = (TQFileInfoList *) dir.entryInfoList();
170 
171  if (!newEntries) return; // Directory not accessible ??
172 
173  for(TQFileInfo *qFileInfo = newEntries->first();
174  qFileInfo;
175  qFileInfo = newEntries->next())
176  {
177  if (qFileInfo->isFile())
178  {
179  FileInfo *fileInfo = readEntry( strDir + "/" + qFileInfo->fileName());
180  if (fileInfo)
181  {
182  fileInfo->name = name + "/" + qFileInfo->fileName();
183  fileInfo->size = (qFileInfo->size() + 1023) / 1024;
184  fileEntries.append(fileInfo);
185  }
186  }
187  }
188 }
189 
190 extern "C" TDE_EXPORT int kdemain(int argc, char **argv)
191 {
192  TDELocale::setMainCatalogue("tdelibs");
193  TDECmdLineArgs::init( argc, argv, appName,
194  I18N_NOOP("TDE HTTP cache maintenance tool"),
195  description, version, true);
196 
197  TDECmdLineArgs::addCmdLineOptions( options );
198 
199  TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
200 
201  bool deleteAll = args->isSet("clear-all");
202 
203  TDEInstance ins( appName );
204 
205  if (!deleteAll)
206  {
207  DCOPClient *dcop = new DCOPClient();
208  TQCString name = dcop->registerAs(appName, false);
209  if (!name.isEmpty() && (name != appName))
210  {
211  fprintf(stderr, "%s: Already running! (%s)\n", appName, name.data());
212  return 0;
213  }
214  }
215 
216  currentDate = time(0);
217  m_maxCacheAge = KProtocolManager::maxCacheAge();
218  m_maxCacheSize = KProtocolManager::maxCacheSize();
219 
220  if (deleteAll)
221  m_maxCacheSize = -1;
222 
223  TQString strCacheDir = TDEGlobal::dirs()->saveLocation("cache", "http");
224 
225  TQDir cacheDir( strCacheDir );
226  if (!cacheDir.exists())
227  {
228  fprintf(stderr, "%s: '%s' does not exist.\n", appName, strCacheDir.ascii());
229  return 0;
230  }
231 
232  TQStringList dirs = cacheDir.entryList( );
233 
234  FileInfoList cachedEntries;
235 
236  for(TQStringList::Iterator it = dirs.begin();
237  it != dirs.end();
238  it++)
239  {
240  if ((*it)[0] != '.')
241  {
242  scanDirectory( cachedEntries, *it, strCacheDir + "/" + *it);
243  }
244  }
245 
246  cachedEntries.sort();
247 
248  int maxCachedSize = m_maxCacheSize / 2;
249 
250  for(FileInfo *fileInfo = cachedEntries.first();
251  fileInfo;
252  fileInfo = cachedEntries.next())
253  {
254  if (fileInfo->size > maxCachedSize)
255  {
256  TQCString filename = TQFile::encodeName( strCacheDir + "/" + fileInfo->name);
257  unlink(filename.data());
258 // kdDebug () << appName << ": Object too big, deleting '" << filename.data() << "' (" << result<< ")" << endl;
259  }
260  }
261 
262  int totalSize = 0;
263 
264  for(FileInfo *fileInfo = cachedEntries.first();
265  fileInfo;
266  fileInfo = cachedEntries.next())
267  {
268  if ((totalSize + fileInfo->size) > m_maxCacheSize)
269  {
270  TQCString filename = TQFile::encodeName( strCacheDir + "/" + fileInfo->name);
271  unlink(filename.data());
272 // kdDebug () << appName << ": Cache too big, deleting '" << filename.data() << "' (" << fileInfo->size << ")" << endl;
273  }
274  else
275  {
276  totalSize += fileInfo->size;
277 // fprintf(stderr, "Keep in cache: %s %d %d total = %d\n", fileInfo->name.ascii(), fileInfo->size, fileInfo->age, totalSize);
278  }
279  }
280  kdDebug () << appName << ": Current size of cache = " << totalSize << " kB." << endl;
281  return 0;
282 }
283 
284 

tdeioslave/http

Skip menu "tdeioslave/http"
  • Main Page
  • Alphabetical List
  • Class List
  • File List

tdeioslave/http

Skip menu "tdeioslave/http"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeioslave/http by doxygen 1.9.1
This website is maintained by Timothy Pearson.