From fc662341e6f85da78ada0e443f6116b978f79f22 Mon Sep 17 00:00:00 2001 From: Igor Pavlov <87184205+ip7z@users.noreply.github.com> Date: Tue, 14 May 2024 00:00:00 +0000 Subject: 24.05 --- CPP/Windows/Control/Dialog.cpp | 1 + CPP/Windows/Control/Dialog.h | 19 +++ CPP/Windows/Control/PropertyPage.cpp | 4 +- CPP/Windows/DLL.cpp | 9 +- CPP/Windows/FileDir.cpp | 26 +++- CPP/Windows/FileFind.cpp | 177 ++++++++++++++++++++++----- CPP/Windows/FileFind.h | 19 ++- CPP/Windows/FileIO.cpp | 125 +++++++++++++------ CPP/Windows/FileIO.h | 76 ++++++++++-- CPP/Windows/FileLink.cpp | 17 ++- CPP/Windows/FileName.cpp | 8 +- CPP/Windows/FileSystem.cpp | 27 ++++- CPP/Windows/MemoryLock.cpp | 3 + CPP/Windows/ProcessUtils.cpp | 2 +- CPP/Windows/PropVariant.cpp | 2 + CPP/Windows/PropVariantConv.cpp | 104 ++++++++++++++-- CPP/Windows/PropVariantConv.h | 9 +- CPP/Windows/Registry.cpp | 15 +-- CPP/Windows/SecurityUtils.h | 2 + CPP/Windows/Shell.cpp | 26 +++- CPP/Windows/Shell.h | 1 - CPP/Windows/StdAfx.h | 1 - CPP/Windows/Synchronization.h | 5 + CPP/Windows/System.cpp | 78 ++++++++---- CPP/Windows/System.h | 2 +- CPP/Windows/SystemInfo.cpp | 225 +++++++++++++++++++++++++++++------ CPP/Windows/TimeUtils.cpp | 90 +++++++++++--- 27 files changed, 857 insertions(+), 216 deletions(-) (limited to 'CPP/Windows') diff --git a/CPP/Windows/Control/Dialog.cpp b/CPP/Windows/Control/Dialog.cpp index c8f1bd2..cbb000b 100644 --- a/CPP/Windows/Control/Dialog.cpp +++ b/CPP/Windows/Control/Dialog.cpp @@ -93,6 +93,7 @@ bool CDialog::OnButtonClicked(unsigned buttonID, HWND /* buttonHWND */) case IDOK: OnOK(); break; case IDCANCEL: OnCancel(); break; case IDCLOSE: OnClose(); break; + case IDCONTINUE: OnContinue(); break; case IDHELP: OnHelp(); break; default: return false; } diff --git a/CPP/Windows/Control/Dialog.h b/CPP/Windows/Control/Dialog.h index 06be4bf..2510c8f 100644 --- a/CPP/Windows/Control/Dialog.h +++ b/CPP/Windows/Control/Dialog.h @@ -8,6 +8,10 @@ namespace NWindows { namespace NControl { +#ifndef IDCONTINUE +#define IDCONTINUE 11 +#endif + class CDialog: public CWindow { // Z7_CLASS_NO_COPY(CDialog) @@ -64,14 +68,26 @@ public: return window.GetText(s); } +/* bool SetItemInt(unsigned itemID, UINT value, bool isSigned) { return BOOLToBool(SetDlgItemInt(_window, (int)itemID, value, BoolToBOOL(isSigned))); } +*/ + bool SetItemUInt(unsigned itemID, UINT value) + { return BOOLToBool(SetDlgItemInt(_window, (int)itemID, value, FALSE)); } +/* bool GetItemInt(unsigned itemID, bool isSigned, UINT &value) { BOOL result; value = GetDlgItemInt(_window, (int)itemID, &result, BoolToBOOL(isSigned)); return BOOLToBool(result); } +*/ + bool GetItemUInt(unsigned itemID, UINT &value) + { + BOOL result; + value = GetDlgItemInt(_window, (int)itemID, &result, FALSE); + return BOOLToBool(result); + } HWND GetNextGroupItem(HWND control, bool previous) { return GetNextDlgGroupItem(_window, control, BoolToBOOL(previous)); } @@ -126,6 +142,7 @@ public: virtual bool OnButtonClicked(unsigned buttonID, HWND buttonHWND); virtual void OnOK() {} + virtual void OnContinue() {} virtual void OnCancel() {} virtual void OnClose() {} virtual bool OnNotify(UINT /* controlID */, LPNMHDR /* lParam */) { return false; } @@ -157,6 +174,7 @@ public: bool Create(LPCWSTR templateName, HWND parentWindow); #endif virtual void OnOK() Z7_override { Destroy(); } + virtual void OnContinue() Z7_override { Destroy(); } virtual void OnCancel() Z7_override { Destroy(); } virtual void OnClose() Z7_override { Destroy(); } }; @@ -172,6 +190,7 @@ public: bool End(INT_PTR result) { return BOOLToBool(::EndDialog(_window, result)); } virtual void OnOK() Z7_override { End(IDOK); } + virtual void OnContinue() Z7_override { End(IDCONTINUE); } virtual void OnCancel() Z7_override { End(IDCANCEL); } virtual void OnClose() Z7_override { End(IDCLOSE); } }; diff --git a/CPP/Windows/Control/PropertyPage.cpp b/CPP/Windows/Control/PropertyPage.cpp index f8effe6..9b36cbe 100644 --- a/CPP/Windows/Control/PropertyPage.cpp +++ b/CPP/Windows/Control/PropertyPage.cpp @@ -141,7 +141,7 @@ INT_PTR MyPropertySheet(const CObjectVector &pagesInfo, HWND hwndPare sheet.pszCaption = titleA; sheet.nPages = pagesA.Size(); sheet.nStartPage = 0; - sheet.ppsp = (LPCPROPSHEETPAGEA)(const void *)&pagesA.Front(); + sheet.ppsp = (LPCPROPSHEETPAGEA)(const void *)pagesA.ConstData(); sheet.pfnCallback = NULL; return ::PropertySheetA(&sheet); } @@ -156,7 +156,7 @@ INT_PTR MyPropertySheet(const CObjectVector &pagesInfo, HWND hwndPare sheet.pszCaption = title; sheet.nPages = pagesW.Size(); sheet.nStartPage = 0; - sheet.ppsp = (LPCPROPSHEETPAGEW)(const void *)&pagesW.Front(); + sheet.ppsp = (LPCPROPSHEETPAGEW)(const void *)pagesW.ConstData(); sheet.pfnCallback = NULL; return ::PropertySheetW(&sheet); } diff --git a/CPP/Windows/DLL.cpp b/CPP/Windows/DLL.cpp index b2499ec..025df17 100644 --- a/CPP/Windows/DLL.cpp +++ b/CPP/Windows/DLL.cpp @@ -61,14 +61,14 @@ bool CLibrary::Load(CFSTR path) throw() bool MyGetModuleFileName(FString &path) { - HMODULE hModule = g_hInstance; + const HMODULE hModule = g_hInstance; path.Empty(); #ifndef _UNICODE if (!g_IsNT) { TCHAR s[MAX_PATH + 2]; s[0] = 0; - DWORD size = ::GetModuleFileName(hModule, s, MAX_PATH + 1); + const DWORD size = ::GetModuleFileName(hModule, s, MAX_PATH + 1); if (size <= MAX_PATH && size != 0) { path = fas2fs(s); @@ -80,7 +80,7 @@ bool MyGetModuleFileName(FString &path) { WCHAR s[MAX_PATH + 2]; s[0] = 0; - DWORD size = ::GetModuleFileNameW(hModule, s, MAX_PATH + 1); + const DWORD size = ::GetModuleFileNameW(hModule, s, MAX_PATH + 1); if (size <= MAX_PATH && size != 0) { path = us2fs(s); @@ -97,7 +97,7 @@ FString GetModuleDirPrefix() FString s; if (MyGetModuleFileName(s)) { - int pos = s.ReverseFind_PathSepar(); + const int pos = s.ReverseFind_PathSepar(); if (pos >= 0) s.DeleteFrom((unsigned)(pos + 1)); } @@ -114,7 +114,6 @@ FString GetModuleDirPrefix() #include #include -#include "../Common/Common.h" // FARPROC void *GetProcAddress(HMODULE module, LPCSTR procName) diff --git a/CPP/Windows/FileDir.cpp b/CPP/Windows/FileDir.cpp index 5b1f340..dfeed82 100644 --- a/CPP/Windows/FileDir.cpp +++ b/CPP/Windows/FileDir.cpp @@ -37,7 +37,12 @@ static bool FiTime_To_timespec(const CFiTime *ft, timespec &ts) { if (ft) { +#if defined(_AIX) + ts.tv_sec = ft->tv_sec; + ts.tv_nsec = ft->tv_nsec; +#else ts = *ft; +#endif return true; } // else @@ -246,6 +251,11 @@ bool MyMoveFile(CFSTR oldFile, CFSTR newFile) } #ifndef UNDER_CE +#if !defined(Z7_WIN32_WINNT_MIN) || Z7_WIN32_WINNT_MIN < 0x0500 // Win2000 +#define Z7_USE_DYN_CreateHardLink +#endif + +#ifdef Z7_USE_DYN_CreateHardLink EXTERN_C_BEGIN typedef BOOL (WINAPI *Func_CreateHardLinkW)( LPCWSTR lpFileName, @@ -253,6 +263,7 @@ typedef BOOL (WINAPI *Func_CreateHardLinkW)( LPSECURITY_ATTRIBUTES lpSecurityAttributes ); EXTERN_C_END +#endif #endif // UNDER_CE bool MyCreateHardLink(CFSTR newFileName, CFSTR existFileName) @@ -270,6 +281,7 @@ bool MyCreateHardLink(CFSTR newFileName, CFSTR existFileName) else #endif { +#ifdef Z7_USE_DYN_CreateHardLink const Func_CreateHardLinkW my_CreateHardLinkW = Z7_GET_PROC_ADDRESS( @@ -277,9 +289,13 @@ bool MyCreateHardLink(CFSTR newFileName, CFSTR existFileName) "CreateHardLinkW"); if (!my_CreateHardLinkW) return false; + #define MY_CreateHardLinkW my_CreateHardLinkW +#else + #define MY_CreateHardLinkW CreateHardLinkW +#endif IF_USE_MAIN_PATH_2(newFileName, existFileName) { - if (my_CreateHardLinkW(fs2us(newFileName), fs2us(existFileName), NULL)) + if (MY_CreateHardLinkW(fs2us(newFileName), fs2us(existFileName), NULL)) return true; } #ifdef Z7_LONG_PATH @@ -287,7 +303,7 @@ bool MyCreateHardLink(CFSTR newFileName, CFSTR existFileName) { UString d1, d2; if (GetSuperPaths(newFileName, existFileName, d1, d2, USE_MAIN_PATH_2)) - return BOOLToBool(my_CreateHardLinkW(d1, d2, NULL)); + return BOOLToBool(MY_CreateHardLinkW(d1, d2, NULL)); } #endif } @@ -780,7 +796,7 @@ bool CreateTempFile2(CFSTR prefix, bool addRandom, AString &postfix, NIO::COutFi unsigned k; for (k = 0; k < 8; k++) { - const unsigned t = val & 0xF; + const unsigned t = (unsigned)val & 0xF; val >>= 4; s[k] = (char)((t < 10) ? ('0' + t) : ('A' + (t - 10))); } @@ -805,7 +821,7 @@ bool CreateTempFile2(CFSTR prefix, bool addRandom, AString &postfix, NIO::COutFi } if (outFile) { - if (outFile->Create(path, false)) + if (outFile->Create_NEW(path)) return true; } else @@ -928,7 +944,7 @@ bool RemoveDir(CFSTR path) static BOOL My_CopyFile(CFSTR oldFile, CFSTR newFile) { NWindows::NFile::NIO::COutFile outFile; - if (!outFile.Create(newFile, false)) + if (!outFile.Create_NEW(newFile)) return FALSE; NWindows::NFile::NIO::CInFile inFile; diff --git a/CPP/Windows/FileFind.cpp b/CPP/Windows/FileFind.cpp index c562a90..ca387f6 100644 --- a/CPP/Windows/FileFind.cpp +++ b/CPP/Windows/FileFind.cpp @@ -25,6 +25,14 @@ using namespace NName; #if defined(_WIN32) && !defined(UNDER_CE) +#if !defined(Z7_WIN32_WINNT_MIN) || Z7_WIN32_WINNT_MIN < 0x0502 // Win2003 +#define Z7_USE_DYN_FindFirstStream +#endif + +#ifdef Z7_USE_DYN_FindFirstStream + +Z7_DIAGNOSTIC_IGNORE_CAST_FUNCTION + EXTERN_C_BEGIN typedef enum @@ -46,6 +54,12 @@ typedef BOOL (APIENTRY *Func_FindNextStreamW)(HANDLE findStream, LPVOID findStre EXTERN_C_END +#else + +#define MY_WIN32_FIND_STREAM_DATA WIN32_FIND_STREAM_DATA +#define My_FindStreamInfoStandard FindStreamInfoStandard + +#endif #endif // defined(_WIN32) && !defined(UNDER_CE) @@ -95,6 +109,86 @@ void CFileInfoBase::ClearBase() throw() #endif } + +bool CFileInfoBase::SetAs_StdInFile() +{ + ClearBase(); + Size = (UInt64)(Int64)-1; + NTime::GetCurUtc_FiTime(MTime); + CTime = ATime = MTime; + +#ifdef _WIN32 + + /* in GUI mode : GetStdHandle(STD_INPUT_HANDLE) returns NULL, + and it doesn't set LastError. */ +#if 1 + SetLastError(0); + const HANDLE h = GetStdHandle(STD_INPUT_HANDLE); + if (!h || h == INVALID_HANDLE_VALUE) + { + if (GetLastError() == 0) + SetLastError(ERROR_INVALID_HANDLE); + return false; + } + BY_HANDLE_FILE_INFORMATION info; + if (GetFileInformationByHandle(h, &info) + && info.dwVolumeSerialNumber) + { + Size = (((UInt64)info.nFileSizeHigh) << 32) + info.nFileSizeLow; + // FileID_Low = (((UInt64)info.nFileIndexHigh) << 32) + info.nFileIndexLow; + // NumLinks = SupportHardLinks ? info.nNumberOfLinks : 1; + Attrib = info.dwFileAttributes; + CTime = info.ftCreationTime; + ATime = info.ftLastAccessTime; + MTime = info.ftLastWriteTime; + } +#if 0 + printf( + "\ndwFileAttributes = %8x" + "\nftCreationTime = %8x" + "\nftLastAccessTime = %8x" + "\nftLastWriteTime = %8x" + "\ndwVolumeSerialNumber = %8x" + "\nnFileSizeHigh = %8x" + "\nnFileSizeLow = %8x" + "\nnNumberOfLinks = %8x" + "\nnFileIndexHigh = %8x" + "\nnFileIndexLow = %8x \n", + (unsigned)info.dwFileAttributes, + (unsigned)info.ftCreationTime.dwHighDateTime, + (unsigned)info.ftLastAccessTime.dwHighDateTime, + (unsigned)info.ftLastWriteTime.dwHighDateTime, + (unsigned)info.dwVolumeSerialNumber, + (unsigned)info.nFileSizeHigh, + (unsigned)info.nFileSizeLow, + (unsigned)info.nNumberOfLinks, + (unsigned)info.nFileIndexHigh, + (unsigned)info.nFileIndexLow); +#endif +#endif + +#else // non-Wiondow + + mode = S_IFIFO | 0777; // 0755 : 0775 : 0664 : 0644 : +#if 1 + struct stat st; + if (fstat(0, &st) == 0) + { + SetFrom_stat(st); + if (!S_ISREG(st.st_mode) + // S_ISFIFO(st->st_mode) + || st.st_size == 0) + { + Size = (UInt64)(Int64)-1; + // mode = S_IFIFO | 0777; + } + } +#endif +#endif + + return true; +} + bool CFileInfo::IsDots() const throw() { if (!IsDir() || Name.IsEmpty()) @@ -252,9 +346,11 @@ bool CFindFile::FindNext(CFileInfo &fi) //////////////////////////////// // AltStreams +#ifdef Z7_USE_DYN_FindFirstStream static Func_FindFirstStreamW g_FindFirstStreamW; -static Func_FindNextStreamW g_FindNextStreamW; - +static Func_FindNextStreamW g_FindNextStreamW; +#define MY_FindFirstStreamW g_FindFirstStreamW +#define MY_FindNextStreamW g_FindNextStreamW static struct CFindStreamLoader { CFindStreamLoader() @@ -268,6 +364,11 @@ static struct CFindStreamLoader "FindNextStreamW"); } } g_FindStreamLoader; +#else +#define MY_FindFirstStreamW FindFirstStreamW +#define MY_FindNextStreamW FindNextStreamW +#endif + bool CStreamInfo::IsMainStream() const throw() { @@ -320,16 +421,18 @@ bool CFindStream::FindFirst(CFSTR path, CStreamInfo &si) { if (!Close()) return false; +#ifdef Z7_USE_DYN_FindFirstStream if (!g_FindFirstStreamW) { ::SetLastError(ERROR_CALL_NOT_IMPLEMENTED); return false; } +#endif { MY_WIN32_FIND_STREAM_DATA sd; SetLastError(0); IF_USE_MAIN_PATH - _handle = g_FindFirstStreamW(fs2us(path), My_FindStreamInfoStandard, &sd, 0); + _handle = MY_FindFirstStreamW(fs2us(path), My_FindStreamInfoStandard, &sd, 0); if (_handle == INVALID_HANDLE_VALUE) { if (::GetLastError() == ERROR_HANDLE_EOF) @@ -340,7 +443,7 @@ bool CFindStream::FindFirst(CFSTR path, CStreamInfo &si) { UString superPath; if (GetSuperPath(path, superPath, USE_MAIN_PATH)) - _handle = g_FindFirstStreamW(superPath, My_FindStreamInfoStandard, &sd, 0); + _handle = MY_FindFirstStreamW(superPath, My_FindStreamInfoStandard, &sd, 0); } #endif } @@ -353,14 +456,16 @@ bool CFindStream::FindFirst(CFSTR path, CStreamInfo &si) bool CFindStream::FindNext(CStreamInfo &si) { +#ifdef Z7_USE_DYN_FindFirstStream if (!g_FindNextStreamW) { ::SetLastError(ERROR_CALL_NOT_IMPLEMENTED); return false; } +#endif { MY_WIN32_FIND_STREAM_DATA sd; - if (!g_FindNextStreamW(_handle, &sd)) + if (!MY_FindNextStreamW(_handle, &sd)) return false; Convert_WIN32_FIND_STREAM_DATA_to_StreamInfo(sd, si); } @@ -622,7 +727,7 @@ bool CFileInfo::Find(CFSTR path, bool followLink) FString s (path); s.Add_PathSepar(); - s += '*'; // CHAR_ANY_MASK + s.Add_Char('*'); // CHAR_ANY_MASK bool isOK = false; if (finder.FindFirst(s, *this)) { @@ -636,7 +741,7 @@ bool CFileInfo::Find(CFSTR path, bool followLink) But it's possible that there are another items */ } { - DWORD attrib = GetFileAttrib(path); + const DWORD attrib = GetFileAttrib(path); if (isOK || (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)) { ClearBase(); @@ -763,7 +868,7 @@ bool DoesFileOrDirExist(CFSTR name) void CEnumerator::SetDirPrefix(const FString &dirPrefix) { _wildcard = dirPrefix; - _wildcard += '*'; + _wildcard.Add_Char('*'); } bool CEnumerator::NextAny(CFileInfo &fi) @@ -925,7 +1030,7 @@ bool MyGetLogicalDriveStrings(CObjectVector &driveStrings) // ---------- POSIX ---------- -static int MY__lstat(CFSTR path, struct stat *st, bool followLink) +static int MY_lstat(CFSTR path, struct stat *st, bool followLink) { memset(st, 0, sizeof(*st)); int res; @@ -941,18 +1046,26 @@ static int MY__lstat(CFSTR path, struct stat *st, bool followLink) // printf("\nstat\n"); res = stat(path, st); } - /* - printf("\nres = %d\n", res); - printf("\n st_dev = %lld \n", (long long)(st->st_dev)); - printf("\n st_ino = %lld \n", (long long)(st->st_ino)); - printf("\n st_mode = %lld \n", (long long)(st->st_mode)); - printf("\n st_nlink = %lld \n", (long long)(st->st_nlink)); - printf("\n st_uid = %lld \n", (long long)(st->st_uid)); - printf("\n st_gid = %lld \n", (long long)(st->st_gid)); - printf("\n st_size = %lld \n", (long long)(st->st_size)); - printf("\n st_blksize = %lld \n", (long long)(st->st_blksize)); - printf("\n st_blocks = %lld \n", (long long)(st->st_blocks)); - */ +#if 0 +#if defined(__clang__) && __clang_major__ >= 14 + #pragma GCC diagnostic ignored "-Wc++98-compat-pedantic" +#endif + + printf("\n st_dev = %lld", (long long)(st->st_dev)); + printf("\n st_ino = %lld", (long long)(st->st_ino)); + printf("\n st_mode = %llx", (long long)(st->st_mode)); + printf("\n st_nlink = %lld", (long long)(st->st_nlink)); + printf("\n st_uid = %lld", (long long)(st->st_uid)); + printf("\n st_gid = %lld", (long long)(st->st_gid)); + printf("\n st_size = %lld", (long long)(st->st_size)); + printf("\n st_blksize = %lld", (long long)(st->st_blksize)); + printf("\n st_blocks = %lld", (long long)(st->st_blocks)); + printf("\n st_ctim = %lld", (long long)(ST_CTIME((*st)).tv_sec)); + printf("\n st_mtim = %lld", (long long)(ST_MTIME((*st)).tv_sec)); + printf("\n st_atim = %lld", (long long)(ST_ATIME((*st)).tv_sec)); + printf(S_ISFIFO(st->st_mode) ? "\n FIFO" : "\n NO FIFO"); + printf("\n"); +#endif return res; } @@ -1006,7 +1119,7 @@ UInt32 Get_WinAttrib_From_stat(const struct stat &st) } */ -void CFileInfo::SetFrom_stat(const struct stat &st) +void CFileInfoBase::SetFrom_stat(const struct stat &st) { // IsDevice = false; @@ -1114,7 +1227,7 @@ int Uid_To_Uname(uid_t uid, AString &name) bool CFileInfo::Find_DontFill_Name(CFSTR path, bool followLink) { struct stat st; - if (MY__lstat(path, &st, followLink) != 0) + if (MY_lstat(path, &st, followLink) != 0) return false; // printf("\nFind_DontFill_Name : name=%s\n", path); SetFrom_stat(st); @@ -1145,7 +1258,7 @@ bool DoesFileExist_Raw(CFSTR name) { // FIXME for symbolic links. struct stat st; - if (MY__lstat(name, &st, false) != 0) + if (MY_lstat(name, &st, false) != 0) return false; return !S_ISDIR(st.st_mode); } @@ -1154,7 +1267,7 @@ bool DoesFileExist_FollowLink(CFSTR name) { // FIXME for symbolic links. struct stat st; - if (MY__lstat(name, &st, true) != 0) + if (MY_lstat(name, &st, true) != 0) return false; return !S_ISDIR(st.st_mode); } @@ -1162,7 +1275,7 @@ bool DoesFileExist_FollowLink(CFSTR name) bool DoesDirExist(CFSTR name, bool followLink) { struct stat st; - if (MY__lstat(name, &st, followLink) != 0) + if (MY_lstat(name, &st, followLink) != 0) return false; return S_ISDIR(st.st_mode); } @@ -1170,7 +1283,7 @@ bool DoesDirExist(CFSTR name, bool followLink) bool DoesFileOrDirExist(CFSTR name) { struct stat st; - if (MY__lstat(name, &st, false) != 0) + if (MY_lstat(name, &st, false) != 0) return false; return true; } @@ -1192,10 +1305,10 @@ bool CDirEntry::IsDots() const throw() /* some systems (like CentOS 7.x on XFS) have (Type == DT_UNKNOWN) we can call fstatat() for that case, but we use only (Name) check here */ - #if !defined(_AIX) +#if !defined(_AIX) && !defined(__sun) if (Type != DT_DIR && Type != DT_UNKNOWN) return false; - #endif +#endif return Name.Len() != 0 && Name.Len() <= 2 @@ -1232,7 +1345,7 @@ bool CEnumerator::NextAny(CDirEntry &fi, bool &found) fi.iNode = de->d_ino; - #if !defined(_AIX) +#if !defined(_AIX) && !defined(__sun) fi.Type = de->d_type; /* some systems (like CentOS 7.x on XFS) have (Type == DT_UNKNOWN) we can set (Type) from fstatat() in that case. @@ -1247,7 +1360,7 @@ bool CEnumerator::NextAny(CDirEntry &fi, bool &found) fi.Type = DT_DIR; } */ - #endif +#endif /* if (de->d_type == DT_DIR) @@ -1313,7 +1426,7 @@ bool CEnumerator::Fill_FileInfo(const CDirEntry &de, CFileInfo &fileInfo, bool f /* const FString path = _wildcard + s; - int res = MY__lstat(path, &st, followLink); + int res = MY_lstat(path, &st, followLink); */ if (res != 0) diff --git a/CPP/Windows/FileFind.h b/CPP/Windows/FileFind.h index 11408d0..f68673a 100644 --- a/CPP/Windows/FileFind.h +++ b/CPP/Windows/FileFind.h @@ -99,12 +99,12 @@ public: CFileInfoBase() { ClearBase(); } void ClearBase() throw(); + bool SetAs_StdInFile(); #ifdef _WIN32 bool Fill_From_ByHandleFileInfo(CFSTR path); void SetAsDir() { Attrib = FILE_ATTRIBUTE_DIRECTORY; } // |= (FILE_ATTRIBUTE_UNIX_EXTENSION + (S_IFDIR << 16)); - void SetAsFile() { Attrib = 0; } bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); } bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); } @@ -132,8 +132,8 @@ public: UInt32 GetWinAttrib() const { return Get_WinAttribPosix_From_PosixMode(mode); } bool IsDir() const { return S_ISDIR(mode); } - void SetAsDir() { mode = S_IFDIR; } - void SetAsFile() { mode = S_IFREG; } + void SetAsDir() { mode = S_IFDIR | 0777; } + void SetFrom_stat(const struct stat &st); bool IsReadOnly() const { @@ -173,7 +173,6 @@ struct CFileInfo: public CFileInfoBase // bool FollowReparse(CFSTR path, bool isDir); #else bool Find_DontFill_Name(CFSTR path, bool followLink = false); - void SetFrom_stat(const struct stat &st); #endif }; @@ -278,19 +277,19 @@ typedef CFileInfo CDirEntry; struct CDirEntry { ino_t iNode; - #if !defined(_AIX) +#if !defined(_AIX) && !defined(__sun) Byte Type; - #endif +#endif FString Name; /* - #if !defined(_AIX) +#if !defined(_AIX) && !defined(__sun) bool IsDir() const { // (Type == DT_UNKNOWN) on some systems return Type == DT_DIR; } - #endif +#endif */ bool IsDots() const throw(); @@ -311,12 +310,12 @@ public: bool Fill_FileInfo(const CDirEntry &de, CFileInfo &fileInfo, bool followLink) const; bool DirEntry_IsDir(const CDirEntry &de, bool followLink) const { - #if !defined(_AIX) +#if !defined(_AIX) && !defined(__sun) if (de.Type == DT_DIR) return true; if (de.Type != DT_UNKNOWN) return false; - #endif +#endif CFileInfo fileInfo; if (Fill_FileInfo(de, fileInfo, followLink)) { diff --git a/CPP/Windows/FileIO.cpp b/CPP/Windows/FileIO.cpp index 4ecbb7e..dc4de14 100644 --- a/CPP/Windows/FileIO.cpp +++ b/CPP/Windows/FileIO.cpp @@ -141,8 +141,17 @@ bool CFileBase::Close() throw() { if (_handle == INVALID_HANDLE_VALUE) return true; - if (!::CloseHandle(_handle)) - return false; +#if 0 + if (!IsStdStream) +#endif + { + if (!::CloseHandle(_handle)) + return false; + } +#if 0 + IsStdStream = false; + IsStdPipeStream = false; +#endif _handle = INVALID_HANDLE_VALUE; return true; } @@ -457,7 +466,7 @@ bool CInFile::Open(CFSTR fileName) // for 32 MB (maybe also for 16 MB). // And message can be "Network connection was lost" -static const UInt32 kChunkSizeMax = (1 << 22); +static const UInt32 kChunkSizeMax = 1 << 22; bool CInFile::Read1(void *data, UInt32 size, UInt32 &processedSize) throw() { @@ -469,8 +478,14 @@ bool CInFile::Read1(void *data, UInt32 size, UInt32 &processedSize) throw() bool CInFile::ReadPart(void *data, UInt32 size, UInt32 &processedSize) throw() { +#if 0 + const UInt32 chunkSizeMax = (0 || IsStdStream) ? (1 << 20) : kChunkSizeMax; + if (size > chunkSizeMax) + size = chunkSizeMax; +#else if (size > kChunkSizeMax) - size = kChunkSizeMax; + size = kChunkSizeMax; +#endif return Read1(data, size, processedSize); } @@ -486,10 +501,10 @@ bool CInFile::Read(void *data, UInt32 size, UInt32 &processedSize) throw() return false; if (processedLoc == 0) return true; - data = (void *)((unsigned char *)data + processedLoc); + data = (void *)((Byte *)data + processedLoc); size -= processedLoc; } - while (size > 0); + while (size); return true; } @@ -506,29 +521,23 @@ bool CInFile::ReadFull(void *data, size_t size, size_t &processedSize) throw() return false; if (processedLoc == 0) return true; - data = (void *)((unsigned char *)data + processedLoc); + data = (void *)((Byte *)data + processedLoc); size -= processedLoc; } - while (size > 0); + while (size); return true; } // ---------- COutFile --------- -static inline DWORD GetCreationDisposition(bool createAlways) - { return createAlways? CREATE_ALWAYS: CREATE_NEW; } - bool COutFile::Open(CFSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); } -bool COutFile::Open(CFSTR fileName, DWORD creationDisposition) +bool COutFile::Open_Disposition(CFSTR fileName, DWORD creationDisposition) { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); } -bool COutFile::Create(CFSTR fileName, bool createAlways) - { return Open(fileName, GetCreationDisposition(createAlways)); } - -bool COutFile::CreateAlways(CFSTR fileName, DWORD flagsAndAttributes) - { return Open(fileName, FILE_SHARE_READ, GetCreationDisposition(true), flagsAndAttributes); } +bool COutFile::Create_ALWAYS_with_Attribs(CFSTR fileName, DWORD flagsAndAttributes) + { return Open(fileName, FILE_SHARE_READ, CREATE_ALWAYS, flagsAndAttributes); } bool COutFile::SetTime(const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime) throw() { return BOOLToBool(::SetFileTime(_handle, cTime, aTime, mTime)); } @@ -557,10 +566,10 @@ bool COutFile::Write(const void *data, UInt32 size, UInt32 &processedSize) throw return false; if (processedLoc == 0) return true; - data = (const void *)((const unsigned char *)data + processedLoc); + data = (const void *)((const Byte *)data + processedLoc); size -= processedLoc; } - while (size != 0); + while (size); return true; } @@ -574,10 +583,10 @@ bool COutFile::WriteFull(const void *data, size_t size) throw() return false; if (processedLoc == 0) return (size == 0); - data = (const void *)((const unsigned char *)data + processedLoc); + data = (const void *)((const Byte *)data + processedLoc); size -= processedLoc; } - while (size != 0); + while (size); return true; } @@ -786,11 +795,11 @@ bool CInFile::ReadFull(void *data, size_t size, size_t &processed) throw() return false; if (res == 0) break; - data = (void *)((unsigned char *)data + (size_t)res); - size -= (size_t)res; + data = (void *)((Byte *)data + (size_t)res); processed += (size_t)res; + size -= (size_t)res; } - while (size > 0); + while (size); return true; } @@ -798,23 +807,63 @@ bool CInFile::ReadFull(void *data, size_t size, size_t &processed) throw() ///////////////////////// // COutFile -bool COutFile::Create(const char *name, bool createAlways) +bool COutFile::OpenBinary_forWrite_oflag(const char *name, int oflag) { Path = name; // change it : set it only if open is success. - if (createAlways) - { - Close(); - _handle = ::creat(name, mode_for_Create); - return _handle != -1; - } - return OpenBinary(name, O_CREAT | O_EXCL | O_WRONLY, mode_for_Create); + return OpenBinary(name, oflag, mode_for_Create); } -bool COutFile::Open(const char *name, DWORD creationDisposition) + +/* + windows exist non-exist posix + CREATE_NEW Fail Create O_CREAT | O_EXCL + CREATE_ALWAYS Trunc Create O_CREAT | O_TRUNC + OPEN_ALWAYS Open Create O_CREAT + OPEN_EXISTING Open Fail 0 + TRUNCATE_EXISTING Trunc Fail O_TRUNC ??? + + // O_CREAT = If the file exists, this flag has no effect except as noted under O_EXCL below. + // If O_CREAT and O_EXCL are set, open() shall fail if the file exists. + // O_TRUNC : If the file exists and the file is successfully opened, its length shall be truncated to 0. +*/ +bool COutFile::Open_EXISTING(const char *name) + { return OpenBinary_forWrite_oflag(name, O_WRONLY); } +bool COutFile::Create_ALWAYS(const char *name) + { return OpenBinary_forWrite_oflag(name, O_WRONLY | O_CREAT | O_TRUNC); } +bool COutFile::Create_NEW(const char *name) + { return OpenBinary_forWrite_oflag(name, O_WRONLY | O_CREAT | O_EXCL); } +bool COutFile::Create_ALWAYS_or_Open_ALWAYS(const char *name, bool createAlways) +{ + return OpenBinary_forWrite_oflag(name, + createAlways ? + O_WRONLY | O_CREAT | O_TRUNC : + O_WRONLY | O_CREAT); +} +/* +bool COutFile::Create_ALWAYS_or_NEW(const char *name, bool createAlways) +{ + return OpenBinary_forWrite_oflag(name, + createAlways ? + O_WRONLY | O_CREAT | O_TRUNC : + O_WRONLY | O_CREAT | O_EXCL); +} +bool COutFile::Open_Disposition(const char *name, DWORD creationDisposition) { - UNUSED_VAR(creationDisposition) // FIXME - return Create(name, false); + int flag; + switch (creationDisposition) + { + case CREATE_NEW: flag = O_WRONLY | O_CREAT | O_EXCL; break; + case CREATE_ALWAYS: flag = O_WRONLY | O_CREAT | O_TRUNC; break; + case OPEN_ALWAYS: flag = O_WRONLY | O_CREAT; break; + case OPEN_EXISTING: flag = O_WRONLY; break; + case TRUNCATE_EXISTING: flag = O_WRONLY | O_TRUNC; break; + default: + SetLastError(EINVAL); + return false; + } + return OpenBinary_forWrite_oflag(name, flag); } +*/ ssize_t COutFile::write_part(const void *data, size_t size) throw() { @@ -833,11 +882,11 @@ ssize_t COutFile::write_full(const void *data, size_t size, size_t &processed) t return res; if (res == 0) break; - data = (const void *)((const unsigned char *)data + (size_t)res); - size -= (size_t)res; + data = (const void *)((const Byte *)data + (size_t)res); processed += (size_t)res; + size -= (size_t)res; } - while (size > 0); + while (size); return (ssize_t)processed; } diff --git a/CPP/Windows/FileIO.h b/CPP/Windows/FileIO.h index 03e061a..6ba40eb 100644 --- a/CPP/Windows/FileIO.h +++ b/CPP/Windows/FileIO.h @@ -141,13 +141,24 @@ public: public: bool PreserveATime; - #ifdef Z7_DEVICE_FILE +#if 0 + bool IsStdStream; + bool IsStdPipeStream; +#endif +#ifdef Z7_DEVICE_FILE bool IsDeviceFile; bool SizeDefined; UInt64 Size; // it can be larger than real available size - #endif +#endif - CFileBase(): _handle(INVALID_HANDLE_VALUE), PreserveATime(false) {} + CFileBase(): + _handle(INVALID_HANDLE_VALUE), + PreserveATime(false) +#if 0 + , IsStdStream(false), + , IsStdPipeStream(false) +#endif + {} ~CFileBase() { Close(); } HANDLE GetHandle() const { return _handle; } @@ -223,6 +234,20 @@ public: bool OpenShared(CFSTR fileName, bool shareForWrite); bool Open(CFSTR fileName); +#if 0 + bool AttachStdIn() + { + IsDeviceFile = false; + const HANDLE h = GetStdHandle(STD_INPUT_HANDLE); + if (h == INVALID_HANDLE_VALUE || !h) + return false; + IsStdStream = true; + IsStdPipeStream = true; + _handle = h; + return true; + } +#endif + #ifndef UNDER_CE bool Open_for_ReadAttributes(CFSTR fileName) @@ -263,11 +288,21 @@ public: class COutFile: public CFileBase { + bool Open_Disposition(CFSTR fileName, DWORD creationDisposition); public: bool Open(CFSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); - bool Open(CFSTR fileName, DWORD creationDisposition); - bool Create(CFSTR fileName, bool createAlways); - bool CreateAlways(CFSTR fileName, DWORD flagsAndAttributes); + bool Open_EXISTING(CFSTR fileName) + { return Open_Disposition(fileName, OPEN_EXISTING); } + bool Create_ALWAYS_or_Open_ALWAYS(CFSTR fileName, bool createAlways) + { return Open_Disposition(fileName, createAlways ? CREATE_ALWAYS : OPEN_ALWAYS); } + bool Create_ALWAYS_or_NEW(CFSTR fileName, bool createAlways) + { return Open_Disposition(fileName, createAlways ? CREATE_ALWAYS : CREATE_NEW); } + bool Create_ALWAYS(CFSTR fileName) + { return Open_Disposition(fileName, CREATE_ALWAYS); } + bool Create_NEW(CFSTR fileName) + { return Open_Disposition(fileName, CREATE_NEW); } + + bool Create_ALWAYS_with_Attribs(CFSTR fileName, DWORD flagsAndAttributes); bool SetTime(const CFiTime *cTime, const CFiTime *aTime, const CFiTime *mTime) throw(); bool SetMTime(const CFiTime *mTime) throw(); @@ -308,8 +343,15 @@ protected: bool OpenBinary(const char *name, int flags, mode_t mode = 0666); public: bool PreserveATime; +#if 0 + bool IsStdStream; +#endif - CFileBase(): _handle(-1), PreserveATime(false) {} + CFileBase(): _handle(-1), PreserveATime(false) +#if 0 + , IsStdStream(false) +#endif + {} ~CFileBase() { Close(); } // void Detach() { _handle = -1; } bool Close(); @@ -331,6 +373,15 @@ class CInFile: public CFileBase public: bool Open(const char *name); bool OpenShared(const char *name, bool shareForWrite); +#if 0 + bool AttachStdIn() + { + _handle = GetStdHandle(STD_INPUT_HANDLE); + if (_handle == INVALID_HANDLE_VALUE || !_handle) + return false; + IsStdStream = true; + } +#endif ssize_t read_part(void *data, size_t size) throw(); // ssize_t read_full(void *data, size_t size, size_t &processed); bool ReadFull(void *data, size_t size, size_t &processedSize) throw(); @@ -347,6 +398,7 @@ class COutFile: public CFileBase AString Path; ssize_t write_part(const void *data, size_t size) throw(); + bool OpenBinary_forWrite_oflag(const char *name, int oflag); public: mode_t mode_for_Create; @@ -358,8 +410,14 @@ public: {} bool Close(); - bool Create(const char *name, bool createAlways); - bool Open(const char *name, DWORD creationDisposition); + + bool Open_EXISTING(CFSTR fileName); + bool Create_ALWAYS_or_Open_ALWAYS(CFSTR fileName, bool createAlways); + bool Create_ALWAYS(CFSTR fileName); + bool Create_NEW(CFSTR fileName); + // bool Create_ALWAYS_or_NEW(CFSTR fileName, bool createAlways); + // bool Open_Disposition(const char *name, DWORD creationDisposition); + ssize_t write_full(const void *data, size_t size, size_t &processed) throw(); bool WriteFull(const void *data, size_t size) throw() diff --git a/CPP/Windows/FileLink.cpp b/CPP/Windows/FileLink.cpp index 2b9fa1a..bb380ec 100644 --- a/CPP/Windows/FileLink.cpp +++ b/CPP/Windows/FileLink.cpp @@ -249,9 +249,13 @@ bool CReparseAttr::Parse(const Byte *p, size_t size) if (size < 8) return false; Tag = Get32(p); - UInt32 len = Get16(p + 4); - if (len + 8 != size) - // if (len + 8 > size) + if (Get16(p + 6) != 0) // padding + return false; + unsigned len = Get16(p + 4); + p += 8; + size -= 8; + if (len != size) + // if (len > size) return false; /* if ((type & kReparseFlags_Alias) == 0 || @@ -259,8 +263,6 @@ bool CReparseAttr::Parse(const Byte *p, size_t size) (type & 0xFFFF) != 3) */ - if (Get16(p + 6) != 0) // padding - return false; HeaderError = false; @@ -276,9 +278,6 @@ bool CReparseAttr::Parse(const Byte *p, size_t size) TagIsUnknown = false; - p += 8; - size -= 8; - if (Tag == Z7_WIN_IO_REPARSE_TAG_LX_SYMLINK) { if (len < 4) @@ -518,7 +517,7 @@ bool SetReparseData(CFSTR path, bool isDir, const void *data, DWORD size) { CreatePrefixDirOfFile(path); COutFile file; - if (!file.Create(path, CREATE_NEW)) + if (!file.Create_NEW(path)) return false; } } diff --git a/CPP/Windows/FileName.cpp b/CPP/Windows/FileName.cpp index c9c4f8b..c16b3d4 100644 --- a/CPP/Windows/FileName.cpp +++ b/CPP/Windows/FileName.cpp @@ -302,13 +302,13 @@ int FindAltStreamColon(CFSTR path) throw() static unsigned GetRootPrefixSize_Of_NetworkPath(CFSTR s) { // Network path: we look "server\path\" as root prefix - int pos = FindSepar(s); + const int pos = FindSepar(s); if (pos < 0) return 0; - int pos2 = FindSepar(s + (unsigned)pos + 1); + const int pos2 = FindSepar(s + (unsigned)pos + 1); if (pos2 < 0) return 0; - return pos + pos2 + 2; + return (unsigned)pos + (unsigned)pos2 + 2; } static unsigned GetRootPrefixSize_Of_SimplePath(CFSTR s) @@ -334,7 +334,7 @@ static unsigned GetRootPrefixSize_Of_SuperPath(CFSTR s) const int pos = FindSepar(s + kSuperPathPrefixSize); if (pos < 0) return 0; - return kSuperPathPrefixSize + pos + 1; + return kSuperPathPrefixSize + (unsigned)pos + 1; } unsigned GetRootPrefixSize(CFSTR s) throw() diff --git a/CPP/Windows/FileSystem.cpp b/CPP/Windows/FileSystem.cpp index 6a262d9..d11f02e 100644 --- a/CPP/Windows/FileSystem.cpp +++ b/CPP/Windows/FileSystem.cpp @@ -71,6 +71,12 @@ UINT MyGetDriveType(CFSTR pathName) } } +#if !defined(Z7_WIN32_WINNT_MIN) || Z7_WIN32_WINNT_MIN < 0x0400 +// GetDiskFreeSpaceEx requires Windows95-OSR2, NT4 +#define Z7_USE_DYN_GetDiskFreeSpaceEx +#endif + +#ifdef Z7_USE_DYN_GetDiskFreeSpaceEx typedef BOOL (WINAPI * Func_GetDiskFreeSpaceExA)( LPCSTR lpDirectoryName, // directory name PULARGE_INTEGER lpFreeBytesAvailable, // bytes available to caller @@ -84,6 +90,7 @@ typedef BOOL (WINAPI * Func_GetDiskFreeSpaceExW)( PULARGE_INTEGER lpTotalNumberOfBytes, // bytes on disk PULARGE_INTEGER lpTotalNumberOfFreeBytes // free bytes on disk ); +#endif bool MyGetDiskFreeSpace(CFSTR rootPath, UInt64 &clusterSize, UInt64 &totalSize, UInt64 &freeSize) { @@ -92,14 +99,22 @@ bool MyGetDiskFreeSpace(CFSTR rootPath, UInt64 &clusterSize, UInt64 &totalSize, #ifndef _UNICODE if (!g_IsNT) { +#ifdef Z7_USE_DYN_GetDiskFreeSpaceEx const Func_GetDiskFreeSpaceExA f = Z7_GET_PROC_ADDRESS( Func_GetDiskFreeSpaceExA, GetModuleHandle(TEXT("kernel32.dll")), "GetDiskFreeSpaceExA"); if (f) +#endif { ULARGE_INTEGER freeBytesToCaller2, totalSize2, freeSize2; - sizeIsDetected = BOOLToBool(f(fs2fas(rootPath), &freeBytesToCaller2, &totalSize2, &freeSize2)); + sizeIsDetected = BOOLToBool( +#ifdef Z7_USE_DYN_GetDiskFreeSpaceEx + f +#else + GetDiskFreeSpaceExA +#endif + (fs2fas(rootPath), &freeBytesToCaller2, &totalSize2, &freeSize2)); totalSize = totalSize2.QuadPart; freeSize = freeSize2.QuadPart; } @@ -109,14 +124,22 @@ bool MyGetDiskFreeSpace(CFSTR rootPath, UInt64 &clusterSize, UInt64 &totalSize, else #endif { +#ifdef Z7_USE_DYN_GetDiskFreeSpaceEx const Func_GetDiskFreeSpaceExW f = Z7_GET_PROC_ADDRESS( Func_GetDiskFreeSpaceExW, GetModuleHandle(TEXT("kernel32.dll")), "GetDiskFreeSpaceExW"); if (f) +#endif { ULARGE_INTEGER freeBytesToCaller2, totalSize2, freeSize2; - sizeIsDetected = BOOLToBool(f(fs2us(rootPath), &freeBytesToCaller2, &totalSize2, &freeSize2)); + sizeIsDetected = BOOLToBool( +#ifdef Z7_USE_DYN_GetDiskFreeSpaceEx + f +#else + GetDiskFreeSpaceExW +#endif + (fs2us(rootPath), &freeBytesToCaller2, &totalSize2, &freeSize2)); totalSize = totalSize2.QuadPart; freeSize = freeSize2.QuadPart; } diff --git a/CPP/Windows/MemoryLock.cpp b/CPP/Windows/MemoryLock.cpp index 0bd7504..bc2d7c7 100644 --- a/CPP/Windows/MemoryLock.cpp +++ b/CPP/Windows/MemoryLock.cpp @@ -37,6 +37,8 @@ bool EnablePrivilege(LPCTSTR privilegeName, bool enable) if (!hModule) return false; +Z7_DIAGNOSTIC_IGNORE_CAST_FUNCTION + GET_PROC_ADDR( OpenProcessToken, "OpenProcessToken") @@ -79,6 +81,7 @@ bool EnablePrivilege(LPCTSTR privilegeName, bool enable) } +Z7_DIAGNOSTIC_IGNORE_CAST_FUNCTION typedef void (WINAPI * Func_RtlGetVersion) (OSVERSIONINFOEXW *); diff --git a/CPP/Windows/ProcessUtils.cpp b/CPP/Windows/ProcessUtils.cpp index 607b4bb..f5e156c 100644 --- a/CPP/Windows/ProcessUtils.cpp +++ b/CPP/Windows/ProcessUtils.cpp @@ -17,7 +17,7 @@ static UString GetQuotedString(const UString &s) { UString s2 ('\"'); s2 += s; - s2 += '\"'; + s2.Add_Char('\"'); return s2; } #endif diff --git a/CPP/Windows/PropVariant.cpp b/CPP/Windows/PropVariant.cpp index 457b1dc..678f970 100644 --- a/CPP/Windows/PropVariant.cpp +++ b/CPP/Windows/PropVariant.cpp @@ -252,6 +252,7 @@ CPropVariant::~CPropVariant() throw() CASE_SIMPLE_VT_VALUES // vt = VT_EMPTY; // it's optional return; + default: break; } ::VariantClear((tagVARIANT *)this); } @@ -296,6 +297,7 @@ HRESULT CPropVariant::Copy(const PROPVARIANT* pSrc) throw() CASE_SIMPLE_VT_VALUES memmove((PROPVARIANT*)this, pSrc, sizeof(PROPVARIANT)); return S_OK; + default: break; } return ::VariantCopy((tagVARIANT *)this, (tagVARIANT *)const_cast(pSrc)); } diff --git a/CPP/Windows/PropVariantConv.cpp b/CPP/Windows/PropVariantConv.cpp index 5fb96a7..21a7b4e 100644 --- a/CPP/Windows/PropVariantConv.cpp +++ b/CPP/Windows/PropVariantConv.cpp @@ -9,12 +9,45 @@ #define UINT_TO_STR_2(c, val) { s[0] = (c); s[1] = (char)('0' + (val) / 10); s[2] = (char)('0' + (val) % 10); s += 3; } -bool ConvertUtcFileTimeToString2(const FILETIME &utc, unsigned ns100, char *s, int level) throw() +static const unsigned k_TimeStringBufferSize = 64; + +bool g_Timestamp_Show_UTC; +#if 0 +bool g_Timestamp_Show_DisableZ; +bool g_Timestamp_Show_TDelimeter; +bool g_Timestamp_Show_ZoneOffset; +#endif + +Z7_NO_INLINE +bool ConvertUtcFileTimeToString2(const FILETIME &utc, unsigned ns100, char *s, int level, unsigned flags) throw() { *s = 0; FILETIME ft; - if (!FileTimeToLocalFileTime(&utc, &ft)) - return false; + +#if 0 + Int64 bias64 = 0; +#endif + + const bool show_utc = + (flags & kTimestampPrintFlags_Force_UTC) ? true : + (flags & kTimestampPrintFlags_Force_LOCAL) ? false : + g_Timestamp_Show_UTC; + + if (show_utc) + ft = utc; + else + { + if (!FileTimeToLocalFileTime(&utc, &ft)) + return false; +#if 0 + if (g_Timestamp_Show_ZoneOffset) + { + const UInt64 utc64 = (((UInt64)utc.dwHighDateTime) << 32) + utc.dwLowDateTime; + const UInt64 loc64 = (((UInt64) ft.dwHighDateTime) << 32) + ft.dwLowDateTime; + bias64 = (Int64)utc64 - (Int64)loc64; + } +#endif + } SYSTEMTIME st; if (!BOOLToBool(FileTimeToSystemTime(&ft, &st))) @@ -41,7 +74,12 @@ bool ConvertUtcFileTimeToString2(const FILETIME &utc, unsigned ns100, char *s, i if (level > kTimestampPrintLevel_DAY) { - UINT_TO_STR_2(' ', st.wHour) + const char setChar = +#if 0 + g_Timestamp_Show_TDelimeter ? 'T' : // ISO 8601 +#endif + ' '; + UINT_TO_STR_2(setChar, st.wHour) UINT_TO_STR_2(':', st.wMinute) if (level >= kTimestampPrintLevel_SEC) @@ -84,6 +122,52 @@ bool ConvertUtcFileTimeToString2(const FILETIME &utc, unsigned ns100, char *s, i } } + if (show_utc) + { + if ((flags & kTimestampPrintFlags_DisableZ) == 0 +#if 0 + && !g_Timestamp_Show_DisableZ +#endif + ) + *s++ = 'Z'; + } +#if 0 + else if (g_Timestamp_Show_ZoneOffset) + { +#if 1 + { + char c; + if (bias64 < 0) + { + bias64 = -bias64; + c = '+'; + } + else + c = '-'; + UInt32 bias = (UInt32)((UInt64)bias64 / (10000000 * 60)); +#else + TIME_ZONE_INFORMATION zi; + const DWORD dw = GetTimeZoneInformation(&zi); + if (dw <= TIME_ZONE_ID_DAYLIGHT) // == 2 + { + // UTC = LOCAL + Bias + Int32 bias = zi.Bias; + char c; + if (bias < 0) + { + bias = -bias; + c = '+'; + } + else + c = '-'; +#endif + const UInt32 hours = (UInt32)bias / 60; + const UInt32 mins = (UInt32)bias % 60; + UINT_TO_STR_2(c, hours) + UINT_TO_STR_2(':', mins) + } + } +#endif *s = 0; return true; } @@ -96,11 +180,11 @@ bool ConvertUtcFileTimeToString(const FILETIME &utc, char *s, int level) throw() bool ConvertUtcFileTimeToString2(const FILETIME &ft, unsigned ns100, wchar_t *dest, int level) throw() { - char s[32]; - bool res = ConvertUtcFileTimeToString2(ft, ns100, s, level); + char s[k_TimeStringBufferSize]; + const bool res = ConvertUtcFileTimeToString2(ft, ns100, s, level); for (unsigned i = 0;; i++) { - Byte c = (Byte)s[i]; + const Byte c = (Byte)s[i]; dest[i] = c; if (c == 0) break; @@ -110,11 +194,11 @@ bool ConvertUtcFileTimeToString2(const FILETIME &ft, unsigned ns100, wchar_t *de bool ConvertUtcFileTimeToString(const FILETIME &ft, wchar_t *dest, int level) throw() { - char s[32]; - bool res = ConvertUtcFileTimeToString(ft, s, level); + char s[k_TimeStringBufferSize]; + const bool res = ConvertUtcFileTimeToString(ft, s, level); for (unsigned i = 0;; i++) { - Byte c = (Byte)s[i]; + const Byte c = (Byte)s[i]; dest[i] = c; if (c == 0) break; diff --git a/CPP/Windows/PropVariantConv.h b/CPP/Windows/PropVariantConv.h index ec5223b..8782507 100644 --- a/CPP/Windows/PropVariantConv.h +++ b/CPP/Windows/PropVariantConv.h @@ -7,6 +7,8 @@ // provide at least 32 bytes for buffer including zero-end +extern bool g_Timestamp_Show_UTC; + #define kTimestampPrintLevel_DAY -3 // #define kTimestampPrintLevel_HOUR -2 #define kTimestampPrintLevel_MIN -1 @@ -14,9 +16,14 @@ #define kTimestampPrintLevel_NTFS 7 #define kTimestampPrintLevel_NS 9 + +#define kTimestampPrintFlags_Force_UTC (1 << 0) +#define kTimestampPrintFlags_Force_LOCAL (1 << 1) +#define kTimestampPrintFlags_DisableZ (1 << 4) + bool ConvertUtcFileTimeToString(const FILETIME &ft, char *s, int level = kTimestampPrintLevel_SEC) throw(); bool ConvertUtcFileTimeToString(const FILETIME &ft, wchar_t *s, int level = kTimestampPrintLevel_SEC) throw(); -bool ConvertUtcFileTimeToString2(const FILETIME &ft, unsigned ns100, char *s, int level = kTimestampPrintLevel_SEC) throw(); +bool ConvertUtcFileTimeToString2(const FILETIME &ft, unsigned ns100, char *s, int level = kTimestampPrintLevel_SEC, unsigned flags = 0) throw(); bool ConvertUtcFileTimeToString2(const FILETIME &ft, unsigned ns100, wchar_t *s, int level = kTimestampPrintLevel_SEC) throw(); // provide at least 32 bytes for buffer including zero-end diff --git a/CPP/Windows/Registry.cpp b/CPP/Windows/Registry.cpp index b20157d..c8b1709 100644 --- a/CPP/Windows/Registry.cpp +++ b/CPP/Windows/Registry.cpp @@ -254,8 +254,8 @@ LONG CKey::QueryValue(LPCTSTR name, CSysString &value) { value.Empty(); DWORD type = 0; - UInt32 curSize = 0; - LONG res = RegQueryValueEx(_object, name, NULL, &type, NULL, (DWORD *)&curSize); + DWORD curSize = 0; + LONG res = RegQueryValueEx(_object, name, NULL, &type, NULL, &curSize); if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) return res; UInt32 curSize2 = curSize; @@ -281,13 +281,11 @@ LONG CKey::QueryValue(LPCWSTR name, UString &value) { value.Empty(); DWORD type = 0; - UInt32 curSize = 0; - + DWORD curSize = 0; LONG res; - if (g_IsNT) { - res = RegQueryValueExW(_object, name, NULL, &type, NULL, (DWORD *)&curSize); + res = RegQueryValueExW(_object, name, NULL, &type, NULL, &curSize); if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) return res; UInt32 curSize2 = curSize; @@ -302,7 +300,6 @@ LONG CKey::QueryValue(LPCWSTR name, UString &value) res = QueryValue(name == 0 ? 0 : (LPCSTR)GetSystemString(name), vTemp); value = GetUnicodeString(vTemp); } - return res; } @@ -377,7 +374,7 @@ LONG CKey::GetValue_Strings(LPCTSTR valueName, UStringVector &strings) strings.Clear(); CByteBuffer buffer; UInt32 dataSize = 0; - LONG res = QueryValue(valueName, buffer, dataSize); + const LONG res = QueryValue(valueName, buffer, dataSize); if (res != ERROR_SUCCESS) return res; if (dataSize > buffer.Size()) @@ -386,7 +383,7 @@ LONG CKey::GetValue_Strings(LPCTSTR valueName, UStringVector &strings) return E_FAIL; const wchar_t *data = (const wchar_t *)(const void *)(const Byte *)buffer; - size_t numChars = dataSize / sizeof(wchar_t); + const size_t numChars = dataSize / sizeof(wchar_t); size_t prev = 0; UString s; diff --git a/CPP/Windows/SecurityUtils.h b/CPP/Windows/SecurityUtils.h index 4ef3939..7219f06 100644 --- a/CPP/Windows/SecurityUtils.h +++ b/CPP/Windows/SecurityUtils.h @@ -18,6 +18,8 @@ typedef NTSTATUS (NTAPI *Func_LsaAddAccountRights)(LSA_HANDLE PolicyHandle, #define MY_STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002L) } +Z7_DIAGNOSTIC_IGNORE_CAST_FUNCTION + #define POLICY_FUNC_CALL(fff, str) \ if (hModule == NULL) return MY_STATUS_NOT_IMPLEMENTED; \ const Func_ ## fff v = Z7_GET_PROC_ADDRESS(Func_ ## fff, hModule, str); \ diff --git a/CPP/Windows/Shell.cpp b/CPP/Windows/Shell.cpp index b2a3489..01ceb22 100644 --- a/CPP/Windows/Shell.cpp +++ b/CPP/Windows/Shell.cpp @@ -162,7 +162,7 @@ static HRESULT ReadAnsiStrings(const char *p, size_t size, UStringVector &names) name.Empty(); } else - name += c; + name.Add_Char(c); } return E_INVALIDARG; } @@ -543,8 +543,18 @@ void CDrop::QueryFileNames(UStringVector &fileNames) typedef int Z7_WIN_GPFIDL_FLAGS; extern "C" { -typedef BOOL (WINAPI * Func_SHGetPathFromIDListW)(LPCITEMIDLIST pidl, LPWSTR pszPath); -typedef BOOL (WINAPI * Func_SHGetPathFromIDListEx)(LPCITEMIDLIST pidl, PWSTR pszPath, DWORD cchPath, Z7_WIN_GPFIDL_FLAGS uOpts); +#ifndef _UNICODE +typedef BOOL (WINAPI * Func_SHGetPathFromIDListW)(LPCITEMIDLIST pidl, LPWSTR pszPath); // nt4 +#endif + +#if !defined(Z7_WIN32_WINNT_MIN) || Z7_WIN32_WINNT_MIN < 0x0600 // Vista +#define Z7_USE_DYN_SHGetPathFromIDListEx +#endif + +#ifdef Z7_USE_DYN_SHGetPathFromIDListEx +Z7_DIAGNOSTIC_IGNORE_CAST_FUNCTION +typedef BOOL (WINAPI * Func_SHGetPathFromIDListEx)(LPCITEMIDLIST pidl, PWSTR pszPath, DWORD cchPath, Z7_WIN_GPFIDL_FLAGS uOpts); // vista +#endif } #ifndef _UNICODE @@ -584,18 +594,26 @@ bool GetPathFromIDList(LPCITEMIDLIST itemIDList, UString &path) /* for long path we need SHGetPathFromIDListEx(). win10: SHGetPathFromIDListEx() for long path returns path with with super path prefix "\\\\?\\". */ +#ifdef Z7_USE_DYN_SHGetPathFromIDListEx const Func_SHGetPathFromIDListEx func_SHGetPathFromIDListEx = Z7_GET_PROC_ADDRESS( Func_SHGetPathFromIDListEx, ::GetModuleHandleW(L"shell32.dll"), "SHGetPathFromIDListEx"); if (func_SHGetPathFromIDListEx) +#endif { ODS("==== GetPathFromIDList() (SHGetPathFromIDListEx)") do { len *= 4; - result = BOOLToBool(func_SHGetPathFromIDListEx(itemIDList, path.GetBuf(len), len, 0)); + result = BOOLToBool( +#ifdef Z7_USE_DYN_SHGetPathFromIDListEx + func_SHGetPathFromIDListEx +#else + SHGetPathFromIDListEx +#endif + (itemIDList, path.GetBuf(len), len, 0)); if (result) break; } diff --git a/CPP/Windows/Shell.h b/CPP/Windows/Shell.h index b4cdb30..9e58e25 100644 --- a/CPP/Windows/Shell.h +++ b/CPP/Windows/Shell.h @@ -3,7 +3,6 @@ #ifndef ZIP7_WINDOWS_SHELL_H #define ZIP7_WINDOWS_SHELL_H -#include "../Common/Common.h" #include "../Common/MyWindows.h" #if defined(__MINGW32__) || defined(__MINGW64__) #include diff --git a/CPP/Windows/StdAfx.h b/CPP/Windows/StdAfx.h index bd5084f..4441a5f 100644 --- a/CPP/Windows/StdAfx.h +++ b/CPP/Windows/StdAfx.h @@ -6,7 +6,6 @@ #if defined(_MSC_VER) && _MSC_VER >= 1800 #pragma warning(disable : 4464) // relative include path contains '..' #endif - #include "../Common/Common.h" #endif diff --git a/CPP/Windows/Synchronization.h b/CPP/Windows/Synchronization.h index afd03d2..1df84d3 100644 --- a/CPP/Windows/Synchronization.h +++ b/CPP/Windows/Synchronization.h @@ -234,6 +234,11 @@ public: } WRes Enter() { +#if defined(Z7_LLVM_CLANG_VERSION) && (__clang_major__ == 13) \ + && defined(__FreeBSD__) + #pragma GCC diagnostic ignored "-Wthread-safety-negative" + #pragma GCC diagnostic ignored "-Wthread-safety-analysis" +#endif return ::pthread_mutex_lock(&_mutex); } WRes Leave() diff --git a/CPP/Windows/System.cpp b/CPP/Windows/System.cpp index dbe287a..03c8988 100644 --- a/CPP/Windows/System.cpp +++ b/CPP/Windows/System.cpp @@ -5,7 +5,8 @@ #ifndef _WIN32 #include #include -#ifdef __APPLE__ +#if defined(__APPLE__) || defined(__DragonFly__) || \ + defined(BSD) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) #include #else #include @@ -28,7 +29,10 @@ UInt32 CountAffinity(DWORD_PTR mask) { UInt32 num = 0; for (unsigned i = 0; i < sizeof(mask) * 8; i++) - num += (UInt32)((mask >> i) & 1); + { + num += (UInt32)(mask & 1); + mask >>= 1; + } return num; } @@ -114,7 +118,7 @@ UInt32 GetNumberOfProcessors() #if !defined(_WIN64) && \ (defined(__MINGW32_VERSION) || defined(Z7_OLD_WIN_SDK)) -typedef struct _MY_MEMORYSTATUSEX { +typedef struct { DWORD dwLength; DWORD dwMemoryLoad; DWORDLONG ullTotalPhys; @@ -187,26 +191,58 @@ bool GetRamSize(UInt64 &size) { size = (UInt64)(sizeof(size_t)) << 29; - #ifdef __APPLE__ +#if defined(__APPLE__) || defined(__DragonFly__) || \ + defined(BSD) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) + + uint64_t val = 0; + int mib[2]; + mib[0] = CTL_HW; #ifdef HW_MEMSIZE - uint64_t val = 0; // support 2Gb+ RAM - int mib[2] = { CTL_HW, HW_MEMSIZE }; + mib[1] = HW_MEMSIZE; + // printf("\n sysctl HW_MEMSIZE"); #elif defined(HW_PHYSMEM64) - uint64_t val = 0; // support 2Gb+ RAM - int mib[2] = { CTL_HW, HW_PHYSMEM64 }; + mib[1] = HW_PHYSMEM64; + // printf("\n sysctl HW_PHYSMEM64"); #else - unsigned int val = 0; // For old system - int mib[2] = { CTL_HW, HW_PHYSMEM }; - #endif // HW_MEMSIZE - size_t size_sys = sizeof(val); + mib[1] = HW_PHYSMEM; + // printf("\n sysctl HW_PHYSMEM"); + #endif - sysctl(mib, 2, &val, &size_sys, NULL, 0); - if (val) - size = val; + size_t size_sys = sizeof(val); + int res = sysctl(mib, 2, &val, &size_sys, NULL, 0); + // printf("\n sysctl res=%d val=%llx size_sys = %d, %d\n", res, (long long int)val, (int)size_sys, errno); + // we use strict check (size_sys == sizeof(val)) for returned value + // because big-endian encoding is possible: + if (res == 0 && size_sys == sizeof(val) && val) + size = val; + else + { + uint32_t val32 = 0; + size_sys = sizeof(val32); + res = sysctl(mib, 2, &val32, &size_sys, NULL, 0); + // printf("\n sysctl res=%d val=%llx size_sys = %d, %d\n", res, (long long int)val32, (int)size_sys, errno); + if (res == 0 && size_sys == sizeof(val32) && val32) + size = val32; + } #elif defined(_AIX) - // fixme + #if defined(_SC_AIX_REALMEM) // AIX + size = (UInt64)sysconf(_SC_AIX_REALMEM) * 1024; + #endif + #elif 0 || defined(__sun) + #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) + // FreeBSD, Linux, OpenBSD, and Solaris. + { + const long phys_pages = sysconf(_SC_PHYS_PAGES); + const long page_size = sysconf(_SC_PAGESIZE); + // #pragma message("GetRamSize : sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE)") + // printf("\n_SC_PHYS_PAGES (hex) = %lx", (unsigned long)phys_pages); + // printf("\n_SC_PAGESIZE = %lu\n", (unsigned long)page_size); + if (phys_pages != -1 && page_size != -1) + size = (UInt64)(Int64)phys_pages * (UInt64)(Int64)page_size; + } + #endif #elif defined(__gnu_hurd__) // fixme #elif defined(__FreeBSD_kernel__) && defined(__GLIBC__) @@ -218,10 +254,6 @@ bool GetRamSize(UInt64 &size) if (::sysinfo(&info) != 0) return false; size = (UInt64)info.mem_unit * info.totalram; - const UInt64 kLimit = (UInt64)1 << (sizeof(size_t) * 8 - 1); - if (size > kLimit) - size = kLimit; - /* printf("\n mem_unit = %lld", (UInt64)info.mem_unit); printf("\n totalram = %lld", (UInt64)info.totalram); @@ -230,6 +262,10 @@ bool GetRamSize(UInt64 &size) #endif + const UInt64 kLimit = (UInt64)1 << (sizeof(size_t) * 8 - 1); + if (size > kLimit) + size = kLimit; + return true; } @@ -269,7 +305,7 @@ unsigned Get_File_OPEN_MAX_Reduced_for_3_tasks() numFiles_OPEN_MAX = 1; numFiles_OPEN_MAX /= 3; // we suppose that we have up to 3 tasks in total for multiple file processing numFiles_OPEN_MAX = MyMax(numFiles_OPEN_MAX, (unsigned long)3); - unsigned n = (UInt32)(UInt32)-1; + unsigned n = (unsigned)(int)-1; if (n > numFiles_OPEN_MAX) n = (unsigned)numFiles_OPEN_MAX; return n; diff --git a/CPP/Windows/System.h b/CPP/Windows/System.h index 0650007..b17111c 100644 --- a/CPP/Windows/System.h +++ b/CPP/Windows/System.h @@ -105,7 +105,7 @@ struct CProcessAffinity } void CpuZero() { } - void CpuSet(unsigned cpuIndex) { UNUSED_VAR(cpuIndex); } + void CpuSet(unsigned /* cpuIndex */) { /* UNUSED_VAR(cpuIndex) */ } int IsCpuSet(unsigned cpuIndex) const { return (cpuIndex < numSysThreads) ? 1 : 0; } BOOL SetProcAffinity() const diff --git a/CPP/Windows/SystemInfo.cpp b/CPP/Windows/SystemInfo.cpp index 6bafc80..d23e84b 100644 --- a/CPP/Windows/SystemInfo.cpp +++ b/CPP/Windows/SystemInfo.cpp @@ -16,9 +16,28 @@ #include #ifdef __APPLE__ #include + #elif !defined(_AIX) +#if defined(__GLIBC__) && (__GLIBC__ * 100 + __GLIBC_MINOR__ >= 216) + #define Z7_GETAUXV_AVAILABLE +#else +// #pragma message("=== is not NEW GLIBC === ") + #if defined __has_include + #if __has_include () +// #pragma message("=== sys/auxv.h is avail=== ") + #define Z7_GETAUXV_AVAILABLE + #endif + #endif +#endif + +#ifdef Z7_GETAUXV_AVAILABLE +// #if defined __has_include +// #if __has_include () #include +#define USE_HWCAP +// #endif +// #endif // #undef AT_HWCAP // to debug // #undef AT_HWCAP2 // to debug @@ -36,11 +55,39 @@ #endif */ +#ifdef USE_HWCAP + +#if defined(__FreeBSD__) + +// #if (__FreeBSD__ >= 13) // (FreeBSD 12.01 is required for elf_aux_info() ???) +static unsigned long MY_getauxval(int aux) +{ + unsigned long val; + if (elf_aux_info(aux, &val, sizeof(val))) + return 0; + return val; +} + +#else // ! __FreeBSD__ + #ifdef MY_CPU_ARM_OR_ARM64 + #if defined __has_include + #if __has_include () #include + #endif + #endif #endif + +#if defined(AT_HWCAP) || defined(AT_HWCAP2) +#define MY_getauxval getauxval #endif +#endif // ! __FreeBSD__ +#endif // USE_HWCAP +#endif // Z7_GETAUXV_AVAILABLE + +#endif // !defined(_AIX) + #ifdef __linux__ #include "../Windows/FileIO.h" #endif @@ -70,13 +117,13 @@ static bool ReadFile_to_Buffer(CFSTR fileName, CByteBuffer &buf) return false; */ size_t size = 0; - size_t addSize = ((size_t)1 << 12); + size_t addSize = (size_t)1 << 12; for (;;) { // printf("\nsize = %d\n", (unsigned)size); buf.ChangeSize_KeepData(size + addSize, size); size_t processed; - if (!file.ReadFull(buf + size, addSize, processed)) + if (!file.ReadFull(buf.NonConstData() + size, addSize, processed)) return false; if (processed == 0) { @@ -107,19 +154,19 @@ static void PrintCpuChars(AString &s, UInt32 v) { for (unsigned j = 0; j < 4; j++) { - Byte b = (Byte)(v & 0xFF); + const Byte b = (Byte)(v & 0xFF); v >>= 8; if (b == 0) break; if (b >= 0x20 && b <= 0x7f) - s += (char)b; + s.Add_Char((char)b); else { - s += '['; + s.Add_Char('['); char temp[16]; ConvertUInt32ToHex(b, temp); s += temp; - s += ']'; + s.Add_Char(']'); } } } @@ -137,7 +184,7 @@ static void x86cpuid_to_String(AString &s) { for (unsigned i = 0; i < 3; i++) { - z7_x86_cpuid(a, 0x80000002 + i); + z7_x86_cpuid(a, (UInt32)(0x80000002 + i)); for (unsigned j = 0; j < 4; j++) PrintCpuChars(s, a[j]); } @@ -353,8 +400,8 @@ void PrintSize_KMGT_Or_Hex(AString &s, UInt64 v) } s.Add_UInt64(v); if (c) - s += c; - s += 'B'; + s.Add_Char(c); + s.Add_Char('B'); } // #endif // #endif @@ -380,7 +427,7 @@ static void SysInfo_To_String(AString &s, const SYSTEM_INFO &si) s += " act:"; PrintHex(s, si.dwActiveProcessorMask); } - s += " cpus:"; + s += " threads:"; s.Add_UInt32(si.dwNumberOfProcessors); if (si.dwPageSize != 1 << 12) { @@ -400,7 +447,7 @@ static void SysInfo_To_String(AString &s, const SYSTEM_INFO &si) if (minAdd != kReserveSize) { PrintSize_KMGT_Or_Hex(s, minAdd); - s += "-"; + s.Add_Minus(); } else { @@ -470,11 +517,10 @@ static void AddBracedString(AString &dest, AString &src) { if (!src.IsEmpty()) { - AString s; - s += '('; - s += src; - s += ')'; - dest.Add_OptSpaced(s); + dest.Add_Space_if_NotEmpty(); + dest.Add_Char('('); + dest += src; + dest.Add_Char(')'); } } @@ -527,6 +573,13 @@ void CCpuName::Fill() { Add_sysctlbyname_to_String("machdep.cpu.brand_string", s); } + #elif defined(MY_CPU_E2K) && defined(Z7_MCST_LCC_VERSION) && (Z7_MCST_LCC_VERSION >= 12323) + { + s += "mcst "; + s += __builtin_cpu_name(); + s.Add_Space(); + s += __builtin_cpu_arch(); + } #endif @@ -592,11 +645,11 @@ void CCpuName::Fill() const CByteBuffer &buf = bufs[i]; if (buf.Size() == 8) { - UInt32 high = GetUi32(buf); + const UInt32 high = GetUi32(buf); if (high != 0) { PrintHex(Microcode, high); - Microcode += "."; + Microcode.Add_Dot(); } PrintHex(Microcode, GetUi32(buf + 4)); } @@ -622,7 +675,7 @@ void AddCpuFeatures(AString &s) UInt64 flags = 0; for (unsigned i = 0; i < kNumFeatures; i++) { - if (IsProcessorFeaturePresent(i)) + if (IsProcessorFeaturePresent((DWORD)i)) { flags += (UInt64)1 << i; // s.Add_Space_if_NotEmpty(); @@ -682,7 +735,7 @@ void AddCpuFeatures(AString &s) #ifdef AT_HWCAP s.Add_OptSpaced("hwcap:"); { - unsigned long h = getauxval(AT_HWCAP); + unsigned long h = MY_getauxval(AT_HWCAP); PrintHex(s, h); #ifdef MY_CPU_ARM64 if (h & HWCAP_CRC32) s += ":CRC32"; @@ -698,7 +751,7 @@ void AddCpuFeatures(AString &s) #ifdef AT_HWCAP2 { - unsigned long h = getauxval(AT_HWCAP2); + unsigned long h = MY_getauxval(AT_HWCAP2); #ifndef MY_CPU_ARM if (h != 0) #endif @@ -722,6 +775,8 @@ void AddCpuFeatures(AString &s) #ifdef _WIN32 #ifndef UNDER_CE +Z7_DIAGNOSTIC_IGNORE_CAST_FUNCTION + EXTERN_C_BEGIN typedef void (WINAPI * Func_RtlGetVersion) (OSVERSIONINFOEXW *); EXTERN_C_END @@ -952,24 +1007,13 @@ void GetVirtCpuid(AString &s) void GetCompiler(AString &s) { - #ifdef __VERSION__ - s += __VERSION__; - #endif - - #ifdef __GNUC__ - s += " GCC "; - s.Add_UInt32(__GNUC__); - s.Add_Dot(); - s.Add_UInt32(__GNUC_MINOR__); - s.Add_Dot(); - s.Add_UInt32(__GNUC_PATCHLEVEL__); - #endif - #ifdef __clang__ s += " CLANG "; s.Add_UInt32(__clang_major__); s.Add_Dot(); s.Add_UInt32(__clang_minor__); + s.Add_Dot(); + s.Add_UInt32(__clang_patchlevel__); #endif #ifdef __xlC__ @@ -985,12 +1029,67 @@ void GetCompiler(AString &s) #endif #endif + // #define __LCC__ 126 + // #define __LCC_MINOR__ 20 + // #define __MCST__ 1 + #ifdef __MCST__ + s += " MCST"; + #endif + #ifdef __LCC__ + s += " LCC "; + s.Add_UInt32(__LCC__ / 100); + s.Add_Dot(); + s.Add_UInt32(__LCC__ % 100 / 10); + s.Add_UInt32(__LCC__ % 10); + #ifdef __LCC_MINOR__ + s.Add_Dot(); + s.Add_UInt32(__LCC_MINOR__ / 10); + s.Add_UInt32(__LCC_MINOR__ % 10); + #endif + #endif + + // #define __EDG_VERSION__ 602 + #ifdef __EDG_VERSION__ + s += " EDG "; + s.Add_UInt32(__EDG_VERSION__ / 100); + s.Add_Dot(); + s.Add_UInt32(__EDG_VERSION__ % 100 / 10); + s.Add_UInt32(__EDG_VERSION__ % 10); + #endif + + #ifdef __VERSION__ + s.Add_Space(); + s += "ver:"; + s += __VERSION__; + #endif + + #ifdef __GNUC__ + s += " GCC "; + s.Add_UInt32(__GNUC__); + s.Add_Dot(); + s.Add_UInt32(__GNUC_MINOR__); + s.Add_Dot(); + s.Add_UInt32(__GNUC_PATCHLEVEL__); + #endif + + #ifdef _MSC_VER s += " MSC "; s.Add_UInt32(_MSC_VER); + #ifdef _MSC_FULL_VER + s.Add_Dot(); + s.Add_UInt32(_MSC_FULL_VER); + #endif + #endif - #if defined(__AVX2__) + #if defined(__AVX512F__) + #if defined(__AVX512VL__) + #define MY_CPU_COMPILE_ISA "AVX512VL" + #else + #define MY_CPU_COMPILE_ISA "AVX512F" + #endif + #elif defined(__AVX2__) #define MY_CPU_COMPILE_ISA "AVX2" #elif defined(__AVX__) #define MY_CPU_COMPILE_ISA "AVX" @@ -1014,9 +1113,61 @@ void GetCompiler(AString &s) #define MY_CPU_COMPILE_ISA "IA32" #endif + AString s2; #ifdef MY_CPU_COMPILE_ISA - s += ':'; - s.Add_OptSpaced(MY_CPU_COMPILE_ISA); + s2.Add_OptSpaced(MY_CPU_COMPILE_ISA); + #endif + +#ifndef MY_CPU_ARM64 + #ifdef __ARM_FP + s2.Add_OptSpaced("FP"); + #endif + #ifdef __ARM_NEON + s2.Add_OptSpaced("NEON"); #endif + #ifdef __NEON__ + s2.Add_OptSpaced("__NEON__"); + #endif + #ifdef __ARM_FEATURE_SIMD32 + s2.Add_OptSpaced("SIMD32"); + #endif +#endif + + #ifdef __ARM_FEATURE_CRYPTO + s2.Add_OptSpaced("CRYPTO"); + #endif + + #ifdef __ARM_FEATURE_SHA2 + s2.Add_OptSpaced("SHA2"); + #endif + + #ifdef __ARM_FEATURE_AES + s2.Add_OptSpaced("AES"); + #endif + + #ifdef __ARM_FEATURE_CRC32 + s2.Add_OptSpaced("CRC32"); + #endif + + #ifdef __ARM_FEATURE_UNALIGNED + s2.Add_OptSpaced("UNALIGNED"); + #endif + + + #ifdef MY_CPU_BE + s2.Add_OptSpaced("BE"); + #endif + + #if defined(MY_CPU_LE_UNALIGN) \ + && !defined(MY_CPU_X86_OR_AMD64) \ + && !defined(MY_CPU_ARM64) + s2.Add_OptSpaced("LE-unaligned"); + #endif + + if (!s2.IsEmpty()) + { + s.Add_OptSpaced(": "); + s += s2; + } } diff --git a/CPP/Windows/TimeUtils.cpp b/CPP/Windows/TimeUtils.cpp index e80ae13..bbd79ba 100644 --- a/CPP/Windows/TimeUtils.cpp +++ b/CPP/Windows/TimeUtils.cpp @@ -4,6 +4,7 @@ #ifndef _WIN32 #include +#include #endif #include "Defs.h" @@ -13,13 +14,13 @@ namespace NWindows { namespace NTime { static const UInt32 kNumTimeQuantumsInSecond = 10000000; -static const UInt32 kFileTimeStartYear = 1601; +static const unsigned kFileTimeStartYear = 1601; #if !defined(_WIN32) || defined(UNDER_CE) -static const UInt32 kDosTimeStartYear = 1980; +static const unsigned kDosTimeStartYear = 1980; #endif -static const UInt32 kUnixTimeStartYear = 1970; +static const unsigned kUnixTimeStartYear = 1970; static const UInt64 kUnixTimeOffset = - (UInt64)60 * 60 * 24 * (89 + 365 * (kUnixTimeStartYear - kFileTimeStartYear)); + (UInt64)60 * 60 * 24 * (89 + 365 * (UInt32)(kUnixTimeStartYear - kFileTimeStartYear)); static const UInt64 kNumSecondsInFileTime = (UInt64)(Int64)-1 / kNumTimeQuantumsInSecond; bool DosTime_To_FileTime(UInt32 dosTime, FILETIME &ft) throw() @@ -30,8 +31,14 @@ bool DosTime_To_FileTime(UInt32 dosTime, FILETIME &ft) throw() ft.dwLowDateTime = 0; ft.dwHighDateTime = 0; UInt64 res; - if (!GetSecondsSince1601(kDosTimeStartYear + (dosTime >> 25), (dosTime >> 21) & 0xF, (dosTime >> 16) & 0x1F, - (dosTime >> 11) & 0x1F, (dosTime >> 5) & 0x3F, (dosTime & 0x1F) * 2, res)) + if (!GetSecondsSince1601( + kDosTimeStartYear + (unsigned)(dosTime >> 25), + (unsigned)((dosTime >> 21) & 0xF), + (unsigned)((dosTime >> 16) & 0x1F), + (unsigned)((dosTime >> 11) & 0x1F), + (unsigned)((dosTime >> 5) & 0x3F), + (unsigned)((dosTime & 0x1F)) * 2, + res)) return false; res *= kNumTimeQuantumsInSecond; ft.dwLowDateTime = (UInt32)res; @@ -77,7 +84,7 @@ bool FileTime_To_DosTime(const FILETIME &ft, UInt32 &dosTime) throw() v = (UInt32)v64; - year = (unsigned)(kFileTimeStartYear + v / PERIOD_400 * 400); + year = kFileTimeStartYear + (unsigned)(v / PERIOD_400 * 400); v %= PERIOD_400; temp = (unsigned)(v / PERIOD_100); @@ -116,7 +123,13 @@ bool FileTime_To_DosTime(const FILETIME &ft, UInt32 &dosTime) throw() dosTime = kHighDosTime; if (year >= 128) return false; - dosTime = (year << 25) | (mon << 21) | (day << 16) | (hour << 11) | (min << 5) | (sec >> 1); + dosTime = + ((UInt32)year << 25) + | ((UInt32)mon << 21) + | ((UInt32)day << 16) + | ((UInt32)hour << 11) + | ((UInt32)min << 5) + | ((UInt32)sec >> 1); #endif return true; } @@ -226,20 +239,34 @@ bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day, if (year < kFileTimeStartYear || year >= 10000 || month < 1 || month > 12 || day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59) return false; - UInt32 numYears = year - kFileTimeStartYear; - UInt32 numDays = numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400; + const unsigned numYears = year - kFileTimeStartYear; + UInt32 numDays = (UInt32)((UInt32)numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400); Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ms[1] = 29; month--; for (unsigned i = 0; i < month; i++) numDays += ms[i]; - numDays += day - 1; + numDays += (UInt32)(day - 1); resSeconds = ((UInt64)(numDays * 24 + hour) * 60 + min) * 60 + sec; return true; } +/* docs: TIME_UTC is not defined on many platforms: + glibc 2.15, macOS 10.13 + FreeBSD 11.0, NetBSD 7.1, OpenBSD 6.0, + Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.3, + Cygwin 2.9, mingw, MSVC 14, Android 9.0. +*/ +#if defined(TIME_UTC) +#define ZIP7_USE_timespec_get +// #pragma message("ZIP7_USE_timespec_get") +#elif defined(CLOCK_REALTIME) +#define ZIP7_USE_clock_gettime +// #pragma message("ZIP7_USE_clock_gettime") +#endif + void GetCurUtc_FiTime(CFiTime &ft) throw() { #ifdef _WIN32 @@ -257,12 +284,33 @@ void GetCurUtc_FiTime(CFiTime &ft) throw() #else FiTime_Clear(ft); +#ifdef ZIP7_USE_timespec_get + timespec_get(&ft, TIME_UTC); +#elif defined ZIP7_USE_clock_gettime + +#if defined(_AIX) + { + timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + ft.tv_sec = ts.tv_sec; + ft.tv_nsec = ts.tv_nsec; + } +#else + clock_gettime(CLOCK_REALTIME, &ft); +#endif + +#else struct timeval now; if (gettimeofday(&now, NULL) == 0) { ft.tv_sec = now.tv_sec; - ft.tv_nsec = now.tv_usec * 1000; + // timeval::tv_usec can be 64-bit signed in some cases + // timespec::tv_nsec can be 32-bit signed in some cases + ft.tv_nsec = + (Int32) // to eliminate compiler conversion error + (now.tv_usec * 1000); } +#endif #endif } @@ -271,12 +319,26 @@ void GetCurUtc_FiTime(CFiTime &ft) throw() void GetCurUtcFileTime(FILETIME &ft) throw() { UInt64 v = 0; +#if defined(ZIP7_USE_timespec_get) || \ + defined(ZIP7_USE_clock_gettime) + timespec ts; +#if defined(ZIP7_USE_timespec_get) + if (timespec_get(&ts, TIME_UTC)) +#else + if (clock_gettime(CLOCK_REALTIME, &ts) == 0) +#endif + { + v = ((UInt64)ts.tv_sec + kUnixTimeOffset) * + kNumTimeQuantumsInSecond + (UInt64)ts.tv_nsec / 100; + } +#else struct timeval now; if (gettimeofday(&now, NULL) == 0) { v = ((UInt64)now.tv_sec + kUnixTimeOffset) * kNumTimeQuantumsInSecond + (UInt64)now.tv_usec * 10; } +#endif ft.dwLowDateTime = (DWORD)v; ft.dwHighDateTime = (DWORD)(v >> 32); } @@ -371,7 +433,7 @@ int Compare_FiTime(const CFiTime *a1, const CFiTime *a2) return 0; } -bool FILETIME_To_timespec(const FILETIME &ft, timespec &ts) +bool FILETIME_To_timespec(const FILETIME &ft, CFiTime &ts) { UInt32 quantums; const Int64 sec = NWindows::NTime::FileTime_To_UnixTime64_and_Quantums(ft, quantums); @@ -380,7 +442,7 @@ bool FILETIME_To_timespec(const FILETIME &ft, timespec &ts) if (sec2 == sec) { ts.tv_sec = sec2; - ts.tv_nsec = (long)(quantums * 100); + ts.tv_nsec = (Int32)(quantums * 100); return true; } return false; -- cgit v1.2.3-55-g6feb