PageRenderTime 92ms CodeModel.GetById 70ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 27#ifndef LL_LLVFS_H
 28#define LL_LLVFS_H
 29
 30#include <deque>
 31#include "lluuid.h"
 32#include "linked_lists.h"
 33#include "llassettype.h"
 34#include "llthread.h"
 35
 36enum EVFSValid 
 37{
 38	VFSVALID_UNKNOWN = 0, 
 39	VFSVALID_OK = 1,
 40	VFSVALID_BAD_CORRUPT = 2,
 41	VFSVALID_BAD_CANNOT_OPEN_READONLY = 3,
 42	VFSVALID_BAD_CANNOT_CREATE = 4
 43};
 44
 45// Lock types for open vfiles, pending async reads, and pending async appends
 46// (There are no async normal writes, currently)
 47enum EVFSLock
 48{
 49	VFSLOCK_OPEN = 0,
 50	VFSLOCK_READ = 1,
 51	VFSLOCK_APPEND = 2,
 52
 53	VFSLOCK_COUNT = 3
 54};
 55
 56// internal classes
 57class LLVFSBlock;
 58class LLVFSFileBlock;
 59class LLVFSFileSpecifier
 60{
 61public:
 62	LLVFSFileSpecifier();
 63	LLVFSFileSpecifier(const LLUUID &file_id, const LLAssetType::EType file_type);
 64	bool operator<(const LLVFSFileSpecifier &rhs) const;
 65	bool operator==(const LLVFSFileSpecifier &rhs) const;
 66
 67public:
 68	LLUUID mFileID;
 69	LLAssetType::EType mFileType;
 70};
 71
 72class LLVFS
 73{
 74private:
 75	// Use createLLVFS() to open a VFS file
 76	// Pass 0 to not presize
 77	LLVFS(const std::string& index_filename, 
 78			const std::string& data_filename, 
 79			const BOOL read_only, 
 80			const U32 presize, 
 81			const BOOL remove_after_crash);
 82public:
 83	~LLVFS();
 84
 85	// Use this function normally to create LLVFS files
 86	// Pass 0 to not presize
 87	static LLVFS * createLLVFS(const std::string& index_filename, 
 88			const std::string& data_filename, 
 89			const BOOL read_only, 
 90			const U32 presize, 
 91			const BOOL remove_after_crash);
 92
 93	BOOL isValid() const			{ return (VFSVALID_OK == mValid); }
 94	EVFSValid getValidState() const	{ return mValid; }
 95
 96	// ---------- The following fucntions lock/unlock mDataMutex ----------
 97	BOOL getExists(const LLUUID &file_id, const LLAssetType::EType file_type);
 98	S32	 getSize(const LLUUID &file_id, const LLAssetType::EType file_type);
 99
100	BOOL checkAvailable(S32 max_size);
101	
102	S32  getMaxSize(const LLUUID &file_id, const LLAssetType::EType file_type);
103	BOOL setMaxSize(const LLUUID &file_id, const LLAssetType::EType file_type, S32 max_size);
104
105	void renameFile(const LLUUID &file_id, const LLAssetType::EType file_type,
106		const LLUUID &new_id, const LLAssetType::EType &new_type);
107	void removeFile(const LLUUID &file_id, const LLAssetType::EType file_type);
108
109	S32 getData(const LLUUID &file_id, const LLAssetType::EType file_type, U8 *buffer, S32 location, S32 length);
110	S32 storeData(const LLUUID &file_id, const LLAssetType::EType file_type, const U8 *buffer, S32 location, S32 length);
111
112	void incLock(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock);
113	void decLock(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock);
114	BOOL isLocked(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock);
115	// ----------------------------------------------------------------
116
117	// Used to trigger evil WinXP behavior of "preloading" entire file into memory.
118	void pokeFiles();
119
120	// Verify that the index file contents match the in-memory file structure
121	// Very slow, do not call routinely. JC
122	void audit();
123	// Check for uninitialized blocks.  Slow, do not call in release. JC
124	void checkMem();
125	// for debugging, prints a map of the vfs
126	void dumpMap();
127	void dumpLockCounts();
128	void dumpStatistics();
129	void listFiles();
130	void dumpFiles();
131
132protected:
133	void removeFileBlock(LLVFSFileBlock *fileblock);
134	
135	void eraseBlockLength(LLVFSBlock *block);
136	void eraseBlock(LLVFSBlock *block);
137	void addFreeBlock(LLVFSBlock *block);
138	//void mergeFreeBlocks();
139	void useFreeSpace(LLVFSBlock *free_block, S32 length);
140	void sync(LLVFSFileBlock *block, BOOL remove = FALSE);
141	void presizeDataFile(const U32 size);
142
143	static LLFILE *openAndLock(const std::string& filename, const char* mode, BOOL read_lock);
144	static void unlockAndClose(FILE *fp);
145	
146	// Can initiate LRU-based file removal to make space.
147	// The immune file block will not be removed.
148	LLVFSBlock *findFreeBlock(S32 size, LLVFSFileBlock *immune = NULL);
149
150	// lock/unlock data mutex (mDataMutex)
151	void lockData() { mDataMutex->lock(); }
152	void unlockData() { mDataMutex->unlock(); }	
153	
154protected:
155	LLMutex* mDataMutex;
156	
157	typedef std::map<LLVFSFileSpecifier, LLVFSFileBlock*> fileblock_map;
158	fileblock_map mFileBlocks;
159
160	typedef std::multimap<S32, LLVFSBlock*>	blocks_length_map_t;
161	blocks_length_map_t 	mFreeBlocksByLength;
162	typedef std::multimap<U32, LLVFSBlock*>	blocks_location_map_t;
163	blocks_location_map_t 	mFreeBlocksByLocation;
164
165	LLFILE *mDataFP;
166	LLFILE *mIndexFP;
167
168	std::deque<S32> mIndexHoles;
169
170	std::string mIndexFilename;
171	std::string mDataFilename;
172	BOOL mReadOnly;
173
174	EVFSValid mValid;
175
176	S32 mLockCounts[VFSLOCK_COUNT];
177	BOOL mRemoveAfterCrash;
178};
179
180extern LLVFS *gVFS;
181
182#endif