1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2007,2010-2011 - TortoiseSVN
4 // Copyright (C) 2008-2011 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "CacheInterface.h"
22 #include "SmartHandle.h"
24 CString
GetCachePipeName()
26 return TGIT_CACHE_PIPE_NAME
+ GetCacheID();
29 CString
GetCacheCommandPipeName()
31 return TGIT_CACHE_COMMANDPIPE_NAME
+ GetCacheID();
34 CString
GetCacheMutexName()
36 return TGIT_CACHE_MUTEX_NAME
+ GetCacheID();
40 CAutoGeneralHandle token
;
42 BOOL result
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, token
.GetPointer());
45 GetTokenInformation(token
, TokenStatistics
, NULL
, 0, &len
);
46 LPBYTE data
= new BYTE
[len
];
47 GetTokenInformation(token
, TokenStatistics
, data
, len
, &len
);
48 LUID uid
= ((PTOKEN_STATISTICS
)data
)->AuthenticationId
;
51 t
.Format(_T("-%08x%08x"), uid
.HighPart
, uid
.LowPart
);
57 bool SendCacheCommand(BYTE command
, const WCHAR
* path
/* = NULL */)
64 GetCacheCommandPipeName(), // pipe name
65 GENERIC_READ
| // read and write access
68 NULL
, // default security attributes
69 OPEN_EXISTING
, // opens existing pipe
70 FILE_FLAG_OVERLAPPED
, // default attributes
71 NULL
); // no template file
75 } while ((!hPipe
) && (retrycount
));
79 //CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Could not connect to pipe\n");
83 // The pipe connected; change to message-read mode.
84 DWORD dwMode
= PIPE_READMODE_MESSAGE
;
85 if (SetNamedPipeHandleState(
87 &dwMode
, // new pipe mode
88 NULL
, // don't set maximum bytes
89 NULL
)) // don't set maximum time
93 SecureZeroMemory(&cmd
, sizeof(TGITCacheCommand
));
94 cmd
.command
= command
;
96 _tcsncpy_s(cmd
.path
, path
, _TRUNCATE
);
99 BOOL fSuccess
= FALSE
;
102 fSuccess
= WriteFile(
103 hPipe
, // handle to pipe
104 &cmd
, // buffer to write from
105 sizeof(cmd
), // number of bytes to write
106 &cbWritten
, // number of bytes written
107 NULL
); // not overlapped I/O
109 if (! fSuccess
|| sizeof(cmd
) != cbWritten
)
111 } while ((retrycount
) && (! fSuccess
|| sizeof(cmd
) != cbWritten
));
113 if (! fSuccess
|| sizeof(cmd
) != cbWritten
)
115 //CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Could not write to pipe\n");
116 DisconnectNamedPipe(hPipe
);
119 // now tell the cache we don't need it's command thread anymore
120 SecureZeroMemory(&cmd
, sizeof(TGITCacheCommand
));
121 cmd
.command
= TGITCACHECOMMAND_END
;
123 hPipe
, // handle to pipe
124 &cmd
, // buffer to write from
125 sizeof(cmd
), // number of bytes to write
126 &cbWritten
, // number of bytes written
127 NULL
); // not overlapped I/O
128 DisconnectNamedPipe(hPipe
);
132 //CTraceToOutputDebugString::Instance()(__FUNCTION__ ": SetNamedPipeHandleState failed");
139 CBlockCacheForPath::CBlockCacheForPath(const WCHAR
* aPath
)
141 wcsncpy_s(path
, aPath
, MAX_PATH
);
144 SendCacheCommand (TGITCACHECOMMAND_BLOCK
, path
);
147 CBlockCacheForPath::~CBlockCacheForPath()
149 SendCacheCommand (TGITCACHECOMMAND_UNBLOCK
, path
);