39
39
40
40
#include " lldiskcache.h"
41
41
42
+ /* *
43
+ * The prefix inserted at the start of a cache file filename to
44
+ * help identify it as a cache file. It's probably not required
45
+ * (just the presence in the cache folder is enough) but I am
46
+ * paranoid about the cache folder being set to something bad
47
+ * like the users' OS system dir by mistake or maliciously and
48
+ * this will help to offset any damage if that happens.
49
+ */
50
+ static const std::string CACHE_FILENAME_PREFIX (" sl_cache" );
51
+
52
+ std::string LLDiskCache::sCacheDir ;
53
+
42
54
LLDiskCache::LLDiskCache (const std::string cache_dir,
43
55
const uintmax_t max_size_bytes,
44
56
const bool enable_cache_debug_info) :
45
- mCacheDir(cache_dir),
46
57
mMaxSizeBytes(max_size_bytes),
47
58
mEnableCacheDebugInfo(enable_cache_debug_info)
48
59
{
49
- mCacheFilenamePrefix = " sl_cache" ;
50
-
60
+ sCacheDir = cache_dir;
51
61
LLFile::mkdir (cache_dir);
52
62
}
53
63
@@ -83,7 +93,7 @@ void LLDiskCache::purge()
83
93
{
84
94
if (mEnableCacheDebugInfo )
85
95
{
86
- LL_INFOS () << " Total dir size before purge is " << dirFileSize (mCacheDir ) << LL_ENDL;
96
+ LL_INFOS () << " Total dir size before purge is " << dirFileSize (sCacheDir ) << LL_ENDL;
87
97
}
88
98
89
99
boost::system::error_code ec;
@@ -93,9 +103,9 @@ void LLDiskCache::purge()
93
103
std::vector<file_info_t > file_info;
94
104
95
105
#if LL_WINDOWS
96
- std::wstring cache_path (utf8str_to_utf16str (mCacheDir ));
106
+ std::wstring cache_path (utf8str_to_utf16str (sCacheDir ));
97
107
#else
98
- std::string cache_path (mCacheDir );
108
+ std::string cache_path (sCacheDir );
99
109
#endif
100
110
if (boost::filesystem::is_directory (cache_path, ec) && !ec.failed ())
101
111
{
@@ -104,7 +114,7 @@ void LLDiskCache::purge()
104
114
{
105
115
if (boost::filesystem::is_regular_file (*iter, ec) && !ec.failed ())
106
116
{
107
- if ((*iter).path ().string ().find (mCacheFilenamePrefix ) != std::string::npos)
117
+ if ((*iter).path ().string ().find (CACHE_FILENAME_PREFIX ) != std::string::npos)
108
118
{
109
119
uintmax_t file_size = boost::filesystem::file_size (*iter, ec);
110
120
if (ec.failed ())
@@ -181,7 +191,7 @@ void LLDiskCache::purge()
181
191
LL_INFOS () << line.str () << LL_ENDL;
182
192
}
183
193
184
- LL_INFOS () << " Total dir size after purge is " << dirFileSize (mCacheDir ) << LL_ENDL;
194
+ LL_INFOS () << " Total dir size after purge is " << dirFileSize (sCacheDir ) << LL_ENDL;
185
195
LL_INFOS () << " Cache purge took " << execute_time << " ms to execute for " << file_info.size () << " files" << LL_ENDL;
186
196
}
187
197
}
@@ -236,97 +246,17 @@ const std::string LLDiskCache::assetTypeToString(LLAssetType::EType at)
236
246
return std::string (" UNKNOWN" );
237
247
}
238
248
239
- const std::string LLDiskCache::metaDataToFilepath (const std::string id,
240
- LLAssetType::EType at,
241
- const std::string extra_info)
249
+ const std::string LLDiskCache::metaDataToFilepath (const std::string& id, LLAssetType::EType at)
242
250
{
243
- std::ostringstream file_path;
244
-
245
- file_path << mCacheDir ;
246
- file_path << gDirUtilp ->getDirDelimiter ();
247
- file_path << mCacheFilenamePrefix ;
248
- file_path << " _" ;
249
- file_path << id;
250
- file_path << " _" ;
251
- file_path << (extra_info.empty () ? " 0" : extra_info);
252
- // file_path << "_";
253
- // file_path << assetTypeToString(at); // see SL-14210 Prune descriptive tag from new cache filenames
254
- // for details of why it was removed. Note that if you put it
255
- // back or change the format of the filename, the cache files
256
- // files will be invalidated (and perhaps, more importantly,
257
- // never deleted unless you delete them manually).
258
- file_path << " .asset" ;
259
-
260
- return file_path.str ();
261
- }
262
-
263
- void LLDiskCache::updateFileAccessTime (const std::string file_path)
264
- {
265
- /* *
266
- * Threshold in time_t units that is used to decide if the last access time
267
- * time of the file is updated or not. Added as a precaution for the concern
268
- * outlined in SL-14582 about frequent writes on older SSDs reducing their
269
- * lifespan. I think this is the right place for the threshold value - rather
270
- * than it being a pref - do comment on that Jira if you disagree...
271
- *
272
- * Let's start with 1 hour in time_t units and see how that unfolds
273
- */
274
- const std::time_t time_threshold = 1 * 60 * 60 ;
275
-
276
- // current time
277
- const std::time_t cur_time = std::time (nullptr );
278
-
279
- boost::system::error_code ec;
280
- #if LL_WINDOWS
281
- // file last write time
282
- const std::time_t last_write_time = boost::filesystem::last_write_time (utf8str_to_utf16str (file_path), ec);
283
- if (ec.failed ())
284
- {
285
- LL_WARNS () << " Failed to read last write time for cache file " << file_path << " : " << ec.message () << LL_ENDL;
286
- return ;
287
- }
288
-
289
- // delta between cur time and last time the file was written
290
- const std::time_t delta_time = cur_time - last_write_time;
291
-
292
- // we only write the new value if the time in time_threshold has elapsed
293
- // before the last one
294
- if (delta_time > time_threshold)
295
- {
296
- boost::filesystem::last_write_time (utf8str_to_utf16str (file_path), cur_time, ec);
297
- }
298
- #else
299
- // file last write time
300
- const std::time_t last_write_time = boost::filesystem::last_write_time (file_path, ec);
301
- if (ec.failed ())
302
- {
303
- LL_WARNS () << " Failed to read last write time for cache file " << file_path << " : " << ec.message () << LL_ENDL;
304
- return ;
305
- }
306
-
307
- // delta between cur time and last time the file was written
308
- const std::time_t delta_time = cur_time - last_write_time;
309
-
310
- // we only write the new value if the time in time_threshold has elapsed
311
- // before the last one
312
- if (delta_time > time_threshold)
313
- {
314
- boost::filesystem::last_write_time (file_path, cur_time, ec);
315
- }
316
- #endif
317
-
318
- if (ec.failed ())
319
- {
320
- LL_WARNS () << " Failed to update last write time for cache file " << file_path << " : " << ec.message () << LL_ENDL;
321
- }
251
+ return llformat (" %s%s%s_%s_0.asset" , sCacheDir .c_str (), gDirUtilp ->getDirDelimiter ().c_str (), CACHE_FILENAME_PREFIX.c_str (), id.c_str ());
322
252
}
323
253
324
254
const std::string LLDiskCache::getCacheInfo ()
325
255
{
326
256
std::ostringstream cache_info;
327
257
328
258
F32 max_in_mb = (F32)mMaxSizeBytes / (1024 .0f * 1024 .0f );
329
- F32 percent_used = ((F32)dirFileSize (mCacheDir ) / (F32)mMaxSizeBytes ) * 100 .0f ;
259
+ F32 percent_used = ((F32)dirFileSize (sCacheDir ) / (F32)mMaxSizeBytes ) * 100 .0f ;
330
260
331
261
cache_info << std::fixed;
332
262
cache_info << std::setprecision (1 );
@@ -346,9 +276,9 @@ void LLDiskCache::clearCache()
346
276
*/
347
277
boost::system::error_code ec;
348
278
#if LL_WINDOWS
349
- std::wstring cache_path (utf8str_to_utf16str (mCacheDir ));
279
+ std::wstring cache_path (utf8str_to_utf16str (sCacheDir ));
350
280
#else
351
- std::string cache_path (mCacheDir );
281
+ std::string cache_path (sCacheDir );
352
282
#endif
353
283
if (boost::filesystem::is_directory (cache_path, ec) && !ec.failed ())
354
284
{
@@ -357,7 +287,7 @@ void LLDiskCache::clearCache()
357
287
{
358
288
if (boost::filesystem::is_regular_file (*iter, ec) && !ec.failed ())
359
289
{
360
- if ((*iter).path ().string ().find (mCacheFilenamePrefix ) != std::string::npos)
290
+ if ((*iter).path ().string ().find (CACHE_FILENAME_PREFIX ) != std::string::npos)
361
291
{
362
292
boost::filesystem::remove (*iter, ec);
363
293
if (ec.failed ())
@@ -431,7 +361,7 @@ uintmax_t LLDiskCache::dirFileSize(const std::string dir)
431
361
{
432
362
if (boost::filesystem::is_regular_file (*iter, ec) && !ec.failed ())
433
363
{
434
- if ((*iter).path ().string ().find (mCacheFilenamePrefix ) != std::string::npos)
364
+ if ((*iter).path ().string ().find (CACHE_FILENAME_PREFIX ) != std::string::npos)
435
365
{
436
366
uintmax_t file_size = boost::filesystem::file_size (*iter, ec);
437
367
if (!ec.failed ())
0 commit comments