/indra/llvfs/llvfs.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 182 lines · 106 code · 33 blank · 43 comment · 1 complexity · 4bc7042b47b097cf430e71fa6aeae254 MD5 · raw file

  1. /**
  2. * @file llvfs.h
  3. * @brief Definition of virtual file system
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLVFS_H
  27. #define LL_LLVFS_H
  28. #include <deque>
  29. #include "lluuid.h"
  30. #include "linked_lists.h"
  31. #include "llassettype.h"
  32. #include "llthread.h"
  33. enum EVFSValid
  34. {
  35. VFSVALID_UNKNOWN = 0,
  36. VFSVALID_OK = 1,
  37. VFSVALID_BAD_CORRUPT = 2,
  38. VFSVALID_BAD_CANNOT_OPEN_READONLY = 3,
  39. VFSVALID_BAD_CANNOT_CREATE = 4
  40. };
  41. // Lock types for open vfiles, pending async reads, and pending async appends
  42. // (There are no async normal writes, currently)
  43. enum EVFSLock
  44. {
  45. VFSLOCK_OPEN = 0,
  46. VFSLOCK_READ = 1,
  47. VFSLOCK_APPEND = 2,
  48. VFSLOCK_COUNT = 3
  49. };
  50. // internal classes
  51. class LLVFSBlock;
  52. class LLVFSFileBlock;
  53. class LLVFSFileSpecifier
  54. {
  55. public:
  56. LLVFSFileSpecifier();
  57. LLVFSFileSpecifier(const LLUUID &file_id, const LLAssetType::EType file_type);
  58. bool operator<(const LLVFSFileSpecifier &rhs) const;
  59. bool operator==(const LLVFSFileSpecifier &rhs) const;
  60. public:
  61. LLUUID mFileID;
  62. LLAssetType::EType mFileType;
  63. };
  64. class LLVFS
  65. {
  66. private:
  67. // Use createLLVFS() to open a VFS file
  68. // Pass 0 to not presize
  69. LLVFS(const std::string& index_filename,
  70. const std::string& data_filename,
  71. const BOOL read_only,
  72. const U32 presize,
  73. const BOOL remove_after_crash);
  74. public:
  75. ~LLVFS();
  76. // Use this function normally to create LLVFS files
  77. // Pass 0 to not presize
  78. static LLVFS * createLLVFS(const std::string& index_filename,
  79. const std::string& data_filename,
  80. const BOOL read_only,
  81. const U32 presize,
  82. const BOOL remove_after_crash);
  83. BOOL isValid() const { return (VFSVALID_OK == mValid); }
  84. EVFSValid getValidState() const { return mValid; }
  85. // ---------- The following fucntions lock/unlock mDataMutex ----------
  86. BOOL getExists(const LLUUID &file_id, const LLAssetType::EType file_type);
  87. S32 getSize(const LLUUID &file_id, const LLAssetType::EType file_type);
  88. BOOL checkAvailable(S32 max_size);
  89. S32 getMaxSize(const LLUUID &file_id, const LLAssetType::EType file_type);
  90. BOOL setMaxSize(const LLUUID &file_id, const LLAssetType::EType file_type, S32 max_size);
  91. void renameFile(const LLUUID &file_id, const LLAssetType::EType file_type,
  92. const LLUUID &new_id, const LLAssetType::EType &new_type);
  93. void removeFile(const LLUUID &file_id, const LLAssetType::EType file_type);
  94. S32 getData(const LLUUID &file_id, const LLAssetType::EType file_type, U8 *buffer, S32 location, S32 length);
  95. S32 storeData(const LLUUID &file_id, const LLAssetType::EType file_type, const U8 *buffer, S32 location, S32 length);
  96. void incLock(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock);
  97. void decLock(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock);
  98. BOOL isLocked(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock);
  99. // ----------------------------------------------------------------
  100. // Used to trigger evil WinXP behavior of "preloading" entire file into memory.
  101. void pokeFiles();
  102. // Verify that the index file contents match the in-memory file structure
  103. // Very slow, do not call routinely. JC
  104. void audit();
  105. // Check for uninitialized blocks. Slow, do not call in release. JC
  106. void checkMem();
  107. // for debugging, prints a map of the vfs
  108. void dumpMap();
  109. void dumpLockCounts();
  110. void dumpStatistics();
  111. void listFiles();
  112. void dumpFiles();
  113. protected:
  114. void removeFileBlock(LLVFSFileBlock *fileblock);
  115. void eraseBlockLength(LLVFSBlock *block);
  116. void eraseBlock(LLVFSBlock *block);
  117. void addFreeBlock(LLVFSBlock *block);
  118. //void mergeFreeBlocks();
  119. void useFreeSpace(LLVFSBlock *free_block, S32 length);
  120. void sync(LLVFSFileBlock *block, BOOL remove = FALSE);
  121. void presizeDataFile(const U32 size);
  122. static LLFILE *openAndLock(const std::string& filename, const char* mode, BOOL read_lock);
  123. static void unlockAndClose(FILE *fp);
  124. // Can initiate LRU-based file removal to make space.
  125. // The immune file block will not be removed.
  126. LLVFSBlock *findFreeBlock(S32 size, LLVFSFileBlock *immune = NULL);
  127. // lock/unlock data mutex (mDataMutex)
  128. void lockData() { mDataMutex->lock(); }
  129. void unlockData() { mDataMutex->unlock(); }
  130. protected:
  131. LLMutex* mDataMutex;
  132. typedef std::map<LLVFSFileSpecifier, LLVFSFileBlock*> fileblock_map;
  133. fileblock_map mFileBlocks;
  134. typedef std::multimap<S32, LLVFSBlock*> blocks_length_map_t;
  135. blocks_length_map_t mFreeBlocksByLength;
  136. typedef std::multimap<U32, LLVFSBlock*> blocks_location_map_t;
  137. blocks_location_map_t mFreeBlocksByLocation;
  138. LLFILE *mDataFP;
  139. LLFILE *mIndexFP;
  140. std::deque<S32> mIndexHoles;
  141. std::string mIndexFilename;
  142. std::string mDataFilename;
  143. BOOL mReadOnly;
  144. EVFSValid mValid;
  145. S32 mLockCounts[VFSLOCK_COUNT];
  146. BOOL mRemoveAfterCrash;
  147. };
  148. extern LLVFS *gVFS;
  149. #endif