1 #include "git-compat-util.h"
3 #if defined(GIT_WINDOWS_NATIVE)
5 static int cmd_sync(void)
9 char szVolumeAccessPath[] = "\\\\.\\X:";
13 dwRet = GetCurrentDirectory(MAX_PATH, Buffer);
14 if ((0 == dwRet) || (dwRet > MAX_PATH))
15 return error("Error getting current directory");
17 if ((Buffer[0] < 'A') || (Buffer[0] > 'Z'))
18 return error("Invalid drive letter '%c'", Buffer[0]);
20 szVolumeAccessPath[4] = Buffer[0];
21 hVolWrite = CreateFile(szVolumeAccessPath, GENERIC_READ | GENERIC_WRITE,
22 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
23 if (INVALID_HANDLE_VALUE == hVolWrite)
24 return error("Unable to open volume for writing, need admin access");
26 success = FlushFileBuffers(hVolWrite);
28 error("Unable to flush volume");
30 CloseHandle(hVolWrite);
35 #define STATUS_SUCCESS (0x00000000L)
36 #define STATUS_PRIVILEGE_NOT_HELD (0xC0000061L)
38 typedef enum _SYSTEM_INFORMATION_CLASS {
39 SystemMemoryListInformation = 80,
40 } SYSTEM_INFORMATION_CLASS;
42 typedef enum _SYSTEM_MEMORY_LIST_COMMAND {
43 MemoryCaptureAccessedBits,
44 MemoryCaptureAndResetAccessedBits,
45 MemoryEmptyWorkingSets,
46 MemoryFlushModifiedList,
47 MemoryPurgeStandbyList,
48 MemoryPurgeLowPriorityStandbyList,
50 } SYSTEM_MEMORY_LIST_COMMAND;
52 static BOOL GetPrivilege(HANDLE TokenHandle, LPCSTR lpName, int flags)
57 TOKEN_PRIVILEGES tpPreviousState;
58 TOKEN_PRIVILEGES tpNewState;
61 bResult = LookupPrivilegeValueA(0, lpName, &luid);
63 tpNewState.PrivilegeCount = 1;
64 tpNewState.Privileges[0].Luid = luid;
65 tpNewState.Privileges[0].Attributes = 0;
66 bResult = AdjustTokenPrivileges(TokenHandle, 0, &tpNewState,
67 (DWORD)((LPBYTE)&(tpNewState.Privileges[1]) - (LPBYTE)&tpNewState),
68 &tpPreviousState, &dwBufferLength);
70 tpPreviousState.PrivilegeCount = 1;
71 tpPreviousState.Privileges[0].Luid = luid;
72 tpPreviousState.Privileges[0].Attributes = flags != 0 ? 2 : 0;
73 bResult = AdjustTokenPrivileges(TokenHandle, 0, &tpPreviousState,
74 dwBufferLength, 0, 0);
80 static int cmd_dropcaches(void)
82 HANDLE hProcess = GetCurrentProcess();
85 DWORD(WINAPI *NtSetSystemInformation)(INT, PVOID, ULONG);
86 SYSTEM_MEMORY_LIST_COMMAND command;
89 if (!OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))
90 return error("Can't open current process token");
92 if (!GetPrivilege(hToken, "SeProfileSingleProcessPrivilege", 1))
93 return error("Can't get SeProfileSingleProcessPrivilege");
97 ntdll = LoadLibrary("ntdll.dll");
99 return error("Can't load ntdll.dll, wrong Windows version?");
101 NtSetSystemInformation =
102 (DWORD(WINAPI *)(INT, PVOID, ULONG))GetProcAddress(ntdll, "NtSetSystemInformation");
103 if (!NtSetSystemInformation)
104 return error("Can't get function addresses, wrong Windows version?");
106 command = MemoryPurgeStandbyList;
107 status = NtSetSystemInformation(
108 SystemMemoryListInformation,
110 sizeof(SYSTEM_MEMORY_LIST_COMMAND)
112 if (status == STATUS_PRIVILEGE_NOT_HELD)
113 error("Insufficient privileges to purge the standby list, need admin access");
114 else if (status != STATUS_SUCCESS)
115 error("Unable to execute the memory list command %d", status);
122 #elif defined(__linux__)
124 static int cmd_sync(void)
126 return system("sync");
129 static int cmd_dropcaches(void)
131 return system("echo 3 | sudo tee /proc/sys/vm/drop_caches");
134 #elif defined(__APPLE__)
136 static int cmd_sync(void)
138 return system("sync");
141 static int cmd_dropcaches(void)
143 return system("sudo purge");
148 static int cmd_sync(void)
153 static int cmd_dropcaches(void)
155 return error("drop caches not implemented on this platform");
160 int cmd_main(int argc, const char **argv)
163 return cmd_dropcaches();