From 3bedf8846f4d7a5cb38276b274662d62a36dcd52 Mon Sep 17 00:00:00 2001 From: Martin Ebourne Date: Mon, 12 Dec 2005 20:50:00 +0000 Subject: Marged chris/win32/merge/07-win32-fixes at r210 to trunk --- lib/common/BoxPlatform.h | 45 +++++++++++++++- lib/common/BoxPortsAndFiles.h | 9 +++- lib/common/BoxTime.h | 4 +- lib/common/BoxTimeToText.cpp | 25 ++++++++- lib/common/BoxTimeToUnix.h | 4 ++ lib/common/CommonException.txt | 1 + lib/common/DebugPrintf.cpp | 6 +++ lib/common/FileStream.cpp | 116 +++++++++++++++++++++++++++++++++++++--- lib/common/FileStream.h | 28 ++++++++-- lib/common/IOStream.cpp | 12 +++++ lib/common/NamedLock.cpp | 2 +- lib/common/TemporaryDirectory.h | 34 +++++++++--- lib/common/UnixUser.cpp | 3 ++ lib/common/WaitForEvent.h | 4 +- 14 files changed, 265 insertions(+), 28 deletions(-) (limited to 'lib/common') diff --git a/lib/common/BoxPlatform.h b/lib/common/BoxPlatform.h index 6ddd1643..55065796 100755 --- a/lib/common/BoxPlatform.h +++ b/lib/common/BoxPlatform.h @@ -11,13 +11,24 @@ #ifndef BOXPLATFORM__H #define BOXPLATFORM__H +#ifdef WIN32 +#define DIRECTORY_SEPARATOR "\\" +#define DIRECTORY_SEPARATOR_ASCHAR '\\' +#else #define DIRECTORY_SEPARATOR "/" -#define DIRECTORY_SEPARATOR_ASCHAR '/' +#define DIRECTORY_SEPARATOR_ASCHAR '/' +#endif #define PLATFORM_DEV_NULL "/dev/null" #include "config.h" +#ifdef WIN32 + // need msvcrt version 6.1 or higher for _gmtime64() + // must define this before importing + #define __MSVCRT_VERSION__ 0x0601 +#endif + #ifdef HAVE_SYS_TYPES_H #include #endif @@ -51,6 +62,27 @@ #define STRUCTURE_PACKING_FOR_WIRE_USE_HEADERS #endif +#if defined WIN32 && !defined __MINGW32__ + typedef __int8 int8_t; + typedef __int16 int16_t; + typedef __int32 int32_t; + typedef __int64 int64_t; + + typedef unsigned __int8 u_int8_t; + typedef unsigned __int16 u_int16_t; + typedef unsigned __int32 u_int32_t; + typedef unsigned __int64 u_int64_t; + + #define HAVE_UINT8_T + #define HAVE_UINT16_T + #define HAVE_UINT32_T + #define HAVE_UINT64_T + + typedef unsigned int uid_t; + typedef unsigned int gid_t; + typedef int pid_t; +#endif // WIN32 && !__MINGW32__ + // Define missing types #ifndef HAVE_UINT8_T typedef u_int8_t uint8_t; @@ -88,4 +120,15 @@ #define INFTIM -1 #endif +#ifdef WIN32 + typedef u_int64_t InodeRefType; +#else + typedef ino_t InodeRefType; +#endif + +#ifdef WIN32 + #define WIN32_LEAN_AND_MEAN + #include "emu.h" +#endif + #endif // BOXPLATFORM__H diff --git a/lib/common/BoxPortsAndFiles.h b/lib/common/BoxPortsAndFiles.h index 7788537e..562c6724 100755 --- a/lib/common/BoxPortsAndFiles.h +++ b/lib/common/BoxPortsAndFiles.h @@ -20,12 +20,17 @@ #define BOX_RAIDFILE_ROOT_BBSTORED "backup" // Backup client daemon +#ifdef WIN32 +#define BOX_FILE_BBACKUPD_DEFAULT_CONFIG "C:\\Program Files\\Box Backup\\bbackupd.conf" +#else #define BOX_FILE_BBACKUPD_DEFAULT_CONFIG "/etc/box/bbackupd.conf" +#endif - -// RaidFile conf location efault +// RaidFile conf location default #define BOX_FILE_RAIDFILE_DEFAULT_CONFIG "/etc/box/raidfile.conf" +// Default name of the named pipe +#define BOX_NAMED_PIPE_NAME L"\\\\.\\pipe\\boxbackup" #endif // BOXPORTSANDFILES__H diff --git a/lib/common/BoxTime.h b/lib/common/BoxTime.h index a7a25ac6..d166d6e7 100755 --- a/lib/common/BoxTime.h +++ b/lib/common/BoxTime.h @@ -31,11 +31,11 @@ inline box_time_t SecondsToBoxTime(uint64_t Seconds) { return ((box_time_t)Seconds * MICRO_SEC_IN_SEC_LL); } -inline int64_t BoxTimeToSeconds(box_time_t Time) +inline box_time_t BoxTimeToSeconds(box_time_t Time) { return Time / MICRO_SEC_IN_SEC_LL; } -inline int64_t BoxTimeToMilliSeconds(box_time_t Time) +inline box_time_t BoxTimeToMilliSeconds(box_time_t Time) { return Time / MILLI_SEC_IN_NANO_SEC_LL; } diff --git a/lib/common/BoxTimeToText.cpp b/lib/common/BoxTimeToText.cpp index 94b0d152..297d2100 100755 --- a/lib/common/BoxTimeToText.cpp +++ b/lib/common/BoxTimeToText.cpp @@ -27,14 +27,35 @@ // -------------------------------------------------------------------------- std::string BoxTimeToISO8601String(box_time_t Time) { +#ifdef WIN32 + struct tm *time; + box_time_t bob = BoxTimeToSeconds(Time); + + __time64_t winTime = bob; + + time = _gmtime64(&winTime); + char str[128]; // more than enough space + + if ( time == NULL ) + { + // ::sprintf(str, "%016I64x ", bob); + return std::string("unable to convert time"); + } + + sprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d", time->tm_year + 1900, + time->tm_mon + 1, time->tm_mday, time->tm_hour, + time->tm_min, time->tm_sec); +#else // ! WIN32 time_t timeInSecs = (time_t)BoxTimeToSeconds(Time); struct tm time; gmtime_r(&timeInSecs, &time); char str[128]; // more than enough space sprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d", time.tm_year + 1900, - time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec); - + time.tm_mon + 1, time.tm_mday, time.tm_hour, + time.tm_min, time.tm_sec); +#endif // WIN32 + return std::string(str); } diff --git a/lib/common/BoxTimeToUnix.h b/lib/common/BoxTimeToUnix.h index 17e57e27..f8a8797e 100755 --- a/lib/common/BoxTimeToUnix.h +++ b/lib/common/BoxTimeToUnix.h @@ -10,7 +10,11 @@ #ifndef FILEMODIFICATIONTIMETOTIMEVAL__H #define FILEMODIFICATIONTIMETOTIMEVAL__H +#ifdef WIN32 +#include +#else #include +#endif #include "BoxTime.h" diff --git a/lib/common/CommonException.txt b/lib/common/CommonException.txt index 3875ed71..f852b7d7 100644 --- a/lib/common/CommonException.txt +++ b/lib/common/CommonException.txt @@ -42,3 +42,4 @@ KEventErrorWait 34 KEventErrorRemove 35 KQueueNotSupportedOnThisPlatform 36 IOStreamGetLineNotEnoughDataToIgnore 37 Bad value passed to IOStreamGetLine::IgnoreBufferedData() +TempDirPathTooLong 38 Your temporary directory path is too long. Check the TMP and TEMP environment variables. diff --git a/lib/common/DebugPrintf.cpp b/lib/common/DebugPrintf.cpp index 02c25496..d07604b7 100755 --- a/lib/common/DebugPrintf.cpp +++ b/lib/common/DebugPrintf.cpp @@ -55,10 +55,16 @@ int BoxDebugTrace(const char *format, ...) // But tracing to syslog is independent of tracing being on or not if(BoxDebugTraceToSyslog) { +#ifdef WIN32 // Remove trailing '\n', if it's there + if(r > 0 && text[r-1] == '\n') + { + text[r-1] = '\0'; +#else if(r > 0 && text[r] == '\n') { text[r] = '\0'; +#endif --r; } // Log it diff --git a/lib/common/FileStream.cpp b/lib/common/FileStream.cpp index ca6894ae..9f5460bb 100755 --- a/lib/common/FileStream.cpp +++ b/lib/common/FileStream.cpp @@ -22,14 +22,25 @@ // // -------------------------------------------------------------------------- FileStream::FileStream(const char *Filename, int flags, int mode) +#ifdef WIN32 + : mOSFileHandle(::openfile(Filename, flags, mode)), +#else : mOSFileHandle(::open(Filename, flags, mode)), +#endif mIsEOF(false) { +#ifdef WIN32 + if(mOSFileHandle == 0) +#else if(mOSFileHandle < 0) +#endif { MEMLEAKFINDER_NOT_A_LEAK(this); THROW_EXCEPTION(CommonException, OSFileOpenError) } +#ifdef WIN32 + this->fileName = Filename; +#endif } @@ -41,7 +52,7 @@ FileStream::FileStream(const char *Filename, int flags, int mode) // Created: 2003/08/28 // // -------------------------------------------------------------------------- -FileStream::FileStream(int FileDescriptor) +FileStream::FileStream(tOSFileHandle FileDescriptor) : mOSFileHandle(FileDescriptor), mIsEOF(false) { @@ -52,7 +63,7 @@ FileStream::FileStream(int FileDescriptor) } } - +#if 0 // -------------------------------------------------------------------------- // // Function @@ -71,6 +82,7 @@ FileStream::FileStream(const FileStream &rToCopy) THROW_EXCEPTION(CommonException, OSFileOpenError) } } +#endif // 0 // -------------------------------------------------------------------------- // @@ -82,7 +94,7 @@ FileStream::FileStream(const FileStream &rToCopy) // -------------------------------------------------------------------------- FileStream::~FileStream() { - if(mOSFileHandle >= 0) + if(mOSFileHandle != INVALID_FILE) { Close(); } @@ -98,8 +110,33 @@ FileStream::~FileStream() // -------------------------------------------------------------------------- int FileStream::Read(void *pBuffer, int NBytes, int Timeout) { - if(mOSFileHandle == -1) {THROW_EXCEPTION(CommonException, FileClosed)} + if(mOSFileHandle == INVALID_FILE) + { + THROW_EXCEPTION(CommonException, FileClosed) + } + +#ifdef WIN32 + int r; + DWORD numBytesRead = 0; + BOOL valid = ReadFile( + this->mOSFileHandle, + pBuffer, + NBytes, + &numBytesRead, + NULL + ); + + if ( valid ) + { + r = numBytesRead; + } + else + { + r = -1; + } +#else int r = ::read(mOSFileHandle, pBuffer, NBytes); +#endif if(r == -1) { THROW_EXCEPTION(CommonException, OSFileReadError) @@ -143,11 +180,34 @@ IOStream::pos_type FileStream::BytesLeftToRead() // -------------------------------------------------------------------------- void FileStream::Write(const void *pBuffer, int NBytes) { - if(mOSFileHandle == -1) {THROW_EXCEPTION(CommonException, FileClosed)} + if(mOSFileHandle == INVALID_FILE) + { + THROW_EXCEPTION(CommonException, FileClosed) + } + +#ifdef WIN32 + DWORD numBytesWritten = 0; + BOOL res = WriteFile( + this->mOSFileHandle, + pBuffer, + NBytes, + &numBytesWritten, + NULL + ); + + if ( (res == 0) || (numBytesWritten != NBytes)) + { + DWORD err = GetLastError(); + THROW_EXCEPTION(CommonException, OSFileWriteError) + } + + +#else if(::write(mOSFileHandle, pBuffer, NBytes) != NBytes) { THROW_EXCEPTION(CommonException, OSFileWriteError) } +#endif } @@ -161,7 +221,21 @@ void FileStream::Write(const void *pBuffer, int NBytes) // -------------------------------------------------------------------------- IOStream::pos_type FileStream::GetPosition() const { - if(mOSFileHandle == -1) {THROW_EXCEPTION(CommonException, FileClosed)} + if(mOSFileHandle == INVALID_FILE) + { + THROW_EXCEPTION(CommonException, FileClosed) + } + +#ifdef WIN32 + LARGE_INTEGER conv; + + conv.HighPart = 0; + conv.LowPart = 0; + + conv.LowPart = SetFilePointer(this->mOSFileHandle, 0, &conv.HighPart, FILE_CURRENT); + + return (IOStream::pos_type)conv.QuadPart; +#else // ! WIN32 off_t p = ::lseek(mOSFileHandle, 0, SEEK_CUR); if(p == -1) { @@ -169,6 +243,7 @@ IOStream::pos_type FileStream::GetPosition() const } return (IOStream::pos_type)p; +#endif // WIN32 } @@ -182,12 +257,28 @@ IOStream::pos_type FileStream::GetPosition() const // -------------------------------------------------------------------------- void FileStream::Seek(IOStream::pos_type Offset, int SeekType) { - if(mOSFileHandle == -1) {THROW_EXCEPTION(CommonException, FileClosed)} + if(mOSFileHandle == INVALID_FILE) + { + THROW_EXCEPTION(CommonException, FileClosed) + } + +#ifdef WIN32 + LARGE_INTEGER conv; + + conv.QuadPart = Offset; + DWORD retVal = SetFilePointer(this->mOSFileHandle, conv.LowPart, &conv.HighPart, ConvertSeekTypeToOSWhence(SeekType)); + + if ( retVal == INVALID_SET_FILE_POINTER && (GetLastError() != NO_ERROR) ) + { + THROW_EXCEPTION(CommonException, OSFileError) + } +#else // ! WIN32 if(::lseek(mOSFileHandle, Offset, ConvertSeekTypeToOSWhence(SeekType)) == -1) { THROW_EXCEPTION(CommonException, OSFileError) } - +#endif // WIN32 + // Not end of file any more! mIsEOF = false; } @@ -207,12 +298,21 @@ void FileStream::Close() { THROW_EXCEPTION(CommonException, FileAlreadyClosed) } +#ifdef WIN32 + if(::CloseHandle(mOSFileHandle) == 0) + { + THROW_EXCEPTION(CommonException, OSFileCloseError) + } + mOSFileHandle = NULL; + mIsEOF = true; +#else if(::close(mOSFileHandle) != 0) { THROW_EXCEPTION(CommonException, OSFileCloseError) } mOSFileHandle = -1; mIsEOF = true; +#endif } diff --git a/lib/common/FileStream.h b/lib/common/FileStream.h index bb3cc459..7d677636 100755 --- a/lib/common/FileStream.h +++ b/lib/common/FileStream.h @@ -17,12 +17,26 @@ #include #include +#ifdef WIN32 + #define INVALID_FILE NULL + typedef HANDLE tOSFileHandle; +#else + #define INVALID_FILE -1 + typedef int tOSFileHandle; +#endif + class FileStream : public IOStream { public: - FileStream(const char *Filename, int flags = O_RDONLY, int mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); - FileStream(int FileDescriptor); - FileStream(const FileStream &rToCopy); + FileStream(const char *Filename, +#ifdef WIN32 + int flags = (O_RDONLY | O_BINARY), +#else + int flags = O_RDONLY, +#endif + int mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); + FileStream(tOSFileHandle FileDescriptor); + virtual ~FileStream(); virtual int Read(void *pBuffer, int NBytes, int Timeout = IOStream::TimeOutInfinite); @@ -36,8 +50,14 @@ public: virtual bool StreamClosed(); private: - int mOSFileHandle; + tOSFileHandle mOSFileHandle; bool mIsEOF; + FileStream(const FileStream &rToCopy) { /* do not call */ } + +#ifdef WIN32 + // for debugging.. + std::string fileName; +#endif }; diff --git a/lib/common/IOStream.cpp b/lib/common/IOStream.cpp index 024eefcc..3c7be561 100755 --- a/lib/common/IOStream.cpp +++ b/lib/common/IOStream.cpp @@ -105,6 +105,17 @@ int IOStream::ConvertSeekTypeToOSWhence(int SeekType) int ostype = SEEK_SET; switch(SeekType) { +#ifdef WIN32 + case SeekType_Absolute: + ostype = FILE_BEGIN; + break; + case SeekType_Relative: + ostype = FILE_CURRENT; + break; + case SeekType_End: + ostype = FILE_END; + break; +#else // ! WIN32 case SeekType_Absolute: ostype = SEEK_SET; break; @@ -114,6 +125,7 @@ int IOStream::ConvertSeekTypeToOSWhence(int SeekType) case SeekType_End: ostype = SEEK_END; break; +#endif // WIN32 default: THROW_EXCEPTION(CommonException, IOStreamBadSeekType) diff --git a/lib/common/NamedLock.cpp b/lib/common/NamedLock.cpp index b9aeb768..fc7b8309 100755 --- a/lib/common/NamedLock.cpp +++ b/lib/common/NamedLock.cpp @@ -105,7 +105,7 @@ bool NamedLock::TryAndGetLock(const char *Filename, int mode) THROW_EXCEPTION(CommonException, OSFileError) } } -#else +#elif HAVE_DECL_F_SETLK struct flock desc; desc.l_type = F_WRLCK; desc.l_whence = SEEK_SET; diff --git a/lib/common/TemporaryDirectory.h b/lib/common/TemporaryDirectory.h index e683863b..9d52ecd9 100755 --- a/lib/common/TemporaryDirectory.h +++ b/lib/common/TemporaryDirectory.h @@ -12,15 +12,35 @@ #include -#ifdef TEMP_DIRECTORY_NAME - // Prefix name with Box to avoid clashing with OS API names - inline std::string BoxGetTemporaryDirectoryName() +#ifdef WIN32 + #include +#endif + +// Prefix name with Box to avoid clashing with OS API names +std::string BoxGetTemporaryDirectoryName() +{ +#ifdef WIN32 + // http://msdn.microsoft.com/library/default.asp? + // url=/library/en-us/fileio/fs/creating_and_using_a_temporary_file.asp + + DWORD dwRetVal; + char lpPathBuffer[1024]; + DWORD dwBufSize = sizeof(lpPathBuffer); + + // Get the temp path. + dwRetVal = GetTempPath(dwBufSize, // length of the buffer + lpPathBuffer); // buffer for path + if (dwRetVal > dwBufSize) { - return std::string(TEMP_DIRECTORY_NAME); + THROW_EXCEPTION(CommonException, TempDirPathTooLong) } -#else - non-static temporary directory names not supported yet + + return std::string(lpPathBuffer); +#elif defined TEMP_DIRECTORY_NAME + return std::string(TEMP_DIRECTORY_NAME); +#else + #error non-static temporary directory names not supported yet #endif +} #endif // TEMPORARYDIRECTORY__H - diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index 7a60b263..8b85c3e1 100755 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -9,7 +9,10 @@ #include "Box.h" +#ifndef WIN32 #include +#endif + #include #include "UnixUser.h" diff --git a/lib/common/WaitForEvent.h b/lib/common/WaitForEvent.h index 46f152c5..52a073e9 100644 --- a/lib/common/WaitForEvent.h +++ b/lib/common/WaitForEvent.h @@ -15,7 +15,9 @@ #include #else #include - #include + #ifndef WIN32 + #include + #endif #endif #include "CommonException.h" -- cgit v1.2.3