LocalFileIO.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/base.h>
  10. #include <AzCore/std/containers/unordered_map.h>
  11. #include <AzCore/std/parallel/mutex.h>
  12. #include <AzCore/std/parallel/atomic.h>
  13. #include <AzCore/IO/FileIO.h>
  14. #include <AzCore/RTTI/RTTI.h>
  15. #include <AzCore/Memory/Memory.h>
  16. // This header file and CPP handles the platform specific implementation of code as defined by the FileIOBase interface class.
  17. // In order to make your code portable and functional with both this and the RemoteFileIO class, use the interface to access
  18. // the methods.
  19. namespace AZ
  20. {
  21. namespace IO
  22. {
  23. class SystemFile;
  24. class LocalFileIO
  25. : public FileIOBase
  26. {
  27. public:
  28. AZ_RTTI(LocalFileIO, "{87A8D32B-F695-4105-9A4D-D99BE15DFD50}", FileIOBase);
  29. AZ_CLASS_ALLOCATOR(LocalFileIO, SystemAllocator, 0);
  30. LocalFileIO();
  31. ~LocalFileIO();
  32. Result Open(const char* filePath, OpenMode mode, HandleType& fileHandle) override;
  33. Result Close(HandleType fileHandle) override;
  34. Result Tell(HandleType fileHandle, AZ::u64& offset) override;
  35. Result Seek(HandleType fileHandle, AZ::s64 offset, SeekType type) override;
  36. Result Size(HandleType fileHandle, AZ::u64& size) override;
  37. Result Read(HandleType fileHandle, void* buffer, AZ::u64 size, bool failOnFewerThanSizeBytesRead = false, AZ::u64* bytesRead = nullptr) override;
  38. Result Write(HandleType fileHandle, const void* buffer, AZ::u64 size, AZ::u64* bytesWritten = nullptr) override;
  39. Result Flush(HandleType fileHandle) override;
  40. bool Eof(HandleType fileHandle) override;
  41. AZ::u64 ModificationTime(HandleType fileHandle) override;
  42. bool Exists(const char* filePath) override;
  43. Result Size(const char* filePath, AZ::u64& size) override;
  44. AZ::u64 ModificationTime(const char* filePath) override;
  45. bool IsDirectory(const char* filePath) override;
  46. bool IsReadOnly(const char* filePath) override;
  47. Result CreatePath(const char* filePath) override;
  48. Result DestroyPath(const char* filePath) override;
  49. Result Remove(const char* filePath) override;
  50. Result Copy(const char* sourceFilePath, const char* destinationFilePath) override;
  51. Result Rename(const char* originalFilePath, const char* newFilePath) override;
  52. Result FindFiles(const char* filePath, const char* filter, FindFilesCallbackType callback) override;
  53. void SetAlias(const char* alias, const char* path) override;
  54. void ClearAlias(const char* alias) override;
  55. const char* GetAlias(const char* alias) const override;
  56. void SetDeprecatedAlias(AZStd::string_view oldAlias, AZStd::string_view newAlias) override;
  57. AZStd::optional<AZ::u64> ConvertToAlias(char* inOutBuffer, AZ::u64 bufferLength) const override;
  58. bool ConvertToAlias(AZ::IO::FixedMaxPath& convertedPath, const AZ::IO::PathView& path) const override;
  59. using FileIOBase::ConvertToAlias;
  60. bool ResolvePath(const char* path, char* resolvedPath, AZ::u64 resolvedPathSize) const override;
  61. bool ResolvePath(AZ::IO::FixedMaxPath& resolvedPath, const AZ::IO::PathView& path) const override;
  62. using FileIOBase::ResolvePath;
  63. bool ReplaceAlias(AZ::IO::FixedMaxPath& replacedAliasPath, const AZ::IO::PathView& path) const override;
  64. bool GetFilename(HandleType fileHandle, char* filename, AZ::u64 filenameSize) const override;
  65. bool ConvertToAbsolutePath(const char* path, char* absolutePath, AZ::u64 maxLength) const;
  66. private:
  67. SystemFile* GetFilePointerFromHandle(HandleType fileHandle);
  68. HandleType GetNextHandle();
  69. AZStd::optional<AZ::u64> ConvertToAliasBuffer(char* outBuffer, AZ::u64 outBufferLength, AZStd::string_view inBuffer) const;
  70. bool ResolveAliases(const char* path, char* resolvedPath, AZ::u64 resolvedPathSize) const;
  71. bool LowerIfBeginsWith(char* inOutBuffer, AZ::u64 bufferLen, const char* alias) const;
  72. private:
  73. static AZStd::string RemoveTrailingSlash(const AZStd::string& pathStr);
  74. static AZStd::string CheckForTrailingSlash(const AZStd::string& pathStr);
  75. mutable AZStd::recursive_mutex m_openFileGuard;
  76. AZStd::atomic<HandleType> m_nextHandle;
  77. AZStd::unordered_map<HandleType, SystemFile> m_openFiles;
  78. AZStd::unordered_map<AZStd::string, AZStd::string> m_aliases;
  79. AZStd::unordered_map<AZStd::string, AZStd::string> m_deprecatedAliases;
  80. void CheckInvalidWrite(const char* path);
  81. };
  82. } // namespace IO
  83. } // namespace AZ