fork download
  1. #define UNICODE /* for TCHAR, ::MessageBox() -> ::MessageBoxW() */
  2.  
  3. #include <windows.h>
  4. #include <iostream>
  5. #include <stack>
  6. #include <cassert>
  7.  
  8. static_assert(sizeof(TCHAR) == sizeof(char16_t));
  9. using stringU16 = std::basic_string<char16_t>;
  10.  
  11. std::ostream &operator<<(std::ostream &os, stringU16 pUnicode) {
  12. int len = ::WideCharToMultiByte(CP_THREAD_ACP, 0, (TCHAR *)pUnicode.c_str(), -1, 0, 0, 0, 0);
  13. char *pAnsi = new char [len + 2];
  14. if (pAnsi) {
  15. int actualLen = ::WideCharToMultiByte(CP_THREAD_ACP, 0, (TCHAR *)pUnicode.c_str(), -1, pAnsi, len + 2, 0, 0);
  16. if (actualLen == len) {
  17. os << pAnsi; /* output */
  18. }
  19. delete pAnsi;
  20. }
  21. return os;
  22. }
  23.  
  24. int const BuffSize = 65536;
  25. bool MyCopyFile(stringU16 src, stringU16 dest) {
  26. static char Buff[BuffSize];
  27. DWORD ActualReadBytes, ActualWriteBytes;
  28. HANDLE hFileSrc, hFileDest;
  29.  
  30. hFileSrc = ::CreateFile((TCHAR *)src.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);
  31. if (hFileSrc == INVALID_HANDLE_VALUE) {
  32. std::cerr << "Cannot open(read) :" << src << std::endl;
  33. return false;
  34. }
  35. FILETIME ftSrcCreate, ftSrcLastAccess, ftSrcLastWrite;
  36. GetFileTime(hFileSrc, &ftSrcCreate, &ftSrcLastAccess, &ftSrcLastWrite);
  37.  
  38. hFileDest = ::CreateFile((TCHAR *)dest.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);
  39. if (hFileSrc == INVALID_HANDLE_VALUE) {
  40. std::cerr << "Cannot open(write) :" << dest << std::endl;
  41. ::CloseHandle(hFileSrc);
  42. return false;
  43. }
  44. SetFileTime(hFileDest, &ftSrcCreate, &ftSrcLastAccess, &ftSrcLastWrite);
  45.  
  46. int nReadTotalBytes = 0;
  47. int nWriteTotalBytes = 0;
  48.  
  49. for (;;) {
  50. if (!::ReadFile(hFileSrc, Buff, BuffSize, &ActualReadBytes, 0)) {
  51. std::cerr << "Opend but cannot read: " << src << " : " << nReadTotalBytes << "/" << nWriteTotalBytes << std::endl;
  52. ::CloseHandle(hFileSrc);
  53. ::CloseHandle(hFileDest);
  54. return false;
  55. }
  56. nReadTotalBytes += ActualReadBytes;
  57.  
  58. /* out of loop */
  59. if (ActualReadBytes == 0)
  60. break;
  61.  
  62. if(!::WriteFile(hFileDest, Buff, ActualReadBytes, &ActualWriteBytes, 0)) {
  63. std::cerr << "Opend and read but cannot write: " << dest << " : " << nReadTotalBytes << "/" << nWriteTotalBytes << std::endl;
  64. ::CloseHandle(hFileSrc);
  65. ::CloseHandle(hFileDest);
  66. return false;
  67. }
  68. nWriteTotalBytes += ActualWriteBytes;
  69. }
  70. std::cout << nReadTotalBytes << "/" << nWriteTotalBytes << " ";
  71. ::CloseHandle(hFileSrc);
  72. ::CloseHandle(hFileDest);
  73. return true;
  74. }
  75.  
  76. struct PathPair {
  77. stringU16 src;
  78. stringU16 dest;
  79. PathPair(stringU16 src, stringU16 dest) : src(src), dest(dest) { }
  80. };
  81.  
  82. std::stack<PathPair> path_stack;
  83. std::stack<PathPair> path_created_directory;
  84.  
  85. void copy_body(stringU16 original_dest) {
  86. while (!path_stack.empty()) {
  87. PathPair path_pair = path_stack.top(); path_stack.pop();
  88. stringU16 now_path = path_pair.src;
  89. stringU16 now_dest_path = path_pair.dest;
  90.  
  91. if (now_path.find_last_of(stringU16(u"\\")) != now_path.length() - 1) now_path += u"\\"; /* '/' で終わらない場合に '/' を追加 */
  92. if (now_dest_path.find_last_of(stringU16(u"\\")) != now_dest_path.length() - 1) now_dest_path += u"\\"; /* '/' で終わらない場合に '/' を追加 */
  93. stringU16 now_path_findfirst = now_path + u"*.*";
  94.  
  95. WIN32_FIND_DATA FindFileData;
  96.  
  97. HANDLE hFindFirstNext = ::FindFirstFile((TCHAR *)now_path_findfirst.c_str(), &FindFileData);
  98. bool bInLoop = (hFindFirstNext != INVALID_HANDLE_VALUE);
  99.  
  100. while (bInLoop) {
  101. stringU16 now_path_filename;
  102. stringU16 now_dest_path_filename;
  103. stringU16 filename = stringU16((char16_t *)FindFileData.cFileName);
  104.  
  105. if (filename == u"." || filename == u"..") goto FINDNEXTFILE;
  106.  
  107. now_path_filename = now_path + filename;
  108. now_dest_path_filename = now_dest_path + filename;
  109.  
  110. /* prevent recursively-endless-loop */
  111. if (now_path_filename == original_dest)
  112. goto FINDNEXTFILE;
  113.  
  114. if (::GetFileAttributes((TCHAR *)now_path_filename.c_str()) & FILE_ATTRIBUTE_DIRECTORY) { /* when now_path_filename is a directory */
  115. if ((signed int)::GetFileAttributes((TCHAR *)now_dest_path_filename.c_str()) == -1) {
  116. if(::CreateDirectory((TCHAR *)now_dest_path_filename.c_str(), 0)) {
  117. std::cout << "Create directory: " << now_dest_path_filename << std::endl;
  118. path_created_directory.push(PathPair(now_path_filename, now_dest_path_filename));
  119. } else {
  120. std::cerr << "Failure to create directory: " << now_dest_path_filename << std::endl;
  121. break;
  122. }
  123. }
  124. path_stack.push(PathPair(now_path_filename, now_dest_path_filename));
  125.  
  126. } else { /* when now_path_filename is a file */
  127. /* when (now_path_filename(Src)'s time) > (now_dest_path_filename(Dest)'s time), copy */
  128. HANDLE hSrc = ::CreateFile((TCHAR *)now_path_filename.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  129. HANDLE hDest = ::CreateFile((TCHAR *)now_dest_path_filename.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  130. FILETIME ftSrc = {0, 0}, ftDest = {0, 0};
  131. ::GetFileTime(hSrc, 0, 0, &ftSrc);
  132. ::GetFileTime(hDest, 0, 0, &ftDest);
  133. uint64_t timeSrc = ftSrc.dwHighDateTime; timeSrc <<= 32, timeSrc |= ftSrc.dwLowDateTime; /* Src's time */
  134. uint64_t timeDest = ftDest.dwHighDateTime; timeDest <<= 32, timeDest |= ftDest.dwLowDateTime; /* Dest's time */
  135. ::CloseHandle(hSrc); ::CloseHandle(hDest);
  136.  
  137. assert(hDest != INVALID_HANDLE_VALUE || timeDest == 0); /* important assumption */
  138.  
  139. if (timeSrc > timeDest) {
  140. std::cout << now_path_filename << " -> " << now_dest_path_filename << " ";
  141. if (MyCopyFile(now_path_filename, now_dest_path_filename)) {
  142. std::cout << " : OK" << std::endl;
  143. }
  144. }
  145. }
  146. FINDNEXTFILE:
  147. bInLoop = ::FindNextFile(hFindFirstNext, &FindFileData);
  148.  
  149. } /* while(bInLoop) */
  150. if (hFindFirstNext != INVALID_HANDLE_VALUE)
  151. ::FindClose(hFindFirstNext);
  152. } /* while(!path_stack.empty()) */
  153. }
  154.  
  155. void setWideChar_fromAnsi(stringU16 &unicodeString, const char *ansiString) {
  156. int len = ::MultiByteToWideChar(CP_THREAD_ACP, 0, ansiString, -1, 0, 0);
  157. char16_t *pUnicode = new char16_t [len + 2];
  158. if (!pUnicode) { unicodeString = stringU16(u""); delete pUnicode; return; }
  159. int actualLen = ::MultiByteToWideChar(CP_THREAD_ACP, 0, ansiString, -1, (TCHAR *)pUnicode, len + 2);
  160. if (actualLen != len) { unicodeString = stringU16(u""); delete pUnicode; return; }
  161. unicodeString = stringU16(pUnicode);
  162. delete pUnicode;
  163. }
  164.  
  165. int main(int argc, char *argv[]) {
  166.  
  167. if (argc != 3) {
  168. std::cerr << argv[0] << " : copy files and directory recursively." << std::endl;
  169. std::cerr << "usage: " << argv[0] << " <src-path> <dest-path> " << std::endl;
  170. return 0;
  171. }
  172.  
  173. int const n = 256;
  174. char16_t buff[n];
  175. GetCurrentDirectory(n, (TCHAR *)buff);
  176. stringU16 current_directory(buff);
  177.  
  178. stringU16 src; setWideChar_fromAnsi(src, argv[1]);
  179. stringU16 dest; setWideChar_fromAnsi(dest, argv[2]);
  180.  
  181. if (dest == u".") {
  182. std::cerr << argv[0] << ": cannot specify current directory for the destination path" << std::endl;
  183. return 0;
  184. }
  185. if (src == u".") {
  186. src.clear();
  187. }
  188. if (src == u".." || dest == u".." ) {
  189. std::cerr << argv[0] << ": cannot use the parent directory's expression \"..\". " << std::endl;
  190. return 0;
  191. }
  192. /* translate to abusolute path */
  193. if (src.find(u":") == stringU16::npos) { /* src is relative path */
  194. if (src.empty()) {
  195. src = current_directory;
  196. } else {
  197. src = current_directory + u"\\" + src;
  198. }
  199. }
  200. if (dest.find(u":") == stringU16::npos) { /* src is relative path */
  201. dest = current_directory + u"\\" + dest;
  202. }
  203. /* cf. https://docs.microsoft.com/ja-jp/windows/desktop/FileIO/naming-a-file */
  204. src = u"\\\\?\\" + src;
  205. dest = u"\\\\?\\" + dest;
  206.  
  207. int attributes = ::GetFileAttributes((TCHAR *)dest.c_str());
  208. if (attributes == -1) {
  209. std::cerr << "Directory: " << dest << " is not exists, aborted." << std::endl;
  210. return 0;
  211. }
  212.  
  213. path_stack.push(PathPair(src, dest));
  214. copy_body(dest);
  215.  
  216. /* set created directory(= dest)'s timestamp as src's directory is */
  217. /* 新規に作ったディレクトリのファイルスタンプはプログラムの最後で、最後に設定するしかない */
  218.  
  219. while (!path_created_directory.empty()) {
  220. PathPair path_created = path_created_directory.top(); path_created_directory.pop();
  221. stringU16 src_directory = path_created.src;
  222. stringU16 dest_directory = path_created.dest;
  223.  
  224. HANDLE hSrc = ::CreateFile((TCHAR *)src_directory.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
  225. HANDLE hDest = ::CreateFile((TCHAR *)dest_directory.c_str(), GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
  226.  
  227. FILETIME ftSrcCreate, ftSrcLastAccess, ftSrcLastWrite;
  228. ::GetFileTime(hSrc, &ftSrcCreate, &ftSrcLastAccess, &ftSrcLastWrite);
  229. ::SetFileTime(hDest, &ftSrcCreate, &ftSrcLastAccess, &ftSrcLastWrite);
  230. ::CloseHandle(hSrc); ::CloseHandle(hDest);
  231. }
  232. return 0;
  233. }
  234. /* end */
  235.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:21: fatal error: windows.h: No such file or directory
 #include <windows.h>
                     ^
compilation terminated.
stdout
Standard output is empty