1 /* 2 * Copyright 2012 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Paweł Dziepak, pdziepak@quarnos.org 7 */ 8 #ifndef METADATACACHE_H 9 #define METADATACACHE_H 10 11 12 #include <fs_interface.h> 13 #include <lock.h> 14 #include <SupportDefs.h> 15 #include <util/AutoLock.h> 16 #include <util/AVLTreeMap.h> 17 18 19 class Inode; 20 21 struct AccessEntry { 22 time_t fExpire; 23 bool fForceValid; 24 25 uint32 fAllowed; 26 }; 27 28 class MetadataCache { 29 public: 30 MetadataCache(Inode* inode); 31 ~MetadataCache(); 32 33 status_t GetStat(struct stat* st); 34 void SetStat(const struct stat& st); 35 void GrowFile(size_t newSize); 36 37 status_t GetAccess(uid_t uid, uint32* allowed); 38 void SetAccess(uid_t uid, uint32 allowed); 39 40 status_t LockValid(); 41 void UnlockValid(); 42 43 inline void InvalidateStat(); 44 inline void InvalidateAccess(); 45 46 inline void Invalidate(); 47 48 static const time_t kExpirationTime = 60; 49 50 protected: 51 void NotifyChanges(const struct stat* oldStat, 52 const struct stat* newStat); 53 54 private: 55 struct stat fStatCache; 56 time_t fExpire; 57 bool fForceValid; 58 59 Inode* fInode; 60 bool fInited; 61 62 AVLTreeMap<uid_t, AccessEntry> fAccessCache; 63 64 mutex fLock; 65 }; 66 67 68 inline void InvalidateStat()69MetadataCache::InvalidateStat() 70 { 71 MutexLocker _(fLock); 72 if (!fForceValid) 73 fExpire = 0; 74 } 75 76 77 inline void InvalidateAccess()78MetadataCache::InvalidateAccess() 79 { 80 MutexLocker _(fLock); 81 if (!fForceValid) 82 fAccessCache.MakeEmpty(); 83 } 84 85 86 inline void Invalidate()87MetadataCache::Invalidate() 88 { 89 InvalidateStat(); 90 InvalidateAccess(); 91 } 92 93 94 #endif // METADATACACHE_H 95 96