From c2d05a5f25afffd9e64b4ff49c1229e95faa5ebe Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Fri, 8 Jul 2016 15:00:41 +0200 Subject: Unify code, no semantic changes. --- fastos/src/vespa/fastos/linux_file.cpp | 50 +++++------------- fastos/src/vespa/fastos/linux_file.h | 92 +++++++++------------------------- fastos/src/vespa/fastos/unix_file.cpp | 51 ++++++++++--------- fastos/src/vespa/fastos/unix_file.h | 89 +++++++++++--------------------- 4 files changed, 93 insertions(+), 189 deletions(-) (limited to 'fastos') diff --git a/fastos/src/vespa/fastos/linux_file.cpp b/fastos/src/vespa/fastos/linux_file.cpp index c78551180f6..9452d1f50f9 100644 --- a/fastos/src/vespa/fastos/linux_file.cpp +++ b/fastos/src/vespa/fastos/linux_file.cpp @@ -35,12 +35,7 @@ FastOS_Linux_File::readInternal(int fh, void *buffer, size_t length, if (readResult < 0 && _failedHandler != NULL) { int error = errno; const char *fileName = GetFileName(); - _failedHandler("read", - fileName, - error, - readOffset, - length, - readResult); + _failedHandler("read", fileName, error, readOffset, length, readResult); errno = error; } return readResult; @@ -55,12 +50,7 @@ FastOS_Linux_File::readInternal(int fh, void *buffer, size_t length) int error = errno; int64_t readOffset = GetPosition(); const char *fileName = GetFileName(); - _failedHandler("read", - fileName, - error, - readOffset, - length, - readResult); + _failedHandler("read", fileName, error, readOffset, length, readResult); errno = error; } return readResult; @@ -75,12 +65,7 @@ FastOS_Linux_File::writeInternal(int fh, const void *buffer, size_t length, if (writeRes < 0 && _failedHandler != NULL) { int error = errno; const char *fileName = GetFileName(); - _failedHandler("write", - fileName, - error, - writeOffset, - length, - writeRes); + _failedHandler("write", fileName, error, writeOffset, length, writeRes); errno = error; } return writeRes; @@ -94,12 +79,7 @@ FastOS_Linux_File::writeInternal(int fh, const void *buffer, size_t length) int error = errno; int64_t writeOffset = GetPosition(); const char *fileName = GetFileName(); - _failedHandler("write", - fileName, - error, - writeOffset, - length, - writeRes); + _failedHandler("write", fileName, error, writeOffset, length, writeRes); errno = error; } return writeRes; @@ -148,16 +128,14 @@ FastOS_Linux_File::ReadBufInternal(void *buffer, size_t length, int64_t readOffs if (DIRECTIOPOSSIBLE(buffer, alignedLength, readOffset)) { size_t remain(length - alignedLength); if (alignedLength > 0) { - readResult = readInternal(_filedes, buffer, alignedLength, - readOffset); + readResult = readInternal(_filedes, buffer, alignedLength, readOffset); } else { readResult = 0; } if (static_cast(readResult) == alignedLength && remain != 0) { - ssize_t readResult2 = readUnalignedEnd( - static_cast(buffer) + alignedLength, - remain, readOffset + alignedLength); + ssize_t readResult2 = readUnalignedEnd(static_cast(buffer) + alignedLength, + remain, readOffset + alignedLength); if (readResult == 0) { readResult = readResult2; } else if (readResult2 > 0) { @@ -224,16 +202,13 @@ FastOS_Linux_File::Write2(const void *buffer, size_t length) if (DIRECTIOPOSSIBLE(buffer, alignedLength, _filePointer)) { size_t remain(length - alignedLength); if (alignedLength > 0) { - writeRes = writeInternal(_filedes, buffer, alignedLength, - _filePointer); + writeRes = writeInternal(_filedes, buffer, alignedLength, _filePointer); } else { writeRes = 0; } - if (static_cast(writeRes) == alignedLength && - remain != 0) { - ssize_t writeRes2 = writeUnalignedEnd( - static_cast(buffer) + alignedLength, - remain, _filePointer + alignedLength); + if (static_cast(writeRes) == alignedLength && remain != 0) { + ssize_t writeRes2 = writeUnalignedEnd(static_cast(buffer) + alignedLength, + remain, _filePointer + alignedLength); if (writeRes == 0) { writeRes = writeRes2; } else if (writeRes2 > 0) { @@ -395,8 +370,9 @@ FastOS_Linux_File::Open(unsigned int openFlags, const char *filename) if (_directIOEnabled && (_openFlags & FASTOS_FILE_OPEN_STDFLAGS) != 0) { _directIOEnabled = false; } - if (_syncWritesEnabled) + if (_syncWritesEnabled) { openFlags |= FASTOS_FILE_OPEN_SYNCWRITES; + } if (_directIOEnabled) { rc = FastOS_UNIX_File::Open(openFlags | FASTOS_FILE_OPEN_DIRECTIO, filename); if ( ! rc ) { //Retry without directIO. diff --git a/fastos/src/vespa/fastos/linux_file.h b/fastos/src/vespa/fastos/linux_file.h index 5dcf4eaf7e3..df443dc3e3d 100644 --- a/fastos/src/vespa/fastos/linux_file.h +++ b/fastos/src/vespa/fastos/linux_file.h @@ -25,76 +25,34 @@ protected: public: FastOS_Linux_File (const char *filename = NULL); - virtual ~FastOS_Linux_File () { + ~FastOS_Linux_File () { Close(); } - - virtual bool - GetDirectIORestrictions(size_t &memoryAlignment, - size_t &transferGranularity, - size_t &transferMaximum); - - virtual bool - DirectIOPadding(int64_t offset, size_t length, - size_t &padBefore, - size_t &padAfter); - - virtual void - EnableDirectIO(void); - - virtual bool - SetPosition(int64_t desiredPosition); - - virtual int64_t - GetPosition(void); - - virtual bool - SetSize(int64_t newSize); - - virtual void - ReadBuf(void *buffer, size_t length, int64_t readOffset); - - virtual void * - AllocateDirectIOBuffer(size_t byteSize, void *&realPtr); - - static void * - allocateGenericDirectIOBuffer(size_t byteSize, - void *&realPtr); - - static size_t - getMaxDirectIOMemAlign(void); - - virtual ssize_t - Read(void *buffer, size_t len); - - virtual ssize_t - Write2(const void *buffer, size_t len); - - virtual bool - Open(unsigned int openFlags, const char *filename = NULL); - - static bool - InitializeClass(void); + bool GetDirectIORestrictions(size_t &memoryAlignment, size_t &transferGranularity, size_t &transferMaximum) override; + bool DirectIOPadding(int64_t offset, size_t length, size_t &padBefore, size_t &padAfter) override; + void EnableDirectIO(void) override; + bool SetPosition(int64_t desiredPosition) override; + int64_t GetPosition(void) override; + bool SetSize(int64_t newSize) override; + void ReadBuf(void *buffer, size_t length, int64_t readOffset) override; + void *AllocateDirectIOBuffer(size_t byteSize, void *&realPtr) override; + + + ssize_t Read(void *buffer, size_t len) override; + ssize_t Write2(const void *buffer, size_t len) override; + bool Open(unsigned int openFlags, const char *filename) override; + + static bool InitializeClass(); + static size_t getMaxDirectIOMemAlign(void); + static void *allocateGenericDirectIOBuffer(size_t byteSize, void *&realPtr); private: - ssize_t - readUnalignedEnd(void *buffer, size_t length, int64_t readOffset); - ssize_t - writeUnalignedEnd(const void *buffer, size_t length, int64_t readOffset); - ssize_t - ReadBufInternal(void *buffer, size_t length, int64_t readOffset); - - ssize_t - readInternal(int fh, void *buffer, size_t length, int64_t readOffset); - - ssize_t - readInternal(int fh, void *buffer, size_t length); - - ssize_t - writeInternal(int fh, const void *buffer, size_t length, - int64_t writeOffset); - - ssize_t - writeInternal(int fh, const void *buffer, size_t length); + ssize_t readUnalignedEnd(void *buffer, size_t length, int64_t readOffset); + ssize_t writeUnalignedEnd(const void *buffer, size_t length, int64_t readOffset); + ssize_t ReadBufInternal(void *buffer, size_t length, int64_t readOffset); + ssize_t readInternal(int fh, void *buffer, size_t length, int64_t readOffset); + ssize_t readInternal(int fh, void *buffer, size_t length); + ssize_t writeInternal(int fh, const void *buffer, size_t length, int64_t writeOffset); + ssize_t writeInternal(int fh, const void *buffer, size_t length); static const size_t _directIOFileAlign; static const size_t _directIOMemAlign; diff --git a/fastos/src/vespa/fastos/unix_file.cpp b/fastos/src/vespa/fastos/unix_file.cpp index 340c258d95a..7b661719d37 100644 --- a/fastos/src/vespa/fastos/unix_file.cpp +++ b/fastos/src/vespa/fastos/unix_file.cpp @@ -29,8 +29,7 @@ FastOS_UNIX_File::GetPosition(void) bool -FastOS_UNIX_File::Stat(const char *filename, - FastOS_StatInfo *statInfo) +FastOS_UNIX_File::Stat(const char *filename, FastOS_StatInfo *statInfo) { bool rc = false; @@ -51,10 +50,11 @@ FastOS_UNIX_File::Stat(const char *filename, statInfo->_modifiedTimeNS += stbuf.st_mtim.tv_nsec; rc = true; } else { - if (errno == ENOENT) + if (errno == ENOENT) { statInfo->_error = FastOS_StatInfo::FileNotFound; - else + } else { statInfo->_error = FastOS_StatInfo::Unknown; + } } return rc; @@ -94,15 +94,15 @@ FastOS_UNIX_File::getCurrentDirectory(void) { std::string res; int maxPathLen = FastOS_File::GetMaximumPathLength("."); - if (maxPathLen == -1) + if (maxPathLen == -1) { maxPathLen = 16384; - else if (maxPathLen < 512) + } else if (maxPathLen < 512) { maxPathLen = 512; + } char *currentDir = new char [maxPathLen + 1]; - if (getcwd(currentDir, maxPathLen) != NULL) - { + if (getcwd(currentDir, maxPathLen) != NULL) { res = currentDir; } delete [] currentDir; @@ -116,8 +116,7 @@ FastOS_UNIX_File::CalcAccessFlags(unsigned int openFlags) { unsigned int accessFlags=0; - if ((openFlags & (FASTOS_FILE_OPEN_READ | - FASTOS_FILE_OPEN_DIRECTIO)) != 0) { + if ((openFlags & (FASTOS_FILE_OPEN_READ | FASTOS_FILE_OPEN_DIRECTIO)) != 0) { if ((openFlags & FASTOS_FILE_OPEN_WRITE) != 0) { // Open for reading and writing accessFlags = O_RDWR; @@ -130,8 +129,7 @@ FastOS_UNIX_File::CalcAccessFlags(unsigned int openFlags) accessFlags = O_WRONLY; } - if (((openFlags & FASTOS_FILE_OPEN_EXISTING) == 0) && - ((openFlags & FASTOS_FILE_OPEN_WRITE) != 0)) { + if (((openFlags & FASTOS_FILE_OPEN_EXISTING) == 0) && ((openFlags & FASTOS_FILE_OPEN_WRITE) != 0)) { // Create file if it does not exist accessFlags |= O_CREAT; } @@ -190,9 +188,9 @@ FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename) _openFlags = openFlags; rc = true; } else { - if (filename != NULL) + if (filename != NULL) { SetFileName(filename); - + } unsigned int accessFlags = CalcAccessFlags(openFlags); _filedes = open(_filename, accessFlags, 0664); @@ -246,9 +244,9 @@ FastOS_UNIX_File::Close(void) bool ok = true; if (_filedes >= 0) { - if ((_openFlags & FASTOS_FILE_OPEN_STDFLAGS) != 0) + if ((_openFlags & FASTOS_FILE_OPEN_STDFLAGS) != 0) { ok = true; - else { + } else { do { ok = (close(_filedes) == 0); } while (!ok && errno == EINTR); @@ -280,8 +278,9 @@ FastOS_UNIX_File::GetSize(void) int res = fstat(_filedes, &stbuf); - if (res == 0) + if (res == 0) { fileSize = stbuf.st_size; + } return fileSize; } @@ -319,16 +318,14 @@ FastOS_UNIX_File::Delete(void) return (unlink(_filename) == 0); } -bool FastOS_UNIX_File::Rename (const char *currentFileName, - const char *newFileName) +bool FastOS_UNIX_File::Rename (const char *currentFileName, const char *newFileName) { bool rc = false; // Enforce documentation. If the destination file exists, // fail Rename. FastOS_StatInfo statInfo; - if (!FastOS_File::Stat(newFileName, &statInfo)) - { + if (!FastOS_File::Stat(newFileName, &statInfo)) { rc = (rename(currentFileName, newFileName) == 0); } else { errno = EEXIST; @@ -350,8 +347,9 @@ FastOS_UNIX_File::SetSize(int64_t newSize) { bool rc = false; - if (ftruncate(_filedes, static_cast(newSize)) == 0) + if (ftruncate(_filedes, static_cast(newSize)) == 0) { rc = SetPosition(newSize); + } return rc; } @@ -403,8 +401,9 @@ int64_t FastOS_UNIX_File::GetFreeDiskSpace (const char *path) struct statfs statBuf; int statVal = -1; statVal = statfs(path, &statBuf); - if (statVal == 0) + if (statVal == 0) { freeSpace = int64_t(statBuf.f_bavail) * int64_t(statBuf.f_bsize); + } return freeSpace; } @@ -495,8 +494,9 @@ FastOS_UNIX_DirectoryScan::DoStat(void) bool FastOS_UNIX_DirectoryScan::IsDirectory(void) { - if (!_statRun) + if (!_statRun) { DoStat(); + } return _isDirectory; } @@ -505,8 +505,9 @@ FastOS_UNIX_DirectoryScan::IsDirectory(void) bool FastOS_UNIX_DirectoryScan::IsRegular(void) { - if (!_statRun) + if (!_statRun) { DoStat(); + } return _isRegular; } diff --git a/fastos/src/vespa/fastos/unix_file.h b/fastos/src/vespa/fastos/unix_file.h index 7fc471374b3..e3817eb4983 100644 --- a/fastos/src/vespa/fastos/unix_file.h +++ b/fastos/src/vespa/fastos/unix_file.h @@ -7,11 +7,8 @@ * Class definitions for FastOS_UNIX_File and FastOS_UNIX_DirectoryScan. *****************************************************************************/ - - #pragma once - #include /** @@ -32,24 +29,21 @@ protected: int _mmapFlags; bool _mmapEnabled; - static unsigned int - CalcAccessFlags(unsigned int openFlags); + static unsigned int CalcAccessFlags(unsigned int openFlags); public: static bool Rename (const char *currentFileName, const char *newFileName); - bool Rename (const char *newFileName) - { return FastOS_FileInterface::Rename(newFileName); } + bool Rename (const char *newFileName) { + return FastOS_FileInterface::Rename(newFileName); + } static bool Stat(const char *filename, FastOS_StatInfo *statInfo); static bool MakeDirectory(const char *name); static void RemoveDirectory(const char *name); - static std::string - getCurrentDirectory(void); + static std::string getCurrentDirectory(void); - static bool SetCurrentDirectory (const char *pathName) { - return (chdir(pathName) == 0); - } + static bool SetCurrentDirectory (const char *pathName) { return (chdir(pathName) == 0); } static int GetMaximumFilenameLength (const char *pathName); static int GetMaximumPathLength (const char *pathName); @@ -60,27 +54,19 @@ public: _filedes(-1), _mmapFlags(0), _mmapEnabled(false) - { - } + { } char *ToString(void); + bool Open(unsigned int openFlags, const char *filename) override; + bool Close() override; + bool IsOpened(void) const override { return _filedes >= 0; } - virtual bool Open(unsigned int openFlags, const char *filename=NULL); - - virtual bool Close(void); - - virtual bool IsOpened(void) const - { - return _filedes >= 0; - } - - virtual void enableMemoryMap(int flags) { + void enableMemoryMap(int flags) override { _mmapEnabled = true; _mmapFlags = flags; } - virtual void *MemoryMapPtr(int64_t position) const - { + void *MemoryMapPtr(int64_t position) const override { if (_mmapbase != NULL) { if (position < int64_t(_mmaplen)) { return static_cast(static_cast(_mmapbase) + position); @@ -92,36 +78,21 @@ public: } } - virtual bool IsMemoryMapped(void) const { - return _mmapbase != NULL; - } - - virtual bool SetPosition(int64_t desiredPosition); - - virtual int64_t GetPosition(void); - - virtual int64_t GetSize(void); - virtual time_t GetModificationTime(void); + bool IsMemoryMapped(void) const override { return _mmapbase != NULL; } + bool SetPosition(int64_t desiredPosition) override; + int64_t GetPosition() override; + int64_t GetSize() override; + time_t GetModificationTime() override; + bool Delete() override; + bool Sync() override; + bool SetSize(int64_t newSize) override; + void dropFromCache() const override; static bool Delete(const char *filename); - virtual bool Delete(void); - - virtual bool Sync(void); - - virtual bool SetSize(int64_t newSize); - - static int GetLastOSError(void) - { - return errno; - }; - + static int GetLastOSError(void) { return errno; } static Error TranslateError(const int osError); - - static std::string - getErrorString(const int osError); - + static std::string getErrorString(const int osError); static int64_t GetFreeDiskSpace (const char *path); - void dropFromCache() const override; }; @@ -149,13 +120,11 @@ protected: public: FastOS_UNIX_DirectoryScan(const char *searchPath); - ~FastOS_UNIX_DirectoryScan(void); - - bool ReadNext(void); - bool IsDirectory(void); - bool IsRegular(void); - - const char *GetName(void); + ~FastOS_UNIX_DirectoryScan(); - bool IsValidScan(void) const; + bool ReadNext() override; + bool IsDirectory() override; + bool IsRegular() override; + const char *GetName() override; + bool IsValidScan() const override; }; -- cgit v1.2.3