Use libgit2 git_reference_is_valid_name() to validate branch name
[TortoiseGit.git] / src / TGitCache / CacheInterface.cpp
blobc8d8ccb0e093a8895ded22d26a3516f4c1971e76
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.
20 #include "stdafx.h"
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();
38 CString GetCacheID()
40 CAutoGeneralHandle token;
41 DWORD len;
42 BOOL result = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token.GetPointer());
43 if(result)
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;
49 delete [ ] data;
50 CString t;
51 t.Format(_T("-%08x%08x"), uid.HighPart, uid.LowPart);
52 return t;
54 return _T("");
57 bool SendCacheCommand(BYTE command, const WCHAR * path /* = NULL */)
59 int retrycount = 2;
60 CAutoFile hPipe;
63 hPipe = CreateFile(
64 GetCacheCommandPipeName(), // pipe name
65 GENERIC_READ | // read and write access
66 GENERIC_WRITE,
67 0, // no sharing
68 NULL, // default security attributes
69 OPEN_EXISTING, // opens existing pipe
70 FILE_FLAG_OVERLAPPED, // default attributes
71 NULL); // no template file
72 retrycount--;
73 if (!hPipe)
74 Sleep(10);
75 } while ((!hPipe) && (retrycount));
77 if (!hPipe)
79 //CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Could not connect to pipe\n");
80 return false;
83 // The pipe connected; change to message-read mode.
84 DWORD dwMode = PIPE_READMODE_MESSAGE;
85 if (SetNamedPipeHandleState(
86 hPipe, // pipe handle
87 &dwMode, // new pipe mode
88 NULL, // don't set maximum bytes
89 NULL)) // don't set maximum time
91 DWORD cbWritten;
92 TGITCacheCommand cmd;
93 SecureZeroMemory(&cmd, sizeof(TGITCacheCommand));
94 cmd.command = command;
95 if (path)
96 _tcsncpy_s(cmd.path, path, _TRUNCATE);
98 retrycount = 2;
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
108 retrycount--;
109 if (! fSuccess || sizeof(cmd) != cbWritten)
110 Sleep(10);
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);
117 return false;
119 // now tell the cache we don't need it's command thread anymore
120 SecureZeroMemory(&cmd, sizeof(TGITCacheCommand));
121 cmd.command = TGITCACHECOMMAND_END;
122 WriteFile(
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);
130 else
132 //CTraceToOutputDebugString::Instance()(__FUNCTION__ ": SetNamedPipeHandleState failed");
133 return false;
136 return true;
139 CBlockCacheForPath::CBlockCacheForPath(const WCHAR * aPath)
141 wcsncpy_s(path, aPath, MAX_PATH);
142 path[MAX_PATH] = 0;
144 SendCacheCommand (TGITCACHECOMMAND_BLOCK, path);
147 CBlockCacheForPath::~CBlockCacheForPath()
149 SendCacheCommand (TGITCACHECOMMAND_UNBLOCK, path);