Delete all but the oldest 5 havecache/statcache dirs, unless they're newer than 30 days.
()
| 355 | // Delete all but the oldest 5 havecache/statcache dirs, unless they're newer |
| 356 | // than 30 days. |
| 357 | func cleanCacheDir() { |
| 358 | dir := osutil.CacheDir() |
| 359 | f, err := os.Open(dir) |
| 360 | if err != nil { |
| 361 | return |
| 362 | } |
| 363 | defer f.Close() |
| 364 | fis, err := f.Readdir(-1) |
| 365 | if err != nil { |
| 366 | return |
| 367 | } |
| 368 | var haveCache, statCache []os.FileInfo |
| 369 | seen := make(map[string]bool) |
| 370 | for _, fi := range fis { |
| 371 | seen[fi.Name()] = true |
| 372 | } |
| 373 | |
| 374 | for _, fi := range fis { |
| 375 | if strings.HasPrefix(fi.Name(), "pk-put.havecache.") { |
| 376 | haveCache = append(haveCache, fi) |
| 377 | continue |
| 378 | } |
| 379 | if strings.HasPrefix(fi.Name(), "pk-put.statcache.") { |
| 380 | statCache = append(statCache, fi) |
| 381 | continue |
| 382 | } |
| 383 | } |
| 384 | for _, list := range [][]os.FileInfo{haveCache, statCache} { |
| 385 | if len(list) <= 5 { |
| 386 | continue |
| 387 | } |
| 388 | sort.Sort(byModtime(list)) |
| 389 | list = list[:len(list)-5] |
| 390 | for _, fi := range list { |
| 391 | if fi.ModTime().Before(time.Now().Add(-30 * 24 * time.Hour)) { |
| 392 | os.RemoveAll(filepath.Join(dir, fi.Name())) |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | type byModtime []os.FileInfo |
| 399 |