1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #if defined(_WIN32) || defined(__WIN32__)
#include <io.h> #include <direct.h> #include <Windows.h> #include <stdlib.h> #include "strutil.h" #include "filesystem.h" #include "logger.h"
namespace cutl { bool copy_attributes(const std::string &srcpath, const std::string &dstpath, bool isdir) { FILETIME t_create, t_access, t_write; HANDLE h_src = NULL; if (isdir) { h_src = CreateFile( srcpath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); } else { h_src = CreateFileA( srcpath.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL); }
if (h_src == INVALID_HANDLE_VALUE) { CUTL_ERROR("Failed to open file " + srcpath + ", error code: " + std::to_string(GetLastError())); CloseHandle(h_src); return false; } if (!GetFileTime(h_src, &t_create, &t_access, &t_write)) { CUTL_ERROR("Failed to get file times for " + srcpath + ", error code: " + std::to_string(GetLastError())); CloseHandle(h_src); return false; } CloseHandle(h_src);
HANDLE h_dst = NULL; if (isdir) { h_dst = CreateFile( dstpath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); } else { h_dst = ::CreateFileA( dstpath.c_str(), GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); }
if (h_dst == INVALID_HANDLE_VALUE) { CUTL_ERROR("Failed to open file " + dstpath + ", error code: " + std::to_string(GetLastError())); CloseHandle(h_dst); return false; } if (!SetFileTime(h_dst, &t_create, &t_access, &t_write)) { CUTL_ERROR("Failed to set file times for " + dstpath + ", error code: " + std::to_string(GetLastError())); CloseHandle(h_dst); return false; } CloseHandle(h_dst);
DWORD attributes = GetFileAttributesA(srcpath.c_str()); if (attributes == INVALID_FILE_ATTRIBUTES) { CUTL_ERROR("Failed to get file attributes for " + srcpath + ", error code: " + std::to_string(GetLastError())); return false; } if (!SetFileAttributesA(dstpath.c_str(), attributes)) { CUTL_ERROR("Failed to set file attributes for " + dstpath + ", error code: " + std::to_string(GetLastError())); return false; } return true; } }
#endif
|