file.go 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package tools
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // File record to track when files were read/written
  7. type fileRecord struct {
  8. path string
  9. readTime time.Time
  10. writeTime time.Time
  11. }
  12. var (
  13. fileRecords = make(map[string]fileRecord)
  14. fileRecordMutex sync.RWMutex
  15. )
  16. func recordFileRead(path string) {
  17. fileRecordMutex.Lock()
  18. defer fileRecordMutex.Unlock()
  19. record, exists := fileRecords[path]
  20. if !exists {
  21. record = fileRecord{path: path}
  22. }
  23. record.readTime = time.Now()
  24. fileRecords[path] = record
  25. }
  26. func getLastReadTime(path string) time.Time {
  27. fileRecordMutex.RLock()
  28. defer fileRecordMutex.RUnlock()
  29. record, exists := fileRecords[path]
  30. if !exists {
  31. return time.Time{}
  32. }
  33. return record.readTime
  34. }
  35. func recordFileWrite(path string) {
  36. fileRecordMutex.Lock()
  37. defer fileRecordMutex.Unlock()
  38. record, exists := fileRecords[path]
  39. if !exists {
  40. record = fileRecord{path: path}
  41. }
  42. record.writeTime = time.Now()
  43. fileRecords[path] = record
  44. }