#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_STORAGE_SOURCE 1 #include "LinuxFile.h" #if defined(NUCLEX_STORAGE_LINUX) #include "LinuxFileApi.h" #include "LinuxPathHelper.h" #include #include // for PATH_MAX #include #include namespace Nuclex { namespace Storage { namespace FileSystem { // ------------------------------------------------------------------------------------------- // LinuxFile::LinuxFile(const std::string &path) : path(path), name(LinuxPathHelper::GetLastPathElement(path)), fileHandle(-1), isOpenedWritable(false) {} // ------------------------------------------------------------------------------------------- // LinuxFile::~LinuxFile() { if(this->fileHandle >= 0) { LinuxFileApi::Close(this->fileHandle); } } // ------------------------------------------------------------------------------------------- // std::uint64_t LinuxFile::GetSize() const { std::lock_guard scope(this->mutex); { if(this->fileHandle < 1) { struct stat fileStatus; if(LinuxFileApi::Stat(this->path, fileStatus)) { return static_cast(fileStatus.st_size); } else { return 0; // File doesn't exist yet - not an error! } } else { // File was already opened struct stat fileStatus; LinuxFileApi::Stat(this->fileHandle, fileStatus); return static_cast(fileStatus.st_size); } } // mutex } // ------------------------------------------------------------------------------------------- // void LinuxFile::ReadAt( std::uint64_t location, void *buffer, std::size_t count ) const { std::lock_guard scope(this->mutex); { // Make sure the file is opened so that we can read from it if(this->fileHandle < 0) { this->fileHandle = LinuxFileApi::OpenFileForReading(this->path); } // Perform the actual read std::size_t actualCount = LinuxFileApi::Read(this->fileHandle, location, buffer, count); if(actualCount != count) { throw std::out_of_range("Attempted to read beyond the end of the file"); } } // mutex } // ------------------------------------------------------------------------------------------- // void LinuxFile::WriteAt( std::uint64_t location, const void *buffer, std::size_t count ) { std::lock_guard scope(this->mutex); { // If the file is still closed or has been opened in the wrong mode, // make sure that it is opened in the right mode for the write. if(this->fileHandle < 0) { this->fileHandle = LinuxFileApi::OpenFileForWriting(this->path); } else if(!this->isOpenedWritable) { close(); this->fileHandle = LinuxFileApi::OpenFileForWriting(this->path); } // Perform the actual write std::size_t actualCount = LinuxFileApi::Write(this->fileHandle, location, buffer, count); if(actualCount != count) { throw std::out_of_range("Write was aborted early"); } } // mutex } // ------------------------------------------------------------------------------------------- // void LinuxFile::Flush() { std::lock_guard scope(this->mutex); { if(this->fileHandle >= 0) { //LinuxFileApi::FlushFileBuffers(this->fileHandle); } } } // ------------------------------------------------------------------------------------------- // void LinuxFile::close() { if(this->fileHandle >= 0) { LinuxFileApi::Close(this->fileHandle); this->fileHandle = -1; } } // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Storage::FileSystem #endif // defined(NUCLEX_STORAGE_LINUX)