llfilesystem.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file llfilesystem.h
  3. * @brief Definition of the local file system implementation.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2020, Linden Research, Inc. (c) 2021 Henri Beauchamp.
  8. *
  9. * Modifications by Henri Beauchamp:
  10. * - Pointless per-asset-type file naming removed.
  11. * - Cached filename for faster operations.
  12. * - Use of faster LLFile operations where possible.
  13. * - Fixed various bugs in write operations. Removed the pointless READ_WRITE
  14. * mode, added the OVERWRITE one, and changed seek() to auto-padding files
  15. * with zeros in WRITE mode when seeking past the end of an existing file.
  16. * - Real time tracking of bytes added to/removed from cache.
  17. * - Proper cache validity verification.
  18. * - Immediate date-stamping on creation of LLFileSystem instances, to prevent
  19. * potential race conditions with the threaded cache purging mechanism.
  20. * - Multiple threads and multiple viewer instances deconfliction.
  21. * - Added LLFile::sFlushOnWrite to work around a bug in Wine (*) which
  22. * reports a wrong file position after non flushed writes. (*) This is for
  23. * people perverted enough to run a Windows build under Wine under Linux
  24. * instead of a Linux native build: yes, I'm perverted since I do it to test
  25. * Windows builds under Linux... :-P
  26. *
  27. * Second Life Viewer Source Code
  28. * The source code in this file ("Source Code") is provided by Linden Lab
  29. * to you under the terms of the GNU General Public License, version 2.0
  30. * ("GPL"), unless you have obtained a separate licensing agreement
  31. * ("Other License"), formally executed by you and Linden Lab. Terms of
  32. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  33. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  34. *
  35. * There are special exceptions to the terms and conditions of the GPL as
  36. * it is applied to this Source Code. View the full text of the exception
  37. * in the file doc/FLOSS-exception.txt in this software distribution, or
  38. * online at
  39. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  40. *
  41. * By copying, modifying or distributing this software, you acknowledge
  42. * that you have read and understood your obligations described above,
  43. * and agree to abide by those obligations.
  44. *
  45. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  46. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  47. * COMPLETENESS OR PERFORMANCE.
  48. * $/LicenseInfo$
  49. */
  50. #ifndef LL_FILESYSTEM_H
  51. #define LL_FILESYSTEM_H
  52. #include "lluuid.h"
  53. // NOTE: this class supports only 2GB or smaller files (way more than what we
  54. // do need).
  55. class LLFileSystem
  56. {
  57. protected:
  58. LOG_CLASS(LLFileSystem);
  59. public:
  60. enum
  61. {
  62. READ = 0x00000001,
  63. WRITE = 0x00000002,
  64. OVERWRITE = 0x00000004,
  65. APPEND = 0x00000008
  66. };
  67. LLFileSystem(const LLUUID& id, S32 mode = READ,
  68. const char* extra_info = NULL); // extra_info not used for now
  69. ~LLFileSystem();
  70. bool read(U8* buffer, S32 bytes);
  71. bool write(const U8* buffer, S32 bytes);
  72. // IMPORTANT: seek() is reserved for READ and WRITE modes (OVERWRITE always
  73. // writes from start of file, and APPEND from its end). A llerrs will occur
  74. // if you try to seek() in OVERWRITE or APPEND mode !
  75. bool seek(S32 offset, S32 origin = -1);
  76. LL_INLINE const std::string& getName() const { return mFilename; }
  77. LL_INLINE S32 tell() const { return mPosition; }
  78. LL_INLINE bool eof() const { return mPosition >= getSize(); }
  79. LL_INLINE S32 getLastBytesRead() const { return mBytesRead; }
  80. S32 getSize() const;
  81. // WARNING: mExists is cached and this method can therefore return a wrong
  82. // value if you touch the file with static methods, or with another program
  83. // (viewer instance) in-between calls to the constructor, read(), write(),
  84. // seek(), remove() or rename()...
  85. LL_INLINE bool exists() const { return mExists; }
  86. bool remove();
  87. bool rename(const LLUUID& new_id);
  88. static bool getExists(const LLUUID& id, const char* extra_info = NULL);
  89. static bool removeFile(const LLUUID& id, const char* extra_info = NULL);
  90. static bool renameFile(const LLUUID& old_id, const LLUUID& new_id,
  91. const char* extra_info = NULL);
  92. static S32 getFileSize(const LLUUID& id, const char* extra_info = NULL);
  93. protected:
  94. LLUUID mFileID;
  95. std::string mFilename;
  96. std::string mExtraInfo;
  97. S32 mMode;
  98. S32 mPosition;
  99. S32 mBytesRead;
  100. S32 mTotalBytesWritten;
  101. bool mExists; // true when the file exists
  102. bool mValid; // true when the disk cache is valid
  103. };
  104. #endif // LL_FILESYSTEM_H