Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.0 KiB

  1. #include "windows.h"
  2. #include <stdio.h>
  3. BOOL
  4. ForceCopy(
  5. LPCSTR lpszSourceFileName,
  6. LPCSTR lpszDestFileName
  7. )
  8. /*++
  9. Params: lpszSourceFileName Pointer to the source file.
  10. lpzDestFileName Pointer to the destination file.
  11. Return: TRUE on success, FALSE otherwise.
  12. Desc: Attempts to copy a file. If it's in use, move it and replace on reboot.
  13. --*/
  14. {
  15. char szTempPath[MAX_PATH];
  16. char szDelFileName[MAX_PATH];
  17. if (!CopyFileA(lpszSourceFileName, lpszDestFileName, FALSE)) {
  18. if (GetTempPathA(MAX_PATH, szTempPath) == 0) {
  19. printf("GetTempPath failed with 0x%x\n", GetLastError());
  20. return FALSE;
  21. }
  22. if (GetTempFileNameA(szTempPath, "DEL", 0, szDelFileName) == 0) {
  23. printf("GetTempFileName failed with 0x%x\n", GetLastError());
  24. return FALSE;
  25. }
  26. if (!MoveFileExA(lpszDestFileName, szDelFileName, MOVEFILE_REPLACE_EXISTING)) {
  27. printf("MoveFileEx failed with 0x%x\n", GetLastError());
  28. return FALSE;
  29. }
  30. if (!MoveFileExA(szDelFileName, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)) {
  31. printf("MoveFileEx failed with 0x%x\n", GetLastError());
  32. return FALSE;
  33. }
  34. if (!CopyFileA(lpszSourceFileName, lpszDestFileName, FALSE)) {
  35. printf("CopyFile failed with 0x%x\n", GetLastError());
  36. return FALSE;
  37. }
  38. }
  39. return TRUE;
  40. }
  41. int __cdecl
  42. main(
  43. int argc,
  44. CHAR* argv[]
  45. )
  46. {
  47. int cchLen;
  48. if (argc != 3) {
  49. printf("Usage: fcopy SourceFile DestFile\n");
  50. return 0;
  51. }
  52. cchLen = lstrlen(argv[1]);
  53. if (cchLen > MAX_PATH) {
  54. printf("Source file path too long\n");
  55. return 0;
  56. }
  57. cchLen = lstrlen(argv[2]);
  58. if (cchLen > MAX_PATH) {
  59. printf("Dest file path too long\n");
  60. return 0;
  61. }
  62. return ForceCopy(argv[1], argv[2]);
  63. }