| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- //
- // Copyright (c) 2008-2016 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../Core/Profiler.h"
- #include "../IO/File.h"
- #include "../IO/FileSystem.h"
- #include "../IO/Log.h"
- #include "../IO/MemoryBuffer.h"
- #include "../IO/PackageFile.h"
- #ifdef __ANDROID__
- // ATOMIC BEGIN
- #include <SDL/include/SDL_rwops.h>
- // ATOMIC END
- #endif
- #include <cstdio>
- #include <LZ4/lz4.h>
- #include "../DebugNew.h"
- namespace Atomic
- {
- #ifdef _WIN32
- static const wchar_t* openMode[] =
- {
- L"rb",
- L"wb",
- L"r+b",
- L"w+b",
- // ATOMIC BEGIN
- L"a+b"
- // ATOMIC END
- };
- #else
- static const char* openMode[] =
- {
- "rb",
- "wb",
- "r+b",
- "w+b",
- // ATOMIC BEGIN
- "a+b"
- // ATOMIC END
- };
- #endif
- #ifdef __ANDROID__
- const char* APK = "/apk/";
- static const unsigned READ_BUFFER_SIZE = 32768;
- #endif
- static const unsigned SKIP_BUFFER_SIZE = 1024;
- File::File(Context* context) :
- Object(context),
- mode_(FILE_READ),
- handle_(0),
- #ifdef __ANDROID__
- assetHandle_(0),
- #endif
- readBufferOffset_(0),
- readBufferSize_(0),
- offset_(0),
- checksum_(0),
- compressed_(false),
- readSyncNeeded_(false),
- writeSyncNeeded_(false)
- {
- }
- File::File(Context* context, const String& fileName, FileMode mode) :
- Object(context),
- mode_(FILE_READ),
- handle_(0),
- #ifdef __ANDROID__
- assetHandle_(0),
- #endif
- readBufferOffset_(0),
- readBufferSize_(0),
- offset_(0),
- checksum_(0),
- compressed_(false),
- readSyncNeeded_(false),
- writeSyncNeeded_(false),
- // ATOMIC BEGIN
- fullPath_(fileName)
- // ATOMIC END
- {
- Open(fileName, mode);
- }
- File::File(Context* context, PackageFile* package, const String& fileName) :
- Object(context),
- mode_(FILE_READ),
- handle_(0),
- #ifdef __ANDROID__
- assetHandle_(0),
- #endif
- readBufferOffset_(0),
- readBufferSize_(0),
- offset_(0),
- checksum_(0),
- compressed_(false),
- readSyncNeeded_(false),
- writeSyncNeeded_(false)
- {
- Open(package, fileName);
- }
- File::~File()
- {
- Close();
- }
- bool File::Open(const String& fileName, FileMode mode)
- {
- return OpenInternal(fileName, mode);
- }
- bool File::Open(PackageFile* package, const String& fileName)
- {
- if (!package)
- return false;
- const PackageEntry* entry = package->GetEntry(fileName);
- if (!entry)
- return false;
- bool success = OpenInternal(package->GetName(), FILE_READ, true);
- if (!success)
- {
- ATOMIC_LOGERROR("Could not open package file " + fileName);
- return false;
- }
- fileName_ = fileName;
- offset_ = entry->offset_;
- checksum_ = entry->checksum_;
- size_ = entry->size_;
- compressed_ = package->IsCompressed();
- // Seek to beginning of package entry's file data
- SeekInternal(offset_);
- return true;
- }
- unsigned File::Read(void* dest, unsigned size)
- {
- if (!IsOpen())
- {
- // If file not open, do not log the error further here to prevent spamming the stderr stream
- return 0;
- }
- if (mode_ == FILE_WRITE)
- {
- ATOMIC_LOGERROR("File not opened for reading");
- return 0;
- }
- if (size + position_ > size_)
- size = size_ - position_;
- if (!size)
- return 0;
- #ifdef __ANDROID__
- if (assetHandle_ && !compressed_)
- {
- // If not using a compressed package file, buffer file reads on Android for better performance
- if (!readBuffer_)
- {
- readBuffer_ = new unsigned char[READ_BUFFER_SIZE];
- readBufferOffset_ = 0;
- readBufferSize_ = 0;
- }
- unsigned sizeLeft = size;
- unsigned char* destPtr = (unsigned char*)dest;
- while (sizeLeft)
- {
- if (readBufferOffset_ >= readBufferSize_)
- {
- readBufferSize_ = Min(size_ - position_, READ_BUFFER_SIZE);
- readBufferOffset_ = 0;
- ReadInternal(readBuffer_.Get(), readBufferSize_);
- }
- unsigned copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
- memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
- destPtr += copySize;
- sizeLeft -= copySize;
- readBufferOffset_ += copySize;
- position_ += copySize;
- }
- return size;
- }
- #endif
- if (compressed_)
- {
- unsigned sizeLeft = size;
- unsigned char* destPtr = (unsigned char*)dest;
- while (sizeLeft)
- {
- if (!readBuffer_ || readBufferOffset_ >= readBufferSize_)
- {
- unsigned char blockHeaderBytes[4];
- ReadInternal(blockHeaderBytes, sizeof blockHeaderBytes);
- MemoryBuffer blockHeader(&blockHeaderBytes[0], sizeof blockHeaderBytes);
- unsigned unpackedSize = blockHeader.ReadUShort();
- unsigned packedSize = blockHeader.ReadUShort();
- if (!readBuffer_)
- {
- readBuffer_ = new unsigned char[unpackedSize];
- inputBuffer_ = new unsigned char[LZ4_compressBound(unpackedSize)];
- }
- /// \todo Handle errors
- ReadInternal(inputBuffer_.Get(), packedSize);
- LZ4_decompress_fast((const char*)inputBuffer_.Get(), (char*)readBuffer_.Get(), unpackedSize);
- readBufferSize_ = unpackedSize;
- readBufferOffset_ = 0;
- }
- unsigned copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
- memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
- destPtr += copySize;
- sizeLeft -= copySize;
- readBufferOffset_ += copySize;
- position_ += copySize;
- }
- return size;
- }
- // Need to reassign the position due to internal buffering when transitioning from writing to reading
- if (readSyncNeeded_)
- {
- SeekInternal(position_ + offset_);
- readSyncNeeded_ = false;
- }
- if (!ReadInternal(dest, size))
- {
- // Return to the position where the read began
- SeekInternal(position_ + offset_);
- ATOMIC_LOGERROR("Error while reading from file " + GetName());
- return 0;
- }
- writeSyncNeeded_ = true;
- position_ += size;
- return size;
- }
- unsigned File::Seek(unsigned position)
- {
- if (!IsOpen())
- {
- // If file not open, do not log the error further here to prevent spamming the stderr stream
- return 0;
- }
- // Allow sparse seeks if writing
- if (mode_ == FILE_READ && position > size_)
- position = size_;
- if (compressed_)
- {
- // Start over from the beginning
- if (position == 0)
- {
- position_ = 0;
- readBufferOffset_ = 0;
- readBufferSize_ = 0;
- SeekInternal(offset_);
- }
- // Skip bytes
- else if (position >= position_)
- {
- unsigned char skipBuffer[SKIP_BUFFER_SIZE];
- while (position > position_)
- Read(skipBuffer, Min(position - position_, SKIP_BUFFER_SIZE));
- }
- else
- ATOMIC_LOGERROR("Seeking backward in a compressed file is not supported");
- return position_;
- }
- SeekInternal(position + offset_);
- position_ = position;
- readSyncNeeded_ = false;
- writeSyncNeeded_ = false;
- return position_;
- }
- unsigned File::Write(const void* data, unsigned size)
- {
- if (!IsOpen())
- {
- // If file not open, do not log the error further here to prevent spamming the stderr stream
- return 0;
- }
- if (mode_ == FILE_READ)
- {
- ATOMIC_LOGERROR("File not opened for writing");
- return 0;
- }
- if (!size)
- return 0;
- // Need to reassign the position due to internal buffering when transitioning from reading to writing
- if (writeSyncNeeded_)
- {
- fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
- writeSyncNeeded_ = false;
- }
- if (fwrite(data, size, 1, (FILE*)handle_) != 1)
- {
- // Return to the position where the write began
- fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
- ATOMIC_LOGERROR("Error while writing to file " + GetName());
- return 0;
- }
- readSyncNeeded_ = true;
- position_ += size;
- if (position_ > size_)
- size_ = position_;
- return size;
- }
- unsigned File::GetChecksum()
- {
- if (offset_ || checksum_)
- return checksum_;
- #ifdef __ANDROID__
- if ((!handle_ && !assetHandle_) || mode_ == FILE_WRITE)
- #else
- if (!handle_ || mode_ == FILE_WRITE)
- #endif
- return 0;
- ATOMIC_PROFILE(CalculateFileChecksum);
- unsigned oldPos = position_;
- checksum_ = 0;
- Seek(0);
- while (!IsEof())
- {
- unsigned char block[1024];
- unsigned readBytes = Read(block, 1024);
- for (unsigned i = 0; i < readBytes; ++i)
- checksum_ = SDBMHash(checksum_, block[i]);
- }
- Seek(oldPos);
- return checksum_;
- }
- void File::Close()
- {
- #ifdef __ANDROID__
- if (assetHandle_)
- {
- SDL_RWclose(assetHandle_);
- assetHandle_ = 0;
- }
- #endif
- readBuffer_.Reset();
- inputBuffer_.Reset();
- if (handle_)
- {
- fclose((FILE*)handle_);
- handle_ = 0;
- position_ = 0;
- size_ = 0;
- offset_ = 0;
- checksum_ = 0;
- }
- }
- void File::Flush()
- {
- if (handle_)
- fflush((FILE*)handle_);
- }
- void File::SetName(const String& name)
- {
- fileName_ = name;
- }
- bool File::IsOpen() const
- {
- #ifdef __ANDROID__
- return handle_ != 0 || assetHandle_ != 0;
- #else
- return handle_ != 0;
- #endif
- }
- bool File::OpenInternal(const String& fileName, FileMode mode, bool fromPackage)
- {
- Close();
- compressed_ = false;
- readSyncNeeded_ = false;
- writeSyncNeeded_ = false;
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
- {
- ATOMIC_LOGERRORF("Access denied to %s", fileName.CString());
- return false;
- }
- if (fileName.Empty())
- {
- ATOMIC_LOGERROR("Could not open file with empty name");
- return false;
- }
- #ifdef __ANDROID__
- if (ATOMIC_IS_ASSET(fileName))
- {
- if (mode != FILE_READ)
- {
- ATOMIC_LOGERROR("Only read mode is supported for Android asset files");
- return false;
- }
- assetHandle_ = SDL_RWFromFile(ATOMIC_ASSET(fileName), "rb");
- if (!assetHandle_)
- {
- ATOMIC_LOGERRORF("Could not open Android asset file %s", fileName.CString());
- return false;
- }
- else
- {
- fileName_ = fileName;
- mode_ = mode;
- position_ = 0;
- if (!fromPackage)
- {
- size_ = SDL_RWsize(assetHandle_);
- offset_ = 0;
- }
- checksum_ = 0;
- return true;
- }
- }
- #endif
- #ifdef _WIN32
- handle_ = _wfopen(GetWideNativePath(fileName).CString(), openMode[mode]);
- #else
- handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode]);
- #endif
- // If file did not exist in readwrite mode, retry with write-update mode
- if (mode == FILE_READWRITE && !handle_)
- {
- #ifdef _WIN32
- handle_ = _wfopen(GetWideNativePath(fileName).CString(), openMode[mode + 1]);
- #else
- handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode + 1]);
- #endif
- }
- if (!handle_)
- {
- ATOMIC_LOGERRORF("Could not open file %s", fileName.CString());
- return false;
- }
- if (!fromPackage)
- {
- fseek((FILE*)handle_, 0, SEEK_END);
- long size = ftell((FILE*)handle_);
- fseek((FILE*)handle_, 0, SEEK_SET);
- if (size > M_MAX_UNSIGNED)
- {
- ATOMIC_LOGERRORF("Could not open file %s which is larger than 4GB", fileName.CString());
- Close();
- size_ = 0;
- return false;
- }
- size_ = (unsigned)size;
- offset_ = 0;
- }
- fileName_ = fileName;
- mode_ = mode;
- position_ = 0;
- checksum_ = 0;
- return true;
- }
- bool File::ReadInternal(void* dest, unsigned size)
- {
- #ifdef __ANDROID__
- if (assetHandle_)
- {
- return SDL_RWread(assetHandle_, dest, size, 1) == 1;
- }
- else
- #endif
- return fread(dest, size, 1, (FILE*)handle_) == 1;
- }
- void File::SeekInternal(unsigned newPosition)
- {
- #ifdef __ANDROID__
- if (assetHandle_)
- {
- SDL_RWseek(assetHandle_, newPosition, SEEK_SET);
- // Reset buffering after seek
- readBufferOffset_ = 0;
- readBufferSize_ = 0;
- }
- else
- #endif
- fseek((FILE*)handle_, newPosition, SEEK_SET);
- }
- // ATOMIC BEGIN
- void File::ReadText(String& text)
- {
- text.Clear();
- if (!size_)
- return;
- text.Resize(size_);
- Read((void*)text.CString(), size_);
- }
- bool File::Copy(File* srcFile)
- {
- if (!srcFile || !srcFile->IsOpen() || srcFile->GetMode() != FILE_READ)
- return false;
- if (!IsOpen() || GetMode() != FILE_WRITE)
- return false;
- unsigned fileSize = srcFile->GetSize();
- SharedArrayPtr<unsigned char> buffer(new unsigned char[fileSize]);
- unsigned bytesRead = srcFile->Read(buffer.Get(), fileSize);
- unsigned bytesWritten = Write(buffer.Get(), fileSize);
- return bytesRead == fileSize && bytesWritten == fileSize;
- }
- // ATOMIC END
- }
|