Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

shellext: return to tmpfile based passing method

Changeset b0b707b12891

Parent d82692de648b

by Steve Borho

Changes to 3 files · Browse files at b0b707b12891 Showing diff from parent d82692de648b Diff from another changeset...

 
18
19
20
21
22
23
24
 
26
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
 
 
 
 
 
33
 
 
34
35
36
 
38
39
40
41
42
43
44
45
46
47
 
48
49
50
 
18
19
20
 
21
22
23
 
25
26
27
 
28
29
30
31
32
33
34
35
36
37
38
39
40
 
 
41
42
43
44
45
46
47
48
49
50
51
 
53
54
55
 
 
 
 
 
 
 
56
57
58
59
@@ -18,7 +18,6 @@
  std::string hgcmd = Quote(dir + "\\hgtk.exe") + " " + cmd;     std::string cwd; - std::string filelist;   if (!myFolder.empty())   {   cwd = myFolder; @@ -26,11 +25,27 @@
  else if (!myFiles.empty())   {   cwd = IsDirectory(myFiles[0])? myFiles[0] : DirName(myFiles[0]); - for( DWORD i = 0 ; i < myFiles.size() ; i++ ) + + std::string tempfile = GetTemporaryFile(); + SECURITY_ATTRIBUTES sa; + memset(&sa, 0, sizeof(sa)); + sa.nLength = sizeof(sa); + sa.bInheritHandle = TRUE; + + TDEBUG_TRACE("DoHgProc: temp file = " << tempfile); + HANDLE tempfileHandle = CreateFileA(tempfile.c_str(), GENERIC_WRITE, + FILE_SHARE_READ, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + + for (int i=0; i<myFiles.size(); i++)   { - filelist += myFiles[i]; - filelist += "\n"; + DWORD dwWritten; + TDEBUG_TRACE("DoHgProc: temp file adding " << myFiles[i]); + WriteFile(tempfileHandle, myFiles[i].c_str(), + static_cast<DWORD>(myFiles[i].size()), &dwWritten, 0); + WriteFile(tempfileHandle, "\n", 1, &dwWritten, 0);   } + CloseHandle(tempfileHandle); + hgcmd += " --listfile " + Quote(tempfile);   }   else   { @@ -38,13 +53,7 @@
  return;   }   - if ( !filelist.empty() ) - { - TDEBUG_TRACE("filelist: " << filelist); - hgcmd += " --listfile -"; - } - - LaunchCommand(hgcmd, cwd, filelist); + LaunchCommand(hgcmd, cwd);  }    STDMETHODIMP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
94
95
96
97
98
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
101
 
 
 
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 
 
 
 
 
 
 
 
 
 
 
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
 #include "stdafx.h"  #include "ShellExt.h"  #include "TortoiseUtils.h"  #include "errno.h"  #include <assert.h>    int WideCharToLocal(LPTSTR pLocal, LPWSTR pWide, DWORD dwChars)  {   *pLocal = 0;     #ifdef UNICODE   lstrcpyn(pLocal, pWide, dwChars);   #else   WideCharToMultiByte( CP_ACP,   0,   pWide,   -1,   pLocal,   dwChars,   NULL,   NULL);   #endif     return lstrlen(pLocal);  }    int LocalToWideChar(LPWSTR pWide, LPTSTR pLocal, DWORD dwChars)  {   *pWide = 0;     #ifdef UNICODE   lstrcpyn(pWide, pLocal, dwChars);   #else   MultiByteToWideChar( CP_ACP,   0,   pLocal,   -1,   pWide,   dwChars);   #endif     return lstrlenW(pWide);  }    LPWSTR hf_mbtowc(LPWSTR lpw, LPCSTR lpa, int nChars)  {   assert(lpa != NULL);   assert(lpw != NULL);     lpw[0] = '\0';   MultiByteToWideChar(CP_ACP, 0, lpa, -1, lpw, nChars);   return lpw;  }    LPSTR hf_wctomb(LPSTR lpa, LPCWSTR lpw, int nChars)  {   assert(lpw != NULL);   assert(lpa != NULL);     lpa[0] = '\0';   WideCharToMultiByte(CP_ACP, 0, lpw, -1, lpa, nChars, NULL, NULL);   return lpa;  }    std::string GetTHgShellRoot()  {   LPCSTR regname = "Software\\TortoiseHgShell";   HKEY key = HKEY_LOCAL_MACHINE;   TCHAR lpszValue[MAX_PATH] = "";   LONG lpcbLonger = MAX_PATH * sizeof(TCHAR);     RegQueryValue(key, regname, lpszValue, &lpcbLonger);   std::string result(reinterpret_cast<char*>(lpszValue));   return result;  }      std::string GetTHgProgRoot()  {   LPCSTR regname = "Software\\TortoiseHg";   HKEY key = HKEY_LOCAL_MACHINE;   TCHAR lpszValue[MAX_PATH] = "";   LONG lpcbLonger = MAX_PATH * sizeof(TCHAR);     RegQueryValue(key, regname, lpszValue, &lpcbLonger);   std::string result(reinterpret_cast<char*>(lpszValue));   return result;  }    // Start an external command  // Note: if the command is a batch file and the [full] path to the  // batch contains spaces, the path must be double-quoted.  // (see http://www.encocoservices.com/createprocess.html) -bool LaunchCommand(const std::string& command, const std::string& cwd, const std::string& filelist) +bool LaunchCommand(const std::string& command, const std::string& cwd)  {   TDEBUG_TRACE("LaunchCommand: " << command);   PROCESS_INFORMATION processInfo;   memset(&processInfo, 0, sizeof(processInfo));   - HANDLE hChildStd_IN_Rd = NULL; - HANDLE hChildStd_IN_Wr = NULL; - - SECURITY_ATTRIBUTES saAttr; - // Set the bInheritHandle flag so pipe handles are inherited. - saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); - saAttr.bInheritHandle = TRUE; - saAttr.lpSecurityDescriptor = NULL; - - // Create a pipe for the child process's STDIN. - if (!CreatePipe(&hChildStd_IN_Rd, &hChildStd_IN_Wr, &saAttr, 0)) - { - TDEBUG_TRACE("LaunchCommand: unable to create stdin pipe"); - return false; - } - - // Ensure the write handle to the pipe for STDIN is not inherited. - if (!SetHandleInformation(hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) ) - { - TDEBUG_TRACE("LaunchCommand: unable to clear stdin write handle"); - return false; - } -   STARTUPINFOA startupInfo;   memset(&startupInfo, 0, sizeof(startupInfo)); - startupInfo.cb = sizeof(startupInfo); - startupInfo.hStdInput = hChildStd_IN_Rd; - startupInfo.dwFlags |= STARTF_USESTDHANDLES;     int res = CreateProcessA(NULL, // No module name, use command line   const_cast<char*>(command.c_str()),   NULL, // Process handle not inherited   NULL, // Thread handle not inherited   FALSE,   CREATE_NO_WINDOW,   NULL, // use parent's environment   const_cast<char*>(cwd.c_str()),   &startupInfo,   &processInfo);   if (res == 0)   {   TDEBUG_TRACE("LaunchCommand: failed to launch");   return false;   }   - if( !filelist.empty() ) - { - DWORD dwWritten; - WriteFile(hChildStd_IN_Wr, filelist.c_str(), filelist.size(), &dwWritten, NULL); - } - - if ( !CloseHandle(hChildStd_IN_Wr) ) - { - TDEBUG_TRACE("LaunchCommand: Unable to close process stdin"); - } -   CloseHandle(processInfo.hProcess);   CloseHandle(processInfo.hThread);   return true;  }    std::string GetTemporaryFile(LPCTSTR prefix)  {   char tempDir[MAX_PATH + 1];   char tempFile[MAX_PATH + 1];     if (GetTempPath(MAX_PATH, tempDir) == 0)   {   TDEBUG_TRACE("GetTemporaryFile: Failed to find temporary path");   }   else if (GetTempFileName(tempDir, prefix, 0, tempFile) != 0)   {   return std::string(tempFile);   }   else   {   TDEBUG_TRACE("GetTemporaryFile: Failed to get temporary file");   }     return std::string();  }      bool IsDirectory(const std::string& filename)  {   DWORD attributes = GetFileAttributesA(filename.c_str());   if (attributes == INVALID_FILE_ATTRIBUTES)   return false;     return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;  }    std::string DirName(const std::string& filename)  {   if (filename.empty())   return filename;   std::string::size_type pos = filename.find_last_of("\\");   std::string myfilename = filename.substr(0, pos);   if (myfilename.size() > 0 && myfilename[myfilename.size()-1] == ':')   myfilename.push_back('\\');   return myfilename;  }    std::string BaseName(const std::string& filename)  {   if (filename.empty())   return filename;   std::string::size_type pos = filename.find_last_of("\\");   std::string myfilename = filename.substr(pos+1);   return myfilename;  }    HICON GetTortoiseIcon(const std::string& iconname)  {   std::string thgdir = GetTHgProgRoot();   if (thgdir.empty())   {   TDEBUG_TRACE("GetTortoiseIcon: THG root is empty");   return NULL;   }     std::string iconpath = thgdir + "\\icons\\" + iconname;   TDEBUG_TRACE(" GetTortoiseIcon: loading " + iconpath);   HICON h = (HICON) LoadImageA(0, iconpath.c_str(), IMAGE_ICON,   16, 16, LR_LOADFROMFILE);   if (!h)   {   TDEBUG_TRACE(" GetTortoiseIcon: can't find " + iconpath);   }     return h;  }    std::string GetHgRepoRoot(const std::string& path)  {   std::string p = IsDirectory(path)? path : DirName(path);   while (!IsDirectory(p + "\\.hg"))   {   std::string oldp = p;   p = DirName(p);   if (p == oldp)   {   p.clear();   break;   }   }   return p;  }    bool IsHgRepo(const std::string& path)  {   return !GetHgRepoRoot(path).empty();  }
 
31
32
33
34
 
35
36
37
 
31
32
33
 
34
35
36
37
@@ -31,7 +31,7 @@
 bool IsDirectory(const std::string&);  std::string DirName(const std::string&);  std::string BaseName(const std::string&); -bool LaunchCommand(const std::string& command, const std::string& cwd, const std::string& filelist); +bool LaunchCommand(const std::string& command, const std::string& cwd);  HICON GetTortoiseIcon(const std::string & iconname);  std::string GetHgRepoRoot(const std::string& path);  bool IsHgRepo(const std::string& path);