2 #include "git-compat-util.h"
4 #if defined(GIT_WINDOWS_NATIVE)
7 static int cmd_sync(void)
11 char szVolumeAccessPath[] = "\\\\.\\X:";
15 dwRet = GetCurrentDirectory(MAX_PATH, Buffer);
16 if ((0 == dwRet) || (dwRet > MAX_PATH))
17 return error("Error getting current directory");
19 if (!has_dos_drive_prefix(Buffer))
20 return error("'%s': invalid drive letter", Buffer);
22 szVolumeAccessPath[4] = Buffer[0];
23 hVolWrite = CreateFile(szVolumeAccessPath, GENERIC_READ | GENERIC_WRITE,
24 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
25 if (INVALID_HANDLE_VALUE == hVolWrite)
26 return error("Unable to open volume for writing, need admin access");
28 success = FlushFileBuffers(hVolWrite);
30 error("Unable to flush volume");
32 CloseHandle(hVolWrite);
37 #define STATUS_SUCCESS (0x00000000L)
38 #define STATUS_PRIVILEGE_NOT_HELD (0xC0000061L)
40 typedef enum _SYSTEM_INFORMATION_CLASS {
41 SystemMemoryListInformation = 80,
42 } SYSTEM_INFORMATION_CLASS;
44 typedef enum _SYSTEM_MEMORY_LIST_COMMAND {
45 MemoryCaptureAccessedBits,
46 MemoryCaptureAndResetAccessedBits,
47 MemoryEmptyWorkingSets,
48 MemoryFlushModifiedList,
49 MemoryPurgeStandbyList,
50 MemoryPurgeLowPriorityStandbyList,
52 } SYSTEM_MEMORY_LIST_COMMAND;
54 static BOOL GetPrivilege(HANDLE TokenHandle, LPCSTR lpName, int flags)
59 TOKEN_PRIVILEGES tpPreviousState;
60 TOKEN_PRIVILEGES tpNewState;
63 bResult = LookupPrivilegeValueA(0, lpName, &luid);
65 tpNewState.PrivilegeCount = 1;
66 tpNewState.Privileges[0].Luid = luid;
67 tpNewState.Privileges[0].Attributes = 0;
68 bResult = AdjustTokenPrivileges(TokenHandle, 0, &tpNewState,
69 (DWORD)((LPBYTE)&(tpNewState.Privileges[1]) - (LPBYTE)&tpNewState),
70 &tpPreviousState, &dwBufferLength);
72 tpPreviousState.PrivilegeCount = 1;
73 tpPreviousState.Privileges[0].Luid = luid;
74 tpPreviousState.Privileges[0].Attributes = flags != 0 ? 2 : 0;
75 bResult = AdjustTokenPrivileges(TokenHandle, 0, &tpPreviousState,
76 dwBufferLength, 0, 0);
82 static int cmd_dropcaches(void)
84 HANDLE hProcess = GetCurrentProcess();
86 DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
87 SYSTEM_MEMORY_LIST_COMMAND command;
90 if (!OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))
91 return error("Can't open current process token");
93 if (!GetPrivilege(hToken, "SeProfileSingleProcessPrivilege", 1))
94 return error("Can't get SeProfileSingleProcessPrivilege");
98 if (!INIT_PROC_ADDR(NtSetSystemInformation))
99 return error("Could not find NtSetSystemInformation() function");
101 command = MemoryPurgeStandbyList;
102 status = NtSetSystemInformation(
103 SystemMemoryListInformation,
105 sizeof(SYSTEM_MEMORY_LIST_COMMAND)
107 if (status == STATUS_PRIVILEGE_NOT_HELD)
108 error("Insufficient privileges to purge the standby list, need admin access");
109 else if (status != STATUS_SUCCESS)
110 error("Unable to execute the memory list command %d", status);
115 #elif defined(__linux__)
117 static int cmd_sync(void)
119 return system("sync");
122 static int cmd_dropcaches(void)
124 return system("echo 3 | sudo tee /proc/sys/vm/drop_caches");
127 #elif defined(__APPLE__)
129 static int cmd_sync(void)
131 return system("sync");
134 static int cmd_dropcaches(void)
136 return system("sudo purge");
141 static int cmd_sync(void)
146 static int cmd_dropcaches(void)
148 return error("drop caches not implemented on this platform");
153 int cmd__drop_caches(int argc, const char **argv)
156 return cmd_dropcaches();